pub struct BitField<T> { /* private fields */ }Expand description
A field within a packed integer.
Defines a contiguous range of bits by start position and length. Precomputes mask for efficient get/set operations.
§Example
use nexus_bits::BitField;
const EXCHANGE: BitField<u64> = BitField::<u64>::new(4, 8); // bits 4-11
let packed = EXCHANGE.set(0, 42).unwrap();
assert_eq!(EXCHANGE.get(packed), 42);Implementations§
Source§impl BitField<u8>
impl BitField<u8>
Sourcepub const fn new(start: u32, len: u32) -> Self
pub const fn new(start: u32, len: u32) -> Self
Creates a new field at bit position start with width len.
§Panics
Panics if len is 0 or start + len exceeds type’s bit width.
Sourcepub const fn set(self, val: u8, field_val: u8) -> Result<u8, Overflow<u8>>
pub const fn set(self, val: u8, field_val: u8) -> Result<u8, Overflow<u8>>
Set field value in packed integer.
Clears existing bits in field, then sets new value.
Returns error if field_val exceeds max_value().
Sourcepub const fn set_unchecked(self, val: u8, field_val: u8) -> u8
pub const fn set_unchecked(self, val: u8, field_val: u8) -> u8
Source§impl BitField<u16>
impl BitField<u16>
Sourcepub const fn new(start: u32, len: u32) -> Self
pub const fn new(start: u32, len: u32) -> Self
Creates a new field at bit position start with width len.
§Panics
Panics if len is 0 or start + len exceeds type’s bit width.
Sourcepub const fn set(self, val: u16, field_val: u16) -> Result<u16, Overflow<u16>>
pub const fn set(self, val: u16, field_val: u16) -> Result<u16, Overflow<u16>>
Set field value in packed integer.
Clears existing bits in field, then sets new value.
Returns error if field_val exceeds max_value().
Sourcepub const fn set_unchecked(self, val: u16, field_val: u16) -> u16
pub const fn set_unchecked(self, val: u16, field_val: u16) -> u16
Source§impl BitField<u32>
impl BitField<u32>
Sourcepub const fn new(start: u32, len: u32) -> Self
pub const fn new(start: u32, len: u32) -> Self
Creates a new field at bit position start with width len.
§Panics
Panics if len is 0 or start + len exceeds type’s bit width.
Sourcepub const fn set(self, val: u32, field_val: u32) -> Result<u32, Overflow<u32>>
pub const fn set(self, val: u32, field_val: u32) -> Result<u32, Overflow<u32>>
Set field value in packed integer.
Clears existing bits in field, then sets new value.
Returns error if field_val exceeds max_value().
Sourcepub const fn set_unchecked(self, val: u32, field_val: u32) -> u32
pub const fn set_unchecked(self, val: u32, field_val: u32) -> u32
Source§impl BitField<u64>
impl BitField<u64>
Sourcepub const fn new(start: u32, len: u32) -> Self
pub const fn new(start: u32, len: u32) -> Self
Creates a new field at bit position start with width len.
§Panics
Panics if len is 0 or start + len exceeds type’s bit width.
Sourcepub const fn set(self, val: u64, field_val: u64) -> Result<u64, Overflow<u64>>
pub const fn set(self, val: u64, field_val: u64) -> Result<u64, Overflow<u64>>
Set field value in packed integer.
Clears existing bits in field, then sets new value.
Returns error if field_val exceeds max_value().
Sourcepub const fn set_unchecked(self, val: u64, field_val: u64) -> u64
pub const fn set_unchecked(self, val: u64, field_val: u64) -> u64
Source§impl BitField<u128>
impl BitField<u128>
Sourcepub const fn new(start: u32, len: u32) -> Self
pub const fn new(start: u32, len: u32) -> Self
Creates a new field at bit position start with width len.
§Panics
Panics if len is 0 or start + len exceeds type’s bit width.
Sourcepub const fn set(
self,
val: u128,
field_val: u128,
) -> Result<u128, Overflow<u128>>
pub const fn set( self, val: u128, field_val: u128, ) -> Result<u128, Overflow<u128>>
Set field value in packed integer.
Clears existing bits in field, then sets new value.
Returns error if field_val exceeds max_value().
Sourcepub const fn set_unchecked(self, val: u128, field_val: u128) -> u128
pub const fn set_unchecked(self, val: u128, field_val: u128) -> u128
Source§impl BitField<i8>
impl BitField<i8>
Sourcepub const fn new(start: u32, len: u32) -> Self
pub const fn new(start: u32, len: u32) -> Self
Creates a new field at bit position start with width len.
§Panics
Panics if len is 0 or start + len exceeds type’s bit width.
Sourcepub const fn set(self, val: i8, field_val: i8) -> Result<i8, Overflow<i8>>
pub const fn set(self, val: i8, field_val: i8) -> Result<i8, Overflow<i8>>
Set field value in packed integer.
Clears existing bits in field, then sets new value.
Returns error if field_val exceeds max_value().
Sourcepub const fn set_unchecked(self, val: i8, field_val: i8) -> i8
pub const fn set_unchecked(self, val: i8, field_val: i8) -> i8
Source§impl BitField<i16>
impl BitField<i16>
Sourcepub const fn new(start: u32, len: u32) -> Self
pub const fn new(start: u32, len: u32) -> Self
Creates a new field at bit position start with width len.
§Panics
Panics if len is 0 or start + len exceeds type’s bit width.
Sourcepub const fn set(self, val: i16, field_val: i16) -> Result<i16, Overflow<i16>>
pub const fn set(self, val: i16, field_val: i16) -> Result<i16, Overflow<i16>>
Set field value in packed integer.
Clears existing bits in field, then sets new value.
Returns error if field_val exceeds max_value().
Sourcepub const fn set_unchecked(self, val: i16, field_val: i16) -> i16
pub const fn set_unchecked(self, val: i16, field_val: i16) -> i16
Source§impl BitField<i32>
impl BitField<i32>
Sourcepub const fn new(start: u32, len: u32) -> Self
pub const fn new(start: u32, len: u32) -> Self
Creates a new field at bit position start with width len.
§Panics
Panics if len is 0 or start + len exceeds type’s bit width.
Sourcepub const fn set(self, val: i32, field_val: i32) -> Result<i32, Overflow<i32>>
pub const fn set(self, val: i32, field_val: i32) -> Result<i32, Overflow<i32>>
Set field value in packed integer.
Clears existing bits in field, then sets new value.
Returns error if field_val exceeds max_value().
Sourcepub const fn set_unchecked(self, val: i32, field_val: i32) -> i32
pub const fn set_unchecked(self, val: i32, field_val: i32) -> i32
Source§impl BitField<i64>
impl BitField<i64>
Sourcepub const fn new(start: u32, len: u32) -> Self
pub const fn new(start: u32, len: u32) -> Self
Creates a new field at bit position start with width len.
§Panics
Panics if len is 0 or start + len exceeds type’s bit width.
Sourcepub const fn set(self, val: i64, field_val: i64) -> Result<i64, Overflow<i64>>
pub const fn set(self, val: i64, field_val: i64) -> Result<i64, Overflow<i64>>
Set field value in packed integer.
Clears existing bits in field, then sets new value.
Returns error if field_val exceeds max_value().
Sourcepub const fn set_unchecked(self, val: i64, field_val: i64) -> i64
pub const fn set_unchecked(self, val: i64, field_val: i64) -> i64
Source§impl BitField<i128>
impl BitField<i128>
Sourcepub const fn new(start: u32, len: u32) -> Self
pub const fn new(start: u32, len: u32) -> Self
Creates a new field at bit position start with width len.
§Panics
Panics if len is 0 or start + len exceeds type’s bit width.
Sourcepub const fn set(
self,
val: i128,
field_val: i128,
) -> Result<i128, Overflow<i128>>
pub const fn set( self, val: i128, field_val: i128, ) -> Result<i128, Overflow<i128>>
Set field value in packed integer.
Clears existing bits in field, then sets new value.
Returns error if field_val exceeds max_value().