Skip to content

Commit bb67944

Browse files
conr2dniklasad1
andauthored
Implement traits from num-traits for uint types (#898)
* Implement traits from num-traits for uint types * Update CHANGELOG.md * Update CHANGELOG.md Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com> --------- Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
1 parent 84ca434 commit bb67944

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

primitive-types/impls/num-traits/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog].
55
[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/
66

77
## [Unreleased]
8+
- Added missing num-traits impls for uint types. [#898](https://github.com/paritytech/parity-common/pull/898)
89

910
## [0.2.0] - 2024-09-11
1011
- Updated `uint` to 0.10. [#859](https://github.com/paritytech/parity-common/pull/859)

primitive-types/impls/num-traits/src/lib.rs

+99
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,28 @@ pub use uint;
2323
#[macro_export]
2424
macro_rules! impl_uint_num_traits {
2525
($name: ident, $len: expr) => {
26+
impl $crate::num_traits::bounds::Bounded for $name {
27+
#[inline]
28+
fn min_value() -> Self {
29+
Self::zero()
30+
}
31+
32+
#[inline]
33+
fn max_value() -> Self {
34+
Self::max_value()
35+
}
36+
}
37+
2638
impl $crate::num_traits::sign::Unsigned for $name {}
2739

40+
impl $crate::num_traits::identities::ConstZero for $name {
41+
const ZERO: Self = Self::zero();
42+
}
43+
44+
impl $crate::num_traits::identities::ConstOne for $name {
45+
const ONE: Self = Self::one();
46+
}
47+
2848
impl $crate::num_traits::identities::Zero for $name {
2949
#[inline]
3050
fn zero() -> Self {
@@ -58,6 +78,30 @@ macro_rules! impl_uint_num_traits {
5878
}
5979
}
6080

81+
impl $crate::num_traits::ops::bytes::FromBytes for $name {
82+
type Bytes = [u8; $len * 8];
83+
84+
fn from_be_bytes(bytes: &Self::Bytes) -> Self {
85+
Self::from_big_endian(&bytes[..])
86+
}
87+
88+
fn from_le_bytes(bytes: &Self::Bytes) -> Self {
89+
Self::from_little_endian(&bytes[..])
90+
}
91+
}
92+
93+
impl $crate::num_traits::ops::bytes::ToBytes for $name {
94+
type Bytes = [u8; $len * 8];
95+
96+
fn to_be_bytes(&self) -> Self::Bytes {
97+
self.to_big_endian()
98+
}
99+
100+
fn to_le_bytes(&self) -> Self::Bytes {
101+
self.to_little_endian()
102+
}
103+
}
104+
61105
impl $crate::num_traits::ops::checked::CheckedAdd for $name {
62106
#[inline]
63107
fn checked_add(&self, v: &Self) -> Option<Self> {
@@ -85,5 +129,60 @@ macro_rules! impl_uint_num_traits {
85129
$name::checked_mul(*self, *v)
86130
}
87131
}
132+
133+
impl $crate::num_traits::ops::checked::CheckedNeg for $name {
134+
#[inline]
135+
fn checked_neg(&self) -> Option<Self> {
136+
Self::checked_neg(*self)
137+
}
138+
}
139+
140+
impl $crate::num_traits::ops::checked::CheckedRem for $name {
141+
#[inline]
142+
fn checked_rem(&self, v: &Self) -> Option<Self> {
143+
Self::checked_rem(*self, *v)
144+
}
145+
}
146+
147+
impl $crate::num_traits::ops::saturating::Saturating for $name {
148+
#[inline]
149+
fn saturating_add(self, v: Self) -> Self {
150+
Self::saturating_add(self, v)
151+
}
152+
153+
#[inline]
154+
fn saturating_sub(self, v: Self) -> Self {
155+
Self::saturating_sub(self, v)
156+
}
157+
}
158+
159+
impl $crate::num_traits::ops::saturating::SaturatingAdd for $name {
160+
#[inline]
161+
fn saturating_add(&self, v: &Self) -> Self {
162+
Self::saturating_add(*self, *v)
163+
}
164+
}
165+
166+
impl $crate::num_traits::ops::saturating::SaturatingMul for $name {
167+
#[inline]
168+
fn saturating_mul(&self, v: &Self) -> Self {
169+
Self::saturating_mul(*self, *v)
170+
}
171+
}
172+
173+
impl $crate::num_traits::ops::saturating::SaturatingSub for $name {
174+
#[inline]
175+
fn saturating_sub(&self, v: &Self) -> Self {
176+
Self::saturating_sub(*self, *v)
177+
}
178+
}
179+
180+
impl $crate::num_traits::pow::Pow<Self> for $name {
181+
type Output = Self;
182+
183+
fn pow(self, rhs: Self) -> Self {
184+
Self::pow(self, rhs)
185+
}
186+
}
88187
};
89188
}

0 commit comments

Comments
 (0)