fastnum/bint/int/impls/
numtraits.rs

1use num_integer::{Integer, Roots};
2use num_traits::{
3    AsPrimitive, Bounded, CheckedAdd, CheckedDiv, CheckedEuclid, CheckedMul, CheckedNeg,
4    CheckedRem, CheckedShl, CheckedShr, CheckedSub, Euclid, FromPrimitive, MulAdd, MulAddAssign,
5    Num, One, Pow, PrimInt, Saturating, SaturatingAdd, SaturatingMul, SaturatingSub, Signed,
6    ToPrimitive, WrappingAdd, WrappingMul, WrappingNeg, WrappingShl, WrappingShr, WrappingSub,
7    Zero,
8};
9
10use crate::bint::{
11    impls::numtraits::{from_primitive_impl, numtraits_impl},
12    intrinsics::ExpType,
13    Int, ParseError, UInt,
14};
15
16numtraits_impl!(Int, I);
17
18impl<const N: usize> Signed for Int<N> {
19    #[inline]
20    fn abs(&self) -> Self {
21        Self::abs(*self)
22    }
23
24    #[inline]
25    fn abs_sub(&self, other: &Self) -> Self {
26        if *self <= *other {
27            Self::ZERO
28        } else {
29            *self - *other
30        }
31    }
32
33    #[inline]
34    fn signum(&self) -> Self {
35        Self::signum(*self)
36    }
37
38    #[inline]
39    fn is_positive(&self) -> bool {
40        Self::is_positive(*self)
41    }
42
43    #[inline]
44    fn is_negative(&self) -> bool {
45        Self::is_negative(*self)
46    }
47}
48
49impl<const N: usize, const M: usize> AsPrimitive<UInt<M>> for Int<N> {
50    #[inline]
51    fn as_(self) -> UInt<M> {
52        UInt(bnum::cast::CastFrom::cast_from(self.0))
53    }
54}
55
56impl<const N: usize, const M: usize> AsPrimitive<Int<M>> for Int<N> {
57    #[inline]
58    fn as_(self) -> Int<M> {
59        Int(bnum::cast::CastFrom::cast_from(self.0))
60    }
61}
62
63from_primitive_impl!(
64    Int, I,
65    from_u8 <- u8,
66    from_u16 <- u16,
67    from_u32 <- u32,
68    from_u64 <- u64 #TRY,
69    from_usize <- usize #TRY,
70    from_u128 <- u128 #TRY,
71
72    from_i8 <- i8,
73    from_i16 <- i16,
74    from_i32 <- i32,
75    from_i64 <- i64,
76    from_isize <- isize,
77    from_i128 <- i128 #TRY,
78
79    from_f32 <- f32 #TRY,
80    from_f64 <- f64 #TRY
81);