dashu_int/third_party/
num_traits.rs

1//! Implement num-traits traits.
2
3use crate::{ibig::IBig, ops::Abs, ubig::UBig, Sign};
4use dashu_base::{DivEuclid, ParseError, RemEuclid};
5use num_traits_v02 as num_traits;
6
7impl num_traits::Zero for UBig {
8    #[inline]
9    fn zero() -> Self {
10        UBig::ZERO
11    }
12    #[inline]
13    fn is_zero(&self) -> bool {
14        UBig::is_zero(self)
15    }
16}
17
18impl num_traits::Zero for IBig {
19    #[inline]
20    fn zero() -> Self {
21        IBig::ZERO
22    }
23    #[inline]
24    fn is_zero(&self) -> bool {
25        IBig::is_zero(self)
26    }
27}
28
29impl num_traits::One for UBig {
30    #[inline]
31    fn one() -> Self {
32        UBig::ONE
33    }
34    #[inline]
35    fn is_one(&self) -> bool {
36        UBig::is_one(self)
37    }
38}
39
40impl num_traits::One for IBig {
41    #[inline]
42    fn one() -> Self {
43        IBig::ONE
44    }
45    #[inline]
46    fn is_one(&self) -> bool {
47        IBig::is_one(self)
48    }
49}
50
51impl num_traits::Pow<usize> for UBig {
52    type Output = UBig;
53
54    #[inline]
55    #[allow(clippy::needless_borrow)]
56    fn pow(self, rhs: usize) -> UBig {
57        (&self).pow(rhs)
58    }
59}
60
61impl num_traits::Pow<usize> for &UBig {
62    type Output = UBig;
63
64    #[inline]
65    fn pow(self, rhs: usize) -> UBig {
66        self.pow(rhs)
67    }
68}
69
70impl num_traits::Pow<usize> for IBig {
71    type Output = IBig;
72
73    #[inline]
74    #[allow(clippy::needless_borrow)]
75    fn pow(self, rhs: usize) -> IBig {
76        (&self).pow(rhs)
77    }
78}
79
80impl num_traits::Pow<usize> for &IBig {
81    type Output = IBig;
82
83    #[inline]
84    fn pow(self, rhs: usize) -> IBig {
85        self.pow(rhs)
86    }
87}
88
89impl num_traits::Unsigned for UBig {}
90
91impl num_traits::Signed for IBig {
92    #[inline]
93    fn abs(&self) -> Self {
94        Abs::abs(self)
95    }
96
97    #[inline]
98    fn abs_sub(&self, other: &Self) -> Self {
99        Abs::abs(self - other)
100    }
101
102    #[inline]
103    fn signum(&self) -> Self {
104        self.signum()
105    }
106
107    #[inline]
108    fn is_positive(&self) -> bool {
109        !self.is_zero() && self.sign() == Sign::Positive
110    }
111
112    #[inline]
113    fn is_negative(&self) -> bool {
114        self.sign() == Sign::Negative
115    }
116}
117
118impl num_traits::Num for UBig {
119    type FromStrRadixErr = ParseError;
120
121    fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseError> {
122        Self::from_str_radix(s, radix)
123    }
124}
125
126impl num_traits::Num for IBig {
127    type FromStrRadixErr = ParseError;
128
129    fn from_str_radix(s: &str, radix: u32) -> Result<Self, ParseError> {
130        Self::from_str_radix(s, radix)
131    }
132}
133
134impl num_traits::Euclid for UBig {
135    #[inline]
136    fn div_euclid(&self, v: &Self) -> Self {
137        DivEuclid::div_euclid(self, v)
138    }
139    #[inline]
140    fn rem_euclid(&self, v: &Self) -> Self {
141        RemEuclid::rem_euclid(self, v)
142    }
143}
144
145impl num_traits::Euclid for IBig {
146    #[inline]
147    fn div_euclid(&self, v: &Self) -> Self {
148        DivEuclid::div_euclid(self, v)
149    }
150    #[inline]
151    fn rem_euclid(&self, v: &Self) -> Self {
152        RemEuclid::rem_euclid(self, v).into()
153    }
154}
155
156macro_rules! impl_to_primitive_int {
157    ($t:ty, $method:ident) => {
158        #[inline]
159        fn $method(&self) -> Option<$t> {
160            self.try_into().ok()
161        }
162    };
163}
164
165impl num_traits::ToPrimitive for UBig {
166    impl_to_primitive_int!(i8, to_i8);
167    impl_to_primitive_int!(i16, to_i16);
168    impl_to_primitive_int!(i32, to_i32);
169    impl_to_primitive_int!(i64, to_i64);
170    impl_to_primitive_int!(i128, to_i128);
171    impl_to_primitive_int!(isize, to_isize);
172    impl_to_primitive_int!(u8, to_u8);
173    impl_to_primitive_int!(u16, to_u16);
174    impl_to_primitive_int!(u32, to_u32);
175    impl_to_primitive_int!(u64, to_u64);
176    impl_to_primitive_int!(u128, to_u128);
177    impl_to_primitive_int!(usize, to_usize);
178
179    #[inline]
180    fn to_f32(&self) -> Option<f32> {
181        Some(self.to_f32().value())
182    }
183    #[inline]
184    fn to_f64(&self) -> Option<f64> {
185        Some(self.to_f64().value())
186    }
187}
188
189impl num_traits::ToPrimitive for IBig {
190    impl_to_primitive_int!(i8, to_i8);
191    impl_to_primitive_int!(i16, to_i16);
192    impl_to_primitive_int!(i32, to_i32);
193    impl_to_primitive_int!(i64, to_i64);
194    impl_to_primitive_int!(i128, to_i128);
195    impl_to_primitive_int!(isize, to_isize);
196    impl_to_primitive_int!(u8, to_u8);
197    impl_to_primitive_int!(u16, to_u16);
198    impl_to_primitive_int!(u32, to_u32);
199    impl_to_primitive_int!(u64, to_u64);
200    impl_to_primitive_int!(u128, to_u128);
201    impl_to_primitive_int!(usize, to_usize);
202
203    #[inline]
204    fn to_f32(&self) -> Option<f32> {
205        Some(self.to_f32().value())
206    }
207    #[inline]
208    fn to_f64(&self) -> Option<f64> {
209        Some(self.to_f64().value())
210    }
211}
212
213macro_rules! impl_from_primitive_int {
214    ($t:ty, $method:ident) => {
215        #[inline]
216        fn $method(n: $t) -> Option<Self> {
217            Some(n.into())
218        }
219    };
220}
221
222macro_rules! impl_unsigned_from_primitive_int {
223    ($t:ty, $method:ident) => {
224        #[inline]
225        fn $method(n: $t) -> Option<Self> {
226            n.try_into().ok()
227        }
228    };
229}
230
231impl num_traits::FromPrimitive for UBig {
232    impl_unsigned_from_primitive_int!(i8, from_i8);
233    impl_unsigned_from_primitive_int!(i16, from_i16);
234    impl_unsigned_from_primitive_int!(i32, from_i32);
235    impl_unsigned_from_primitive_int!(i64, from_i64);
236    impl_unsigned_from_primitive_int!(i128, from_i128);
237    impl_unsigned_from_primitive_int!(isize, from_isize);
238    impl_from_primitive_int!(u8, from_u8);
239    impl_from_primitive_int!(u16, from_u16);
240    impl_from_primitive_int!(u32, from_u32);
241    impl_from_primitive_int!(u64, from_u64);
242    impl_from_primitive_int!(u128, from_u128);
243    impl_from_primitive_int!(usize, from_usize);
244
245    #[inline]
246    fn from_f32(n: f32) -> Option<Self> {
247        n.try_into().ok()
248    }
249    #[inline]
250    fn from_f64(n: f64) -> Option<Self> {
251        n.try_into().ok()
252    }
253}
254
255impl num_traits::FromPrimitive for IBig {
256    impl_from_primitive_int!(i8, from_i8);
257    impl_from_primitive_int!(i16, from_i16);
258    impl_from_primitive_int!(i32, from_i32);
259    impl_from_primitive_int!(i64, from_i64);
260    impl_from_primitive_int!(i128, from_i128);
261    impl_from_primitive_int!(isize, from_isize);
262    impl_from_primitive_int!(u8, from_u8);
263    impl_from_primitive_int!(u16, from_u16);
264    impl_from_primitive_int!(u32, from_u32);
265    impl_from_primitive_int!(u64, from_u64);
266    impl_from_primitive_int!(u128, from_u128);
267    impl_from_primitive_int!(usize, from_usize);
268
269    #[inline]
270    fn from_f32(n: f32) -> Option<Self> {
271        n.try_into().ok()
272    }
273    #[inline]
274    fn from_f64(n: f64) -> Option<Self> {
275        n.try_into().ok()
276    }
277}