Skip to main content

BitSet

Struct BitSet 

Source
pub struct BitSet<'a> { /* private fields */ }
Expand description

Compact bit array overlaid on a byte slice.

  • O(1) get/set/clear/toggle per bit
  • No overhead – 1 bit per flag
  • Used for feature flags, user permission masks, state bitfields

Implementations§

Source§

impl<'a> BitSet<'a>

Source

pub fn from_bytes(data: &'a mut [u8]) -> Self

Overlay a BitSet on a mutable byte slice.

Source

pub fn capacity(&self) -> usize

Number of bits available.

Source

pub fn get(&self, index: usize) -> Result<bool, ProgramError>

Get a bit by index.

Source

pub fn set(&mut self, index: usize) -> Result<(), ProgramError>

Set a bit to 1.

Source

pub fn clear(&mut self, index: usize) -> Result<(), ProgramError>

Clear a bit to 0.

Source

pub fn toggle(&mut self, index: usize) -> Result<(), ProgramError>

Toggle a bit.

Source

pub fn count_ones(&self) -> usize

Count the number of set bits (popcount).

Source

pub fn count_zeros(&self) -> usize

Count the number of clear bits.

Source

pub fn check_flags( &self, byte_offset: usize, required: u8, ) -> Result<(), ProgramError>

Check if ALL bits in a mask are set (starting at byte offset).

Source

pub fn check_any_flag( &self, byte_offset: usize, any_of: u8, ) -> Result<(), ProgramError>

Check if ANY bit in a mask is set.

Source

pub const fn required_bytes(num_bits: usize) -> usize

Compute the byte size needed for a BitSet with the given number of bits.

Auto Trait Implementations§

§

impl<'a> Freeze for BitSet<'a>

§

impl<'a> RefUnwindSafe for BitSet<'a>

§

impl<'a> Send for BitSet<'a>

§

impl<'a> Sync for BitSet<'a>

§

impl<'a> Unpin for BitSet<'a>

§

impl<'a> UnsafeUnpin for BitSet<'a>

§

impl<'a> !UnwindSafe for BitSet<'a>

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