bnum/bint/
const_trait_fillers.rs

1use crate::doc;
2use crate::ExpType;
3use core::cmp::Ordering;
4
5macro_rules! const_trait_fillers {
6    ($BUint: ident, $BInt: ident, $Digit: ident) => {
7        #[doc = doc::const_trait_fillers::impl_desc!()]
8        impl<const N: usize> $BInt<N> {
9            #[inline]
10            pub const fn bitand(self, rhs: Self) -> Self {
11                Self::from_bits(self.bits.bitand(rhs.bits))
12            }
13
14            #[inline]
15            pub const fn bitor(self, rhs: Self) -> Self {
16                Self::from_bits(self.bits.bitor(rhs.bits))
17            }
18
19            #[inline]
20            pub const fn bitxor(self, rhs: Self) -> Self {
21                Self::from_bits(self.bits.bitxor(rhs.bits))
22            }
23
24            #[inline]
25            pub const fn not(self) -> Self {
26                Self::from_bits(self.bits.not())
27            }
28
29            #[inline]
30            pub const fn eq(&self, other: &Self) -> bool {
31                $BUint::eq(&self.bits, &other.bits)
32            }
33
34            #[inline]
35            pub const fn ne(&self, other: &Self) -> bool {
36                !Self::eq(self, other)
37            }
38
39            #[inline]
40            pub const fn cmp(&self, other: &Self) -> Ordering {
41                let s1 = self.signed_digit();
42                let s2 = other.signed_digit();
43
44                // Don't use match here as `cmp` is not yet const for primitive integers
45                #[allow(clippy::comparison_chain)]
46                if s1 == s2 {
47                    $BUint::cmp(&self.bits, &other.bits)
48                } else if s1 > s2 {
49                    Ordering::Greater
50                } else {
51                    Ordering::Less
52                }
53            }
54
55            crate::int::cmp::impls!();
56            #[inline]
57            pub const fn neg(self) -> Self {
58                #[cfg(debug_assertions)]
59                return self.strict_neg();
60
61                #[cfg(not(debug_assertions))]
62                self.wrapping_neg()
63            }
64
65            crate::int::ops::trait_fillers!();
66
67            #[inline]
68            pub const fn div(self, rhs: Self) -> Self {
69                if self.eq(&Self::MIN) && rhs.eq(&Self::NEG_ONE) {
70                    panic!(crate::errors::err_msg!("attempt to divide with overflow"))
71                } else {
72                    if rhs.is_zero() {
73                        crate::errors::div_zero!()
74                    }
75                    self.div_rem_unchecked(rhs).0
76                }
77            }
78
79            #[inline]
80            pub const fn rem(self, rhs: Self) -> Self {
81                if self.eq(&Self::MIN) && rhs.eq(&Self::NEG_ONE) {
82                    panic!(crate::errors::err_msg!("attempt to calculate remainder with overflow"))
83                } else {
84                    if rhs.is_zero() {
85                        crate::errors::rem_zero!()
86                    }
87                    self.div_rem_unchecked(rhs).1
88                }
89            }
90        }
91    };
92}
93
94crate::macro_impl!(const_trait_fillers);