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 theBitWord
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>
impl<'a, T: BitWord> BitViewMut<'a, T>
Sourcepub unsafe fn from_raw_parts(ptr: *const T, len: usize) -> Self
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 storagelen
- 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>
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>)
fn bitand_assign(&mut self, rhs: &'b BitView<'b, T>)
&=
operation. Read moreSource§impl<'a, 'b, T: BitWord> BitOrAssign<&'b BitView<'b, T>> for BitViewMut<'a, T>
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>)
fn bitor_assign(&mut self, rhs: &'b BitView<'b, T>)
|=
operation. Read moreSource§impl<'a, T: BitWord> BitRead for BitViewMut<'a, T>
impl<'a, T: BitWord> BitRead for BitViewMut<'a, T>
Source§type Iter<'b> = Iter<'b, T>
where
Self: 'b
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 count_ones(&self) -> usize
fn count_ones(&self) -> usize
Counts the number of set bits (1s) in this bit view.
Source§fn all(&self) -> bool
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
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<'_>
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.
fn none(&self) -> bool
Source§impl<'a, T: BitWord> BitWrite for BitViewMut<'a, T>
impl<'a, T: BitWord> BitWrite for BitViewMut<'a, T>
Source§impl<'a, 'b, T: BitWord> BitXorAssign<&'b BitView<'b, T>> for BitViewMut<'a, T>
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>)
fn bitxor_assign(&mut self, rhs: &'b BitView<'b, T>)
^=
operation. Read moreSource§impl<'a, T: BitWord> From<&'a mut BitFixed<T>> for BitViewMut<'a, T>
Implements conversion from a mutable reference to BitFixed
to BitViewMut
.
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));