pub trait Bits: Copy {
const BITS: u32;
// Required methods
fn into_bits(self) -> u128;
fn from_bits(raw: u128) -> Self;
}Expand description
A value that occupies a fixed number of bits within a bitfield.
The contract: into_bits yields the value in the low
BITS bits of a u128 (higher bits zero), and
from_bits reconstructs from the low BITS bits of its
argument (higher bits ignored). Implementations must round-trip:
T::from_bits(x.into_bits()) == x for every representable x.
bool, the primitive unsigned integers, and the UInt types
implement it out of the box; #[bitfield] and #[derive(BitEnum)] generate
impls so those types nest as fields too.
use bnb::Bits;
assert_eq!(<u8 as Bits>::BITS, 8);
assert_eq!(0xABu8.into_bits(), 0xAB);
assert_eq!(u8::from_bits(0x1FF), 0xFF); // from_bits truncates to the width
assert!(!bool::from_bits(0b10)); // only the low bit is readRequired Associated Constants§
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".