-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(traits): Add trait impl for buildin types (#2964)
Co-authored-by: Yordan Madzhunkov <[email protected]>
- Loading branch information
1 parent
d49492c
commit 2c87b27
Showing
7 changed files
with
233 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
tooling/nargo_cli/tests/execution_success/trait_impl_base_type/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "trait_impl_base_type" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = "0.10.5" | ||
|
||
[dependencies] |
1 change: 1 addition & 0 deletions
1
tooling/nargo_cli/tests/execution_success/trait_impl_base_type/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
x = "5" |
141 changes: 141 additions & 0 deletions
141
tooling/nargo_cli/tests/execution_success/trait_impl_base_type/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
trait Fieldable { | ||
fn to_field(self) -> Field; | ||
} | ||
|
||
impl Fieldable for u32 { | ||
fn to_field(self) -> Field { | ||
let res = self as Field; | ||
res * 3 | ||
} | ||
} | ||
|
||
|
||
impl Fieldable for [u32; 3] { | ||
fn to_field(self) -> Field { | ||
let res = self[0] + self[1] + self[2]; | ||
res as Field | ||
} | ||
} | ||
|
||
impl Fieldable for bool { | ||
fn to_field(self) -> Field { | ||
if self { | ||
14 | ||
} else { | ||
3 | ||
} | ||
} | ||
} | ||
|
||
impl Fieldable for (u32, bool) { | ||
fn to_field(self) -> Field { | ||
if self.1 { | ||
self.0 as Field | ||
} else { | ||
32 | ||
} | ||
} | ||
} | ||
|
||
impl Fieldable for Field { | ||
fn to_field(self) -> Field { | ||
self | ||
} | ||
} | ||
|
||
impl Fieldable for str<6> { | ||
fn to_field(self) -> Field { | ||
6 | ||
} | ||
} | ||
|
||
impl Fieldable for () { | ||
fn to_field(self) -> Field { | ||
0 | ||
} | ||
} | ||
|
||
type Point2D = [Field; 2]; | ||
type Point2DAlias = Point2D; | ||
|
||
impl Fieldable for Point2DAlias { | ||
fn to_field(self) -> Field { | ||
self[0] + self[1] | ||
} | ||
} | ||
|
||
impl Fieldable for fmtstr<14, (Field, Field)> { | ||
fn to_field(self) -> Field { | ||
52 | ||
} | ||
} | ||
|
||
impl Fieldable for fn(u32) -> u32 { | ||
fn to_field(self) -> Field { | ||
self(10) as Field | ||
} | ||
} | ||
|
||
fn some_func(x: u32) -> u32 { | ||
x * 2 - 3 | ||
} | ||
|
||
|
||
trait MutFieldable { | ||
fn mut_to_field(self) -> Field; | ||
} | ||
|
||
impl MutFieldable for &mut u64 { | ||
fn mut_to_field(self) -> Field { | ||
1337 as Field | ||
} | ||
} | ||
|
||
fn a(y: &mut u64) -> Field { | ||
y.mut_to_field() | ||
} | ||
|
||
impl Fieldable for &mut u64 { | ||
fn to_field(self) -> Field { | ||
777 as Field | ||
} | ||
} | ||
|
||
impl Fieldable for u64 { | ||
fn to_field(self) -> Field { | ||
66 as Field | ||
} | ||
} | ||
|
||
// x = 15 | ||
fn main(x: u32) { | ||
assert(x.to_field() == 15); | ||
let arr: [u32; 3] = [3, 5, 8]; | ||
assert(arr.to_field() == 16); | ||
let b_true = 2 == 2; | ||
assert(b_true.to_field() == 14); | ||
let b_false = 2 == 3; | ||
assert(b_false.to_field() == 3); | ||
let f = 13 as Field; | ||
assert(f.to_field() == 13); | ||
let k_true = (12 as u32, true); | ||
assert(k_true.to_field() == 12); | ||
let k_false = (11 as u32, false); | ||
assert(k_false.to_field() == 32); | ||
let m = "String"; | ||
assert(m.to_field() == 6); | ||
let unit = (); | ||
assert(unit.to_field() == 0); | ||
let point: Point2DAlias = [2, 3]; | ||
assert(point.to_field() == 5); | ||
let i: Field = 2; | ||
let j: Field = 6; | ||
assert(f"i: {i}, j: {j}".to_field() == 52); | ||
assert(some_func.to_field() == 17); | ||
|
||
let mut y = 0 as u64; | ||
assert(a(&mut y) == 1337); | ||
assert((&mut y).mut_to_field() == 1337); | ||
assert((&mut y).to_field() == 777); | ||
assert(y.to_field() == 66); | ||
} |