|
| 1 | +pub(super) const WIDTH: usize = std::mem::size_of::<usize>() * 8; |
| 2 | + |
| 3 | +/// Trait encapsulating the calculations required for bit-packing slab indices. |
| 4 | +/// |
| 5 | +/// This allows us to avoid manually repeating some calculations when packing |
| 6 | +/// and unpacking indices. |
| 7 | +pub(crate) trait Pack: Sized { |
| 8 | + // ====== provided by each implementation ================================= |
| 9 | + |
| 10 | + /// The number of bits occupied by this type when packed into a usize. |
| 11 | + /// |
| 12 | + /// This must be provided to determine the number of bits into which to pack |
| 13 | + /// the type. |
| 14 | + const LEN: usize; |
| 15 | + /// The type packed on the less significant side of this type. |
| 16 | + /// |
| 17 | + /// If this type is packed into the least significant bit of a usize, this |
| 18 | + /// should be `()`, which occupies no bytes. |
| 19 | + /// |
| 20 | + /// This is used to calculate the shift amount for packing this value. |
| 21 | + type Prev: Pack; |
| 22 | + |
| 23 | + // ====== calculated automatically ======================================== |
| 24 | + |
| 25 | + /// A number consisting of `Self::LEN` 1 bits, starting at the least |
| 26 | + /// significant bit. |
| 27 | + /// |
| 28 | + /// This is the higest value this type can represent. This number is shifted |
| 29 | + /// left by `Self::SHIFT` bits to calculate this type's `MASK`. |
| 30 | + /// |
| 31 | + /// This is computed automatically based on `Self::LEN`. |
| 32 | + const BITS: usize = { |
| 33 | + let shift = 1 << (Self::LEN - 1); |
| 34 | + shift | (shift - 1) |
| 35 | + }; |
| 36 | + /// The number of bits to shift a number to pack it into a usize with other |
| 37 | + /// values. |
| 38 | + /// |
| 39 | + /// This is caculated automatically based on the `LEN` and `SHIFT` constants |
| 40 | + /// of the previous value. |
| 41 | + const SHIFT: usize = Self::Prev::SHIFT + Self::Prev::LEN; |
| 42 | + |
| 43 | + /// The mask to extract only this type from a packed `usize`. |
| 44 | + /// |
| 45 | + /// This is calculated by shifting `Self::BITS` left by `Self::SHIFT`. |
| 46 | + const MASK: usize = Self::BITS << Self::SHIFT; |
| 47 | + |
| 48 | + fn as_usize(&self) -> usize; |
| 49 | + fn from_usize(val: usize) -> Self; |
| 50 | + |
| 51 | + #[inline(always)] |
| 52 | + fn pack(&self, to: usize) -> usize { |
| 53 | + let value = self.as_usize(); |
| 54 | + debug_assert!(value <= Self::BITS); |
| 55 | + |
| 56 | + (to & !Self::MASK) | (value << Self::SHIFT) |
| 57 | + } |
| 58 | + |
| 59 | + #[inline(always)] |
| 60 | + fn from_packed(from: usize) -> Self { |
| 61 | + let value = (from & Self::MASK) >> Self::SHIFT; |
| 62 | + debug_assert!(value <= Self::BITS); |
| 63 | + Self::from_usize(value) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Pack for () { |
| 68 | + const BITS: usize = 0; |
| 69 | + const LEN: usize = 0; |
| 70 | + const SHIFT: usize = 0; |
| 71 | + const MASK: usize = 0; |
| 72 | + |
| 73 | + type Prev = (); |
| 74 | + |
| 75 | + fn as_usize(&self) -> usize { |
| 76 | + unreachable!() |
| 77 | + } |
| 78 | + fn from_usize(_val: usize) -> Self { |
| 79 | + unreachable!() |
| 80 | + } |
| 81 | + |
| 82 | + fn pack(&self, _to: usize) -> usize { |
| 83 | + unreachable!() |
| 84 | + } |
| 85 | + |
| 86 | + fn from_packed(_from: usize) -> Self { |
| 87 | + unreachable!() |
| 88 | + } |
| 89 | +} |
0 commit comments