Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Derive Eq & Default #5667

Merged
merged 1 commit into from
Aug 2, 2024
Merged
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
28 changes: 28 additions & 0 deletions noir_stdlib/src/cmp.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
use crate::meta::derive_via;

#[derive_via(derive_eq)]
// docs:start:eq-trait
trait Eq {
fn eq(self, other: Self) -> bool;
}
// docs:end:eq-trait

comptime fn derive_eq(s: StructDefinition) -> Quoted {
let typ = s.as_type();

let impl_generics = s.generics().join(quote {,});

let where_clause = s.generics().map(|name| quote { $name: Default }).join(quote {,});

// `(self.a == other.a) & (self.b == other.b) & ...`
let equalities = s.fields().map(
|f: (Quoted, Type)| {
let name = f.0;
quote { (self.$name == other.$name) }
}
);
jfecher marked this conversation as resolved.
Show resolved Hide resolved
let body = equalities.join(quote { & });

quote {
impl<$impl_generics> Eq for $typ where $where_clause {
fn eq(self, other: Self) -> bool {
$body
}
}
}
}

impl Eq for Field { fn eq(self, other: Field) -> bool { self == other } }

impl Eq for u64 { fn eq(self, other: u64) -> bool { self == other } }
Expand Down
28 changes: 28 additions & 0 deletions noir_stdlib/src/default.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
use crate::meta::derive_via;

#[derive_via(derive_default)]
// docs:start:default-trait
trait Default {
fn default() -> Self;
}
// docs:end:default-trait

comptime fn derive_default(s: StructDefinition) -> Quoted {
let typ = s.as_type();

let impl_generics = s.generics().join(quote {,});

let where_clause = s.generics().map(|name| quote { $name: Default }).join(quote {,});

// `foo: Default::default(), bar: Default::default(), ...`
let fields = s.fields().map(
|f: (Quoted, Type)| {
let name = f.0;
quote { $name: Default::default() }
}
);
let fields = fields.join(quote {,});

quote {
impl<$impl_generics> Default for $typ where $where_clause {
fn default() -> Self {
Self { $fields }
}
}
}
}

impl Default for Field { fn default() -> Field { 0 } }

impl Default for u8 { fn default() -> u8 { 0 } }
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/hash/poseidon2.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::hash::Hasher;
use crate::default::Default;

global RATE: u32 = 3;
comptime global RATE: u32 = 3;
jfecher marked this conversation as resolved.
Show resolved Hide resolved

struct Poseidon2 {
cache: [Field;3],
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/meta/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::collections::umap::UHashMap;
use crate::hash::BuildHasherDefault;
use crate::hash::poseidon2::Poseidon2Hasher;

mod struct_def;
jfecher marked this conversation as resolved.
Show resolved Hide resolved
mod trait_constraint;
mod trait_def;
mod type_def;
mod quoted;

/// Calling unquote as a macro (via `unquote!(arg)`) will unquote
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions test_programs/execution_success/derive/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ comptime fn derive_do_nothing(s: StructDefinition) -> Quoted {
}
}

// Test stdlib derive fns & multiple traits
#[derive(Eq, Default)]
struct MyOtherStruct {
field1: u32,
field2: u64,
}

fn main() {
let s = MyStruct { my_field: 1 };
s.do_nothing();

let o = MyOtherStruct::default();
assert_eq(o, o);
}
Loading