pub trait BitfieldAccess: AsRef<[u8]> {
// Provided methods
fn read_field<T>(&self, bitrange: impl RangeBounds<usize>) -> T
where T: PrimInt + Unsigned { ... }
fn write_field<T>(&mut self, bitrange: impl RangeBounds<usize>, value: T)
where Self: AsMut<[u8]>,
T: PrimInt + Unsigned + TryInto<u8> + UpperHex + CheckedShr,
<T as TryInto<u8>>::Error: Debug { ... }
}Provided Methods§
Sourcefn read_field<T>(&self, bitrange: impl RangeBounds<usize>) -> T
fn read_field<T>(&self, bitrange: impl RangeBounds<usize>) -> T
Read a bitfield with the given bit indices from a buffer.
§Examples
use bitfield_access::BitfieldAccess;
let buffer = [0x12, 0x34, 0x56, 0x78];
assert_eq!(buffer.read_field::<u8>(4..8), 0x2);
assert_eq!(buffer.read_field::<u16>(12..24), 0x456);
assert_eq!(buffer.read_field::<u8>(25..=25), 0x1);§Panics
Panics if the range of bits is wider than the integer type T
or the bit indices are out of bounds.
Sourcefn write_field<T>(&mut self, bitrange: impl RangeBounds<usize>, value: T)
fn write_field<T>(&mut self, bitrange: impl RangeBounds<usize>, value: T)
Write a bitfield with the given bit indices to a buffer.
§Examples
use bitfield_access::BitfieldAccess;
let mut buffer = [0x12, 0x34, 0x56, 0x78];
buffer.write_field(4..8, 0xA_u8);
assert_eq!(buffer, [0x1A, 0x34, 0x56, 0x78]);
buffer.write_field(20..=27, 0xBC_u8);
assert_eq!(buffer, [0x1A, 0x34, 0x5B, 0xC8]);§Panics
Panics if the bit indices are out of bounds or the value is too large.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.