/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bnum-0.12.1/src/helpers.rs
Line | Count | Source |
1 | | use crate::ExpType; |
2 | | |
3 | | pub trait Bits { |
4 | | const BITS: ExpType; |
5 | | |
6 | | fn bits(&self) -> ExpType; |
7 | | fn bit(&self, index: ExpType) -> bool; |
8 | | } |
9 | | |
10 | | macro_rules! impl_bits_for_uint { |
11 | | ($($uint: ty), *) => { |
12 | | $(impl Bits for $uint { |
13 | | const BITS: ExpType = Self::BITS as ExpType; |
14 | | |
15 | | #[inline] |
16 | 0 | fn bits(&self) -> ExpType { |
17 | 0 | (Self::BITS - self.leading_zeros()) as ExpType |
18 | 0 | } Unexecuted instantiation: <u8 as bnum::helpers::Bits>::bits Unexecuted instantiation: <u32 as bnum::helpers::Bits>::bits Unexecuted instantiation: <u64 as bnum::helpers::Bits>::bits Unexecuted instantiation: <u128 as bnum::helpers::Bits>::bits Unexecuted instantiation: <usize as bnum::helpers::Bits>::bits Unexecuted instantiation: <u16 as bnum::helpers::Bits>::bits |
19 | | |
20 | | #[inline] |
21 | 0 | fn bit(&self, index: ExpType) -> bool { |
22 | 0 | self & (1 << index) != 0 |
23 | 0 | } Unexecuted instantiation: <u8 as bnum::helpers::Bits>::bit Unexecuted instantiation: <u32 as bnum::helpers::Bits>::bit Unexecuted instantiation: <u64 as bnum::helpers::Bits>::bit Unexecuted instantiation: <u128 as bnum::helpers::Bits>::bit Unexecuted instantiation: <usize as bnum::helpers::Bits>::bit Unexecuted instantiation: <u16 as bnum::helpers::Bits>::bit |
24 | | })* |
25 | | }; |
26 | | } |
27 | | |
28 | | impl_bits_for_uint!(u8, u16, u32, u64, u128, usize); |
29 | | |
30 | | macro_rules! impl_bits_for_buint { |
31 | | ($BUint: ident, $BInt: ident, $Digit: ident) => { |
32 | | impl<const N: usize> crate::helpers::Bits for $BUint<N> { |
33 | | const BITS: ExpType = Self::BITS; |
34 | | |
35 | | #[inline] |
36 | 0 | fn bits(&self) -> ExpType { |
37 | 0 | Self::bits(&self) |
38 | 0 | } Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::helpers::Bits>::bits Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::helpers::Bits>::bits Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::helpers::Bits>::bits Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::helpers::Bits>::bits |
39 | | |
40 | | #[inline] |
41 | 0 | fn bit(&self, index: ExpType) -> bool { |
42 | 0 | Self::bit(&self, index) |
43 | 0 | } Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::helpers::Bits>::bit Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::helpers::Bits>::bit Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::helpers::Bits>::bit Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::helpers::Bits>::bit |
44 | | } |
45 | | }; |
46 | | } |
47 | | |
48 | | crate::macro_impl!(impl_bits_for_buint); |
49 | | |
50 | | pub trait Zero: Sized + PartialEq { |
51 | | const ZERO: Self; |
52 | | |
53 | 0 | fn is_zero(&self) -> bool { |
54 | 0 | self == &Self::ZERO |
55 | 0 | } |
56 | | } |
57 | | |
58 | | pub trait One: Sized + PartialEq { |
59 | | const ONE: Self; |
60 | | |
61 | 0 | fn is_one(&self) -> bool { |
62 | 0 | self == &Self::ONE |
63 | 0 | } |
64 | | } |
65 | | |
66 | | macro_rules! impl_zero_for_uint { |
67 | | ($($uint: ty), *) => { |
68 | | $(impl Zero for $uint { |
69 | | const ZERO: Self = 0; |
70 | | })* |
71 | | }; |
72 | | } |
73 | | |
74 | | impl_zero_for_uint!(u8, u16, u32, u64, u128, usize); |
75 | | |
76 | | macro_rules! impl_one_for_int { |
77 | | ($($uint: ty), *) => { |
78 | | $(impl One for $uint { |
79 | | const ONE: Self = 1; |
80 | | })* |
81 | | }; |
82 | | } |
83 | | |
84 | | impl_one_for_int!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); |
85 | | |
86 | | macro_rules! impl_zero_for_buint { |
87 | | ($BUint: ident, $BInt: ident, $Digit: ident) => { |
88 | | impl<const N: usize> crate::helpers::Zero for $BUint<N> { |
89 | | const ZERO: Self = Self::ZERO; |
90 | | } |
91 | | }; |
92 | | } |
93 | | |
94 | | crate::macro_impl!(impl_zero_for_buint); |
95 | | |
96 | | macro_rules! impl_one_for_buint { |
97 | | ($BUint: ident, $BInt: ident, $Digit: ident) => { |
98 | | impl<const N: usize> crate::helpers::One for $BUint<N> { |
99 | | const ONE: Self = Self::ONE; |
100 | | } |
101 | | }; |
102 | | } |
103 | | |
104 | | crate::macro_impl!(impl_one_for_buint); |
105 | | |
106 | | #[inline] |
107 | 0 | pub const fn tuple_to_option<T: Copy>((int, overflow): (T, bool)) -> Option<T> { |
108 | 0 | if overflow { |
109 | 0 | None |
110 | | } else { |
111 | 0 | Some(int) |
112 | | } |
113 | 0 | } Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<16>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<17>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<1>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<2>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<3>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<4>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<5>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<6>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<7>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<8>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<9>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<10>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<11>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<12>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<13>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<14>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<bnum::buint::BUint<15>> Unexecuted instantiation: bnum::helpers::tuple_to_option::<_> |