Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add set and set_unchecked methods to Vec and BoundedVec #5241

Merged
merged 6 commits into from
Jun 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,35 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
BoundedVec { storage: [zeroed; MaxLen], len: 0 }
}

/// Get an element from the vector at the given index.
/// Panics if the given index points beyond the end of the vector (`self.len()`).
pub fn get(mut self: Self, index: u32) -> T {
assert(index < self.len);
self.storage[index]
self.get_unchecked(index)
}

/// Get an element from the vector at the given index.
/// Responds with undefined data for `index` where `self.len < index < self.max_len()`.
pub fn get_unchecked(mut self: Self, index: u32) -> T {
self.storage[index]
}

/// Write an element to the vector at the given index.
/// Panics if the given index points beyond the end of the vector (`self.len()`).
pub fn set(mut self: Self, index: u32, value: T) {
assert(index < self.len);
self.set_unchecked(index, value)
}

/// Write an element to the vector at the given index.
/// Does not check whether the passed `index` is a valid index within the vector.
///
/// Silently writes past the end of the vector for `index` where `self.len < index < self.max_len()`
/// Panics if the given index points beyond the maximum length of the vector (`self.max_len()`).
pub fn set_unchecked(mut self: Self, index: u32, value: T) {
self.storage[index] = value;
}

pub fn push(&mut self, elem: T) {
assert(self.len < MaxLen, "push out of bounds");

Expand Down
Loading