diff --git a/primitive-types/impls/num-traits/CHANGELOG.md b/primitive-types/impls/num-traits/CHANGELOG.md index 1f811b9c..4582887d 100644 --- a/primitive-types/impls/num-traits/CHANGELOG.md +++ b/primitive-types/impls/num-traits/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog]. [Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ ## [Unreleased] +- Added missing num-traits impls for uint types. [#898](https://github.com/paritytech/parity-common/pull/898) ## [0.2.0] - 2024-09-11 - Updated `uint` to 0.10. [#859](https://github.com/paritytech/parity-common/pull/859) diff --git a/primitive-types/impls/num-traits/src/lib.rs b/primitive-types/impls/num-traits/src/lib.rs index 5c0973ea..b0ef2a8b 100644 --- a/primitive-types/impls/num-traits/src/lib.rs +++ b/primitive-types/impls/num-traits/src/lib.rs @@ -23,8 +23,28 @@ pub use uint; #[macro_export] macro_rules! impl_uint_num_traits { ($name: ident, $len: expr) => { + impl $crate::num_traits::bounds::Bounded for $name { + #[inline] + fn min_value() -> Self { + Self::zero() + } + + #[inline] + fn max_value() -> Self { + Self::max_value() + } + } + impl $crate::num_traits::sign::Unsigned for $name {} + impl $crate::num_traits::identities::ConstZero for $name { + const ZERO: Self = Self::zero(); + } + + impl $crate::num_traits::identities::ConstOne for $name { + const ONE: Self = Self::one(); + } + impl $crate::num_traits::identities::Zero for $name { #[inline] fn zero() -> Self { @@ -58,6 +78,30 @@ macro_rules! impl_uint_num_traits { } } + impl $crate::num_traits::ops::bytes::FromBytes for $name { + type Bytes = [u8; $len * 8]; + + fn from_be_bytes(bytes: &Self::Bytes) -> Self { + Self::from_big_endian(&bytes[..]) + } + + fn from_le_bytes(bytes: &Self::Bytes) -> Self { + Self::from_little_endian(&bytes[..]) + } + } + + impl $crate::num_traits::ops::bytes::ToBytes for $name { + type Bytes = [u8; $len * 8]; + + fn to_be_bytes(&self) -> Self::Bytes { + self.to_big_endian() + } + + fn to_le_bytes(&self) -> Self::Bytes { + self.to_little_endian() + } + } + impl $crate::num_traits::ops::checked::CheckedAdd for $name { #[inline] fn checked_add(&self, v: &Self) -> Option { @@ -85,5 +129,60 @@ macro_rules! impl_uint_num_traits { $name::checked_mul(*self, *v) } } + + impl $crate::num_traits::ops::checked::CheckedNeg for $name { + #[inline] + fn checked_neg(&self) -> Option { + Self::checked_neg(*self) + } + } + + impl $crate::num_traits::ops::checked::CheckedRem for $name { + #[inline] + fn checked_rem(&self, v: &Self) -> Option { + Self::checked_rem(*self, *v) + } + } + + impl $crate::num_traits::ops::saturating::Saturating for $name { + #[inline] + fn saturating_add(self, v: Self) -> Self { + Self::saturating_add(self, v) + } + + #[inline] + fn saturating_sub(self, v: Self) -> Self { + Self::saturating_sub(self, v) + } + } + + impl $crate::num_traits::ops::saturating::SaturatingAdd for $name { + #[inline] + fn saturating_add(&self, v: &Self) -> Self { + Self::saturating_add(*self, *v) + } + } + + impl $crate::num_traits::ops::saturating::SaturatingMul for $name { + #[inline] + fn saturating_mul(&self, v: &Self) -> Self { + Self::saturating_mul(*self, *v) + } + } + + impl $crate::num_traits::ops::saturating::SaturatingSub for $name { + #[inline] + fn saturating_sub(&self, v: &Self) -> Self { + Self::saturating_sub(*self, *v) + } + } + + impl $crate::num_traits::pow::Pow for $name { + type Output = Self; + + fn pow(self, rhs: Self) -> Self { + Self::pow(self, rhs) + } + } }; }