#[repr(transparent)]pub struct BitBox<O = Lsb0, T = usize> where
O: BitOrder,
T: BitStore, { /* private fields */ }
Expand description
A frozen heap-allocated buffer of individual bits.
This is essentially a BitVec
that has frozen its allocation, and given up
the ability to change size. It is analagous to Box<[bool]>
. You should prefer
BitArray
over BitBox
where possible, and may freely box it if you need the
indirection.
Documentation
All APIs that mirror something in the standard library will have an Original
section linking to the corresponding item. All APIs that have a different
signature or behavior than the original will have an API Differences
section
explaining what has changed, and how to adapt your existing code to the change.
These sections look like this:
Original
API Differences
The buffer type Box<[bool]>
has no type parameters. BitBox<O, T>
has the
same two type parameters as BitSlice<O, T>
. Otherwise, BitBox
is able to
implement the full API surface of Box<[bool]>
.
Behavior
Because BitBox
is a fully-owned buffer, it is able to operate on its memory
without concern for any other views that may alias. This enables it to
specialize some BitSlice
behavior to be faster or more efficient.
Type Parameters
This takes the same BitOrder
and BitStore
parameters as BitSlice
.
Unlike BitSlice
, it is restricted to only accept the fundamental integers as
its BitStore
arguments; BitBox
buffers can never be aliased by other
BitBox
es, and do not need to share memory access.
Safety
BitBox
is a wrapper over a NonNull<BitSlice<O, T>>
pointer; this allows it
to remain exactly two words in size, and means that it is subject to the same
representational incompatibility restrictions as BitSlice
references. You
must never attempt to type-cast between Box<[bool]>
and BitBox
in any way,
nor may you attempt to modify the memory value of a BitBox
handle. Doing so
will cause allocator and memory errors in your program, likely inducing a panic.
Everything in the BitBox
public API, even the unsafe
parts, are guaranteed
to have no more unsafety or potential for incorrectness than their equivalent
items in the standard library. All unsafe
APIs will have documentation
explicitly detailing what the API requires you to uphold in order for it to
function safely and correctly. All safe APIs will do so themselves.
Macro Construction
Heap allocation can only occur at runtime, but the bitbox!
macro will
construct an appropriate BitSlice
buffer at compile-time, and at run-time,
only copy the buffer into a heap allocation.
Implementations
sourceimpl<O, T> BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> BitBox<O, T> where
O: BitOrder,
T: BitStore,
sourcepub fn new(x: &BitSlice<O, T>) -> Self
👎 Deprecated: Prefer from_bitslice
pub fn new(x: &BitSlice<O, T>) -> Self
Prefer from_bitslice
Allocates memory on the heap and then copies x
into it.
This doesn’t actually allocate if x
is zero-length.
Original
API Differences
Box::<[T]>::new
does not exist, because unsized types cannot be taken
by value. Instead, this takes a slice reference, and boxes the referent
slice.
Examples
use bitvec::prelude::*;
let boxed = BitBox::new(bits![0; 5]);
sourcepub unsafe fn from_raw(raw: *mut BitSlice<O, T>) -> Self
pub unsafe fn from_raw(raw: *mut BitSlice<O, T>) -> Self
Constructs a bit-box from a raw bit-slice pointer.
After calling this function, the raw bit-slice pointer is owned by the
resulting BitBox
. Specifically, the BitBox
destructor will free the
memory allocation at the bit-slice pointer’s address. For this to be
safe, the bit-slice pointer can only have been produced by a BitBox
previously destroyed using into_raw
.
Original
Safety
This function is unsafe because improper use may lead to memory problems. For example, a double-free may occur if the function is called twice on the same raw bit-slice pointer.
Examples
Recreate a BitBox
which was previously converted to a raw bit-slice
pointer using BitBox::into_raw
:
use bitvec::prelude::*;
let x = bitbox![0; 10];
let ptr = BitBox::into_raw(x);
let x = unsafe { BitBox::from_raw(ptr) };
sourcepub fn into_raw(this: Self) -> *mut BitSlice<O, T>
pub fn into_raw(this: Self) -> *mut BitSlice<O, T>
Consumes the BitBox
, returning a raw bit-slice pointer.
The bit-slice pointer will be properly encoded and non-null.
After calling this function, the caller is responsible for the memory
previously managed by the BitBox
. In particular, the caller should
properly release the memory by converting the bit-slice pointer back
into a BitBox
with the from_raw
function, allowing the BitBox
destructor to perform the cleanup.
Note: this is an associated function, which means that you have to call
it as BitBox::into_raw(b)
instead of b.into_raw()
. This is to match
signatures with the standard library’s Box
API; there will never be
a name conflict with BitSlice
.
Original
Examples
Converting the raw bit-slice pointer back into a BitBox
with
BitBox::from_raw
for automatic cleanup:
use bitvec::prelude::*;
let x = bitbox![0; 50];
let p = BitBox::into_raw(x);
let x = unsafe { BitBox::from_raw(p) };
You may not deällocate pointers produced by this function through any other manner.
sourcepub fn leak<'a>(this: Self) -> &'a mut BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
where
T: 'a,
pub fn leak<'a>(this: Self) -> &'a mut BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
where
T: 'a,
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
Consumes and leaks the BitBox
, returning a mutable reference, &'a mut BitSlice<O, T>
. This is eligible to be promoted to the 'static
lifetime.
Original
This function is mainly useful for data that lives for the remainder
of the program’s life. Dropping the returned reference will cause a
memory leak. If this is not acceptable, the reference should first be
wrapped with the BitBox::from_raw
function producing a BitBox
.
This BitBox
can then be dropped which will properly deällocate the
memory.
Note: this is an associated function, which means that you have to call
it as BitBox::leak(b)
instead of b.leak()
. This is to match
signatures with the standard library’s Box
API; there will never be
a name conflict with BitSlice
.
Original
Examples
Simple usage:
use bitvec::prelude::*;
let b = bitbox![0; 50];
let static_ref: &'static mut BitSlice = BitBox::leak(b);
static_ref.set(0, true);
assert!(static_ref[0]);
sourceimpl<O, T> BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> BitBox<O, T> where
O: BitOrder,
T: BitStore,
General-purpose functions not present on Box<[T]>
.
sourcepub fn from_bitslice(slice: &BitSlice<O, T>) -> Self
pub fn from_bitslice(slice: &BitSlice<O, T>) -> Self
Copies a BitSlice
region into a new BitBox
allocation.
Effects
This delegates to BitVec::from_bitslice
, then discards the excess
capacity.
Examples
use bitvec::prelude::*;
let bits = bits![0, 1, 0, 1, 1, 0, 1, 1];
let bb = BitBox::from_bitslice(&bits[2 ..]);
assert_eq!(bb, bits[2 ..]);
assert_eq!(bb.as_slice(), bits.as_slice());
sourcepub fn from_boxed_slice(boxed: Box<[T]>) -> Self
pub fn from_boxed_slice(boxed: Box<[T]>) -> Self
Converts a Box<[T]>
into a BitBox
<O, T>` without copying its buffer.
Parameters
boxed
: A boxed slice to view as bits.
Returns
A BitBox
over the boxed
buffer.
Panics
This panics if boxed
is too long to convert into a BitBox
. See
BitSlice::MAX_ELTS
.
Examples
use bitvec::prelude::*;
let boxed: Box<[u8]> = Box::new([0; 4]);
let addr = boxed.as_ptr();
let bb = BitBox::<LocalBits, _>::from_boxed_slice(boxed);
assert_eq!(bb, bits![0; 32]);
assert_eq!(addr, bb.as_slice().as_ptr());
sourcepub fn try_from_boxed_slice(boxed: Box<[T]>) -> Result<Self, Box<[T]>>
pub fn try_from_boxed_slice(boxed: Box<[T]>) -> Result<Self, Box<[T]>>
Converts a Box<[T]>
into a BitBox<O, T>
without copying its buffer.
This method takes ownership of a memory buffer and enables it to be used
as a bit-box. Because Box<[T]>
can be longer than BitBox
es, this is
a fallible method, and the original box will be returned if it cannot be
converted.
Parameters
boxed
: Some boxed slice of memory, to be viewed as bits.
Returns
If boxed
is short enough to be viewed as a BitBox
, then this returns
a BitBox
over the boxed
buffer. If boxed
is too long, then this
returns boxed
unmodified.
Examples
use bitvec::prelude::*;
let boxed: Box<[u8]> = Box::new([0; 4]);
let addr = boxed.as_ptr();
let bb = BitBox::<LocalBits, _>::try_from_boxed_slice(boxed).unwrap();
assert_eq!(bb[..], bits![0; 32]);
assert_eq!(addr, bb.as_slice().as_ptr());
sourcepub fn into_boxed_slice(self) -> Box<[T]>
pub fn into_boxed_slice(self) -> Box<[T]>
Converts the slice back into an ordinary slice of memory elements.
This does not affect the slice’s buffer, only the handle used to control it.
Parameters
self
Returns
An ordinary boxed slice containing all of the bit-slice’s memory buffer.
Examples
use bitvec::prelude::*;
let bb = bitbox![0; 5];
let addr = bb.as_slice().as_ptr();
let boxed = bb.into_boxed_slice();
assert_eq!(boxed[..], [0][..]);
assert_eq!(addr, boxed.as_ptr());
sourcepub fn into_bitvec(self) -> BitVec<O, T>ⓘNotable traits for BitVec<O, T>impl<O, T> Write for BitVec<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
pub fn into_bitvec(self) -> BitVec<O, T>ⓘNotable traits for BitVec<O, T>impl<O, T> Write for BitVec<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
Converts self
into a vector without clones or allocation.
The resulting vector can be converted back into a box via BitVec<O, T>
’s .into_boxed_bitslice()
method.
Original
API Differences
Despite taking a Box<[T]>
receiver, this function is written in an
impl<T> [T]
block.
Rust does not allow the text
impl<O, T> BitSlice<O, T> {
fn into_bitvec(self: BitBox<O, T>);
}
to be written, and BitBox
exists specifically because
Box<BitSlice<>>
cannot be written either, so this function must be
implemented directly on BitBox
rather than on BitSlice
with a boxed
receiver.
Examples
use bitvec::prelude::*;
let bb = bitbox![0, 1, 0, 1];
let bv = bb.into_bitvec();
assert_eq!(bv, bitvec![0, 1, 0, 1]);
sourcepub fn as_bitslice(&self) -> &BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
pub fn as_bitslice(&self) -> &BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
Views the buffer’s contents as a BitSlice
.
This is equivalent to &bb[..]
.
Examples
use bitvec::prelude::*;
let bb = bitbox![0, 1, 1, 0];
let bits = bb.as_bitslice();
sourcepub fn as_mut_bitslice(&mut self) -> &mut BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
pub fn as_mut_bitslice(&mut self) -> &mut BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
Extracts a mutable bit-slice of the entire vector.
Equivalent to &mut bv[..]
.
Examples
use bitvec::prelude::*;
let mut bv = bitvec![0, 1, 0, 1];
let bits = bv.as_mut_bitslice();
bits.set(0, true);
sourcepub fn as_slice(&self) -> &[T]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn as_slice(&self) -> &[T]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Extracts an element slice containing the entire box.
Analogue
See .as_bitslice()
for a &BitBox -> &BitSlice
transform.
Examples
use bitvec::prelude::*;
use std::io::{self, Write};
let buffer = bitbox![Msb0, u8; 0, 1, 0, 1, 1, 0, 0, 0];
io::sink().write(buffer.as_slice()).unwrap();
sourcepub fn as_mut_slice(&mut self) -> &mut [T]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn as_mut_slice(&mut self) -> &mut [T]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Extracts a mutable slice of the entire box.
Analogue
See .as_mut_bitslice()
for a &mut BitBox -> &mut BitSlice
transform.
Examples
use bitvec::prelude::*;
use std::io::{self, Read};
let mut buffer = bitbox![Msb0, u8; 0; 24];
io::repeat(0b101).read_exact(buffer.as_mut_slice()).unwrap();
sourcepub fn set_uninitialized(&mut self, value: bool)
pub fn set_uninitialized(&mut self, value: bool)
Sets the uninitialized bits of the vector to a fixed value.
This method modifies all bits in the allocated buffer that are outside
the self.as_bitslice()
view so that they have a consistent value. This
can be used to zero the uninitialized memory so that when viewed as a
raw memory slice, bits outside the live region have a predictable value.
Examples
use bitvec::prelude::*;
let mut bb = BitBox::new(&220u8.view_bits::<Lsb0>()[.. 4]);
assert_eq!(bb.count_ones(), 2);
assert_eq!(bb.as_slice(), &[220u8]);
bb.set_uninitialized(false);
assert_eq!(bb.as_slice(), &[12u8]);
bb.set_uninitialized(true);
assert_eq!(bb.as_slice(), &[!3u8]);
Methods from Deref<Target = BitSlice<O, T>>
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
sourcepub fn first_mut(&mut self) -> Option<BitRef<'_, Mut, O, T>>
pub fn first_mut(&mut self) -> Option<BitRef<'_, Mut, O, T>>
Returns a mutable pointer to the first bit of the slice, or None
if it is empty.
Original
API Differences
This crate cannot manifest &mut bool
references, and must use the
BitRef
proxy type where &mut bool
exists in the standard library
API. The proxy value must be bound as mut
in order to write through
it.
Examples
use bitvec::prelude::*;
let x = bits![mut 0, 1, 0];
if let Some(mut first) = x.first_mut() {
*first = true;
}
assert_eq!(x, bits![1, 1, 0]);
sourcepub fn split_first(&self) -> Option<(BitRef<'_, Const, O, T>, &Self)>
pub fn split_first(&self) -> Option<(BitRef<'_, Const, O, T>, &Self)>
sourcepub fn split_first_mut(
&mut self
) -> Option<(BitRef<'_, Mut, O, T::Alias>, &mut BitSlice<O, T::Alias>)>
pub fn split_first_mut(
&mut self
) -> Option<(BitRef<'_, Mut, O, T::Alias>, &mut BitSlice<O, T::Alias>)>
Returns the first and all the rest of the bits of the slice, or
None
if it is empty.
Original
API Differences
This crate cannot manifest &mut bool
references, and must use the
BitRef
proxy type where &mut bool
exists in the standard library
API. The proxy value must be bound as mut
in order to write through
it.
Because the references are permitted to use the same memory address, they are marked as aliasing in order to satisfy Rust’s requirements about freedom from data races.
Examples
use bitvec::prelude::*;
let x = bits![mut 0, 0, 1];
if let Some((mut first, rest)) = x.split_first_mut() {
*first = true;
rest.set(0, true);
rest.set(1, false);
}
assert_eq!(x, bits![1, 1, 0]);
sourcepub fn split_last(&self) -> Option<(BitRef<'_, Const, O, T>, &Self)>
pub fn split_last(&self) -> Option<(BitRef<'_, Const, O, T>, &Self)>
sourcepub fn split_last_mut(
&mut self
) -> Option<(BitRef<'_, Mut, O, T::Alias>, &mut BitSlice<O, T::Alias>)>
pub fn split_last_mut(
&mut self
) -> Option<(BitRef<'_, Mut, O, T::Alias>, &mut BitSlice<O, T::Alias>)>
Returns the last and all the rest of the bits of the slice, or
None
if it is empty.
Original
API Differences
This crate cannot manifest &mut bool
references, and must use the
BitRef
proxy type where &mut bool
exists in the standard library
API. The proxy value must be bound as mut
in order to write through
it.
Because the references are permitted to use the same memory address, they are marked as aliasing in order to satisfy Rust’s requirements about freedom from data races.
Examples
use bitvec::prelude::*;
let x = bits![mut 1, 0, 0];
if let Some((mut last, rest)) = x.split_last_mut() {
*last = true;
rest.set(0, false);
rest.set(1, true);
}
assert_eq!(x, bits![0, 1, 1]);
sourcepub fn last(&self) -> Option<BitRef<'_, Const, O, T>>
pub fn last(&self) -> Option<BitRef<'_, Const, O, T>>
sourcepub fn last_mut(&mut self) -> Option<BitRef<'_, Mut, O, T>>
pub fn last_mut(&mut self) -> Option<BitRef<'_, Mut, O, T>>
Returns a mutable pointer to the last bit in the slice.
Original
API Differences
This crate cannot manifest &mut bool
references, and must use the
BitRef
proxy type where &mut bool
exists in the standard library
API. The proxy value must be bound as mut
in order to write through
it.
Examples
use bitvec::prelude::*;
let x = bits![mut 0, 1, 0];
if let Some(mut last) = x.last_mut() {
*last = true;
}
assert_eq!(x, bits![0, 1, 1]);
sourcepub fn get<'a, I>(&'a self, index: I) -> Option<I::Immut> where
I: BitSliceIndex<'a, O, T>,
pub fn get<'a, I>(&'a self, index: I) -> Option<I::Immut> where
I: BitSliceIndex<'a, O, T>,
Returns a reference to a bit or subslice depending on the type of index.
- If given a position, returns a reference to the bit at that position
or
None
if out of bounds. - If given a range, returns the subslice corresponding to that range, or
None
if out of bounds.
Original
Examples
use bitvec::prelude::*;
let v = bits![0, 1, 0];
assert_eq!(Some(&true), v.get(1).as_deref());
assert_eq!(Some(bits![0, 1]), v.get(0 .. 2));
assert_eq!(None, v.get(3));
assert_eq!(None, v.get(0 .. 4));
sourcepub fn get_mut<'a, I>(&'a mut self, index: I) -> Option<I::Mut> where
I: BitSliceIndex<'a, O, T>,
pub fn get_mut<'a, I>(&'a mut self, index: I) -> Option<I::Mut> where
I: BitSliceIndex<'a, O, T>,
Returns a mutable reference to a bit or subslice depending on the type
of index (see .get()
) or None
if the index is out of bounds.
Original
API Differences
This crate cannot manifest &mut bool
references, and must use the
BitRef
proxy type where &mut bool
exists in the standard library
API. The proxy value must be bound as mut
in order to write through
it.
Examples
use bitvec::prelude::*;
let x = bits![mut 0, 0, 1];
if let Some(mut bit) = x.get_mut(1) {
*bit = true;
}
assert_eq!(x, bits![0, 1, 1]);
sourcepub unsafe fn get_unchecked<'a, I>(&'a self, index: I) -> I::Immut where
I: BitSliceIndex<'a, O, T>,
pub unsafe fn get_unchecked<'a, I>(&'a self, index: I) -> I::Immut where
I: BitSliceIndex<'a, O, T>,
Returns a reference to a bit or subslice, without doing bounds checking.
This is generally not recommended; use with caution! Calling this method
with an out-of-bounds index is undefined behavior even if the
resulting reference is not used. For a safe alternative, see .get()
.
Original
Examples
use bitvec::prelude::*;
let x = bits![0, 1, 0];
unsafe {
assert_eq!(x.get_unchecked(1), &true);
}
sourcepub unsafe fn get_unchecked_mut<'a, I>(&'a mut self, index: I) -> I::Mut where
I: BitSliceIndex<'a, O, T>,
pub unsafe fn get_unchecked_mut<'a, I>(&'a mut self, index: I) -> I::Mut where
I: BitSliceIndex<'a, O, T>,
Returns a mutable reference to a bit or subslice, without doing bounds checking.
This is generally not recommended; use with caution! Calling this method
with an out-of-bounds index is undefined behavior even if the
resulting reference is not used. For a safe alternative, see
[.get_mut()
].
Original
API Differences
This crate cannot manifest &mut bool
references, and must use the
BitRef
proxy type where &mut bool
exists in the standard library
API. The proxy value must be bound as mut
in order to write through
it.
Examples
use bitvec::prelude::*;
let x = bits![mut 0; 3];
unsafe {
let mut bit = x.get_unchecked_mut(1);
*bit = true;
}
assert_eq!(x, bits![0, 1, 0]);
sourcepub fn swap(&mut self, a: usize, b: usize)
pub fn swap(&mut self, a: usize, b: usize)
sourcepub fn reverse(&mut self)
pub fn reverse(&mut self)
sourcepub fn iter(&self) -> Iter<'_, O, T>ⓘNotable traits for Iter<'a, O, T>impl<'a, O, T> Iterator for Iter<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = <usize as BitSliceIndex<'a, O, T>>::Immut;
pub fn iter(&self) -> Iter<'_, O, T>ⓘNotable traits for Iter<'a, O, T>impl<'a, O, T> Iterator for Iter<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = <usize as BitSliceIndex<'a, O, T>>::Immut;
O: BitOrder,
T: BitStore, type Item = <usize as BitSliceIndex<'a, O, T>>::Immut;
Returns an iterator over the slice.
Original
API Differences
This iterator yields BitRef
proxy references, rather than &bool
ordinary references. It does so in order to promote consistency in the
crate, and make switching between immutable and mutable single-bit
access easier.
The produced iterator has a by_ref
adapter that yields &bool
references, and a by_val
adapter that yields bool
values. Use
these methods to fit this iterator into APIs that expect ordinary bool
inputs.
Examples
use bitvec::prelude::*;
let x = bits![0, 0, 1];
let mut iterator = x.iter();
assert_eq!(iterator.next().as_deref(), Some(&false));
assert_eq!(iterator.next().as_deref(), Some(&false));
assert_eq!(iterator.next().as_deref(), Some(&true));
assert_eq!(iterator.next().as_deref(), None);
sourcepub fn iter_mut(&mut self) -> IterMut<'_, O, T>ⓘNotable traits for IterMut<'a, O, T>impl<'a, O, T> Iterator for IterMut<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = <usize as BitSliceIndex<'a, O, T::Alias>>::Mut;
pub fn iter_mut(&mut self) -> IterMut<'_, O, T>ⓘNotable traits for IterMut<'a, O, T>impl<'a, O, T> Iterator for IterMut<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = <usize as BitSliceIndex<'a, O, T::Alias>>::Mut;
O: BitOrder,
T: BitStore, type Item = <usize as BitSliceIndex<'a, O, T::Alias>>::Mut;
Returns an iterator that allows modifying each bit.
Original
API Differences
This crate cannot manifest &mut bool
references, and must use the
BitRef
proxy type where &mut bool
exists in the standard library
API. The proxy value must be bound as mut
in order to write through
it.
This iterator marks each yielded item as aliased, as iterators can be
used to yield multiple items into the same scope. If you are using
the iterator in a manner that ensures that all yielded items have
disjoint lifetimes, you can use the .remove_alias()
adapter on it to
remove the alias marker from the yielded subslices.
Examples
use bitvec::prelude::*;
let x = bits![mut 0, 0, 1];
for mut bit in x.iter_mut() {
*bit = !*bit;
}
assert_eq!(x, bits![1, 1, 0]);
sourcepub fn windows(&self, size: usize) -> Windows<'_, O, T>ⓘNotable traits for Windows<'a, O, T>impl<'a, O, T> Iterator for Windows<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
pub fn windows(&self, size: usize) -> Windows<'_, O, T>ⓘNotable traits for Windows<'a, O, T>impl<'a, O, T> Iterator for Windows<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
Returns an iterator over all contiguous windows of length size
. The
windows overlap. If the slice is shorter than size
, the iterator
returns no values.
Original
Panics
Panics if size
is 0.
Examples
use bitvec::prelude::*;
let slice = bits![0, 0, 1, 1];
let mut iter = slice.windows(2);
assert_eq!(iter.next().unwrap(), bits![0; 2]);
assert_eq!(iter.next().unwrap(), bits![0, 1]);
assert_eq!(iter.next().unwrap(), bits![1; 2]);
assert!(iter.next().is_none());
If the slice is shorter than size
:
use bitvec::prelude::*;
let slice = bits![0; 3];
let mut iter = slice.windows(4);
assert!(iter.next().is_none());
sourcepub fn chunks(&self, chunk_size: usize) -> Chunks<'_, O, T>ⓘNotable traits for Chunks<'a, O, T>impl<'a, O, T> Iterator for Chunks<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, O, T>ⓘNotable traits for Chunks<'a, O, T>impl<'a, O, T> Iterator for Chunks<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
Returns an iterator over chunk_size
bits of the slice at a time,
starting at the beginning of the slice.
The chunks are slices and do not overlap. If chunk_size
does not
divide the length of the slice, then the last chunk will not have length
chunk_size
.
See .chunks_exact()
for a variant of this iterator that returns
chunks of always exactly chunk_size
bits, and .rchunks()
for the
same iterator but starting at the end of the slice.
Original
Panics
Panics if chunk_size
is 0.
Examples
use bitvec::prelude::*;
let slice = bits![0, 1, 0, 0, 1];
let mut iter = slice.chunks(2);
assert_eq!(iter.next().unwrap(), bits![0, 1]);
assert_eq!(iter.next().unwrap(), bits![0, 0]);
assert_eq!(iter.next().unwrap(), bits![1]);
assert!(iter.next().is_none());
sourcepub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, O, T>ⓘNotable traits for ChunksMut<'a, O, T>impl<'a, O, T> Iterator for ChunksMut<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, O, T>ⓘNotable traits for ChunksMut<'a, O, T>impl<'a, O, T> Iterator for ChunksMut<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
Returns an iterator over chunk_size
bits of the slice at a time,
starting at the beginning of the slice.
The chunks are mutable slices, and do not overlap. If chunk_size
does
not divide the length of the slice, then the last chunk will not have
length chunk_size
.
See .chunks_exact_mut()
for a variant of this iterator that returns
chunks of always exactly chunk_size
bits, and .rchunks_mut()
for
the same iterator but starting at the end of the slice.
Original
API Differences
This iterator marks each yielded item as aliased, as iterators can be
used to yield multiple items into the same scope. If you are using
the iterator in a manner that ensures that all yielded items have
disjoint lifetimes, you can use the .remove_alias()
adapter on it to
remove the alias marker from the yielded subslices.
Panics
Panics if chunk_size
is 0.
Examples
use bitvec::prelude::*;
let v = bits![mut 0; 5];
let mut count = 1;
for chunk in v.chunks_mut(2) {
for mut bit in chunk.iter_mut() {
*bit = count % 2 == 0;
}
count += 1;
}
assert_eq!(v, bits![0, 0, 1, 1, 0]);
sourcepub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, O, T>ⓘNotable traits for ChunksExact<'a, O, T>impl<'a, O, T> Iterator for ChunksExact<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, O, T>ⓘNotable traits for ChunksExact<'a, O, T>impl<'a, O, T> Iterator for ChunksExact<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
Returns an iterator over chunk_size
bits of the slice at a time,
starting at the beginning of the slice.
The chunks are slices and do not overlap. If chunk_size
does not
divide the length of the slice, then the last up to chunk_size-1
bits
will be omitted and can be retrieved from the .remainder()
method of
the iterator.
Due to each chunk having exactly chunk_size
bits, the compiler may be
able to optimize the resulting code better than in the case of
.chunks()
.
See .chunks()
for a variant of this iterator that also returns the
remainder as a smaller chunk, and .rchunks_exact()
for the same
iterator but starting at the end of the slice.
Original
Panics
Panics if chunk_size
is 0.
Examples
use bitvec::prelude::*;
let slice = bits![0, 1, 1, 0, 0];
let mut iter = slice.chunks_exact(2);
assert_eq!(iter.next().unwrap(), bits![0, 1]);
assert_eq!(iter.next().unwrap(), bits![1, 0]);
assert!(iter.next().is_none());
assert_eq!(iter.remainder(), bits![0]);
sourcepub fn chunks_exact_mut(
&mut self,
chunk_size: usize
) -> ChunksExactMut<'_, O, T>ⓘNotable traits for ChunksExactMut<'a, O, T>impl<'a, O, T> Iterator for ChunksExactMut<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
pub fn chunks_exact_mut(
&mut self,
chunk_size: usize
) -> ChunksExactMut<'_, O, T>ⓘNotable traits for ChunksExactMut<'a, O, T>impl<'a, O, T> Iterator for ChunksExactMut<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
Returns an iterator over chunk_size
bits of the slice at a time,
starting at the beginning of the slice.
The chunks are mutable slices, and do not overlap. If chunk_size
does
not divide the length of the slice, then the last up to chunk_size-1
bits will be omitted and can be retrieved from the .into_remainder()
method of the iterator.
Due to each chunk having exactly chunk_size
bits, the compiler may be
able to optimize the resulting code better than in the case of
.chunks_mut()
.
See .chunks_mut()
for a variant of this iterator that also returns
the remainder as a smaller chunk, and .rchunks_exact_mut()
for the
same iterator but starting at the end of the slice.
Original
API Differences
This iterator marks each yielded item as aliased, as iterators can be
used to yield multiple items into the same scope. If you are using
the iterator in a manner that ensures that all yielded items have
disjoint lifetimes, you can use the .remove_alias()
adapter on it to
remove the alias marker from the yielded subslices.
Panics
Panics if chunk_size
is 0.
Examples
use bitvec::prelude::*;
let v = bits![mut 0; 5];
for chunk in v.chunks_exact_mut(2) {
chunk.set_all(true);
}
assert_eq!(v, bits![1, 1, 1, 1, 0]);
sourcepub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, O, T>ⓘNotable traits for RChunks<'a, O, T>impl<'a, O, T> Iterator for RChunks<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, O, T>ⓘNotable traits for RChunks<'a, O, T>impl<'a, O, T> Iterator for RChunks<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
Returns an iterator over chunk_size
bits of the slice at a time,
starting at the end of the slice.
The chunks are slices and do not overlap. If chunk_size
does not
divide the length of the slice, then the last chunk will not have length
chunk_size
.
See .rchunks_exact()
for a variant of this iterator that returns
chunks of always exactly chunk_size
bits, and .chunks()
for the
same iterator but starting at the beginning of the slice.
Original
Panics
Panics if chunk_size
is 0.
Examples
use bitvec::prelude::*;
let slice = bits![0, 1, 0, 0, 1];
let mut iter = slice.rchunks(2);
assert_eq!(iter.next().unwrap(), bits![0, 1]);
assert_eq!(iter.next().unwrap(), bits![1, 0]);
assert_eq!(iter.next().unwrap(), bits![0]);
assert!(iter.next().is_none());
sourcepub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, O, T>ⓘNotable traits for RChunksMut<'a, O, T>impl<'a, O, T> Iterator for RChunksMut<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, O, T>ⓘNotable traits for RChunksMut<'a, O, T>impl<'a, O, T> Iterator for RChunksMut<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
Returns an iterator over chunk_size
bits of the slice at a time,
starting at the end of the slice.
The chunks are mutable slices, and do not overlap. If chunk_size
does
not divide the length of the slice, then the last chunk will not have
length chunk_size
.
See .rchunks_exact_mut()
for a variant of this iterator that returns
chunks of always exactly chunk_size
bits, and .chunks_mut()
for
the same iterator but starting at the beginning of the slice.
Original
API Differences
This iterator marks each yielded item as aliased, as iterators can be
used to yield multiple items into the same scope. If you are using
the iterator in a manner that ensures that all yielded items have
disjoint lifetimes, you can use the .remove_alias()
adapter on it to
remove the alias marker from the yielded subslices.
Panics
Panics if chunk_size
is 0.
Examples
use bitvec::prelude::*;
let v = bits![mut 0; 5];
let mut count = 1;
for chunk in v.rchunks_mut(2) {
for mut bit in chunk.iter_mut() {
*bit = count % 2 == 0;
}
count += 1;
}
assert_eq!(v, bits![0, 1, 1, 0, 0]);
sourcepub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, O, T>ⓘNotable traits for RChunksExact<'a, O, T>impl<'a, O, T> Iterator for RChunksExact<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, O, T>ⓘNotable traits for RChunksExact<'a, O, T>impl<'a, O, T> Iterator for RChunksExact<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
O: BitOrder,
T: BitStore, type Item = &'a BitSlice<O, T>;
Returns an iterator over chunk_size
bits of the slice at a time,
starting at the end of the slice.
The chunks are slices and do not overlap. If chunk_size
does not
divide the length of the slice, then the last up to chunk_size-1
bits
will be omitted and can be retrieved from the .remainder()
method of
the iterator.
Due to each chunk having exactly chunk_size
bits, the compiler may be
able to optimize the resulting code better than in the case of
.rchunks()
.
See .rchunks()
for a variant of this iterator that also returns the
remainder as a smaller chunk, and .chunks_exact()
for the same
iterator but starting at the beginning of the slice.
Original
Panics
Panics if chunk_size
is 0.
Examples
use bitvec::prelude::*;
let slice = bits![0, 0, 1, 1, 0];
let mut iter = slice.rchunks_exact(2);
assert_eq!(iter.next().unwrap(), bits![1, 0]);
assert_eq!(iter.next().unwrap(), bits![0, 1]);
assert!(iter.next().is_none());
assert_eq!(iter.remainder(), bits![0]);
sourcepub fn rchunks_exact_mut(
&mut self,
chunk_size: usize
) -> RChunksExactMut<'_, O, T>ⓘNotable traits for RChunksExactMut<'a, O, T>impl<'a, O, T> Iterator for RChunksExactMut<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
pub fn rchunks_exact_mut(
&mut self,
chunk_size: usize
) -> RChunksExactMut<'_, O, T>ⓘNotable traits for RChunksExactMut<'a, O, T>impl<'a, O, T> Iterator for RChunksExactMut<'a, O, T> where
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
O: BitOrder,
T: BitStore, type Item = &'a mut BitSlice<O, T::Alias>;
Returns an iterator over chunk_size
bits of the slice at a time,
starting at the end of the slice.
The chunks are mutable slices, and do not overlap. If chunk_size
does
not divide the length of the slice, then the last up to chunk_size-1
bits will be omitted and can be retrieved from the .into_remainder()
method of the iterator.
Due to each chunk having exactly chunk_size
bits, the compiler may be
able to optimize the resulting code better than in the case of
.rchunks_mut()
.
See .rchunks_mut()
for a variant of this iterator that also returns
the remainder as a smaller chunk, and .chunks_exact_mut()
for the
same iterator but starting at the beginning of the slice.
Original
API Differences
This iterator marks each yielded item as aliased, as iterators can be
used to yield multiple items into the same scope. If you are using
the iterator in a manner that ensures that all yielded items have
disjoint lifetimes, you can use the .remove_alias()
adapter on it to
remove the alias marker from the yielded subslices.
Panics
Panics if chunk_size
is 0.
Examples
use bitvec::prelude::*;
let v = bits![mut 0; 5];
for chunk in v.rchunks_exact_mut(2) {
chunk.set_all(true);
}
assert_eq!(v, bits![0, 1, 1, 1, 1]);
sourcepub fn split_at(&self, mid: usize) -> (&Self, &Self)
pub fn split_at(&self, mid: usize) -> (&Self, &Self)
Divides one slice into two at an index.
The first will contain all indices from [0, mid)
(excluding the index
mid
itself) and the second will contain all indices from [mid, len)
(excluding the index len
itself).
Original
Panics
Panics if mid > len
.
Behavior
When mid
is 0
or self.len()
, then the left or right return values,
respectively, are empty slices. Empty slice references produced by this
method are specified to have the address information you would expect:
a left empty slice has the same base address and start bit as self
,
and a right empty slice will have its address raised by self.len()
.
Examples
use bitvec::prelude::*;
let v = bits![0, 0, 0, 1, 1, 1];
{
let (left, right) = v.split_at(0);
assert_eq!(left, bits![]);
assert_eq!(right, v);
}
{
let (left, right) = v.split_at(2);
assert_eq!(left, bits![0, 0]);
assert_eq!(right, bits![0, 1, 1, 1]);
}
{
let (left, right) = v.split_at(6);
assert_eq!(left, v);
assert_eq!(right, bits![]);
}
sourcepub fn split_at_mut(
&mut self,
mid: usize
) -> (&mut BitSlice<O, T::Alias>, &mut BitSlice<O, T::Alias>)
pub fn split_at_mut(
&mut self,
mid: usize
) -> (&mut BitSlice<O, T::Alias>, &mut BitSlice<O, T::Alias>)
Divides one mutable slice into two at an index.
The first will contain all indices from [0, mid)
(excluding the index
mid
itself) and the second will contain all indices from [mid, len)
(excluding the index len
itself).
Original
API Differences
The partition index mid
may occur anywhere in the slice, and as a
result the two returned slices may both have write access to the memory
address containing mid
. As such, the returned slices must be marked
with T::Alias
in order to correctly manage memory access going
forward.
This marking is applied to all memory accesses in both slices,
regardless of whether any future accesses actually require it. To limit
the alias marking to only the addresses that need it, use
[.bit_domain()
] or [.bit_domain_mut()
] to split either slice into
its aliased and unaliased subslices.
Panics
Panics if mid > len
.
Behavior
When mid
is 0
or self.len()
, then the left or right return values,
respectively, are empty slices. Empty slice references produced by this
method are specified to have the address information you would expect:
a left empty slice has the same base address and start bit as self
,
and a right empty slice will have its address raised by self.len()
.
Examples
use bitvec::prelude::*;
let v = bits![mut 0, 0, 0, 1, 1, 1];
// scoped to restrict the lifetime of the borrows
{
let (left, right) = v.split_at_mut(2);
assert_eq!(left, bits![0, 0]);
assert_eq!(right, bits![0, 1, 1, 1]);
left.set(1, true);
right.set(1, false);
}
assert_eq!(v, bits![0, 1, 0, 0, 1, 1]);
sourcepub fn split<F>(&self, pred: F) -> Split<'_, O, T, F>ⓘNotable traits for Split<'a, O, T, P>impl<'a, O, T, P> Iterator for Split<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a BitSlice<O, T>;
where
F: FnMut(usize, &bool) -> bool,
pub fn split<F>(&self, pred: F) -> Split<'_, O, T, F>ⓘNotable traits for Split<'a, O, T, P>impl<'a, O, T, P> Iterator for Split<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a BitSlice<O, T>;
where
F: FnMut(usize, &bool) -> bool,
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a BitSlice<O, T>;
Returns an iterator over subslices separated by bits that match pred
.
The matched bit is not contained in the subslices.
Original
API Differences
In order to allow more than one bit of information for the split decision, the predicate receives the index of each bit, as well as its value.
Examples
use bitvec::prelude::*;
let slice = bits![0, 1, 1, 0];
let mut iter = slice.split(|pos, _bit| pos % 3 == 2);
assert_eq!(iter.next().unwrap(), bits![0, 1]);
assert_eq!(iter.next().unwrap(), bits![0]);
assert!(iter.next().is_none());
If the first bit is matched, an empty slice will be the first item returned by the iterator. Similarly, if the last bit in the slice is matched, an empty slice will be the last item returned by the iterator:
use bitvec::prelude::*;
let slice = bits![0, 0, 1];
let mut iter = slice.split(|_pos, bit| *bit);
assert_eq!(iter.next().unwrap(), bits![0, 0]);
assert_eq!(iter.next().unwrap(), bits![]);
assert!(iter.next().is_none());
If two matched bits are directly adjacent, an empty slice will be present between them:
use bitvec::prelude::*;
let slice = bits![1, 0, 0, 1];
let mut iter = slice.split(|_pos, bit| !*bit);
assert_eq!(iter.next().unwrap(), bits![1]);
assert_eq!(iter.next().unwrap(), bits![]);
assert_eq!(iter.next().unwrap(), bits![1]);
assert!(iter.next().is_none());
sourcepub fn split_mut<F>(&mut self, pred: F) -> SplitMut<'_, O, T, F>ⓘNotable traits for SplitMut<'a, O, T, P>impl<'a, O, T, P> Iterator for SplitMut<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a mut BitSlice<O, T::Alias>;
where
F: FnMut(usize, &bool) -> bool,
pub fn split_mut<F>(&mut self, pred: F) -> SplitMut<'_, O, T, F>ⓘNotable traits for SplitMut<'a, O, T, P>impl<'a, O, T, P> Iterator for SplitMut<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a mut BitSlice<O, T::Alias>;
where
F: FnMut(usize, &bool) -> bool,
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a mut BitSlice<O, T::Alias>;
Returns an iterator over mutable subslices separated by bits that match
pred
. The matched bit is not contained in the subslices.
Original
API Differences
In order to allow more than one bit of information for the split decision, the predicate receives the index of each bit, as well as its value.
This iterator marks each yielded item as aliased, as iterators can be
used to yield multiple items into the same scope. If you are using
the iterator in a manner that ensures that all yielded items have
disjoint lifetimes, you can use the .remove_alias()
adapter on it to
remove the alias marker from the yielded subslices.
Examples
use bitvec::prelude::*;
let v = bits![mut 0, 0, 1, 0, 1, 0];
for group in v.split_mut(|_pos, bit| *bit) {
group.set(0, true);
}
assert_eq!(v, bits![1, 0, 1, 1, 1, 1]);
sourcepub fn rsplit<F>(&self, pred: F) -> RSplit<'_, O, T, F>ⓘNotable traits for RSplit<'a, O, T, P>impl<'a, O, T, P> Iterator for RSplit<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a BitSlice<O, T>;
where
F: FnMut(usize, &bool) -> bool,
pub fn rsplit<F>(&self, pred: F) -> RSplit<'_, O, T, F>ⓘNotable traits for RSplit<'a, O, T, P>impl<'a, O, T, P> Iterator for RSplit<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a BitSlice<O, T>;
where
F: FnMut(usize, &bool) -> bool,
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a BitSlice<O, T>;
Returns an iterator over subslices separated by bits that match pred
,
starting at the end of the slice and working backwards. The matched bit
is not contained in the subslices.
Original
API Differences
In order to allow more than one bit of information for the split decision, the predicate receives the index of each bit, as well as its value.
Examples
use bitvec::prelude::*;
let slice = bits![1, 1, 1, 0, 1, 1];
let mut iter = slice.rsplit(|_pos, bit| !*bit);
assert_eq!(iter.next().unwrap(), bits![1; 2]);
assert_eq!(iter.next().unwrap(), bits![1; 3]);
assert!(iter.next().is_none());
As with .split()
, if the first or last bit is matched, an empty
slice will be the first (or last) item returned by the iterator.
use bitvec::prelude::*;
let v = bits![1, 0, 0, 1, 0, 0, 1];
let mut it = v.rsplit(|_pos, bit| *bit);
assert_eq!(it.next().unwrap(), bits![]);
assert_eq!(it.next().unwrap(), bits![0; 2]);
assert_eq!(it.next().unwrap(), bits![0; 2]);
assert_eq!(it.next().unwrap(), bits![]);
assert!(it.next().is_none());
sourcepub fn rsplit_mut<F>(&mut self, pred: F) -> RSplitMut<'_, O, T, F>ⓘNotable traits for RSplitMut<'a, O, T, P>impl<'a, O, T, P> Iterator for RSplitMut<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a mut BitSlice<O, T::Alias>;
where
F: FnMut(usize, &bool) -> bool,
pub fn rsplit_mut<F>(&mut self, pred: F) -> RSplitMut<'_, O, T, F>ⓘNotable traits for RSplitMut<'a, O, T, P>impl<'a, O, T, P> Iterator for RSplitMut<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a mut BitSlice<O, T::Alias>;
where
F: FnMut(usize, &bool) -> bool,
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = &'a mut BitSlice<O, T::Alias>;
Returns an iterator over mutable subslices separated by bits that match
pred
, starting at the end of the slice and working backwards. The
matched bit is not contained in the subslices.
Original
API Differences
In order to allow more than one bit of information for the split decision, the predicate receives the index of each bit, as well as its value.
This iterator marks each yielded item as aliased, as iterators can be
used to yield multiple items into the same scope. If you are using
the iterator in a manner that ensures that all yielded items have
disjoint lifetimes, you can use the .remove_alias()
adapter on it to
remove the alias marker from the yielded subslices.
Examples
use bitvec::prelude::*;
let v = bits![mut 0, 0, 1, 0, 1, 0];
for group in v.rsplit_mut(|_pos, bit| *bit) {
group.set(0, true);
}
assert_eq!(v, bits![1, 0, 1, 1, 1, 1]);
sourcepub fn splitn<F>(&self, n: usize, pred: F) -> SplitN<'_, O, T, F>ⓘNotable traits for SplitN<'a, O, T, P>impl<'a, O, T, P> Iterator for SplitN<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <Split<'a, O, T, P> as Iterator>::Item;
where
F: FnMut(usize, &bool) -> bool,
pub fn splitn<F>(&self, n: usize, pred: F) -> SplitN<'_, O, T, F>ⓘNotable traits for SplitN<'a, O, T, P>impl<'a, O, T, P> Iterator for SplitN<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <Split<'a, O, T, P> as Iterator>::Item;
where
F: FnMut(usize, &bool) -> bool,
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <Split<'a, O, T, P> as Iterator>::Item;
Returns an iterator over subslices separated by bits that match pred
,
limited to returning at most n
items. The matched bit is not contained
in the subslices.
The last item returned, if any, will contain the remainder of the slice.
Original
API Differences
In order to allow more than one bit of information for the split decision, the predicate receives the index of each bit, as well as its value.
Examples
Print the slice split once by set bits (i.e., [0, 0,]
, [0, 1, 0]
):
use bitvec::prelude::*;
let v = bits![0, 0, 1, 0, 1, 0];
for group in v.splitn(2, |_pos, bit| *bit) {
println!("{:b}", group);
}
sourcepub fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<'_, O, T, F>ⓘNotable traits for SplitNMut<'a, O, T, P>impl<'a, O, T, P> Iterator for SplitNMut<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <SplitMut<'a, O, T, P> as Iterator>::Item;
where
F: FnMut(usize, &bool) -> bool,
pub fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<'_, O, T, F>ⓘNotable traits for SplitNMut<'a, O, T, P>impl<'a, O, T, P> Iterator for SplitNMut<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <SplitMut<'a, O, T, P> as Iterator>::Item;
where
F: FnMut(usize, &bool) -> bool,
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <SplitMut<'a, O, T, P> as Iterator>::Item;
Returns an iterator over subslices separated by bits that match pred
,
limited to returning at most n
items. The matched bit is not contained
in the subslices.
The last item returned, if any, will contain the remainder of the slice.
Original
API Differences
In order to allow more than one bit of information for the split decision, the predicate receives the index of each bit, as well as its value.
This iterator marks each yielded item as aliased, as iterators can be
used to yield multiple items into the same scope. If you are using
the iterator in a manner that ensures that all yielded items have
disjoint lifetimes, you can use the .remove_alias()
adapter on it to
remove the alias marker from the yielded subslices.
Examples
use bitvec::prelude::*;
let v = bits![mut 0, 0, 1, 0, 1, 0];
for group in v.splitn_mut(2, |_pos, bit| *bit) {
group.set(0, true);
}
assert_eq!(v, bits![1, 0, 1, 1, 1, 0]);
sourcepub fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<'_, O, T, F>ⓘNotable traits for RSplitN<'a, O, T, P>impl<'a, O, T, P> Iterator for RSplitN<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <RSplit<'a, O, T, P> as Iterator>::Item;
where
F: FnMut(usize, &bool) -> bool,
pub fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<'_, O, T, F>ⓘNotable traits for RSplitN<'a, O, T, P>impl<'a, O, T, P> Iterator for RSplitN<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <RSplit<'a, O, T, P> as Iterator>::Item;
where
F: FnMut(usize, &bool) -> bool,
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <RSplit<'a, O, T, P> as Iterator>::Item;
Returns an iterator over subslices separated by bits that match pred
,
limited to returning at most n
items. This starts at the end of the
slice and works backwards. The matched bit is not contained in the
subslices.
The last item returned, if any, will contain the remainder of the slice.
Original
API Differences
In order to allow more than one bit of information for the split decision, the predicate receives the index of each bit, as well as its value.
Examples
Print the slice split once, starting from the end, by set bits (i.e.,
[0]
, [0, 0, 1, 0]
):
use bitvec::prelude::*;
let v = bits![0, 0, 1, 0, 1, 0];
for group in v.rsplitn(2, |_pos, bit| *bit) {
println!("{:b}", group);
}
sourcepub fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<'_, O, T, F>ⓘNotable traits for RSplitNMut<'a, O, T, P>impl<'a, O, T, P> Iterator for RSplitNMut<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <RSplitMut<'a, O, T, P> as Iterator>::Item;
where
F: FnMut(usize, &bool) -> bool,
pub fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<'_, O, T, F>ⓘNotable traits for RSplitNMut<'a, O, T, P>impl<'a, O, T, P> Iterator for RSplitNMut<'a, O, T, P> where
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <RSplitMut<'a, O, T, P> as Iterator>::Item;
where
F: FnMut(usize, &bool) -> bool,
O: BitOrder,
T: BitStore,
P: FnMut(usize, &bool) -> bool, type Item = <RSplitMut<'a, O, T, P> as Iterator>::Item;
Returns an iterator over subslices separated by bits that match pred
,
limited to returning at most n
items. This starts at the end of the
slice and works backwards. The matched bit is not contained in the
subslices.
The last item returned, if any, will contain the remainder of the slice.
Original
API Differences
In order to allow more than one bit of information for the split decision, the predicate receives the index of each bit, as well as its value.
This iterator marks each yielded item as aliased, as iterators can be
used to yield multiple items into the same scope. If you are using
the iterator in a manner that ensures that all yielded items have
disjoint lifetimes, you can use the .remove_alias()
adapter on it to
remove the alias marker from the yielded subslices.
Examples
use bitvec::prelude::*;
let v = bits![mut 0, 0, 1, 0, 1, 0];
for group in v.rsplitn_mut(2, |_pos, bit| *bit) {
group.set(0, true);
}
assert_eq!(v, bits![1, 0, 1, 0, 1, 1]);
sourcepub fn contains<O2, T2>(&self, x: &BitSlice<O2, T2>) -> bool where
O2: BitOrder,
T2: BitStore,
pub fn contains<O2, T2>(&self, x: &BitSlice<O2, T2>) -> bool where
O2: BitOrder,
T2: BitStore,
Returns true
if the slice contains a subslice that matches the given
span.
Original
API Differences
This searches for a matching subslice (allowing different type
parameters) rather than for a specific bit. Searching for a contained
element with a given value is not as useful on a collection of bool
.
Furthermore, BitSlice
defines any
and not_all
, which are
optimized searchers for any true
or false
bit, respectively, in a
sequence.
Examples
use bitvec::prelude::*;
let data = 0b0101_1010u8;
let bits_msb = data.view_bits::<Msb0>();
let bits_lsb = data.view_bits::<Lsb0>();
assert!(bits_msb.contains(&bits_lsb[1 .. 5]));
This example uses a palindrome pattern to demonstrate that the slice being searched for does not need to have the same type parameters as the slice being searched.
sourcepub fn starts_with<O2, T2>(&self, needle: &BitSlice<O2, T2>) -> bool where
O2: BitOrder,
T2: BitStore,
pub fn starts_with<O2, T2>(&self, needle: &BitSlice<O2, T2>) -> bool where
O2: BitOrder,
T2: BitStore,
Returns true
if needle
is a prefix of the slice.
Original
Examples
use bitvec::prelude::*;
let v = bits![0, 1, 0, 0];
assert!(v.starts_with(bits![0]));
assert!(v.starts_with(bits![0, 1]));
assert!(!v.starts_with(bits![1]));
assert!(!v.starts_with(bits![1, 0]));
Always returns true
if needle
is an empty slice:
use bitvec::prelude::*;
let v = bits![0, 1, 0];
assert!(v.starts_with(bits![]));
let v = bits![];
assert!(v.starts_with(bits![]));
sourcepub fn ends_with<O2, T2>(&self, needle: &BitSlice<O2, T2>) -> bool where
O2: BitOrder,
T2: BitStore,
pub fn ends_with<O2, T2>(&self, needle: &BitSlice<O2, T2>) -> bool where
O2: BitOrder,
T2: BitStore,
Returns true
if needle
is a suffix of the slice.
Original
Examples
use bitvec::prelude::*;
let v = bits![0, 1, 0, 0];
assert!(v.ends_with(bits![0]));
assert!(v.ends_with(bits![0; 2]));
assert!(!v.ends_with(bits![1]));
assert!(!v.ends_with(bits![1, 0]));
Always returns true
if needle
is an empty slice:
use bitvec::prelude::*;
let v = bits![0, 1, 0];
assert!(v.ends_with(bits![]));
let v = bits![];
assert!(v.ends_with(bits![]));
sourcepub fn rotate_left(&mut self, by: usize)
pub fn rotate_left(&mut self, by: usize)
Rotates the slice in-place such that the first by
bits of the slice
move to the end while the last self.len() - by
bits move to the
front. After calling .rotate_left()
, the bit previously at index by
will become the first bit in the slice.
Original
Panics
This function will panic if by
is greater than the length of the
slice. Note that by == self.len()
does not panic and is a noöp.
Complexity
Takes linear (in self.len()
) time.
Examples
use bitvec::prelude::*;
let a = bits![mut 0, 0, 1, 0, 1, 0];
a.rotate_left(2);
assert_eq!(a, bits![1, 0, 1, 0, 0, 0]);
Rotating a subslice:
use bitvec::prelude::*;
let a = bits![mut 0, 0, 1, 0, 1, 1];
a[1 .. 5].rotate_left(1);
assert_eq!(a, bits![0, 1, 0, 1, 0, 1]);
sourcepub fn rotate_right(&mut self, by: usize)
pub fn rotate_right(&mut self, by: usize)
Rotates the slice in-place such that the first self.len() - by
bits of
the slice move to the end while the last by
bits move to the front.
After calling .rotate_right()
, the bit previously at index `self.len()
- by` will become the first bit in the slice.
Original
Panics
This function will panic if by
is greater than the length of the
slice. Note that by == self.len()
does not panic and is a noöp.
Complexity
Takes linear (in self.len()
) time.
Examples
use bitvec::prelude::*;
let a = bits![mut 0, 0, 1, 1, 1, 0];
a.rotate_right(2);
assert_eq!(a, bits![1, 0, 0, 0, 1, 1]);
Rotating a subslice:
use bitvec::prelude::*;
let a = bits![mut 0, 0, 1, 0, 1, 1];
a[1 .. 5].rotate_right(1);
assert_eq!(a, bits![0, 1, 0, 1, 0, 1]);
sourcepub fn copy_within<R>(&mut self, src: R, dest: usize) where
R: RangeBounds<usize>,
pub fn copy_within<R>(&mut self, src: R, dest: usize) where
R: RangeBounds<usize>,
Copies bits from one part of the slice to another part of itself.
src
is the range within self
to copy from. dest
is the starting
index of the range within self
to copy to, which will have the same
length as src
. The two ranges may overlap. The ends of the two ranges
must be less than or equal to self.len()
.
Original
Panics
This function will panic if either range exceeds the end of the slice,
or if the end of src
is before the start.
Examples
Copying four bits within a slice:
use bitvec::prelude::*;
let bits = bits![mut 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0];
bits.copy_within(1 .. 5, 8);
assert_eq!(bits, bits![1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0]);
sourcepub unsafe fn align_to<U>(&self) -> (&Self, &BitSlice<O, U>, &Self) where
U: BitStore,
pub unsafe fn align_to<U>(&self) -> (&Self, &BitSlice<O, U>, &Self) where
U: BitStore,
Transmute the bit-slice to a bit-slice of another type, ensuring alignment of the types is maintained.
Original
API Differences
Type U
is required to have the same BitStore
type family as
type T
. If T
is a fundamental integer, so must U
be; if T
is an
::Alias
type, then so must U
. Changing the type family with this
method is unsound and strictly forbidden. Unfortunately, this cannot
be encoded in the type system, so you are required to abide by this
limitation yourself.
Implementation
The algorithm used to implement this function attempts to create the
widest possible span for the middle slice. However, the slice divisions
must abide by the Domain
restrictions: the left and right slices
produced by this function will include the head and tail elements of the
domain (if present), as well as the left and right subslices (if any)
produced by calling slice::align_to
on the domain body (if present).
The standard library implementation currently maximizes the width of the center slice, but its API does not guarantee this property, and retains the right to produce pessimal slices. As such, this function cannot guarantee maximal center slice width either, and you cannot rely on this behavior for correctness of your work; it is only a possible performance improvement.
Safety
This method is essentially a mem::transmute
with respect to the
memory region in the retured middle slice, so all of the usual caveats
pertaining to mem::transmute::<T, U>
also apply here.
Examples
Basic usage:
use bitvec::prelude::*;
unsafe {
let bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
let bits = bytes.view_bits::<LocalBits>();
let (prefix, shorts, suffix) = bits.align_to::<u16>();
match prefix.len() {
0 => {
assert_eq!(shorts, bits[.. 48]);
assert_eq!(suffix, bits[48 ..]);
},
8 => {
assert_eq!(prefix, bits[.. 8]);
assert_eq!(shorts, bits[8 ..]);
},
_ => unreachable!("This case will not occur")
}
}
sourcepub unsafe fn align_to_mut<U>(
&mut self
) -> (&mut Self, &mut BitSlice<O, U>, &mut Self) where
U: BitStore,
pub unsafe fn align_to_mut<U>(
&mut self
) -> (&mut Self, &mut BitSlice<O, U>, &mut Self) where
U: BitStore,
Transmute the bit-slice to a bit-slice of another type, ensuring alignment of the types is maintained.
Original
API Differences
Type U
is required to have the same BitStore
type family as
type T
. If T
is a fundamental integer, so must U
be; if T
is an
::Alias
type, then so must U
. Changing the type family with this
method is unsound and strictly forbidden. Unfortunately, this cannot
be encoded in the type system, so you are required to abide by this
limitation yourself.
Implementation
The algorithm used to implement this function attempts to create the
widest possible span for the middle slice. However, the slice divisions
must abide by the DomainMut
restrictions: the left and right slices
produced by this function will include the head and tail elements of the
domain (if present), as well as the left and right subslices (if any)
produced by calling slice::align_to_mut
on the domain body (if
present).
The standard library implementation currently maximizes the width of the center slice, but its API does not guarantee this property, and retains the right to produce pessimal slices. As such, this function cannot guarantee maximal center slice width either, and you cannot rely on this behavior for correctness of your work; it is only a possible performance improvement.
Safety
This method is essentially a mem::transmute
with respect to the
memory region in the retured middle slice, so all of the usual caveats
pertaining to mem::transmute::<T, U>
also apply here.
Examples
Basic usage:
use bitvec::prelude::*;
unsafe {
let mut bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
let bits = bytes.view_bits_mut::<LocalBits>();
let (prefix, shorts, suffix) = bits.align_to_mut::<u16>();
// same access and behavior as in `align_to`
}
sourcepub fn repeat(&self, n: usize) -> BitVec<O, T::Unalias>ⓘNotable traits for BitVec<O, T>impl<O, T> Write for BitVec<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
pub fn repeat(&self, n: usize) -> BitVec<O, T::Unalias>ⓘNotable traits for BitVec<O, T>impl<O, T> Write for BitVec<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
Creates a vector by repeating a slice n
times.
Original
Panics
This function will panic if the capacity would overflow.
Examples
Basic usage:
use bitvec::prelude::*;
assert_eq!(bits![0, 1].repeat(3), bits![0, 1, 0, 1, 0, 1]);
A panic upon overflow:
use bitvec::prelude::*;
// this will panic at runtime
bits![0, 1].repeat(BitSlice::<LocalBits, usize>::MAX_BITS);
sourcepub fn set(&mut self, index: usize, value: bool)
pub fn set(&mut self, index: usize, value: bool)
Writes a new bit at a given index.
Parameters
&mut self
index
: The bit index at which to write. It must be in the range0 .. self.len()
.value
: The value to be written;true
for1
orfalse
for0
.
Effects
If index
is valid, then the bit to which it refers is set to value
.
Panics
This method panics if index
is not less than self.len()
.
Examples
use bitvec::prelude::*;
let bits = bits![mut 0];
assert!(!bits[0]);
bits.set(0, true);
assert!(bits[0]);
This example panics when it attempts to set a bit that is out of bounds.
use bitvec::prelude::*;
let bits = bits![mut 0];
bits.set(1, false);
sourcepub fn set_aliased(&self, index: usize, value: bool) where
T: Radium,
pub fn set_aliased(&self, index: usize, value: bool) where
T: Radium,
Writes a new bit at a given index.
This method supports writing through a shared reference to a bit that
may be observed by other BitSlice
handles. It is only present when the
T
type parameter supports such shared mutation (measured by the
Radium
trait).
Parameters
&self
index
: The bit index at which to write. It must be in the range0 .. self.len()
.value
: The value to be written;true
for1
orfalse
for0
.
Effects
If index
is valid, then the bit to which it refers is set to value
.
If T
is an atomic, this will lock the memory bus for the referent
address, and may cause stalls.
Panics
This method panics if index
is not less than self.len()
.
Examples
use bitvec::prelude::*;
use core::cell::Cell;
let byte = Cell::new(0u8);
let bits = byte.view_bits::<Msb0>();
let bits_2 = bits;
bits.set_aliased(1, true);
assert!(bits_2[1]);
This example panics when it attempts to set a bit that is out of bounds.
use bitvec::prelude::*;
use core::cell::Cell;
let byte = Cell::new(0u8);
let bits = byte.view_bits::<Lsb0>();
bits.set_aliased(8, false);
sourcepub fn any(&self) -> bool
pub fn any(&self) -> bool
Tests if any bit in the slice is set (logical ∨
).
Truth Table
0 0 => 0
0 1 => 1
1 0 => 1
1 1 => 1
Parameters
&self
Returns
Whether any bit in the slice domain is set. The empty slice returns
false
.
Examples
use bitvec::prelude::*;
let bits = bits![0, 1, 0, 0];
assert!(bits[.. 2].any());
assert!(!bits[2 ..].any());
sourcepub fn all(&self) -> bool
pub fn all(&self) -> bool
Tests if all bits in the slice domain are set (logical ∧
).
Truth Table
0 0 => 0
0 1 => 0
1 0 => 0
1 1 => 1
Parameters
&self
Returns
Whether all bits in the slice domain are set. The empty slice returns
true
.
Examples
use bitvec::prelude::*;
let bits = bits![1, 1, 0, 1];
assert!(bits[.. 2].all());
assert!(!bits[2 ..].all());
sourcepub fn not_any(&self) -> bool
pub fn not_any(&self) -> bool
Tests if all bits in the slice are unset (logical ¬∨
).
Truth Table
0 0 => 1
0 1 => 0
1 0 => 0
1 1 => 0
Parameters
&self
Returns
Whether all bits in the slice domain are unset.
Examples
use bitvec::prelude::*;
let bits = bits![0, 1, 0, 0];
assert!(!bits[.. 2].not_any());
assert!(bits[2 ..].not_any());
sourcepub fn not_all(&self) -> bool
pub fn not_all(&self) -> bool
Tests if any bit in the slice is unset (logical ¬∧
).
Truth Table
0 0 => 1
0 1 => 1
1 0 => 1
1 1 => 0
Parameters
&self
Returns
Whether any bit in the slice domain is unset.
Examples
use bitvec::prelude::*;
let bits = bits![1, 1, 0, 1];
assert!(!bits[.. 2].not_all());
assert!(bits[2 ..].not_all());
sourcepub fn some(&self) -> bool
pub fn some(&self) -> bool
Tests whether the slice has some, but not all, bits set and some, but not all, bits unset.
This is false
if either .all()
or .not_any()
are true
.
Truth Table
0 0 => 0
0 1 => 1
1 0 => 1
1 1 => 0
Parameters
&self
Returns
Whether the slice domain has mixed content. The empty slice returns
false
.
Examples
use bitvec::prelude::*;
let data = 0b111_000_10u8;
let bits = bits![1, 1, 0, 0, 1, 0];
assert!(!bits[.. 2].some());
assert!(!bits[2 .. 4].some());
assert!(bits.some());
sourcepub fn count_ones(&self) -> usize
pub fn count_ones(&self) -> usize
Counts the number of bits set to 1
in the slice contents.
Parameters
&self
Returns
The number of bits in the slice domain that are set to 1
.
Examples
Basic usage:
use bitvec::prelude::*;
let bits = bits![1, 1, 0, 0];
assert_eq!(bits[.. 2].count_ones(), 2);
assert_eq!(bits[2 ..].count_ones(), 0);
sourcepub fn count_zeros(&self) -> usize
pub fn count_zeros(&self) -> usize
Counts the number of bits cleared to 0
in the slice contents.
Parameters
&self
Returns
The number of bits in the slice domain that are cleared to 0
.
Examples
Basic usage:
use bitvec::prelude::*;
let bits = bits![1, 1, 0, 0];
assert_eq!(bits[.. 2].count_zeros(), 0);
assert_eq!(bits[2 ..].count_zeros(), 2);
sourcepub fn iter_ones(&self) -> IterOnes<'_, O, T>ⓘNotable traits for IterOnes<'_, O, T>impl<O, T> Iterator for IterOnes<'_, O, T> where
O: BitOrder,
T: BitStore, type Item = usize;
pub fn iter_ones(&self) -> IterOnes<'_, O, T>ⓘNotable traits for IterOnes<'_, O, T>impl<O, T> Iterator for IterOnes<'_, O, T> where
O: BitOrder,
T: BitStore, type Item = usize;
O: BitOrder,
T: BitStore, type Item = usize;
Enumerates all bits in a BitSlice
that are set to 1
.
Examples
use bitvec::prelude::*;
let bits = bits![0, 1, 0, 0, 1, 0, 0, 0, 1];
let mut indices = [1, 4, 8].iter().copied();
let mut iter_ones = bits.iter_ones();
let mut compose = bits.iter()
.copied()
.enumerate()
.filter_map(|(idx, bit)| if bit { Some(idx) } else { None });
for ((a, b), c) in iter_ones.zip(compose).zip(indices) {
assert_eq!(a, b);
assert_eq!(b, c);
}
sourcepub fn iter_zeros(&self) -> IterZeros<'_, O, T>ⓘNotable traits for IterZeros<'_, O, T>impl<O, T> Iterator for IterZeros<'_, O, T> where
O: BitOrder,
T: BitStore, type Item = usize;
pub fn iter_zeros(&self) -> IterZeros<'_, O, T>ⓘNotable traits for IterZeros<'_, O, T>impl<O, T> Iterator for IterZeros<'_, O, T> where
O: BitOrder,
T: BitStore, type Item = usize;
O: BitOrder,
T: BitStore, type Item = usize;
Enumerates all bits in a BitSlice
that are cleared to 0
.
Examples
use bitvec::prelude::*;
let bits = bits![1, 0, 1, 1, 0, 1, 1, 1, 0];
let mut indices = [1, 4, 8].iter().copied();
let mut iter_zeros = bits.iter_zeros();
let mut compose = bits.iter()
.copied()
.enumerate()
.filter_map(|(idx, bit)| if !bit { Some(idx) } else { None });
for ((a, b), c) in iter_zeros.zip(compose).zip(indices) {
assert_eq!(a, b);
assert_eq!(b, c);
}
sourcepub fn first_one(&self) -> Option<usize>
pub fn first_one(&self) -> Option<usize>
Gets the index of the first bit in the bit-slice set to 1
.
Examples
use bitvec::prelude::*;
assert!(bits![].first_one().is_none());
assert_eq!(bits![0, 0, 1].first_one().unwrap(), 2);
sourcepub fn first_zero(&self) -> Option<usize>
pub fn first_zero(&self) -> Option<usize>
Gets the index of the first bit in the bit-slice set to 0
.
Examples
use bitvec::prelude::*;
assert!(bits![].first_zero().is_none());
assert_eq!(bits![1, 1, 0].first_zero().unwrap(), 2);
sourcepub fn last_one(&self) -> Option<usize>
pub fn last_one(&self) -> Option<usize>
Gets the index of the last bit in the bit-slice set to 1
.
Examples
use bitvec::prelude::*;
assert!(bits![].last_one().is_none());
assert_eq!(bits![1, 0, 0, 1].last_one().unwrap(), 3);
sourcepub fn last_zero(&self) -> Option<usize>
pub fn last_zero(&self) -> Option<usize>
Gets the index of the last bit in the bit-slice set to 0
.
Examples
use bitvec::prelude::*;
assert!(bits![].last_zero().is_none());
assert_eq!(bits![0, 1, 1, 0].last_zero().unwrap(), 3);
sourcepub fn leading_ones(&self) -> usize
pub fn leading_ones(&self) -> usize
Counts the number of bits from the start of the bit-slice to the first
bit set to 0
.
This returns 0
if the bit-slice is empty.
Examples
use bitvec::prelude::*;
assert_eq!(bits![].leading_ones(), 0);
assert_eq!(bits![0].leading_ones(), 0);
assert_eq!(bits![1, 0, 1, 1].leading_ones(), 1);
assert_eq!(bits![1, 1, 1, 1].leading_ones(), 4);
sourcepub fn leading_zeros(&self) -> usize
pub fn leading_zeros(&self) -> usize
Counts the number of bits from the start of the bit-slice to the first
bit set to 1
.
This returns 0
if the bit-slice is empty.
Examples
use bitvec::prelude::*;
assert_eq!(bits![].leading_zeros(), 0);
assert_eq!(bits![1].leading_zeros(), 0);
assert_eq!(bits![0, 1, 0, 0].leading_zeros(), 1);
assert_eq!(bits![0, 0, 0, 0].leading_zeros(), 4);
sourcepub fn trailing_ones(&self) -> usize
pub fn trailing_ones(&self) -> usize
Counts the number of bits from the end of the bit-slice to the last bit
set to 0
.
This returns 0
if the bit-slice is empty.
Examples
use bitvec::prelude::*;
assert_eq!(bits![].trailing_ones(), 0);
assert_eq!(bits![0].trailing_ones(), 0);
assert_eq!(bits![1, 0, 1, 1].trailing_ones(), 2);
sourcepub fn trailing_zeros(&self) -> usize
pub fn trailing_zeros(&self) -> usize
Counts the number of bits from the end of the bit-slice to the last bit
set to 1
.
This returns 0
if the bit-slice is empty.
Examples
use bitvec::prelude::*;
assert_eq!(bits![].trailing_zeros(), 0);
assert_eq!(bits![1].trailing_zeros(), 0);
assert_eq!(bits![0, 1, 0, 0].trailing_zeros(), 2);
sourcepub fn clone_from_bitslice<O2, T2>(&mut self, src: &BitSlice<O2, T2>) where
O2: BitOrder,
T2: BitStore,
pub fn clone_from_bitslice<O2, T2>(&mut self, src: &BitSlice<O2, T2>) where
O2: BitOrder,
T2: BitStore,
Copies the bits from src
into self
.
The length of src
must be the same as `self.
If src
has the same type arguments as self
, it can be more
performant to use .copy_from_bitslice()
.
Original
API Differences
This method is renamed, as it takes a bit slice rather than an element slice.
Panics
This function will panic if the two slices have different lengths.
Examples
Cloning two bits from a slice into another:
use bitvec::prelude::*;
let src = bits![Msb0, u16; 1; 4];
let dst = bits![mut Lsb0, u8; 0; 2];
dst.clone_from_bitslice(&src[2 ..]);
assert_eq!(dst, bits![1; 2]);
Rust enforces that there can only be one mutable reference with no immutable references to a particular piece of data in a particular scope. Because of this, attempting to use clone_from_slice on a single slice will result in a compile failure:
use bitvec::prelude::*;
let slice = bits![mut 0, 0, 0, 1, 1];
slice[.. 2].clone_from_bitslice(&slice[3 ..]); // compile fail!
To work around this, we can use .split_at_mut()
to create two
distinct sub-slices from a slice:
use bitvec::prelude::*;
let slice = bits![mut 0, 0, 0, 1, 1];
{
let (left, right) = slice.split_at_mut(2);
left.clone_from_bitslice(&right[1 ..]);
}
assert_eq!(slice, bits![1, 1, 0, 1, 1]);
Performance
If self
and src
use the same type arguments, this specializes to
.copy_from_bitslice()
; if you know statically that this is the case,
prefer to call that method directly and avoid the cost of detection at
runtime. Otherwise, this is a bit-by-bit crawl across both slices, which
is a slow process.
sourcepub fn copy_from_bitslice(&mut self, src: &Self)
pub fn copy_from_bitslice(&mut self, src: &Self)
Copies all bits from src
into self
, using a memcpy wherever
possible.
The length of src
must be same as self
.
If src
does not use the same type arguments as self
, use
.clone_from_bitslice()
.
Original
API Differences
This method is renamed, as it takes a bit slice rather than an element slice.
Panics
This function will panic if the two slices have different lengths.
Examples
Copying two bits from a slice into another:
use bitvec::prelude::*;
let src = bits![1; 4];
let dst = bits![mut 0; 2];
// Because the slices have to be the same length,
// we slice the source slice from four bits to
// two. It will panic if we don't do this.
dst.clone_from_bitslice(&src[2..]);
Rust enforces that there can only be one mutable reference with no immutable references to a particular piece of data in a particular scope. Because of this, attempting to use [.copy_from_slice()] on a single slice will result in a compile failure:
use bitvec::prelude::*;
let slice = bits![mut 0, 0, 0, 1, 1];
slice[.. 2].copy_from_bitslice(&bits[3 ..]); // compile fail!
To work around this, we can use .split_at_mut()
to create two
distinct sub-slices from a slice:
use bitvec::prelude::*;
let slice = bits![mut 0, 0, 0, 1, 1];
{
let (left, right) = slice.split_at_mut(2);
left.copy_from_bitslice(&right[1 ..]);
}
assert_eq!(slice, bits![1, 1, 0, 1, 1]);
sourcepub fn swap_with_bitslice<O2, T2>(&mut self, other: &mut BitSlice<O2, T2>) where
O2: BitOrder,
T2: BitStore,
pub fn swap_with_bitslice<O2, T2>(&mut self, other: &mut BitSlice<O2, T2>) where
O2: BitOrder,
T2: BitStore,
Swaps all bits in self
with those in other
.
The length of other
must be the same as self
.
Original
API Differences
This method is renamed, as it takes a bit slice rather than an element slice.
Panics
This function will panic if the two slices have different lengths.
Examples
use bitvec::prelude::*;
let mut one = [0xA5u8, 0x69];
let mut two = 0x1234u16;
let one_bits = one.view_bits_mut::<Msb0>();
let two_bits = two.view_bits_mut::<Lsb0>();
one_bits.swap_with_bitslice(two_bits);
assert_eq!(one, [0x2C, 0x48]);
assert_eq!(two, 0x96A5);
sourcepub fn shift_left(&mut self, by: usize)
pub fn shift_left(&mut self, by: usize)
Shifts the contents of a bit-slice left (towards index 0
).
This moves the contents of the slice from by ..
down to
0 .. len - by
, and erases len - by ..
to 0
. As this is a
destructive (and linearly expensive) operation, you may prefer instead
to use range subslicing.
Parameters
&mut self
by
: The distance by which to shift the slice contents.
Panics
This panics if by
is not less than self.len()
.
Examples
use bitvec::prelude::*;
let bits = bits![mut 1; 6];
bits.shift_left(2);
assert_eq!(bits, bits![1, 1, 1, 1, 0, 0]);
sourcepub fn shift_right(&mut self, by: usize)
pub fn shift_right(&mut self, by: usize)
Shifts the contents of a bit-slice right (towards index self.len()
).
This moves the contents of the slice from .. len - by
up to by ..
,
and erases .. by
to 0
. As this is a destructive (and linearly
expensive) operation, you may prefer instead to use range subslicing.
Parameters
&mut self
by
: The distance by which to shift the slice contents.
Panics
This panics if by
is not less than self.len()
.
Examples
use bitvec::prelude::*;
let bits = bits![mut 1; 6];
bits.shift_right(2);
assert_eq!(bits, bits![0, 0, 1, 1, 1, 1]);
sourcepub fn set_all(&mut self, value: bool)
pub fn set_all(&mut self, value: bool)
Sets all bits in the slice to a value.
Parameters
&mut self
value
: The bit value to which all bits in the slice will be set.
Examples
use bitvec::prelude::*;
let mut src = 0u8;
let bits = src.view_bits_mut::<Msb0>();
bits[2 .. 6].set_all(true);
assert_eq!(bits.as_slice(), &[0b0011_1100]);
bits[3 .. 5].set_all(false);
assert_eq!(bits.as_slice(), &[0b0010_0100]);
bits[.. 1].set_all(true);
assert_eq!(bits.as_slice(), &[0b1010_0100]);
sourcepub fn for_each<F>(&mut self, func: F) where
F: FnMut(usize, bool) -> bool,
pub fn for_each<F>(&mut self, func: F) where
F: FnMut(usize, bool) -> bool,
Applies a function to each bit in the slice.
BitSlice
cannot implement IndexMut
, as it cannot manifest &mut bool
references, and the BitRef
proxy reference has an unavoidable
overhead. This method bypasses both problems, by applying a function to
each pair of index and value in the slice, without constructing a proxy
reference. Benchmarks indicate that this method is about 2–4 times
faster than the .iter_mut().enumerate()
equivalent.
Parameters
&mut self
func
: A function which receives two arguments,index: usize
andvalue: bool
, and returns abool
.
Effects
For each index in the slice, the result of invoking func
with the
index number and current bit value is written into the slice.
Examples
use bitvec::prelude::*;
let mut data = 0u8;
let bits = data.view_bits_mut::<Msb0>();
bits.for_each(|idx, _bit| idx % 3 == 0);
assert_eq!(data, 0b100_100_10);
sourcepub fn offset_from(&self, other: &Self) -> isize
pub fn offset_from(&self, other: &Self) -> isize
Produces the absolute offset in bits between two slice heads.
While this method is sound for any two arbitrary bit slices, the answer it produces is meaningful only when one argument is a strict subslice of the other. If the two slices are created from different buffers entirely, a comparison is undefined; if the two slices are disjoint regions of the same buffer, then the semantically correct distance is between the tail of the lower and the head of the upper, which this does not measure.
Visual Description
Consider the following sequence of bits:
[ 0 1 2 3 4 5 6 7 8 9 a b ]
| ^^^^^^^ |
^^^^^^^^^^^^^^^^^^^^^^^
It does not matter whether there are bits between the tail of the smaller and the larger slices. The offset is computed from the bit distance between the two heads.
Behavior
This function computes the semantic distance between the heads, rather
than the *electrical. It does not take into account the BitOrder
implementation of the slice.
Safety and Soundness
One of self
or other
must contain the other for this comparison to
be meaningful.
Parameters
&self
other
: Another bit slice. This must be either a strict subregion or a strict superregion ofself
.
Returns
The distance in (semantic) bits betwen the heads of each region. The
value is positive when other
is higher in the address space than
self
, and negative when other
is lower in the address space than
self
.
sourcepub unsafe fn set_unchecked(&mut self, index: usize, value: bool)
pub unsafe fn set_unchecked(&mut self, index: usize, value: bool)
Writes a new bit at a given index, without doing bounds checking.
This is generally not recommended; use with caution! Calling this method
with an out-of-bounds index is undefined behavior. For a safe
alternative, see .set()
.
Parameters
&mut self
index
: The bit index at which to write. It must be in the range0 .. self.len()
.value
: The value to be written;true
for1
orfalse
for0
.
Effects
The bit at index
is set to value
. If index
is out of bounds, then
the memory access is incorrect, and its behavior is unspecified.
Safety
This method is not safe. It performs raw pointer arithmetic to seek
from the start of the slice to the requested index, and set the bit
there. It does not inspect the length of self
, and it is free to
perform out-of-bounds memory write access.
Use this method only when you have already performed the bounds check, and can guarantee that the call occurs with a safely in-bounds index.
Examples
This example uses a bit slice of length 2, and demonstrates out-of-bounds access to the last bit in the element.
use bitvec::prelude::*;
let bits = bits![mut 0; 2];
let (first, _) = bits.split_at_mut(1);
unsafe {
first.set_unchecked(1, true);
}
assert_eq!(bits, bits![0, 1]);
sourcepub unsafe fn set_aliased_unchecked(&self, index: usize, value: bool) where
T: Radium,
pub unsafe fn set_aliased_unchecked(&self, index: usize, value: bool) where
T: Radium,
Writes a new bit at a given index, without doing bounds checking.
This method supports writing through a shared reference to a bit that
may be observed by other BitSlice
handles. It is only present when the
T
type parameter supports such shared mutation (measured by the
Radium
trait).
Effects
The bit at index
is set to value
. If index
is out of bounds, then
the memory access is incorrect, and its behavior is unspecified. If T
is an atomic, this will lock the memory bus for the referent
address, and may cause stalls.
Safety
This method is not safe. It performs raw pointer arithmetic to seek
from the start of the slice to the requested index, and set the bit
there. It does not inspect the length of self
, and it is free to
perform out-of-bounds memory write access.
Use this method only when you have already performed the bounds check, and can guarantee that the call occurs with a safely in-bounds index.
Examples
use bitvec::prelude::*;
use core::cell::Cell;
let byte = Cell::new(0u8);
let bits = byte.view_bits::<Msb0>();
let bits_2 = bits;
let (first, _) = bits.split_at(1);
assert_eq!(first.len(), 1);
unsafe { first.set_aliased_unchecked(2, true); }
assert!(bits_2[2]);
sourcepub unsafe fn swap_unchecked(&mut self, a: usize, b: usize)
pub unsafe fn swap_unchecked(&mut self, a: usize, b: usize)
sourcepub unsafe fn split_at_unchecked(&self, mid: usize) -> (&Self, &Self)
pub unsafe fn split_at_unchecked(&self, mid: usize) -> (&Self, &Self)
Divides one slice into two at an index, without performing any bounds checking.
See .split_at()
.
Safety
mid
must not be greater than self.len()
. If this condition is
violated, the function behavior is unspecified.
Examples
use bitvec::prelude::*;
let bits = bits![0, 0, 0, 1, 1, 1];
let (l, r) = unsafe { bits.split_at_unchecked(3) };
assert!(l.not_any());
assert!(r.all());
let (l, r) = unsafe { bits.split_at_unchecked(6) };
assert_eq!(l, bits);
assert!(r.is_empty());
sourcepub unsafe fn split_at_unchecked_mut(
&mut self,
mid: usize
) -> (&mut BitSlice<O, T::Alias>, &mut BitSlice<O, T::Alias>)
pub unsafe fn split_at_unchecked_mut(
&mut self,
mid: usize
) -> (&mut BitSlice<O, T::Alias>, &mut BitSlice<O, T::Alias>)
Divides one mutable slice into two at an index.
See .split_at_mut()
.
Safety
mid
must not be greater than self.len()
.
sourcepub unsafe fn copy_within_unchecked<R>(&mut self, src: R, dest: usize) where
R: RangeBounds<usize>,
pub unsafe fn copy_within_unchecked<R>(&mut self, src: R, dest: usize) where
R: RangeBounds<usize>,
Copies bits from one part of the slice to another part of itself, without doing bounds checks.
The ranges are allowed to overlap.
Parameters
&mut self
src
: The range withinself
from which to copy.dst
: The starting index withinself
at which to paste.
Effects
self[src]
is copied to self[dest .. dest + src.end() - src.start()]
.
Safety
src
and dest .. dest + src.len()
must be entirely within
self.len()
.
sourcepub fn as_bitptr(&self) -> BitPtr<Const, O, T>
pub fn as_bitptr(&self) -> BitPtr<Const, O, T>
Returns a raw bit-pointer to the base of the bit-slice’s region.
The caller must ensure that the bit-slice outlives the bit-pointer this function returns, or else it will end up pointing to garbage.
The caller must also ensure that the memory the bit-pointer
(non-transitively) points to is never written to using this bit-pointer
or any bit-pointer derived from it. If you need to mutate the contents
of the slice, use .as_mut_bitptr()
.
Modifying the container referenced by this bit-slice may cause its buffer to be reällocated, which would also make any bit-pointers to it invalid.
Original
API Differences
This returns a structure, BitPtr
, rather than an actual raw pointer
*Bit
. The information required to address a bit within a memory
element cannot be encoded into a single pointer.
This structure can be converted back into a &BitSlice
with the
function from_raw_parts
.
Examples
use bitvec::prelude::*;
let x = bits![0, 0, 1];
let x_ptr = x.as_ptr();
unsafe {
for i in 0 .. x.len() {
assert_eq!(*x.get_unchecked(i), (&*x)[i]);
}
}
sourcepub fn as_mut_bitptr(&mut self) -> BitPtr<Mut, O, T>
pub fn as_mut_bitptr(&mut self) -> BitPtr<Mut, O, T>
Returns an unsafe mutable bit-pointer to the bit-slice’s region.
The caller must ensure that the bit-slice outlives the bit-pointer this function returns, or else it will end up pointing to garbage.
Modifying the container referenced by this bit-slice may cause its buffer to be reällocated, which would also make any bit-pointers to it invalid.
Original
API Differences
This returns *mut BitSlice
, which is the equivalont of *mut [T]
instead of *mut T
. The pointer encoding used requires more than one
CPU word of space to address a single bit, so there is no advantage to
removing the length information from the encoded pointer value.
Examples
use bitvec::prelude::*;
let bits = bits![mut Lsb0, u8; 0; 8];
let bits_ptr = bits.as_mut_ptr();
for i in 0 .. bits.len() {
unsafe {
bits_ptr.add(i).write(i % 3 == 0);
}
}
assert_eq!(bits.as_slice()[0], 0b0100_1001);
sourcepub fn as_bitptr_range(&self) -> BitPtrRange<Const, O, T>ⓘNotable traits for BitPtrRange<M, O, T>impl<M, O, T> Iterator for BitPtrRange<M, O, T> where
M: Mutability,
O: BitOrder,
T: BitStore, type Item = BitPtr<M, O, T>;
pub fn as_bitptr_range(&self) -> BitPtrRange<Const, O, T>ⓘNotable traits for BitPtrRange<M, O, T>impl<M, O, T> Iterator for BitPtrRange<M, O, T> where
M: Mutability,
O: BitOrder,
T: BitStore, type Item = BitPtr<M, O, T>;
M: Mutability,
O: BitOrder,
T: BitStore, type Item = BitPtr<M, O, T>;
Returns the two raw bit-pointers spanning the bit-slice.
The returned range is half-open, which means that the end bit-pointer points one past the last bit of the bit-slice. This way, an empty bit-slice is represented by two equal bit-pointers, and the difference between the two bit-pointers represents the size of the bit-slice.
See as_bitptr
for warnings on using these bit-pointers. The end
bit-pointer requires extra caution, as it does not point to a valid bit
in the bit-slice.
This function allows a more direct access to bit-pointers, without
paying the cost of encoding into a *BitSlice
, at the cost of no longer
fitting into ordinary Rust interfaces.
Original
API Differences
This returns a dedicated structure, rather than a range of BitPtr
s,
because the traits needed for non-core
types to correctly operate in
ranges are still unstable. The structure can be converted into a range,
but that range will not be an iterator.
Examples
use bitvec::prelude::*;
let bits = bits![0, 1, 0, 0, 1];
let mid_ptr = bits.get(2).unwrap().into_bitptr();
let mut range = bits.as_bitptr_range();
assert!(range.contains(&mid_ptr));
unsafe {
assert!(!range.next().unwrap().read());
assert!(range.next_back().unwrap().read())
}
sourcepub fn as_mut_bitptr_range(&mut self) -> BitPtrRange<Mut, O, T>ⓘNotable traits for BitPtrRange<M, O, T>impl<M, O, T> Iterator for BitPtrRange<M, O, T> where
M: Mutability,
O: BitOrder,
T: BitStore, type Item = BitPtr<M, O, T>;
pub fn as_mut_bitptr_range(&mut self) -> BitPtrRange<Mut, O, T>ⓘNotable traits for BitPtrRange<M, O, T>impl<M, O, T> Iterator for BitPtrRange<M, O, T> where
M: Mutability,
O: BitOrder,
T: BitStore, type Item = BitPtr<M, O, T>;
M: Mutability,
O: BitOrder,
T: BitStore, type Item = BitPtr<M, O, T>;
Returns the two unsafe mutable bit-pointers spanning the bit-slice.
The returned range is half-open, which means that the end bit-pointer points one past the last bitt of the bit-slice. This way, an empty bit-slice is represented by two equal bit-pointers, and the difference between the two bit-pointers represents the size of the bit-slice.
See as_mut_bitptr
for warnings on using these bit-pointers. The end
bit-pointer requires extra caution, as it does not point to a valid bit
in the bit-slice.
Original
API Differences
This returns a dedicated structure, rather than a range of BitPtr
s,
because the traits needed for non-core
types to correctly operate in
ranges are still unstable. The structure can be converted into a range,
but that range will not be an iterator.
Examples
use bitvec::prelude::*;
use bitvec::ptr as bv_ptr;
let mut data = 0u8;
let bits = data.view_bits_mut::<Msb0>();
for mut bitptr in bits.as_mut_bitptr_range() {
unsafe { bv_ptr::write(bitptr, true); }
}
assert_eq!(data, !0);
sourcepub fn bit_domain(&self) -> BitDomain<'_, O, T>
pub fn bit_domain(&self) -> BitDomain<'_, O, T>
Splits the slice into subslices at alias boundaries.
This splits self
into the memory locations that it partially fills and
the memory locations that it completely fills. The locations that are
completely filled may be accessed without any bitvec
-imposed alias
conditions, while the locations that are only partially filled are left
unchanged.
You can read more about the BitDomain
splitting in its
documentation.
Examples
use bitvec::prelude::*;
let mut data = [0u16; 3];
let all = data.view_bits_mut::<Msb0>();
let (_, rest) = all.split_at_mut(8);
let bits: &BitSlice<Msb0, <u16 as BitStore>::Alias> = &rest[.. 32];
let (head, body, tail) = bits
.bit_domain()
.region()
.unwrap();
assert_eq!(head.len(), 8);
assert_eq!(tail.len(), 8);
let _: &BitSlice<Msb0, <u16 as BitStore>::Alias> = head;
let _: &BitSlice<Msb0, <u16 as BitStore>::Alias> = tail;
let _: &BitSlice<Msb0, u16> = body;
sourcepub fn bit_domain_mut(&mut self) -> BitDomainMut<'_, O, T>
pub fn bit_domain_mut(&mut self) -> BitDomainMut<'_, O, T>
Splits the slice into subslices at alias boundaries.
This splits self
into the memory locations that it partially fills and
the memory locations that it completely fills. The locations that are
completely filled may be accessed without any bitvec
-imposed alias
conditions, while the locations that are only partially filled are left
unchanged.
You can read more about the BitDomainMut
splitting in its
documentation.
Examples
use bitvec::prelude::*;
let mut data = [0u16; 3];
let all = data.view_bits_mut::<Msb0>();
let (_, rest) = all.split_at_mut(8);
let bits: &mut BitSlice<Msb0, <u16 as BitStore>::Alias>
= &mut rest[.. 32];
let (head, body, tail) = bits
.bit_domain_mut()
.region()
.unwrap();
assert_eq!(head.len(), 8);
assert_eq!(tail.len(), 8);
let _: &mut BitSlice<Msb0, <u16 as BitStore>::Alias> = head;
let _: &mut BitSlice<Msb0, <u16 as BitStore>::Alias> = tail;
let _: &mut BitSlice<Msb0, u16> = body;
sourcepub fn domain(&self) -> Domain<'_, T>ⓘNotable traits for Domain<'a, T>impl<'a, T> Iterator for Domain<'a, T> where
T: BitStore, type Item = T::Mem;
pub fn domain(&self) -> Domain<'_, T>ⓘNotable traits for Domain<'a, T>impl<'a, T> Iterator for Domain<'a, T> where
T: BitStore, type Item = T::Mem;
T: BitStore, type Item = T::Mem;
Views the underlying memory containing the slice, split at alias boundaries.
This splits self
into the memory locations that it partially fills and
the memory locatinos that it completely fills. The locations that are
completely filled may be accessed without any bitvec
-imposed alias
conditions, while the locations that are only partially filled are left
unchanged.
You can read more about the Domain
splitting in its documentation.
Examples
use bitvec::prelude::*;
let mut data = [0u16; 3];
let all = data.view_bits_mut::<Msb0>();
let (_, rest) = all.split_at_mut(8);
let bits: &BitSlice<Msb0, <u16 as BitStore>::Alias> = &rest[.. 32];
let (head, body, tail) = bits
.domain()
.region()
.unwrap();
assert_eq!(body.len(), 1);
let _: &<u16 as BitStore>::Alias = head.unwrap().1;
let _: &<u16 as BitStore>::Alias = tail.unwrap().0;
let _: &[u16] = body;
sourcepub fn domain_mut(&mut self) -> DomainMut<'_, T>
pub fn domain_mut(&mut self) -> DomainMut<'_, T>
Views the underlying memory containing the slice, split at alias boundaries.
This splits self
into the memory locations that it partially fills and
the memory locations that it completely fills. The locations that are
completely filled may be accessed without any bitvec
-imposed alias
conditions, while the locations that are only partially filled are left
unchanged.
You can read more about the DomainMut
splitting in its
documentation.
Examples
use bitvec::prelude::*;
let mut data = [0u16; 3];
let all = data.view_bits_mut::<Msb0>();
let (_, rest) = all.split_at_mut(8);
let bits: &mut BitSlice<Msb0, <u16 as BitStore>::Alias> = &mut rest[.. 32];
let (head, body, tail) = bits
.domain_mut()
.region()
.unwrap();
assert_eq!(body.len(), 1);
let _: &<<u16 as BitStore>::Alias as BitStore>::Access = head.unwrap().1;
let _: &<<u16 as BitStore>::Alias as BitStore>::Access = tail.unwrap().0;
let _: &mut [u16] = body;
sourcepub fn as_slice(&self) -> &[T]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn as_slice(&self) -> &[T]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Views the underlying memory containing the slice.
The returned slice handle views all elements touched by self
, and
marks them all with self
’s current aliasing state. For a more precise
view, or one that permits mutation, use .domain()
or
.domain_mut()
.
sourcepub fn split_at_aliased_mut(&mut self, mid: usize) -> (&mut Self, &mut Self)
pub fn split_at_aliased_mut(&mut self, mid: usize) -> (&mut Self, &mut Self)
Splits a mutable slice at some mid-point.
This method has the same behavior as .split_at_mut()
, except that it
does not apply an aliasing marker to the partitioned subslices.
Safety
Because this method is defined only on BitSlice
s whose T
type is
alias-safe, the subslices do not need to be additionally marked.
Trait Implementations
sourceimpl<O, T> AsMut<BitSlice<O, T>> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> AsMut<BitSlice<O, T>> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
sourcefn as_mut(&mut self) -> &mut BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
fn as_mut(&mut self) -> &mut BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
Performs the conversion.
sourceimpl<O, T> AsRef<BitSlice<O, T>> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> AsRef<BitSlice<O, T>> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
sourcefn as_ref(&self) -> &BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
fn as_ref(&self) -> &BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
Performs the conversion.
sourceimpl<O, T, Rhs> BitAnd<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitAndAssign<Rhs>,
impl<O, T, Rhs> BitAnd<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitAndAssign<Rhs>,
sourceimpl<O, T, Rhs> BitAndAssign<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitAndAssign<Rhs>,
impl<O, T, Rhs> BitAndAssign<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitAndAssign<Rhs>,
sourcefn bitand_assign(&mut self, rhs: Rhs)
fn bitand_assign(&mut self, rhs: Rhs)
Performs the &=
operation. Read more
sourceimpl<O, T> BitField for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
impl<O, T> BitField for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
sourcefn load_le<M>(&self) -> M where
M: BitMemory,
fn load_le<M>(&self) -> M where
M: BitMemory,
Loads from self
, using little-endian element T
ordering. Read more
sourcefn load_be<M>(&self) -> M where
M: BitMemory,
fn load_be<M>(&self) -> M where
M: BitMemory,
Loads from self
, using big-endian element T
ordering. Read more
sourcefn store_le<M>(&mut self, value: M) where
M: BitMemory,
fn store_le<M>(&mut self, value: M) where
M: BitMemory,
Stores into self
, using little-endian element ordering. Read more
sourcefn store_be<M>(&mut self, value: M) where
M: BitMemory,
fn store_be<M>(&mut self, value: M) where
M: BitMemory,
Stores into self
, using big-endian element ordering. Read more
sourceimpl<O, T, Rhs> BitOr<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitOrAssign<Rhs>,
impl<O, T, Rhs> BitOr<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitOrAssign<Rhs>,
sourceimpl<O, T, Rhs> BitOrAssign<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitOrAssign<Rhs>,
impl<O, T, Rhs> BitOrAssign<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitOrAssign<Rhs>,
sourcefn bitor_assign(&mut self, rhs: Rhs)
fn bitor_assign(&mut self, rhs: Rhs)
Performs the |=
operation. Read more
sourceimpl<O, T, Rhs> BitXor<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitXorAssign<Rhs>,
impl<O, T, Rhs> BitXor<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitXorAssign<Rhs>,
sourceimpl<O, T, Rhs> BitXorAssign<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitXorAssign<Rhs>,
impl<O, T, Rhs> BitXorAssign<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitXorAssign<Rhs>,
sourcefn bitxor_assign(&mut self, rhs: Rhs)
fn bitxor_assign(&mut self, rhs: Rhs)
Performs the ^=
operation. Read more
sourceimpl<O, T> Borrow<BitSlice<O, T>> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> Borrow<BitSlice<O, T>> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
sourcefn borrow(&self) -> &BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
fn borrow(&self) -> &BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
Immutably borrows from an owned value. Read more
sourceimpl<O, T> BorrowMut<BitSlice<O, T>> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> BorrowMut<BitSlice<O, T>> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
sourcefn borrow_mut(&mut self) -> &mut BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
fn borrow_mut(&mut self) -> &mut BitSlice<O, T>ⓘNotable traits for &'a BitSlice<O, T>impl<'a, O, T> Read for &'a BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField, impl<'a, O, T> Write for &'a mut BitSlice<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: BitField,
Mutably borrows from an owned value. Read more
sourceimpl<O, T, Idx> Index<Idx> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: Index<Idx>,
impl<O, T, Idx> Index<Idx> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: Index<Idx>,
sourceimpl<O, T, Idx> IndexMut<Idx> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: IndexMut<Idx>,
impl<O, T, Idx> IndexMut<Idx> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
BitSlice<O, T>: IndexMut<Idx>,
sourceimpl<O, T> IntoIterator for BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> IntoIterator for BitBox<O, T> where
O: BitOrder,
T: BitStore,
This is not present on Box<[T]>
, but is needed to fit into the general
operator implementations.
sourceimpl<O, T> Ord for BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> Ord for BitBox<O, T> where
O: BitOrder,
T: BitStore,
sourceimpl<O1, O2, T1, T2> PartialEq<BitBox<O2, T2>> for BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
impl<O1, O2, T1, T2> PartialEq<BitBox<O2, T2>> for BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
sourceimpl<O1, O2, T1, T2> PartialEq<BitBox<O2, T2>> for &BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
impl<O1, O2, T1, T2> PartialEq<BitBox<O2, T2>> for &BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
sourceimpl<O1, O2, T1, T2> PartialEq<BitBox<O2, T2>> for &mut BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
impl<O1, O2, T1, T2> PartialEq<BitBox<O2, T2>> for &mut BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
sourceimpl<O, T, Rhs> PartialEq<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
Rhs: ?Sized + PartialEq<BitSlice<O, T>>,
impl<O, T, Rhs> PartialEq<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
Rhs: ?Sized + PartialEq<BitSlice<O, T>>,
sourceimpl<O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
impl<O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
sourcefn partial_cmp(&self, other: &BitBox<O2, T2>) -> Option<Ordering>
fn partial_cmp(&self, other: &BitBox<O2, T2>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<'a, O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for &'a BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
impl<'a, O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for &'a BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
sourcefn partial_cmp(&self, other: &BitBox<O2, T2>) -> Option<Ordering>
fn partial_cmp(&self, other: &BitBox<O2, T2>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<'a, O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for &'a mut BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
impl<'a, O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for &'a mut BitSlice<O1, T1> where
O1: BitOrder,
O2: BitOrder,
T1: BitStore,
T2: BitStore,
sourcefn partial_cmp(&self, other: &BitBox<O2, T2>) -> Option<Ordering>
fn partial_cmp(&self, other: &BitBox<O2, T2>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<O, T, Rhs> PartialOrd<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
Rhs: ?Sized + PartialOrd<BitSlice<O, T>>,
impl<O, T, Rhs> PartialOrd<Rhs> for BitBox<O, T> where
O: BitOrder,
T: BitStore,
Rhs: ?Sized + PartialOrd<BitSlice<O, T>>,
sourcefn partial_cmp(&self, other: &Rhs) -> Option<Ordering>
fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl<O, T> Eq for BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> Send for BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> Sync for BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> Unpin for BitBox<O, T> where
O: BitOrder,
T: BitStore,
Auto Trait Implementations
impl<O, T> RefUnwindSafe for BitBox<O, T> where
O: RefUnwindSafe,
T: RefUnwindSafe,
impl<O, T> UnwindSafe for BitBox<O, T> where
O: UnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> FmtForward for T
impl<T> FmtForward for T
sourcefn fmt_binary(self) -> FmtBinary<Self> where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self> where
Self: Binary,
Causes self
to use its Binary
implementation when Debug
-formatted.
sourcefn fmt_display(self) -> FmtDisplay<Self> where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self> where
Self: Display,
Causes self
to use its Display
implementation when
Debug
-formatted. Read more
sourcefn fmt_lower_exp(self) -> FmtLowerExp<Self> where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self> where
Self: LowerExp,
Causes self
to use its LowerExp
implementation when
Debug
-formatted. Read more
sourcefn fmt_lower_hex(self) -> FmtLowerHex<Self> where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self> where
Self: LowerHex,
Causes self
to use its LowerHex
implementation when
Debug
-formatted. Read more
sourcefn fmt_octal(self) -> FmtOctal<Self> where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self> where
Self: Octal,
Causes self
to use its Octal
implementation when Debug
-formatted.
sourcefn fmt_pointer(self) -> FmtPointer<Self> where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self> where
Self: Pointer,
Causes self
to use its Pointer
implementation when
Debug
-formatted. Read more
sourcefn fmt_upper_exp(self) -> FmtUpperExp<Self> where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self> where
Self: UpperExp,
Causes self
to use its UpperExp
implementation when
Debug
-formatted. Read more
sourcefn fmt_upper_hex(self) -> FmtUpperHex<Self> where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self> where
Self: UpperHex,
Causes self
to use its UpperHex
implementation when
Debug
-formatted. Read more
sourceimpl<T> Pipe for T where
T: ?Sized,
impl<T> Pipe for T where
T: ?Sized,
sourcefn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
Pipes by value. This is generally the method you want to use. Read more
sourcefn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R where
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R where
R: 'a,
Borrows self
and passes that borrow into the pipe function. Read more
sourcefn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R where
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R where
R: 'a,
Mutably borrows self
and passes that borrow into the pipe function. Read more
sourcefn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R where
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R where
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
Borrows self
, then passes self.borrow()
into the pipe function. Read more
sourcefn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> R where
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> R where
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
Mutably borrows self
, then passes self.borrow_mut()
into the pipe
function. Read more
sourcefn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R where
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R where
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
Borrows self
, then passes self.as_ref()
into the pipe function.
sourcefn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R where
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R where
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
Mutably borrows self
, then passes self.as_mut()
into the pipe
function. Read more
sourceimpl<T> PipeAsRef for T
impl<T> PipeAsRef for T
sourceimpl<T> PipeBorrow for T
impl<T> PipeBorrow for T
sourceimpl<T> PipeDeref for T
impl<T> PipeDeref for T
sourceimpl<T> PipeRef for T
impl<T> PipeRef for T
sourceimpl<T> Tap for T
impl<T> Tap for T
sourcefn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self where
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self where
Self: Borrow<B>,
B: ?Sized,
Immutable access to the Borrow<B>
of a value. Read more
sourcefn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self where
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self where
Self: BorrowMut<B>,
B: ?Sized,
Mutable access to the BorrowMut<B>
of a value. Read more
sourcefn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self where
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self where
Self: AsRef<R>,
R: ?Sized,
Immutable access to the AsRef<R>
view of a value. Read more
sourcefn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self where
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self where
Self: AsMut<R>,
R: ?Sized,
Mutable access to the AsMut<R>
view of a value. Read more
sourcefn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self where
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self where
Self: Deref<Target = T>,
T: ?Sized,
Immutable access to the Deref::Target
of a value. Read more
sourcefn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self where
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self where
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Mutable access to the Deref::Target
of a value. Read more
sourcefn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
Calls .tap()
only in debug builds, and is erased in release builds.
sourcefn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
Calls .tap_mut()
only in debug builds, and is erased in release
builds. Read more
sourcefn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self where
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self where
Self: Borrow<B>,
B: ?Sized,
Calls .tap_borrow()
only in debug builds, and is erased in release
builds. Read more
sourcefn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self where
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self where
Self: BorrowMut<B>,
B: ?Sized,
Calls .tap_borrow_mut()
only in debug builds, and is erased in release
builds. Read more
sourcefn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self where
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self where
Self: AsRef<R>,
R: ?Sized,
Calls .tap_ref()
only in debug builds, and is erased in release
builds. Read more
sourcefn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self where
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self where
Self: AsMut<R>,
R: ?Sized,
Calls .tap_ref_mut()
only in debug builds, and is erased in release
builds. Read more
sourceimpl<T> Tap for T
impl<T> Tap for T
sourcefn tap<F, R>(self, func: F) -> Self where
F: FnOnce(&Self) -> R,
fn tap<F, R>(self, func: F) -> Self where
F: FnOnce(&Self) -> R,
Provides immutable access for inspection. Read more
sourcefn tap_dbg<F, R>(self, func: F) -> Self where
F: FnOnce(&Self) -> R,
fn tap_dbg<F, R>(self, func: F) -> Self where
F: FnOnce(&Self) -> R,
Calls tap
in debug builds, and does nothing in release builds.
sourcefn tap_mut<F, R>(self, func: F) -> Self where
F: FnOnce(&mut Self) -> R,
fn tap_mut<F, R>(self, func: F) -> Self where
F: FnOnce(&mut Self) -> R,
Provides mutable access for modification. Read more
sourcefn tap_mut_dbg<F, R>(self, func: F) -> Self where
F: FnOnce(&mut Self) -> R,
fn tap_mut_dbg<F, R>(self, func: F) -> Self where
F: FnOnce(&mut Self) -> R,
Calls tap_mut
in debug builds, and does nothing in release builds.
sourceimpl<T, U> TapAsRef<U> for T where
U: ?Sized,
impl<T, U> TapAsRef<U> for T where
U: ?Sized,
sourcefn tap_ref<F, R>(self, func: F) -> Self where
Self: AsRef<T>,
F: FnOnce(&T) -> R,
fn tap_ref<F, R>(self, func: F) -> Self where
Self: AsRef<T>,
F: FnOnce(&T) -> R,
Provides immutable access to the reference for inspection.
sourcefn tap_ref_dbg<F, R>(self, func: F) -> Self where
Self: AsRef<T>,
F: FnOnce(&T) -> R,
fn tap_ref_dbg<F, R>(self, func: F) -> Self where
Self: AsRef<T>,
F: FnOnce(&T) -> R,
Calls tap_ref
in debug builds, and does nothing in release builds.
sourcefn tap_ref_mut<F, R>(self, func: F) -> Self where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
fn tap_ref_mut<F, R>(self, func: F) -> Self where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
Provides mutable access to the reference for modification.
sourcefn tap_ref_mut_dbg<F, R>(self, func: F) -> Self where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
fn tap_ref_mut_dbg<F, R>(self, func: F) -> Self where
Self: AsMut<T>,
F: FnOnce(&mut T) -> R,
Calls tap_ref_mut
in debug builds, and does nothing in release builds.
sourceimpl<T, U> TapBorrow<U> for T where
U: ?Sized,
impl<T, U> TapBorrow<U> for T where
U: ?Sized,
sourcefn tap_borrow<F, R>(self, func: F) -> Self where
Self: Borrow<T>,
F: FnOnce(&T) -> R,
fn tap_borrow<F, R>(self, func: F) -> Self where
Self: Borrow<T>,
F: FnOnce(&T) -> R,
Provides immutable access to the borrow for inspection. Read more
sourcefn tap_borrow_dbg<F, R>(self, func: F) -> Self where
Self: Borrow<T>,
F: FnOnce(&T) -> R,
fn tap_borrow_dbg<F, R>(self, func: F) -> Self where
Self: Borrow<T>,
F: FnOnce(&T) -> R,
Calls tap_borrow
in debug builds, and does nothing in release builds.
sourcefn tap_borrow_mut<F, R>(self, func: F) -> Self where
Self: BorrowMut<T>,
F: FnOnce(&mut T) -> R,
fn tap_borrow_mut<F, R>(self, func: F) -> Self where
Self: BorrowMut<T>,
F: FnOnce(&mut T) -> R,
Provides mutable access to the borrow for modification.
sourceimpl<T> TapDeref for T
impl<T> TapDeref for T
sourcefn tap_deref<F, R>(self, func: F) -> Self where
Self: Deref,
F: FnOnce(&Self::Target) -> R,
fn tap_deref<F, R>(self, func: F) -> Self where
Self: Deref,
F: FnOnce(&Self::Target) -> R,
Immutably dereferences self
for inspection.
sourcefn tap_deref_dbg<F, R>(self, func: F) -> Self where
Self: Deref,
F: FnOnce(&Self::Target) -> R,
fn tap_deref_dbg<F, R>(self, func: F) -> Self where
Self: Deref,
F: FnOnce(&Self::Target) -> R,
Calls tap_deref
in debug builds, and does nothing in release builds.
sourcefn tap_deref_mut<F, R>(self, func: F) -> Self where
Self: DerefMut,
F: FnOnce(&mut Self::Target) -> R,
fn tap_deref_mut<F, R>(self, func: F) -> Self where
Self: DerefMut,
F: FnOnce(&mut Self::Target) -> R,
Mutably dereferences self
for modification.
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more