diff --git a/crates/nargo_cli/tests/compile_success_empty/slice_as_array/Nargo.toml b/crates/nargo_cli/tests/compile_success_empty/slice_as_array/Nargo.toml new file mode 100644 index 00000000000..92e21f444ca --- /dev/null +++ b/crates/nargo_cli/tests/compile_success_empty/slice_as_array/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "slice_as_array" +type = "bin" +authors = [""] +compiler_version = "0.10.3" + +[dependencies] \ No newline at end of file diff --git a/crates/nargo_cli/tests/compile_success_empty/slice_as_array/Prover.toml b/crates/nargo_cli/tests/compile_success_empty/slice_as_array/Prover.toml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/crates/nargo_cli/tests/compile_success_empty/slice_as_array/src/main.nr b/crates/nargo_cli/tests/compile_success_empty/slice_as_array/src/main.nr new file mode 100644 index 00000000000..54396bc80fa --- /dev/null +++ b/crates/nargo_cli/tests/compile_success_empty/slice_as_array/src/main.nr @@ -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); +} diff --git a/noir_stdlib/src/array.nr b/noir_stdlib/src/array.nr index bcdf56dd7aa..b1166354e66 100644 --- a/noir_stdlib/src/array.nr +++ b/noir_stdlib/src/array.nr @@ -83,3 +83,12 @@ impl [T; N] { ret } } + +fn repeated(element: T, length: Field) -> [T] { + crate::assert_constant(length); + let mut result = []; + for _ in 0..length { + result = result.push_back(element); + } + result +} diff --git a/noir_stdlib/src/slice.nr b/noir_stdlib/src/slice.nr index ca06e2aae44..7d6ea8ff853 100644 --- a/noir_stdlib/src/slice.nr +++ b/noir_stdlib/src/slice.nr @@ -1,4 +1,9 @@ impl [T] { + fn as_array(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. @@ -53,3 +58,11 @@ impl [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(_slice: [T]) -> [T; N] {} +}