enum_ptr/utils/
shift.rs

1use crate::Aligned;
2
3/// [`isize`] that shifts left by `N` bits.
4#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Default, Hash)]
5pub struct ShiftIsize<const N: isize>(isize);
6
7impl<const N: isize> ShiftIsize<N> {
8    /// Creates a new value from an unshifted number.
9    #[inline]
10    pub fn new(val: isize) -> Self {
11        Self(val << N)
12    }
13
14    /// Returns the unshifted number.
15    #[inline]
16    pub fn get(&self) -> isize {
17        self.0 >> N
18    }
19
20    /// Sets the value by an unshifted number.
21    #[inline]
22    pub fn set(&mut self, val: isize) {
23        self.0 = val << N;
24    }
25}
26
27unsafe impl<const N: isize> Aligned for ShiftIsize<N> {
28    const ALIGNMENT: usize = 1 << N;
29}
30
31/// [`usize`] that shifts left by `N` bits.
32#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Default, Hash)]
33pub struct ShiftUsize<const N: usize>(usize);
34
35impl<const N: usize> ShiftUsize<N> {
36    /// Creates a new value from a unshifted number.
37    #[inline]
38    pub fn new(val: usize) -> Self {
39        Self(val << N)
40    }
41
42    /// Returns the unshifted number.
43    #[inline]
44    pub fn get(&self) -> usize {
45        self.0 >> N
46    }
47
48    /// Sets the value by an unshifted number.
49    #[inline]
50    pub fn set(&mut self, val: usize) {
51        self.0 = val << N;
52    }
53}
54
55unsafe impl<const N: usize> Aligned for ShiftUsize<N> {
56    const ALIGNMENT: usize = 1 << N;
57}
58
59// TODO: impl more traits for these two types, like `From<usize>`