Skip to content

Commit

Permalink
bounds check
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-zh authored and bobzhang committed Mar 12, 2024
1 parent 3270e6b commit f782750
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions vec/vec.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,24 @@ pub fn push[T](self : Vec[T], value : T) {
}

/// Retrieves the element at the specified index from the vector.
///
/// If you try to access an index which isn’t in the Vec, it will panic.
pub fn op_get[T](self : Vec[T], index : Int) -> T {
if index >= self.len {
let len = self.len
abort("index out of bounds: the len is \(len) but the index is \(index)")
}
self.buf[index]
}

/// Sets the value of the element at the specified index.
///
/// If you try to access an index which isn’t in the Vec, it will panic.
pub fn op_set[T](self : Vec[T], index : Int, value : T) {
if index >= self.len {
let len = self.len
abort("index out of bounds: the len is \(len) but the index is \(index)")
}
self.buf[index] = value
}

Expand Down

0 comments on commit f782750

Please sign in to comment.