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

Implement traits from num-traits for uint types #898

Merged
merged 4 commits into from
Feb 11, 2025
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
1 change: 1 addition & 0 deletions primitive-types/impls/num-traits/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
99 changes: 99 additions & 0 deletions primitive-types/impls/num-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Self> {
Expand Down Expand Up @@ -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> {
Self::checked_neg(*self)
}
}

impl $crate::num_traits::ops::checked::CheckedRem for $name {
#[inline]
fn checked_rem(&self, v: &Self) -> Option<Self> {
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<Self> for $name {
type Output = Self;

fn pow(self, rhs: Self) -> Self {
Self::pow(self, rhs)
}
}
};
}
Loading