From 661319e43af8191166a6484d5d217bb512464dc1 Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 25 Jan 2024 16:55:29 +0000 Subject: [PATCH 1/3] feat: add definitions for `From` and `Into` traits to Noir prelude --- noir_stdlib/src/convert.nr | 13 +++++++++++++ noir_stdlib/src/lib.nr | 1 + noir_stdlib/src/prelude.nr | 1 + 3 files changed, 15 insertions(+) create mode 100644 noir_stdlib/src/convert.nr diff --git a/noir_stdlib/src/convert.nr b/noir_stdlib/src/convert.nr new file mode 100644 index 00000000000..a70c4d61a49 --- /dev/null +++ b/noir_stdlib/src/convert.nr @@ -0,0 +1,13 @@ +trait From { + fn from(input: T) -> Self; +} + +trait Into { + fn into(input: Self) -> T; +} + +impl Into for U where T: From { + fn into(input: U) -> T { + T::from(input) + } +} diff --git a/noir_stdlib/src/lib.nr b/noir_stdlib/src/lib.nr index 23a7c71ff45..b86834712a6 100644 --- a/noir_stdlib/src/lib.nr +++ b/noir_stdlib/src/lib.nr @@ -16,6 +16,7 @@ mod ec; mod unsafe; mod collections; mod compat; +mod convert; mod option; mod string; mod test; diff --git a/noir_stdlib/src/prelude.nr b/noir_stdlib/src/prelude.nr index b57ff460371..26c6a805d54 100644 --- a/noir_stdlib/src/prelude.nr +++ b/noir_stdlib/src/prelude.nr @@ -4,3 +4,4 @@ use crate::{print, println, assert_constant}; use crate::uint128::U128; use crate::cmp::{Eq, Ord}; use crate::default::Default; +use crate::convert::{From, Into}; From 4d43e24404598e4718977b5adb2d11a96897456f Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 25 Jan 2024 17:04:42 +0000 Subject: [PATCH 2/3] feat: add implementations for basic types --- noir_stdlib/src/convert.nr | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/noir_stdlib/src/convert.nr b/noir_stdlib/src/convert.nr index a70c4d61a49..294b5097cd7 100644 --- a/noir_stdlib/src/convert.nr +++ b/noir_stdlib/src/convert.nr @@ -2,6 +2,12 @@ trait From { fn from(input: T) -> Self; } +impl From for T { + fn from(input: T) -> T { + input + } +} + trait Into { fn into(input: Self) -> T; } @@ -11,3 +17,40 @@ impl Into for U where T: From { T::from(input) } } + +// Unsigned integers +impl From for u16 { fn from(value: u8) -> u16 { value as u16 } } + +impl From for u32 { fn from(value: u8) -> u32 { value as u32 } } +impl From for u32 { fn from(value: u16) -> u32 { value as u32 } } + +impl From for u64 { fn from(value: u8) -> u64 { value as u64 } } +impl From for u64 { fn from(value: u16) -> u64 { value as u64 } } +impl From for u64 { fn from(value: u32) -> u64 { value as u64 } } + +impl From for Field { fn from(value: u8) -> Field { value as Field } } +impl From for Field { fn from(value: u16) -> Field { value as Field } } +impl From for Field { fn from(value: u32) -> Field { value as Field } } +impl From for Field { fn from(value: u64) -> Field { value as Field } } + +// Signed integers +impl From for i16 { fn from(value: i8) -> i16 { value as i16 } } + +impl From for i32 { fn from(value: i8) -> i32 { value as i32 } } +impl From for i32 { fn from(value: i16) -> i32 { value as i32 } } + +impl From for i64 { fn from(value: i8) -> i64 { value as i64 } } +impl From for i64 { fn from(value: i16) -> i64 { value as i64 } } +impl From for i64 { fn from(value: i32) -> i64 { value as i64 } } + +// Booleans +impl From for u8 { fn from(value: bool) -> u8 { value as u8 } } +impl From for u16 { fn from(value: bool) -> u16 { value as u16 } } +impl From for u32 { fn from(value: bool) -> u32 { value as u32 } } +impl From for u64 { fn from(value: bool) -> u64 { value as u64 } } +impl From for i8 { fn from(value: bool) -> i8 { value as i8 } } +impl From for i16 { fn from(value: bool) -> i16 { value as i16 } } +impl From for i32 { fn from(value: bool) -> i32 { value as i32 } } +impl From for i64 { fn from(value: bool) -> i64 { value as i64 } } +impl From for Field { fn from(value: bool) -> Field { value as Field } } + From a3eac9bff4a6d07dae1ae3bcf5148be1be99883b Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 25 Jan 2024 17:10:39 +0000 Subject: [PATCH 3/3] chore: disambiguate trait used in test --- .../compile_success_empty/trait_generics/src/main.nr | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test_programs/compile_success_empty/trait_generics/src/main.nr b/test_programs/compile_success_empty/trait_generics/src/main.nr index 9a3c54c3fa1..30b2e79d579 100644 --- a/test_programs/compile_success_empty/trait_generics/src/main.nr +++ b/test_programs/compile_success_empty/trait_generics/src/main.nr @@ -1,4 +1,3 @@ - fn main() { let xs: [Field; 1] = [3]; let ys: [u32; 1] = [3]; @@ -8,21 +7,21 @@ fn main() { assert_eq(15, sum_static(Data { a: 5, b: 10 })); } -fn foo(x: T, u: U) where T: Into, U: Eq { +fn foo(x: T, u: U) where T: MyInto, U: Eq { assert(x.into() == u); } -trait Into { +trait MyInto { fn into(self) -> T; } -impl Into<[U; N]> for [T; N] where T: Into { +impl MyInto<[U; N]> for [T; N] where T: MyInto { fn into(self) -> [U; N] { self.map(|x: T| x.into()) } } -impl Into for Field { +impl MyInto for Field { fn into(self) -> u32 { self as u32 }