1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
//! Traits for dealing with bitset endianness.
//!
//! Endianness typically refers to the order in which bytes are in memory, but
//! here the concept refers to the order in which bits are addressed when you
//! consider the binary literal of a primitive.
//!
//! * [`BigEndian`] where the bit furthest to the left refers to the highest
//! index.
//! * [`LittleEndian`] where the bit furthest to the left refers to the lowest
//! index.
#![allow(clippy::module_name_repetitions)]
use crate::number::Number;
mod sealed {
pub trait Sealed {}
impl Sealed for super::BigEndian {}
impl Sealed for super::LittleEndian {}
}
/// Trait governing endian-dependent operations for a primitive.
pub trait Endian: self::sealed::Sealed {
#[doc(hidden)]
fn mask<T>(index: u32) -> T
where
T: Number;
#[doc(hidden)]
fn ones<T>(value: T) -> u32
where
T: Number;
#[doc(hidden)]
fn ones_rev<T>(value: T) -> u32
where
T: Number;
#[doc(hidden)]
fn zeros<T>(value: T) -> u32
where
T: Number;
#[doc(hidden)]
fn zeros_rev<T>(value: T) -> u32
where
T: Number;
}
/// Big-endian indexing for bit sets.
///
/// This can be used in combination with methods such as [`Bits::test_bit_in`].
///
/// Big-endian indexing is constructed increasingly from right to left for
/// individual primitives, such as the following [`u8`] literal:
///
/// ```text
/// 0b0010_0010u8
/// ^ ^- index 1
/// '------ index 5
/// ```
///
/// Arrays are treated the same as expected where the index grows from smallest
/// to largest address, but each interior primitive is indexed in big endian
/// ordering.
///
/// ```text
/// 0 --------- 8 8 -------- 15
/// [0b0010_0010u8, 0b1000_0000u8]
/// ^ ^ ^- index 15
/// | '--------- index 1
/// '-------------- index 5
/// ```
///
/// [`Bits::test_bit_in`]: crate::Bits::test_bit_in
#[non_exhaustive]
pub struct BigEndian;
impl Endian for BigEndian {
#[inline]
fn mask<T>(index: u32) -> T
where
T: Number,
{
T::BIT_RIGHT.wrapping_shl(index)
}
#[inline]
fn ones<T>(value: T) -> u32
where
T: Number,
{
value.trailing_ones()
}
#[inline]
fn ones_rev<T>(value: T) -> u32
where
T: Number,
{
value.leading_ones()
}
#[inline]
fn zeros<T>(value: T) -> u32
where
T: Number,
{
value.trailing_zeros()
}
#[inline]
fn zeros_rev<T>(value: T) -> u32
where
T: Number,
{
value.leading_zeros()
}
}
/// Little-endian indexing for bit sets.
///
/// This can be used in combination with methods such as [`Bits::test_bit_in`].
///
/// Little-endian indexing is constructed increasingly from left to right for
/// individual primitives, such as the following [`u8`] literal:
///
/// ```text
/// 0b0010_0010u8
/// ^ ^- index 6
/// '------ index 2
/// ```
///
/// Arrays are treated the same as expected where the index grows from smallest
/// to largest address:
///
/// ```text
/// 0 --------- 8 8 -------- 15
/// [0b0010_0010u8, 0b1000_0000u8]
/// ^ ^ ^- index 8
/// | '--------- index 6
/// '-------------- index 2
/// ```
///
/// [`Bits::test_bit_in`]: crate::Bits::test_bit_in
#[non_exhaustive]
pub struct LittleEndian;
impl Endian for LittleEndian {
#[inline]
fn mask<T>(index: u32) -> T
where
T: Number,
{
T::BIT_LEFT.wrapping_shr(index)
}
#[inline]
fn ones<T>(value: T) -> u32
where
T: Number,
{
value.leading_ones()
}
#[inline]
fn ones_rev<T>(value: T) -> u32
where
T: Number,
{
value.trailing_ones()
}
#[inline]
fn zeros<T>(value: T) -> u32
where
T: Number,
{
value.leading_zeros()
}
#[inline]
fn zeros_rev<T>(value: T) -> u32
where
T: Number,
{
value.trailing_ones()
}
}
/// The default endianness to use.
pub type DefaultEndian = BigEndian;