Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "slice_as_array"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let slice = [1, 2, 3];
let array: [Field; 3] = slice.as_array();

assert(array[0] == 1);
assert(array[1] == 2);
assert(array[2] == 3);
}
9 changes: 9 additions & 0 deletions noir_stdlib/src/array.nr
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ impl<T, N> [T; N] {
ret
}
}

fn repeated<T>(element: T, length: Field) -> [T] {
crate::assert_constant(length);
let mut result = [];
for _ in 0..length {
result = result.push_back(element);
}
result
}
13 changes: 13 additions & 0 deletions noir_stdlib/src/slice.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
impl<T> [T] {
fn as_array<N>(self) -> [T; N] {
assert(self.len() == N);
unsafe::slice_as_array(self)
}

/// Push a new element to the end of the slice, returning a
/// new slice with a length one greater than the
/// original unmodified slice.
Expand Down Expand Up @@ -53,3 +58,11 @@ impl<T> [T] {
self
}
}

mod unsafe {
// This is a private built-in compiler function `slice_as_array`.
// It's not intended for general use and should not be used directly.
// Users should prefer to use `slice.as_array()` instead
#[builtin(slice_as_array)]
fn slice_as_array<T, N>(_slice: [T]) -> [T; N] {}
}