Struct BitViewMut

Source
pub struct BitViewMut<'a, T: BitWord> { /* private fields */ }
Expand description

A mutable view into a bit set stored in memory.

BitViewMut provides a safe interface for accessing and modifying bits in an existing memory region without taking ownership of the underlying data. It’s similar to a mutable slice (&mut [T]) but operates at the bit level rather than the element level.

The view is parameterized by:

  • A lifetime 'a that ties it to the lifetime of the referenced data
  • A word type T that implements the BitWord trait, which defines the storage unit

§Safety

This type uses raw pointers internally and relies on the caller to ensure:

  • The pointer is valid for the entire lifetime 'a
  • The pointer points to a properly aligned instance of T
  • The memory region contains at least enough bits to cover the specified length
  • The memory is not mutated through other references while this view exists
  • The caller has exclusive access to the memory region for the duration of the view

§Examples

use fastbit::{BitViewMut, BitRead, BitWrite};

// Create a mutable bit pattern in a byte array
let mut data: [u8; 2] = [0b10101010, 0b01010101];

// Create a BitViewMut over the data (unsafe because we're working with raw pointers)
let mut view = unsafe { BitViewMut::from_raw_parts(data.as_mut_ptr(), 16) };

// Now we can safely read and modify bits
assert!(!view.test(0));
view.set(0);
assert!(view.test(0));
view.clear();
assert_eq!(view.count_ones(), 0);

Implementations§

Source§

impl<'a, T: BitWord> BitViewMut<'a, T>

Source

pub unsafe fn from_raw_parts(ptr: *const T, len: usize) -> Self

Creates a new BitViewMut from raw parts.

§Arguments
  • ptr - A pointer to the start of the bit storage
  • len - The number of bits in the view
§Safety

The caller must ensure:

  • The pointer is valid for reads and writes for the entire lifetime 'a
  • The pointer points to a properly aligned instance of T
  • The memory region contains at least enough bits to cover the specified length
  • The caller has exclusive access to the memory region for the duration of the view

Trait Implementations§

Source§

impl<'a, 'b, T: BitWord> BitAndAssign<&'b BitView<'b, T>> for BitViewMut<'a, T>

Source§

fn bitand_assign(&mut self, rhs: &'b BitView<'b, T>)

Performs the &= operation. Read more
Source§

impl<'a, 'b, T: BitWord> BitOrAssign<&'b BitView<'b, T>> for BitViewMut<'a, T>

Source§

fn bitor_assign(&mut self, rhs: &'b BitView<'b, T>)

Performs the |= operation. Read more
Source§

impl<'a, T: BitWord> BitRead for BitViewMut<'a, T>

Source§

type Iter<'b> = Iter<'b, T> where Self: 'b

The iterator type returned by the iter method.

This iterator yields the indices of all set bits (1s) in the bit view.

Source§

fn len(&self) -> usize

Returns the number of bits in this bit view.

Source§

fn is_empty(&self) -> bool

Returns true if this bit view contains no bits.

Source§

fn test(&self, idx: usize) -> bool

Tests if the bit at the specified index is set (1).

§Arguments
  • idx - The index of the bit to test
§Panics

Panics if idx is out of bounds (>= self.len()).

Source§

fn count_ones(&self) -> usize

Counts the number of set bits (1s) in this bit view.

Source§

fn all(&self) -> bool

Returns true if all bits in this view are set (1).

Returns true for empty bit views.

Source§

fn any(&self) -> bool

Returns true if any bit in this view is set (1).

Returns false for empty bit views.

Source§

fn iter(&self) -> Self::Iter<'_>

Returns an iterator over the indices of all set bits (1s) in this bit view.

The iterator yields the indices in ascending order.

Source§

fn none(&self) -> bool

Source§

impl<'a, T: BitWord> BitWrite for BitViewMut<'a, T>

Source§

fn set(&mut self, idx: usize)

Sets the bit at the specified index to 1.

§Arguments
  • idx - The index of the bit to set
§Panics

Panics if idx is out of bounds (>= self.len()).

Source§

fn reset(&mut self, idx: usize)

Sets the bit at the specified index to 0.

§Arguments
  • idx - The index of the bit to reset
§Panics

Panics if idx is out of bounds (>= self.len()).

Source§

fn flip(&mut self, idx: usize)

Flips the bit at the specified index (0 becomes 1, 1 becomes 0).

§Arguments
  • idx - The index of the bit to flip
§Panics

Panics if idx is out of bounds (>= self.len()).

Source§

fn test_and_set(&mut self, idx: usize) -> bool

Tests the bit at the specified index and then sets it to 1.

§Arguments
  • idx - The index of the bit to test and set
§Returns

The previous value of the bit (true if it was 1, false if it was 0).

§Panics

Panics if idx is out of bounds (>= self.len()).

Source§

fn fill(&mut self)

Sets all bits in this view to 1.

Source§

fn clear(&mut self)

Sets all bits in this view to 0.

Source§

fn take(&mut self, idx: usize) -> bool

return and clear bit at idx.
Source§

fn replace(&mut self, idx: usize, value: bool) -> bool

set bit to value, return old.
Source§

impl<'a, 'b, T: BitWord> BitXorAssign<&'b BitView<'b, T>> for BitViewMut<'a, T>

Source§

fn bitxor_assign(&mut self, rhs: &'b BitView<'b, T>)

Performs the ^= operation. Read more
Source§

impl<'a, T: BitWord> From<&'a mut BitFixed<T>> for BitViewMut<'a, T>

Implements conversion from a mutable reference to BitFixed to BitViewMut.

This allows creating a mutable view of a BitFixed instance without copying the data. Changes made through the view will affect the original BitFixed instance.

§Examples

use fastbit::{BitFixed, BitViewMut, BitWrite, BitRead};

let mut bf: BitFixed<u8> = BitFixed::new(8);
bf.set(1);

// Create a BitViewMut from a mutable reference to BitFixed
let mut view: BitViewMut<u8> = BitViewMut::from(&mut bf);

// Modify bits through the view
view.set(3);
view.reset(1);

// Changes are reflected in the original BitFixed
assert!(!bf.test(1));
assert!(bf.test(3));
Source§

fn from(value: &'a mut BitFixed<T>) -> Self

Converts a mutable reference to BitFixed into a BitViewMut.

Auto Trait Implementations§

§

impl<'a, T> Freeze for BitViewMut<'a, T>

§

impl<'a, T> RefUnwindSafe for BitViewMut<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> !Send for BitViewMut<'a, T>

§

impl<'a, T> !Sync for BitViewMut<'a, T>

§

impl<'a, T> Unpin for BitViewMut<'a, T>

§

impl<'a, T> !UnwindSafe for BitViewMut<'a, T>

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.