BitSpanMut

Struct BitSpanMut 

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

A non-owning mutable view into a bit array.

BitSpanMut provides a view into a sequence of bits stored in memory, allowing both reading and writing of individual bits or ranges of bits. It can be used to efficiently operate on a subset of bits without copying or allocating memory.

The span is defined by:

  • A pointer to the start of the word array
  • A bit offset from the start of the first word
  • A length in bits

§Safety

This type uses raw pointers internally. The caller must ensure that:

  • The referenced memory remains valid for the lifetime of the span
  • No other references (mutable or immutable) to the same memory are used during the lifetime of the span to avoid aliasing violations
  • The offset and length parameters don’t cause out-of-bounds access

Implementations§

Source§

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

Source

pub fn from_raw_parts_mut(ptr: *mut T, offset: usize, len: usize) -> Self

Creates a new BitSpanMut from raw parts.

This function allows creating a mutable span directly from a raw pointer, which is useful for interfacing with external memory or creating mutable views into existing bit arrays.

§Arguments
  • ptr - A mutable pointer to the word array (will be converted to non-null)
  • offset - The bit offset from the start of the first word
  • len - The number of bits in the span
§Safety

The caller must ensure that:

  • The pointer is valid for reads and writes of len bits starting at offset
  • The memory remains valid for the lifetime of the returned span
  • No other references to the same memory are used during the lifetime of the span

If ptr is null, a dangling pointer will be used. This is safe as long as len is 0, but will lead to undefined behavior otherwise.

Trait Implementations§

Source§

impl<'a, 'b, T: BitWord> BitAndAssign<&'b BitSpan<'b, T>> for BitSpanMut<'a, T>

Source§

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

Performs the &= operation. Read more
Source§

impl<'a, 'b, T: BitWord> BitOrAssign<&'b BitSpan<'b, T>> for BitSpanMut<'a, T>

Source§

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

Performs the |= operation. Read more
Source§

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

Source§

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

The iterator type returned by the iter method.

Source§

fn len(&self) -> usize

Returns the number of bits in the span.

Source§

fn is_empty(&self) -> bool

Returns true if the span is empty or contains no set bits.

This implementation considers a span empty if either:

  • It has zero length
  • It has no bits set to 1
Source§

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

Tests if the bit at the given index is set.

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

true if the bit is set, false otherwise

§Panics

Debug builds will assert if idx is out of bounds (>= self.len).

Source§

fn count_ones(&self) -> usize

Counts the number of bits set to 1 in the span.

§Returns

The number of bits set to 1

Source§

fn all(&self) -> bool

Returns true if all bits in the span are set to 1.

§Returns

true if the span is non-empty and all bits are set to 1, false otherwise

Source§

fn any(&self) -> bool

Returns true if any bit in the span is set to 1.

§Returns

true if at least one bit is set to 1, false otherwise

Source§

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

Returns an iterator over the indices of bits set to 1.

§Returns

An iterator that yields the indices of all bits set to 1 in ascending order

Source§

fn none(&self) -> bool

Source§

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

Source§

fn set(&mut self, idx: usize)

Sets the bit at the given index to 1.

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

Debug builds will assert if idx is out of bounds (>= self.len).

Source§

fn reset(&mut self, idx: usize)

Sets the bit at the given index to 0.

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

Debug builds will assert if idx is out of bounds (>= self.len).

Source§

fn flip(&mut self, idx: usize)

Flips the bit at the given 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

Sets the bit at the given index to 1 and returns the previous value.

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

true if the bit was already set, false otherwise

§Panics

Debug builds will assert if idx is out of bounds (>= self.len).

Source§

fn fill(&mut self)

Sets all bits in the span to 1.

This method efficiently sets all bits within the span’s range to 1, taking care to only modify bits that are part of the span.

Source§

fn clear(&mut self)

Sets all bits in the span to 0.

This method efficiently clears all bits within the span’s range, taking care to only modify bits that are part of the span.

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 BitSpan<'b, T>> for BitSpanMut<'a, T>

Source§

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

Performs the ^= operation. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<'a, T> !UnwindSafe for BitSpanMut<'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.