endian_num/
internal_macros.rs

1//! Based on <https://github.com/rust-lang/rust/blob/1.78.0/library/core/src/internal_macros.rs>.
2
3// implements the unary operator "op &T"
4// based on "op T" where T is expected to be `Copy`able
5macro_rules! forward_ref_unop {
6    (impl $Trait:ident, $method:ident for $T:ty) => {
7        impl $Trait for &$T {
8            type Output = <$T as $Trait>::Output;
9
10            #[inline]
11            #[track_caller]
12            fn $method(self) -> <$T as $Trait>::Output {
13                $Trait::$method(*self)
14            }
15        }
16    };
17}
18
19// implements binary operators "&T op Rhs", "T op &Rhs", "&T op &Rhs"
20// based on "T op Rhs" where T and Rhs are expected to be `Copy`able
21macro_rules! forward_ref_binop {
22    (impl $Trait:ident<$Rhs:ty>, $method:ident for $T:ty) => {
23        impl<'a> $Trait<$Rhs> for &'a $T {
24            type Output = <$T as $Trait<$Rhs>>::Output;
25
26            #[inline]
27            #[track_caller]
28            fn $method(self, rhs: $Rhs) -> <$T as $Trait<$Rhs>>::Output {
29                $Trait::$method(*self, rhs)
30            }
31        }
32
33        impl $Trait<&$Rhs> for $T {
34            type Output = <$T as $Trait<$Rhs>>::Output;
35
36            #[inline]
37            #[track_caller]
38            fn $method(self, rhs: &$Rhs) -> <$T as $Trait<$Rhs>>::Output {
39                $Trait::$method(self, *rhs)
40            }
41        }
42
43        impl $Trait<&$Rhs> for &$T {
44            type Output = <$T as $Trait<$Rhs>>::Output;
45
46            #[inline]
47            #[track_caller]
48            fn $method(self, rhs: &$Rhs) -> <$T as $Trait<$Rhs>>::Output {
49                $Trait::$method(*self, *rhs)
50            }
51        }
52    };
53}
54
55// implements "T op= &Rhs", based on "T op= Rhs"
56// where Rhs is expected to be `Copy`able
57macro_rules! forward_ref_op_assign {
58    (impl $Trait:ident<$Rhs:ty>, $method:ident for $T:ty) => {
59        impl $Trait<&$Rhs> for $T {
60            #[inline]
61            #[track_caller]
62            fn $method(&mut self, rhs: &$Rhs) {
63                $Trait::$method(self, *rhs);
64            }
65        }
66    };
67}