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: implement From array trait for BoundedVec #4927

Merged
merged 7 commits into from
May 6, 2024
Merged
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
14 changes: 14 additions & 0 deletions docs/docs/noir/standard_library/containers/boundedvec.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ Example:

#include_code bounded-vec-extend-from-bounded-vec-example test_programs/noir_test_success/bounded_vec/src/main.nr rust

### from_array

```rust
pub fn from_array<Len>(array: [T; Len]) -> Self
```

Creates a new vector, populating it with values derived from an array input.
The maximum length of the vector is determined based on the type signature.

Example:
```rust
let bounded_vec: BoundedVec<Field, 10> = BoundedVec::from_array([1, 2, 3])
```

### any

```rust
Expand Down
71 changes: 70 additions & 1 deletion noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cmp::Eq;
use crate::{cmp::Eq, convert::From};

struct BoundedVec<T, MaxLen> {
storage: [T; MaxLen],
Expand Down Expand Up @@ -74,6 +74,13 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
self.len = new_len;
}

pub fn from_array<Len>(array: [T; Len]) -> Self {
assert(Len <= MaxLen, "from array out of bounds");
let mut vec: BoundedVec<T, MaxLen> = BoundedVec::new();
vec.extend_from_array(array);
vec
}

pub fn pop(&mut self) -> T {
assert(self.len > 0);
self.len -= 1;
Expand Down Expand Up @@ -107,6 +114,12 @@ impl<T, MaxLen> Eq for BoundedVec<T, MaxLen> where T: Eq {
}
}

impl<T, MaxLen, Len> From<[T; Len]> for BoundedVec<T, MaxLen> {
fn from(array: [T; Len]) -> BoundedVec<T, MaxLen> {
BoundedVec::from_array(array)
}
}

mod bounded_vec_tests {
// TODO: Allow imports from "super"
use crate::collections::bounded_vec::BoundedVec;
Expand All @@ -128,4 +141,60 @@ mod bounded_vec_tests {

assert(bounded_vec1 != bounded_vec2);
}

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

#[test]
fn empty() {
let empty_array: [Field; 0] = [];
let bounded_vec = BoundedVec::from_array([]);

assert_eq(bounded_vec.max_len(), 0);
assert_eq(bounded_vec.len(), 0);
assert_eq(bounded_vec.storage(), empty_array);
}

#[test]
fn equal_len() {
let array = [1, 2, 3];
let bounded_vec = BoundedVec::from_array(array);

assert_eq(bounded_vec.max_len(), 3);
assert_eq(bounded_vec.len(), 3);
assert_eq(bounded_vec.storage(), array);
}

#[test]
fn max_len_greater_then_array_len() {
let array = [1, 2, 3];
let bounded_vec: BoundedVec<Field, 10> = BoundedVec::from_array(array);

assert_eq(bounded_vec.max_len(), 10);
assert_eq(bounded_vec.len(), 3);
assert_eq(bounded_vec.storage()[0], 1);
assert_eq(bounded_vec.storage()[1], 2);
assert_eq(bounded_vec.storage()[2], 3);
}

#[test(should_fail_with="from array out of bounds")]
fn max_len_lower_then_array_len() {
let _: BoundedVec<Field, 2> = BoundedVec::from_array([0; 3]);
}
}

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

#[test]
fn simple() {
let array = [1, 2];
let bounded_vec: BoundedVec<Field, 10> = BoundedVec::from(array);

assert_eq(bounded_vec.max_len(), 10);
assert_eq(bounded_vec.len(), 2);
assert_eq(bounded_vec.storage()[0], 1);
assert_eq(bounded_vec.storage()[1], 2);
}
}
}
Loading