simple_endian/
shift_ops.rs

1//! Bitshift operations, for integer types only.
2
3#[allow(unused_imports)]
4use core::ops::{Shl, ShlAssign, Shr, ShrAssign};
5
6#[allow(unused_imports)]
7use super::*;
8
9#[allow(unused_macros)]
10macro_rules! add_shift_ops {
11    ($wrap_ty:ty) => {
12        impl Shl for $wrap_ty {
13            type Output = Self;
14
15            fn shl(self, other: Self) -> Self {
16                Self::from(self.to_native() << other.to_native())
17            }
18        }
19        impl ShlAssign for $wrap_ty {
20            fn shl_assign(&mut self, rhs: Self) {
21                *self = Self::from((*self).to_native() << rhs.to_native());
22            }
23        }
24        impl Shr for $wrap_ty {
25            type Output = Self;
26
27            fn shr(self, other: Self) -> Self {
28                Self::from(self.to_native() >> other.to_native())
29            }
30        }
31        impl ShrAssign for $wrap_ty {
32            fn shr_assign(&mut self, rhs: Self) {
33                *self = Self::from((*self).to_native() >> rhs.to_native());
34            }
35        }
36    };
37}
38
39#[cfg(feature = "big_endian")]
40mod be {
41    use super::*;
42    #[cfg(feature = "byte_impls")]
43    mod bytes {
44        use super::*;
45        add_shift_ops!(BigEndian<u8>);
46        add_shift_ops!(BigEndian<i8>);
47    }
48
49    #[cfg(feature = "integer_impls")]
50    mod integers {
51        use super::*;
52        add_shift_ops!(BigEndian<u16>);
53        add_shift_ops!(BigEndian<i16>);
54        add_shift_ops!(BigEndian<u32>);
55        add_shift_ops!(BigEndian<i32>);
56        add_shift_ops!(BigEndian<u64>);
57        add_shift_ops!(BigEndian<i64>);
58        add_shift_ops!(BigEndian<u128>);
59        add_shift_ops!(BigEndian<i128>);
60        add_shift_ops!(BigEndian<usize>);
61        add_shift_ops!(BigEndian<isize>);
62    }
63}
64
65#[cfg(feature = "big_endian")]
66mod le {
67    use super::*;
68    #[cfg(feature = "byte_impls")]
69    mod bytes {
70        use super::*;
71        add_shift_ops!(LittleEndian<u8>);
72        add_shift_ops!(LittleEndian<i8>);
73    }
74
75    #[cfg(feature = "integer_impls")]
76    mod integers {
77        use super::*;
78        add_shift_ops!(LittleEndian<u16>);
79        add_shift_ops!(LittleEndian<i16>);
80        add_shift_ops!(LittleEndian<u32>);
81        add_shift_ops!(LittleEndian<i32>);
82        add_shift_ops!(LittleEndian<u64>);
83        add_shift_ops!(LittleEndian<i64>);
84        add_shift_ops!(LittleEndian<u128>);
85        add_shift_ops!(LittleEndian<i128>);
86        add_shift_ops!(LittleEndian<usize>);
87        add_shift_ops!(LittleEndian<isize>);
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use crate::*;
94
95    #[test]
96    fn shl_be() {
97        let mut ne1 = 0xfee1;
98        let mut be1 = u64be::from(ne1);
99        be1 <<= 5.into();
100        ne1 <<= 5;
101        be1 <<= 5.into();
102        ne1 <<= 5;
103        assert_eq!(ne1, be1.into());
104    }
105
106    #[test]
107    fn shr_be() {
108        let mut ne1 = 0xfee1;
109        let mut be1 = u64be::from(ne1);
110        be1 >>= 5.into();
111        ne1 >>= 5;
112        be1 >>= 5.into();
113        ne1 >>= 5;
114        assert_eq!(ne1, be1.into());
115    }
116}