Skip to content

Commit

Permalink
feat: implement the From trait for array to BoundedVec conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
akonior committed Apr 25, 2024
1 parent 7dd2c5e commit 03e204c
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ impl<T, MaxLen> Eq for BoundedVec<T, MaxLen> where T: Eq {
}
}

impl<T, MaxLen> From<[T; MaxLen]> for BoundedVec<T, MaxLen> {
fn from(value: [T; MaxLen]) -> BoundedVec<T, MaxLen> {
BoundedVec { storage: value, len: MaxLen }
}
}

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

assert(bounded_vec1 != bounded_vec2);
}

mod from_array {
#[test]
fn empty() {
let bounded_vec = BoundedVec::from([]);

assert_eq(bounded_vec.len(), 0);
assert_eq(bounded_vec.storage(), []);
}

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

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

0 comments on commit 03e204c

Please sign in to comment.