Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions docs/docs/noir/standard_library/containers/boundedvec.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ Example:

#include_code bounded-vec-max-len-example test_programs/noir_test_success/bounded_vec/src/main.nr rust

### set_len

```rust
pub fn set_len(&mut self, len: u32) {
```

Sets the length of the BoundedVec manually. This is useful to update the length after
updating the content of the vector using `set_unchecked`.

Example:

#include_code bounded-vec-set-len-example test_programs/noir_test_success/bounded_vec/src/main.nr rust

### storage

```rust
Expand Down
49 changes: 49 additions & 0 deletions noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,26 @@ impl<T, let MaxLen: u32> BoundedVec<T, MaxLen> {
self.len
}

/// Allows user to update the length of the vector once
/// after doing multiple pushes.
/// Example:
///
/// ```noir
/// let mut v: BoundedVec<Field, 10> = BoundedVec::from([1, 2, 3, 4, 5]);
///
/// assert(v.len() == 5);
/// v.set_unchecked(5, 6);
/// v.set_unchecked(6, 7);
/// v.set_unchecked(7, 8);
/// v.set_len(8);
/// assert(v.len() == 8);
/// ```
pub fn set_len(&mut self, len: u32) {
assert(len <= MaxLen, "set_len out of bounds");
assert(len >= self.len(), "can not set_len to a length shorter than the current length");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's meant to be used with the already unsafe set_unchecked, and it is allowed to be set to anything up to MaxLen, which can result in get returning zeroed items if just increase the length of without setting elements, why not also allow it to shorten the vector, which would make it work a bit like Vec::truncate in Rust?

self.len = len;
}

/// Returns the maximum length of this vector. This is always
/// equal to the `MaxLen` parameter this vector was initialized with.
///
Expand Down Expand Up @@ -713,6 +733,35 @@ mod bounded_vec_tests {
}
}

mod set_len {
use crate::collections::bounded_vec::BoundedVec;

#[test]
fn set_len_works_after_multiple_unchecked_sets() {
let mut vec: BoundedVec<u8, 10> = BoundedVec::from_array([1, 2, 3]);
vec.set_unchecked(3, 44);
vec.set_unchecked(4, 55);
vec.set_unchecked(5, 66);
vec.set_unchecked(6, 77);
vec.set_len(8);
assert_eq(vec.len(), 8);
vec.extend_from_array([88, 99]);
assert_eq(vec.len(), 10);
}

#[test(should_fail_with = "can not set_len to a length shorter than the current length")]
fn panics_when_set_len_to_shorter_length() {
let mut vec: BoundedVec<u32, 5> = BoundedVec::from_array([1, 2, 3]);
vec.set_len(2);
}

#[test(should_fail_with = "set_len out of bounds")]
fn panics_when_set_len_beyond_max_len() {
let mut vec: BoundedVec<u32, 5> = BoundedVec::from_array([1, 2, 3]);
vec.set_len(6);
}
}

mod any {
use crate::collections::bounded_vec::BoundedVec;

Expand Down
14 changes: 14 additions & 0 deletions test_programs/noir_test_success/bounded_vec/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ fn max_len_docs_example() {
// docs:end:bounded-vec-max-len-example
}

#[test]
fn set_len_docs_example() {
// docs:start:bounded-vec-set-len-example
let mut v: BoundedVec<Field, 10> = BoundedVec::from_array([1, 2, 3, 4, 5]);

v.set_unchecked(5, 6);
v.set_unchecked(6, 7);
v.set_unchecked(7, 8);
assert(v.len() == 5);
v.set_len(9);
assert(v.len() == 9);
// docs:end:bounded-vec-set-len-example
}

#[test]
fn storage_docs_example() {
// docs:start:bounded-vec-storage-example
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading