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

fix: add missing trait impls for integer types to stdlib #5738

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 26 additions & 0 deletions noir_stdlib/src/cmp.nr
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ impl Eq for Field { fn eq(self, other: Field) -> bool { self == other } }

impl Eq for u64 { fn eq(self, other: u64) -> bool { self == other } }
impl Eq for u32 { fn eq(self, other: u32) -> bool { self == other } }
impl Eq for u16 { fn eq(self, other: u16) -> bool { self == other } }
impl Eq for u8 { fn eq(self, other: u8) -> bool { self == other } }
impl Eq for u1 { fn eq(self, other: u1) -> bool { self == other } }

impl Eq for i8 { fn eq(self, other: i8) -> bool { self == other } }
impl Eq for i16 { fn eq(self, other: i16) -> bool { self == other } }
impl Eq for i32 { fn eq(self, other: i32) -> bool { self == other } }
impl Eq for i64 { fn eq(self, other: i64) -> bool { self == other } }

Expand Down Expand Up @@ -157,6 +159,18 @@ impl Ord for u32 {
}
}

impl Ord for u16 {
fn cmp(self, other: u16) -> Ordering {
if self < other {
Ordering::less()
} else if self > other {
Ordering::greater()
} else {
Ordering::equal()
}
}
}

impl Ord for u8 {
fn cmp(self, other: u8) -> Ordering {
if self < other {
Expand All @@ -181,6 +195,18 @@ impl Ord for i8 {
}
}

impl Ord for i16 {
fn cmp(self, other: i16) -> Ordering {
if self < other {
Ordering::less()
} else if self > other {
Ordering::greater()
} else {
Ordering::equal()
}
}
}

impl Ord for i32 {
fn cmp(self, other: i32) -> Ordering {
if self < other {
Expand Down
2 changes: 2 additions & 0 deletions noir_stdlib/src/default.nr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ comptime fn derive_default(s: StructDefinition) -> Quoted {

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

impl Default for u1 { fn default() -> u1 { 0 } }
impl Default for u8 { fn default() -> u8 { 0 } }
impl Default for u16 { fn default() -> u16 { 0 } }
jfecher marked this conversation as resolved.
Show resolved Hide resolved
impl Default for u32 { fn default() -> u32 { 0 } }
impl Default for u64 { fn default() -> u64 { 0 } }

Expand Down
12 changes: 12 additions & 0 deletions noir_stdlib/src/hash/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,24 @@ impl Hash for Field {
}
}

impl Hash for u1 {
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, self as Field);
}
}

impl Hash for u8 {
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, self as Field);
}
}

impl Hash for u16 {
jfecher marked this conversation as resolved.
Show resolved Hide resolved
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, self as Field);
}
}

impl Hash for u32 {
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, self as Field);
Expand Down
5 changes: 5 additions & 0 deletions noir_stdlib/src/ops/arith.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ impl Add for u64 { fn add(self, other: u64) -> u64 { self + other } }
impl Add for u32 { fn add(self, other: u32) -> u32 { self + other } }
impl Add for u16 { fn add(self, other: u16) -> u16 { self + other } }
impl Add for u8 { fn add(self, other: u8) -> u8 { self + other } }
impl Add for u1 { fn add(self, other: u1) -> u1 { self + other } }

impl Add for i8 { fn add(self, other: i8) -> i8 { self + other } }
impl Add for i16 { fn add(self, other: i16) -> i16 { self + other } }
Expand All @@ -28,6 +29,7 @@ impl Sub for u64 { fn sub(self, other: u64) -> u64 { self - other } }
impl Sub for u32 { fn sub(self, other: u32) -> u32 { self - other } }
impl Sub for u16 { fn sub(self, other: u16) -> u16 { self - other } }
impl Sub for u8 { fn sub(self, other: u8) -> u8 { self - other } }
impl Sub for u1 { fn sub(self, other: u1) -> u1 { self - other } }

impl Sub for i8 { fn sub(self, other: i8) -> i8 { self - other } }
impl Sub for i16 { fn sub(self, other: i16) -> i16 { self - other } }
Expand All @@ -46,6 +48,7 @@ impl Mul for u64 { fn mul(self, other: u64) -> u64 { self * other } }
impl Mul for u32 { fn mul(self, other: u32) -> u32 { self * other } }
impl Mul for u16 { fn mul(self, other: u16) -> u16 { self * other } }
impl Mul for u8 { fn mul(self, other: u8) -> u8 { self * other } }
impl Mul for u1 { fn mul(self, other: u1) -> u1 { self * other } }

impl Mul for i8 { fn mul(self, other: i8) -> i8 { self * other } }
impl Mul for i16 { fn mul(self, other: i16) -> i16 { self * other } }
Expand All @@ -64,6 +67,7 @@ impl Div for u64 { fn div(self, other: u64) -> u64 { self / other } }
impl Div for u32 { fn div(self, other: u32) -> u32 { self / other } }
impl Div for u16 { fn div(self, other: u16) -> u16 { self / other } }
impl Div for u8 { fn div(self, other: u8) -> u8 { self / other } }
impl Div for u1 { fn div(self, other: u1) -> u1 { self / other } }

impl Div for i8 { fn div(self, other: i8) -> i8 { self / other } }
impl Div for i16 { fn div(self, other: i16) -> i16 { self / other } }
Expand All @@ -80,6 +84,7 @@ impl Rem for u64 { fn rem(self, other: u64) -> u64 { self % other } }
impl Rem for u32 { fn rem(self, other: u32) -> u32 { self % other } }
impl Rem for u16 { fn rem(self, other: u16) -> u16 { self % other } }
impl Rem for u8 { fn rem(self, other: u8) -> u8 { self % other } }
impl Rem for u1 { fn rem(self, other: u1) -> u1 { self % other } }

impl Rem for i8 { fn rem(self, other: i8) -> i8 { self % other } }
impl Rem for i16 { fn rem(self, other: i16) -> i16 { self % other } }
Expand Down
3 changes: 3 additions & 0 deletions noir_stdlib/src/ops/bit.nr
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl BitOr for u64 { fn bitor(self, other: u64) -> u64 { self | other } }
impl BitOr for u32 { fn bitor(self, other: u32) -> u32 { self | other } }
impl BitOr for u16 { fn bitor(self, other: u16) -> u16 { self | other } }
impl BitOr for u8 { fn bitor(self, other: u8) -> u8 { self | other } }
impl BitOr for u1 { fn bitor(self, other: u1) -> u1 { self | other } }

impl BitOr for i8 { fn bitor(self, other: i8) -> i8 { self | other } }
impl BitOr for i16 { fn bitor(self, other: i16) -> i16 { self | other } }
Expand All @@ -49,6 +50,7 @@ impl BitAnd for u64 { fn bitand(self, other: u64) -> u64 { self & other } }
impl BitAnd for u32 { fn bitand(self, other: u32) -> u32 { self & other } }
impl BitAnd for u16 { fn bitand(self, other: u16) -> u16 { self & other } }
impl BitAnd for u8 { fn bitand(self, other: u8) -> u8 { self & other } }
impl BitAnd for u1 { fn bitand(self, other: u1) -> u1 { self & other } }

impl BitAnd for i8 { fn bitand(self, other: i8) -> i8 { self & other } }
impl BitAnd for i16 { fn bitand(self, other: i16) -> i16 { self & other } }
Expand All @@ -67,6 +69,7 @@ impl BitXor for u64 { fn bitxor(self, other: u64) -> u64 { self ^ other } }
impl BitXor for u32 { fn bitxor(self, other: u32) -> u32 { self ^ other } }
impl BitXor for u16 { fn bitxor(self, other: u16) -> u16 { self ^ other } }
impl BitXor for u8 { fn bitxor(self, other: u8) -> u8 { self ^ other } }
impl BitXor for u1 { fn bitxor(self, other: u1) -> u1 { self ^ other } }

impl BitXor for i8 { fn bitxor(self, other: i8) -> i8 { self ^ other } }
impl BitXor for i16 { fn bitxor(self, other: i16) -> i16 { self ^ other } }
Expand Down
Loading