reweb3_num/buint/
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> $BUint<N> {
9            #[inline]
10            pub const fn bitand(self, rhs: Self) -> Self {
11                let mut out = Self::ZERO;
12                let mut i = 0;
13                while i < N {
14                    out.digits[i] = self.digits[i] & rhs.digits[i];
15                    i += 1;
16                }
17                out
18            }
19
20            #[inline]
21            pub const fn bitor(self, rhs: Self) -> Self {
22                let mut out = Self::ZERO;
23                let mut i = 0;
24                while i < N {
25                    out.digits[i] = self.digits[i] | rhs.digits[i];
26                    i += 1;
27                }
28                out
29            }
30
31            #[inline]
32            pub const fn bitxor(self, rhs: Self) -> Self {
33                let mut out = Self::ZERO;
34                let mut i = 0;
35                while i < N {
36                    out.digits[i] = self.digits[i] ^ rhs.digits[i];
37                    i += 1;
38                }
39                out
40            }
41
42            #[inline]
43            pub const fn not(self) -> Self {
44                let mut out = Self::ZERO;
45                let mut i = 0;
46                while i < N {
47                    out.digits[i] = !self.digits[i];
48                    i += 1;
49                }
50                out
51            }
52
53            #[inline]
54            pub const fn eq(&self, other: &Self) -> bool {
55                let mut i = 0;
56                while i < N {
57                    if self.digits[i] != other.digits[i] {
58                        return false;
59                    }
60                    i += 1;
61                }
62                true
63            }
64
65            #[inline]
66            pub const fn ne(&self, other: &Self) -> bool {
67                !Self::eq(self, other)
68            }
69
70            #[inline]
71            pub const fn cmp(&self, other: &Self) -> Ordering {
72                let mut i = N;
73                while i > 0 {
74                    i -= 1;
75                    let a = self.digits[i];
76                    let b = other.digits[i];
77
78                    // Clippy: don't use match here as `cmp` is not yet const for primitive integers
79                    #[allow(clippy::comparison_chain)]
80                    if a > b {
81                        return Ordering::Greater;
82                    } else if a < b {
83                        return Ordering::Less;
84                    }
85                }
86                Ordering::Equal
87            }
88
89            crate::int::cmp::impls!();
90
91            crate::int::ops::trait_fillers!();
92
93            #[inline]
94            pub const fn div(self, rhs: Self) -> Self {
95                self.wrapping_div(rhs)
96            }
97
98            #[inline]
99            pub const fn rem(self, rhs: Self) -> Self {
100                self.wrapping_rem(rhs)
101            }
102        }
103    };
104}
105
106crate::macro_impl!(const_trait_fillers);