pub struct PackedInt { /* private fields */ }Expand description
An unsigned integer packed into 16 bits with variable precision.
See the crate-level documentation for the encoding, the rounding behaviour, and the ordering guarantee.
Implementations§
Source§impl PackedInt
impl PackedInt
Sourcepub const fn from_12_bits(bits: &[u8; 2]) -> PackedInt
pub const fn from_12_bits(bits: &[u8; 2]) -> PackedInt
Reads a packed value from 12 bits: all of bits[0], and the high
nibble of bits[1].
The low nibble of bits[1] is reserved for the caller and is ignored.
Sourcepub const fn to_12_bits(self) -> [u8; 2]
pub const fn to_12_bits(self) -> [u8; 2]
Writes the packed value into 12 bits: all of bits[0], and the high
nibble of bits[1].
The low nibble of bits[1] is left zero for the caller to use.
Twelve bits hold only the low nibble of the exponent, so this is
lossless for values up to 511 × 2¹⁴ (8 372 224) and no further.
Beyond that the exponent is truncated and the value reads back as an
unrelated number.
Sourcepub const fn from_16_bits(bits: &[u8; 2]) -> PackedInt
pub const fn from_16_bits(bits: &[u8; 2]) -> PackedInt
Reads a packed value from two little-endian bytes.
Sourcepub const fn to_16_bits(self) -> [u8; 2]
pub const fn to_16_bits(self) -> [u8; 2]
Writes the packed value as two little-endian bytes.
Sourcepub const fn from_inner_u16(inner: u16) -> PackedInt
pub const fn from_inner_u16(inner: u16) -> PackedInt
Reinterprets a u16 as a packed value.
Every u16 is a valid representation, so this cannot fail. It is the
inverse of to_inner_u16.
Sourcepub const fn to_inner_u16(self) -> u16
pub const fn to_inner_u16(self) -> u16
Returns the underlying representation.
Sourcepub const fn from_usize(value: usize) -> PackedInt
pub const fn from_usize(value: usize) -> PackedInt
Packs a usize, rounding up.
Returns the smallest representable value greater than or equal to
value, which is value itself whenever it is below 256.
Sourcepub const fn from_u128(value: u128) -> PackedInt
pub const fn from_u128(value: u128) -> PackedInt
Packs a u128, rounding up.
Returns the smallest representable value greater than or equal to
value, which is value itself whenever it is below 256.
Sourcepub const fn from_u64(value: u64) -> PackedInt
pub const fn from_u64(value: u64) -> PackedInt
Packs a u64, rounding up.
Returns the smallest representable value greater than or equal to
value, which is value itself whenever it is below 256.
Sourcepub const fn from_u32(value: u32) -> PackedInt
pub const fn from_u32(value: u32) -> PackedInt
Packs a u32, rounding up.
Returns the smallest representable value greater than or equal to
value, which is value itself whenever it is below 256.
Sourcepub const fn from_u16(value: u16) -> PackedInt
pub const fn from_u16(value: u16) -> PackedInt
Packs a u16, rounding up.
Returns the smallest representable value greater than or equal to
value, which is value itself whenever it is below 256.
Sourcepub const fn to_usize(self) -> usize
pub const fn to_usize(self) -> usize
Unpacks into a usize, saturating at usize::MAX.
Values that do not fit in the target type saturate rather than wrap.