Skip to content

Commit

Permalink
Traits...
Browse files Browse the repository at this point in the history
  • Loading branch information
recmo committed Aug 27, 2023
1 parent f527841 commit b409b6e
Showing 1 changed file with 179 additions and 7 deletions.
186 changes: 179 additions & 7 deletions src/support/num_traits.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
//! Support for the [`num-traits`](https://crates.io/crates/num-traits) crate.
#![cfg(feature = "num-traits")]
#![cfg_attr(docsrs, doc(cfg(feature = "num-traits")))]

// This is a particularly big risk with these traits. Make sure
// to call functions on the `Uint::` type.
#![deny(unconditional_recursion)]
use crate::Uint;
use num_traits::{
bounds::Bounded,
ops::bytes::{FromBytes, ToBytes},
One, Zero,
bounds::*,
ops::{bytes::*, checked::*, overflowing::*, saturating::*, wrapping::*, *},
*,
};

// TODO: PrimInt, Unsigned

impl<const BITS: usize, const LIMBS: usize> Zero for Uint<BITS, LIMBS> {
#[inline(always)]
fn zero() -> Self {
Self::ZERO
}

Check warning on line 20 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L18-L20

Added lines #L18 - L20 were not covered by tests

#[inline(always)]
fn is_zero(&self) -> bool {
self.is_zero()
self == &Self::ZERO
}

Check warning on line 25 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L23-L25

Added lines #L23 - L25 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> One for Uint<BITS, LIMBS> {
#[inline(always)]
fn one() -> Self {
Self::one()
Uint::from(1)
}

Check warning on line 32 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L30-L32

Added lines #L30 - L32 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> Bounded for Uint<BITS, LIMBS> {
#[inline(always)]
fn min_value() -> Self {
Self::ZERO
}

Check warning on line 39 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L37-L39

Added lines #L37 - L39 were not covered by tests

#[inline(always)]
fn max_value() -> Self {
Self::MAX
}

Check warning on line 44 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L42-L44

Added lines #L42 - L44 were not covered by tests
Expand All @@ -38,10 +47,12 @@ impl<const BITS: usize, const LIMBS: usize> Bounded for Uint<BITS, LIMBS> {
impl<const BITS: usize, const LIMBS: usize> FromBytes for Uint<BITS, LIMBS> {
type Bytes = [u8];

#[inline(always)]
fn from_le_bytes(bytes: &[u8]) -> Self {
Self::try_from_le_slice(bytes).unwrap()
}

Check warning on line 53 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L51-L53

Added lines #L51 - L53 were not covered by tests

#[inline(always)]
fn from_be_bytes(bytes: &[u8]) -> Self {
Self::try_from_be_slice(bytes).unwrap()
}

Check warning on line 58 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L56-L58

Added lines #L56 - L58 were not covered by tests
Expand All @@ -50,13 +61,174 @@ impl<const BITS: usize, const LIMBS: usize> FromBytes for Uint<BITS, LIMBS> {
impl<const BITS: usize, const LIMBS: usize> ToBytes for Uint<BITS, LIMBS> {
type Bytes = Vec<u8>;

#[inline(always)]
fn to_le_bytes(&self) -> Self::Bytes {
self.to_le_bytes_vec()
}

Check warning on line 67 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L65-L67

Added lines #L65 - L67 were not covered by tests

#[inline(always)]
fn to_be_bytes(&self) -> Self::Bytes {
self.to_be_bytes_vec()
}

Check warning on line 72 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L70-L72

Added lines #L70 - L72 were not covered by tests
}

// TODO: PrimInt
impl<const BITS: usize, const LIMBS: usize> CheckedAdd for Uint<BITS, LIMBS> {
#[inline(always)]
fn checked_add(&self, other: &Self) -> Option<Self> {
<Self>::checked_add(*self, *other)
}

Check warning on line 79 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L77-L79

Added lines #L77 - L79 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> CheckedDiv for Uint<BITS, LIMBS> {
#[inline(always)]
fn checked_div(&self, other: &Self) -> Option<Self> {
<Self>::checked_div(*self, *other)
}

Check warning on line 86 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L84-L86

Added lines #L84 - L86 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> CheckedMul for Uint<BITS, LIMBS> {
#[inline(always)]
fn checked_mul(&self, other: &Self) -> Option<Self> {
<Self>::checked_mul(*self, *other)
}

Check warning on line 93 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L91-L93

Added lines #L91 - L93 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> CheckedNeg for Uint<BITS, LIMBS> {
#[inline(always)]
fn checked_neg(&self) -> Option<Self> {
<Self>::checked_neg(*self)
}

Check warning on line 100 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L98-L100

Added lines #L98 - L100 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> CheckedRem for Uint<BITS, LIMBS> {
#[inline(always)]
fn checked_rem(&self, other: &Self) -> Option<Self> {
<Self>::checked_rem(*self, *other)
}

Check warning on line 107 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L105-L107

Added lines #L105 - L107 were not covered by tests
}

// impl<const BITS: usize, const LIMBS: usize> CheckedShl for Uint<BITS, LIMBS>
// { fn checked_shl(&self, other: u32) -> Option<Self> {
// Uint::checked_shl(self, other as usize)
// }
// }

// impl<const BITS: usize, const LIMBS: usize> CheckedShr for Uint<BITS, LIMBS>
// { fn checked_shr(&self, other: u32) -> Option<Self> {
// Uint::checked_shr(self, other as usize)
// }
// }

impl<const BITS: usize, const LIMBS: usize> CheckedSub for Uint<BITS, LIMBS> {
#[inline(always)]
fn checked_sub(&self, other: &Self) -> Option<Self> {
<Self>::checked_sub(*self, *other)
}

Check warning on line 126 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L124-L126

Added lines #L124 - L126 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> CheckedEuclid for Uint<BITS, LIMBS> {
#[inline(always)]
fn checked_div_euclid(&self, v: &Self) -> Option<Self> {
<Self>::checked_div(*self, *v)
}

Check warning on line 133 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L131-L133

Added lines #L131 - L133 were not covered by tests

#[inline(always)]
fn checked_rem_euclid(&self, v: &Self) -> Option<Self> {
<Self>::checked_rem(*self, *v)
}

Check warning on line 138 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L136-L138

Added lines #L136 - L138 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> Euclid for Uint<BITS, LIMBS> {
#[inline(always)]
fn div_euclid(&self, v: &Self) -> Self {
<Self>::wrapping_div(*self, *v)
}

Check warning on line 145 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L143-L145

Added lines #L143 - L145 were not covered by tests

#[inline(always)]
fn rem_euclid(&self, v: &Self) -> Self {
<Self>::wrapping_rem(*self, *v)
}

Check warning on line 150 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L148-L150

Added lines #L148 - L150 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> Inv for Uint<BITS, LIMBS> {
type Output = Option<Self>;

#[inline(always)]
fn inv(self) -> Self::Output {
<Self>::inv_ring(self)
}

Check warning on line 159 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L157-L159

Added lines #L157 - L159 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> MulAdd for Uint<BITS, LIMBS> {
type Output = Self;

#[inline(always)]
fn mul_add(self, a: Self, b: Self) -> Self::Output {
// OPT: Expose actual merged mul_add algo.
(self * a) + b
}

Check warning on line 169 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L166-L169

Added lines #L166 - L169 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> MulAddAssign for Uint<BITS, LIMBS> {
#[inline(always)]
fn mul_add_assign(&mut self, a: Self, b: Self) {
*self *= a;
*self += b;
}

Check warning on line 177 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L174-L177

Added lines #L174 - L177 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> Saturating for Uint<BITS, LIMBS> {
#[inline(always)]
fn saturating_add(self, v: Self) -> Self {
<Self>::saturating_add(self, v)
}

Check warning on line 184 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L182-L184

Added lines #L182 - L184 were not covered by tests

#[inline(always)]
fn saturating_sub(self, v: Self) -> Self {
<Self>::saturating_sub(self, v)
}

Check warning on line 189 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L187-L189

Added lines #L187 - L189 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> SaturatingAdd for Uint<BITS, LIMBS> {
#[inline(always)]
fn saturating_add(&self, v: &Self) -> Self {
<Self>::saturating_add(*self, *v)
}

Check warning on line 196 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L194-L196

Added lines #L194 - L196 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> SaturatingSub for Uint<BITS, LIMBS> {
#[inline(always)]
fn saturating_sub(&self, v: &Self) -> Self {
<Self>::saturating_sub(*self, *v)
}

Check warning on line 203 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L201-L203

Added lines #L201 - L203 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> SaturatingMul for Uint<BITS, LIMBS> {
#[inline(always)]
fn saturating_mul(&self, v: &Self) -> Self {
<Self>::saturating_mul(*self, *v)
}

Check warning on line 210 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L208-L210

Added lines #L208 - L210 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> Num for Uint<BITS, LIMBS> {
type FromStrRadixErr = crate::ParseError;

fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
<Self>::from_str_radix(str, radix as u64)
}

Check warning on line 218 in src/support/num_traits.rs

View check run for this annotation

Codecov / codecov/patch

src/support/num_traits.rs#L216-L218

Added lines #L216 - L218 were not covered by tests
}

impl<const BITS: usize, const LIMBS: usize> Unsigned for Uint<BITS, LIMBS> {}

#[cfg(test)]
mod tests {
use super::*;
use crate::aliases::U256;

#[test]
fn test_assert_impl() {
fn assert_impl<T: Num + NumOps + NumAssign + NumAssignOps + NumAssignRef + NumRef>() {}

assert_impl::<U256>();
}
}

0 comments on commit b409b6e

Please sign in to comment.