Skip to main content

BitField

Struct BitField 

Source
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>

Source

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.

Source

pub const fn start(self) -> u32

Start bit position.

Source

pub const fn len(self) -> u32

Field width in bits.

Source

pub const fn mask(self) -> u8

Mask with 1s in field position.

Source

pub const fn max_value(self) -> u8

Maximum value this field can hold.

Source

pub const fn get(self, val: u8) -> u8

Extract field value from packed integer.

Source

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().

Source

pub const fn set_unchecked(self, val: u8, field_val: u8) -> u8

Set field value without bounds checking.

§Safety

Caller must ensure field_val <= max_value().

Source

pub const fn clear(self, val: u8) -> u8

Clear field to zero.

Source§

impl BitField<u16>

Source

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.

Source

pub const fn start(self) -> u32

Start bit position.

Source

pub const fn len(self) -> u32

Field width in bits.

Source

pub const fn mask(self) -> u16

Mask with 1s in field position.

Source

pub const fn max_value(self) -> u16

Maximum value this field can hold.

Source

pub const fn get(self, val: u16) -> u16

Extract field value from packed integer.

Source

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().

Source

pub const fn set_unchecked(self, val: u16, field_val: u16) -> u16

Set field value without bounds checking.

§Safety

Caller must ensure field_val <= max_value().

Source

pub const fn clear(self, val: u16) -> u16

Clear field to zero.

Source§

impl BitField<u32>

Source

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.

Source

pub const fn start(self) -> u32

Start bit position.

Source

pub const fn len(self) -> u32

Field width in bits.

Source

pub const fn mask(self) -> u32

Mask with 1s in field position.

Source

pub const fn max_value(self) -> u32

Maximum value this field can hold.

Source

pub const fn get(self, val: u32) -> u32

Extract field value from packed integer.

Source

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().

Source

pub const fn set_unchecked(self, val: u32, field_val: u32) -> u32

Set field value without bounds checking.

§Safety

Caller must ensure field_val <= max_value().

Source

pub const fn clear(self, val: u32) -> u32

Clear field to zero.

Source§

impl BitField<u64>

Source

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.

Source

pub const fn start(self) -> u32

Start bit position.

Source

pub const fn len(self) -> u32

Field width in bits.

Source

pub const fn mask(self) -> u64

Mask with 1s in field position.

Source

pub const fn max_value(self) -> u64

Maximum value this field can hold.

Source

pub const fn get(self, val: u64) -> u64

Extract field value from packed integer.

Source

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().

Source

pub const fn set_unchecked(self, val: u64, field_val: u64) -> u64

Set field value without bounds checking.

§Safety

Caller must ensure field_val <= max_value().

Source

pub const fn clear(self, val: u64) -> u64

Clear field to zero.

Source§

impl BitField<u128>

Source

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.

Source

pub const fn start(self) -> u32

Start bit position.

Source

pub const fn len(self) -> u32

Field width in bits.

Source

pub const fn mask(self) -> u128

Mask with 1s in field position.

Source

pub const fn max_value(self) -> u128

Maximum value this field can hold.

Source

pub const fn get(self, val: u128) -> u128

Extract field value from packed integer.

Source

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().

Source

pub const fn set_unchecked(self, val: u128, field_val: u128) -> u128

Set field value without bounds checking.

§Safety

Caller must ensure field_val <= max_value().

Source

pub const fn clear(self, val: u128) -> u128

Clear field to zero.

Source§

impl BitField<i8>

Source

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.

Source

pub const fn start(self) -> u32

Start bit position.

Source

pub const fn len(self) -> u32

Field width in bits.

Source

pub const fn mask(self) -> i8

Mask with 1s in field position.

Source

pub const fn max_value(self) -> i8

Maximum value this field can hold.

Source

pub const fn get(self, val: i8) -> i8

Extract field value from packed integer.

Source

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().

Source

pub const fn set_unchecked(self, val: i8, field_val: i8) -> i8

Set field value without bounds checking.

§Safety

Caller must ensure field_val <= max_value().

Source

pub const fn clear(self, val: i8) -> i8

Clear field to zero.

Source§

impl BitField<i16>

Source

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.

Source

pub const fn start(self) -> u32

Start bit position.

Source

pub const fn len(self) -> u32

Field width in bits.

Source

pub const fn mask(self) -> i16

Mask with 1s in field position.

Source

pub const fn max_value(self) -> i16

Maximum value this field can hold.

Source

pub const fn get(self, val: i16) -> i16

Extract field value from packed integer.

Source

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().

Source

pub const fn set_unchecked(self, val: i16, field_val: i16) -> i16

Set field value without bounds checking.

§Safety

Caller must ensure field_val <= max_value().

Source

pub const fn clear(self, val: i16) -> i16

Clear field to zero.

Source§

impl BitField<i32>

Source

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.

Source

pub const fn start(self) -> u32

Start bit position.

Source

pub const fn len(self) -> u32

Field width in bits.

Source

pub const fn mask(self) -> i32

Mask with 1s in field position.

Source

pub const fn max_value(self) -> i32

Maximum value this field can hold.

Source

pub const fn get(self, val: i32) -> i32

Extract field value from packed integer.

Source

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().

Source

pub const fn set_unchecked(self, val: i32, field_val: i32) -> i32

Set field value without bounds checking.

§Safety

Caller must ensure field_val <= max_value().

Source

pub const fn clear(self, val: i32) -> i32

Clear field to zero.

Source§

impl BitField<i64>

Source

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.

Source

pub const fn start(self) -> u32

Start bit position.

Source

pub const fn len(self) -> u32

Field width in bits.

Source

pub const fn mask(self) -> i64

Mask with 1s in field position.

Source

pub const fn max_value(self) -> i64

Maximum value this field can hold.

Source

pub const fn get(self, val: i64) -> i64

Extract field value from packed integer.

Source

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().

Source

pub const fn set_unchecked(self, val: i64, field_val: i64) -> i64

Set field value without bounds checking.

§Safety

Caller must ensure field_val <= max_value().

Source

pub const fn clear(self, val: i64) -> i64

Clear field to zero.

Source§

impl BitField<i128>

Source

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.

Source

pub const fn start(self) -> u32

Start bit position.

Source

pub const fn len(self) -> u32

Field width in bits.

Source

pub const fn mask(self) -> i128

Mask with 1s in field position.

Source

pub const fn max_value(self) -> i128

Maximum value this field can hold.

Source

pub const fn get(self, val: i128) -> i128

Extract field value from packed integer.

Source

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().

Source

pub const fn set_unchecked(self, val: i128, field_val: i128) -> i128

Set field value without bounds checking.

§Safety

Caller must ensure field_val <= max_value().

Source

pub const fn clear(self, val: i128) -> i128

Clear field to zero.

Trait Implementations§

Source§

impl<T: Clone> Clone for BitField<T>

Source§

fn clone(&self) -> BitField<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for BitField<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: PartialEq> PartialEq for BitField<T>

Source§

fn eq(&self, other: &BitField<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Copy> Copy for BitField<T>

Source§

impl<T: Eq> Eq for BitField<T>

Source§

impl<T> StructuralPartialEq for BitField<T>

Auto Trait Implementations§

§

impl<T> Freeze for BitField<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for BitField<T>
where T: RefUnwindSafe,

§

impl<T> Send for BitField<T>
where T: Send,

§

impl<T> Sync for BitField<T>
where T: Sync,

§

impl<T> Unpin for BitField<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for BitField<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for BitField<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.