ruint/
macros.rs

1/// Wrapper for [`ruint_macro::uint!`]. See its documentation for details.
2#[macro_export]
3#[cfg(not(doc))] // Show the actual macro in docs.
4#[doc(hidden)]
5macro_rules! uint {
6    ($($t:tt)*) => {
7        $crate::__private::ruint_macro::uint_with_path! { [$crate] $($t)* }
8    }
9}
10
11macro_rules! impl_bin_op {
12    ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident, $fdel:ident) => {
13        impl<const BITS: usize, const LIMBS: usize> $trait_assign<Uint<BITS, LIMBS>>
14            for Uint<BITS, LIMBS>
15        {
16            #[inline(always)]
17            #[track_caller]
18            fn $fn_assign(&mut self, rhs: Uint<BITS, LIMBS>) {
19                *self = self.$fdel(rhs);
20            }
21        }
22        impl<const BITS: usize, const LIMBS: usize> $trait_assign<&Uint<BITS, LIMBS>>
23            for Uint<BITS, LIMBS>
24        {
25            #[inline(always)]
26            #[track_caller]
27            fn $fn_assign(&mut self, rhs: &Uint<BITS, LIMBS>) {
28                *self = self.$fdel(*rhs);
29            }
30        }
31        impl<const BITS: usize, const LIMBS: usize> $trait<Uint<BITS, LIMBS>>
32            for Uint<BITS, LIMBS>
33        {
34            type Output = Uint<BITS, LIMBS>;
35
36            #[inline(always)]
37            #[track_caller]
38            fn $fn(self, rhs: Uint<BITS, LIMBS>) -> Self::Output {
39                self.$fdel(rhs)
40            }
41        }
42        impl<const BITS: usize, const LIMBS: usize> $trait<&Uint<BITS, LIMBS>>
43            for Uint<BITS, LIMBS>
44        {
45            type Output = Uint<BITS, LIMBS>;
46
47            #[inline(always)]
48            #[track_caller]
49            fn $fn(self, rhs: &Uint<BITS, LIMBS>) -> Self::Output {
50                self.$fdel(*rhs)
51            }
52        }
53        impl<const BITS: usize, const LIMBS: usize> $trait<Uint<BITS, LIMBS>>
54            for &Uint<BITS, LIMBS>
55        {
56            type Output = Uint<BITS, LIMBS>;
57
58            #[inline(always)]
59            #[track_caller]
60            fn $fn(self, rhs: Uint<BITS, LIMBS>) -> Self::Output {
61                self.$fdel(rhs)
62            }
63        }
64        impl<const BITS: usize, const LIMBS: usize> $trait<&Uint<BITS, LIMBS>>
65            for &Uint<BITS, LIMBS>
66        {
67            type Output = Uint<BITS, LIMBS>;
68
69            #[inline(always)]
70            #[track_caller]
71            fn $fn(self, rhs: &Uint<BITS, LIMBS>) -> Self::Output {
72                self.$fdel(*rhs)
73            }
74        }
75    };
76}
77
78#[allow(unused)]
79macro_rules! assume {
80    ($e:expr $(,)?) => {
81        if !$e {
82            debug_unreachable!(stringify!($e));
83        }
84    };
85
86    ($e:expr, $($t:tt)+) => {
87        if !$e {
88            debug_unreachable!($($t)+);
89        }
90    };
91}
92
93#[allow(unused)]
94macro_rules! debug_unreachable {
95    ($($t:tt)*) => {
96        if cfg!(debug_assertions) {
97            unreachable!($($t)*);
98        } else {
99            unsafe { core::hint::unreachable_unchecked() };
100        }
101    };
102}
103
104#[cfg(test)]
105mod tests {
106    // https://github.com/recmo/uint/issues/359
107    ruint_macro::uint_with_path! {
108        [crate]
109        const _A: [crate::aliases::U256; 2] = [
110            0x00006f85d6f68a85ec10345351a23a3aaf07f38af8c952a7bceca70bd2af7ad5_U256,
111            0x00004b4110c9ae997782e1509b1d0fdb20a7c02bbd8bea7305462b9f8125b1e8_U256,
112        ];
113    }
114
115    crate::uint! {
116        const _B: [crate::aliases::U256; 2] = [
117            0x00006f85d6f68a85ec10345351a23a3aaf07f38af8c952a7bceca70bd2af7ad5_U256,
118            0x00004b4110c9ae997782e1509b1d0fdb20a7c02bbd8bea7305462b9f8125b1e8_U256,
119        ];
120    }
121
122    #[test]
123    fn test_uint_macro_with_paths() {
124        extern crate self as aaa;
125        use crate as ruint;
126        use crate as __ruint;
127        let value = crate::aliases::U256::from(0x10);
128        assert_eq!(value, uint!(0x10U256));
129        assert_eq!(value, ruint_macro::uint_with_path!([crate] 0x10U256));
130        assert_eq!(value, ruint_macro::uint_with_path!([aaa] 0x10U256));
131        assert_eq!(value, ruint_macro::uint_with_path!([aaa] 0x10U256));
132        assert_eq!(value, ruint_macro::uint_with_path!([ruint] 0x10U256));
133        assert_eq!(value, ruint_macro::uint_with_path!([__ruint] 0x10U256));
134    }
135}