[−][src]Struct bitvec::slice::BitSlice
A compact slice of bits, whose cursor and storage types can be customized.
BitSlice
is a specialized slice type, which can only ever be held by
reference. The value patterns of its reference handles are opaque binary
structures, which cannot be meaningfully inspected by user code.
BitSlice
can only be dynamically allocated by this library. Creation of any
other BitSlice
collections will result in catastrophically incorrect behavior.
A BitSlice
reference can be created through the bitvec!
macro, from a
BitVec
collection, or from any slice of elements by using the appropriate
From
implementation.
BitSlice
s are a view into a block of memory at bit-level resolution. They are
represented by a crate-internal pointer structure that cannot be used with
other Rust code except through the provided conversion APIs.
use bitvec::prelude::*; let store: &[u8] = &[0x69]; // slicing a bitvec let bslice: &BitSlice = store.into(); // coercing an array to a bitslice let bslice: &BitSlice = (&[1u8, 254u8][..]).into();
Bit slices are either mutable or shared. The shared slice type is
&BitSlice<C, T>
, while the mutable slice type is &mut BitSlice<C, T>
. For
example, you can mutate bits in the memory to which a mutable BitSlice
points:
use bitvec::prelude::*; let mut base = [0u8, 0, 0, 0]; { let bs: &mut BitSlice = (&mut base[..]).into(); bs.set(13, true); eprintln!("{:?}", bs.as_ref()); assert!(bs[13]); } assert_eq!(base[1], 4);
Type Parameters
C: Cursor
: An implementor of theCursor
trait. This type is used to convert semantic indices into concrete bit positions in elements, and store or retrieve bit values from the storage type.T: Bits
: An implementor of theBits
trait:u8
,u16
,u32
,u64
. This is the actual type in memory the slice will use to store data.
Safety
The &BitSlice
reference handle has the same size as standard Rust slice
handles, but it is extremely binary incompatible with them. Attempting to
treat &BitSlice<_, T>
as &[T]
in any manner except through the provided APIs
is catastrophically unsafe and unsound.
Methods
impl<C, T> BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
pub fn empty<'a>() -> &'a Self
[src]
Produces the empty slice. This is equivalent to &[]
for Rust slices.
Returns
An empty &BitSlice
handle.
Examples
use bitvec::prelude::*; let bv: &BitSlice = BitSlice::empty();
pub fn empty_mut<'a>() -> &'a mut Self
[src]
Produces the empty mutable slice. This is equivalent to &mut []
for
Rust slices.
Returns
An empty &mut BitSlice
handle.
Examples
use bitvec::prelude::*; let bv: &mut BitSlice = BitSlice::empty_mut();
pub fn from_element(elt: &T) -> &Self
[src]
Produces an immutable BitSlice
over a single element.
Parameters
elt
: A reference to an element over which theBitSlice
will be created.
Returns
A BitSlice
over the provided element.
Examples
use bitvec::prelude::*; let elt: u8 = !0; let bs: &BitSlice = BitSlice::from_element(&elt); assert!(bs.all());
pub fn from_element_mut(elt: &mut T) -> &mut Self
[src]
Produces a mutable BitSlice
over a single element.
Parameters
elt
: A reference to an element over which theBitSlice
will be created.
Returns
A BitSlice
over the provided element.
Examples
use bitvec::prelude::*; let mut elt: u8 = !0; let bs: &mut BitSlice = BitSlice::from_element_mut(&mut elt); bs.set(0, false); assert!(!bs.all());
pub fn from_slice(slice: &[T]) -> &Self
[src]
Wraps a &[T: Bits]
in a &BitSlice<C: Cursor, T>
. The endianness must
be specified at the call site. The element type cannot be changed.
Parameters
src
: The elements over which the newBitSlice
will operate.
Returns
A BitSlice
representing the original element slice.
Panics
The source slice must not exceed the maximum number of elements that a
BitSlice
can contain. This value is documented in BitPtr
.
Examples
use bitvec::prelude::*; let src: &[u8] = &[1, 2, 3]; let bits: &BitSlice = src.into(); assert_eq!(bits.len(), 24); assert_eq!(bits.as_ref().len(), 3); assert!(bits[7]); // src[0] == 0b0000_0001 assert!(bits[14]); // src[1] == 0b0000_0010 assert!(bits[22]); // src[2] == 0b0000_0011 assert!(bits[23]);
pub fn from_slice_mut(slice: &mut [T]) -> &mut Self
[src]
Wraps a &mut [T: Bits]
in a &mut BitSlice<C: Cursor, T>
. The
endianness must be specified by the call site. The element type cannot
be changed.
Parameters
src
: The elements over which the newBitSlice
will operate.
Returns
A BitSlice
representing the original element slice.
Panics
The source slice must not exceed the maximum number of elements that a
BitSlice
can contain. This value is documented in BitPtr
.
Examples
use bitvec::prelude::*; let src: &mut [u8] = &mut [1, 2, 3]; let bits: &mut BitSlice<LittleEndian, _> = src.into(); // The first bit is the LSb of the first element. assert!(bits[0]); bits.set(0, false); assert!(!bits[0]); assert_eq!(bits.as_ref(), &[0, 2, 3]);
pub fn len(&self) -> usize
[src]
Returns the number of bits contained in the BitSlice
.
Parameters
&self
Returns
The number of live bits in the slice domain.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0]; let bv: &BitSlice = store.into(); assert_eq!(bv.len(), 8);
pub fn is_empty(&self) -> bool
[src]
Tests if the slice is empty.
Parameters
&self
Returns
Whether the slice has no live bits.
Examples
use bitvec::prelude::*; let bv: &BitSlice = BitSlice::empty(); assert!(bv.is_empty()); let bv: &BitSlice = (&[0u8] as &[u8]).into();; assert!(!bv.is_empty());
pub fn first(&self) -> Option<bool>
[src]
Gets the first element of the slice, if present.
Parameters
&self
Returns
None
if the slice is empty, or Some(bit)
if it is not.
Examples
use bitvec::prelude::*; assert!(BitSlice::<BigEndian, u8>::empty().first().is_none()); let bv: &BitSlice = (&[128u8] as &[u8]).into(); assert!(bv.first().unwrap());
pub fn split_first(&self) -> Option<(bool, &Self)>
[src]
Returns the first and all the rest of the bits of the slice, or None
if it is empty.
Parameters
&self
Returns
If the slice is empty, this returns None
, otherwise, it returns Some
of:
- the first bit
- a
&BitSlice
of all the rest of the bits (this may be empty)
Examples
use bitvec::prelude::*; assert!(BitSlice::<BigEndian, u8>::empty().split_first().is_none()); let store: &[u8] = &[128]; let bv: &BitSlice = store.into(); let (h, t) = bv.split_first().unwrap(); assert!(h); assert!(t.not_any()); let bv = &bv[0 .. 1]; let (h, t) = bv.split_first().unwrap(); assert!(h); assert!(t.is_empty());
pub fn split_first_mut(&mut self) -> Option<(bool, &mut Self)>
[src]
Returns the first and all the rest of the bits of the slice, or None
if it is empty.
Parameters
&self
Returns
If the slice is empty, this returns None
, otherwise, it returns Some
of:
- the first bit
- a
&mut BitSlice
of all the rest of the bits (this may be empty)
pub fn split_last(&self) -> Option<(bool, &Self)>
[src]
Returns the last and all the rest of the bits in the slice, or None
if it is empty.
Parameters
&self
Returns
If the slice is empty, this returns None
, otherwise, it returns Some
of:
- the last bit
- a
&BitSlice
of all the rest of the bits (this may be empty)
Examples
use bitvec::prelude::*; assert!(BitSlice::<BigEndian, u8>::empty().split_last().is_none()); let bv: &BitSlice = (&[1u8] as &[u8]).into(); let (t, h) = bv.split_last().unwrap(); assert!(t); assert!(h.not_any()); let bv = &bv[7 .. 8]; let (t, h) = bv.split_last().unwrap(); assert!(t); assert!(h.is_empty());
pub fn split_last_mut(&mut self) -> Option<(bool, &mut Self)>
[src]
Returns the last and all the rest of the bits in the slice, or None
if it is empty.
Parameters
&self
Returns
If the slice is empty, this returns None
, otherwise, it returns Some
of:
- the last bit
- a
&BitSlice
of all the rest of the bits (this may be empty)
pub fn last(&self) -> Option<bool>
[src]
Gets the last element of the slice, or None
if it is empty.
Parameters
&self
Returns
None
if the slice is empty, or Some(bit)
if it is not.
Examples
use bitvec::prelude::*; assert!(BitSlice::<BigEndian, u8>::empty().last().is_none()); let bv: &BitSlice = (&[1u8] as &[u8]).into(); assert!(bv.last().unwrap());
pub fn get(&self, index: usize) -> Option<bool>
[src]
Gets the bit value at the given position.
Parameters
&self
index
: The bit index to retrieve.
Returns
The bit at the specified index, if any.
Examples
use bitvec::prelude::*; let bv: &BitSlice = (&[8u8] as &[u8]).into(); assert!(bv.get(4).unwrap()); assert!(!bv.get(3).unwrap()); assert!(bv.get(10).is_none());
pub fn set(&mut self, index: usize, value: bool)
[src]
Sets the bit value at the given position.
Parameters
&mut self
index
: The bit index to set. It must be in the domain0 .. self.len()
.value
: The value to be set,true
for1
andfalse
for0
.
Panics
This method panics if index
is outside the slice domain.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut [8u8]; let bv: &mut BitSlice = store.into(); assert!(!bv[3]); bv.set(3, true); assert!(bv[3]);
pub fn as_ptr(&self) -> *const T
[src]
Retrieves a read pointer to the start of the underlying data slice.
Parameters
&self
Returns
A pointer to the first element, partial or not, in the underlying store.
Safety
The caller must ensure that the slice outlives the pointer this function returns, or else it will dangle and point to garbage.
Modifying the container referenced by this slice may cause its buffer to reallocate, which would also make any pointers to it invalid.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0; 4]; let bv: &BitSlice = store.into(); assert_eq!(store.as_ptr(), bv.as_ptr());
pub fn as_mut_ptr(&mut self) -> *mut T
[src]
Retrieves a write pointer to the start of the underlying data slice.
Parameters
&mut self
Returns
A pointer to the first element, partial or not, in the underlying store.
Safety
The caller must ensure that the slice outlives the pointer this function returns, or else it will dangle and point to garbage.
Modifying the container referenced by this slice may cause its buffer to reallocate, which would also make any pointers to it invalid.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut[0; 4]; let store_ptr = store.as_mut_ptr(); let bv: &mut BitSlice = store.into(); assert_eq!(store_ptr, bv.as_mut_ptr());
pub fn swap(&mut self, a: usize, b: usize)
[src]
Swaps two bits in the slice.
Parameters
&mut self
a
: The first index to be swapped.b
: The second index to be swapped.
Panics
Panics if either a
or b
are out of bounds.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut[32u8]; let bv: &mut BitSlice = store.into(); assert!(!bv[0]); assert!(bv[2]); bv.swap(0, 2); assert!(bv[0]); assert!(!bv[2]);
pub fn reverse(&mut self)
[src]
Reverses the order of bits in the slice, in place.
Parameters
&mut self
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut[0b1010_1010]; { let bv: &mut BitSlice = store.into(); bv[1 .. 7].reverse(); } eprintln!("{:b}", store[0]); assert_eq!(store[0], 0b1101_0100);
ⓘImportant traits for Iter<'a, C, T>pub fn iter(&self) -> Iter<C, T>
[src]
Provides read-only iteration across the slice domain.
The iterator returned from this method implements ExactSizeIterator
and DoubleEndedIterator
just as the consuming .into_iter()
method’s
iterator does.
Parameters
&self
Returns
An iterator over all bits in the slice domain, in C
and T
ordering.
Examples
use bitvec::prelude::*; let store: &[u8] = &[64]; let bv: &BitSlice = store.into(); let mut iter = bv[.. 2].iter(); assert!(!iter.next().unwrap()); assert!(iter.next().unwrap()); assert!(iter.next().is_none());
ⓘImportant traits for Windows<'a, C, T>pub fn windows(&self, size: usize) -> Windows<C, T>
[src]
Produces a sliding iterator over consecutive windows in the slice. Each
windows has the width size
. The windows overlap. If the slice is
shorter than size
, the produced iterator is empty.
Parameters
&self
size
: The width of each window.
Returns
An iterator which yields sliding views into the slice.
Panics
This function panics if the size
is zero.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0b0100_1011]; let bv: &BitSlice = store.into(); let mut windows = bv.windows(4); assert_eq!(windows.next(), Some(&bv[0 .. 4])); assert_eq!(windows.next(), Some(&bv[1 .. 5])); assert_eq!(windows.next(), Some(&bv[2 .. 6])); assert_eq!(windows.next(), Some(&bv[3 .. 7])); assert_eq!(windows.next(), Some(&bv[4 .. 8])); assert!(windows.next().is_none());
ⓘImportant traits for Chunks<'a, C, T>pub fn chunks(&self, size: usize) -> Chunks<C, T>
[src]
Produces a galloping iterator over consecutive chunks in the slice. Each
chunk, except possibly the last, has the width size
. The chunks do not
overlap. If the slice is shorter than size
, the produced iterator
produces only one chunk.
Parameters
&self
size
: The width of each chunk.
Returns
An iterator which yields consecutive chunks of the slice.
Panics
This function panics if the size
is zero.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0b0100_1011]; let bv: &BitSlice = store.into(); let mut chunks = bv.chunks(3); assert_eq!(chunks.next(), Some(&bv[0 .. 3])); assert_eq!(chunks.next(), Some(&bv[3 .. 6])); assert_eq!(chunks.next(), Some(&bv[6 .. 8])); assert!(chunks.next().is_none());
ⓘImportant traits for ChunksMut<'a, C, T>pub fn chunks_mut(&mut self, size: usize) -> ChunksMut<C, T>
[src]
Produces a galloping iterator over consecutive chunks in the slice. Each
chunk, except possibly the last, has the width size
. The chunks do not
overlap. If the slice is shorter than size
, the produced iterator
produces only one chunk.
Parameters
&mut self
size
: The width of each chunk.
Returns
An iterator which yields consecutive mutable chunks of the slice.
Panics
This function panics if the size
is zero.
ⓘImportant traits for ChunksExact<'a, C, T>pub fn chunks_exact(&self, size: usize) -> ChunksExact<C, T>
[src]
Produces a galloping iterator over consecutive chunks in the slice. Each
chunk has the width size
. If size
does not evenly divide the slice,
then the remainder is not part of the iteration, and can be accessed
separately with the .remainder()
method.
Parameters
&self
size
: The width of each chunk.
Returns
An iterator which yields consecutive chunks of the slice.
Panics
This function panics if size
is zero.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0b0100_1011]; let bv: &BitSlice = store.into(); let mut chunks_exact = bv.chunks_exact(3); assert_eq!(chunks_exact.next(), Some(&bv[0 .. 3])); assert_eq!(chunks_exact.next(), Some(&bv[3 .. 6])); assert!(chunks_exact.next().is_none()); assert_eq!(chunks_exact.remainder(), &bv[6 .. 8]);
ⓘImportant traits for ChunksExactMut<'a, C, T>pub fn chunks_exact_mut(&mut self, size: usize) -> ChunksExactMut<C, T>
[src]
Produces a galloping iterator over consecutive chunks in the slice. Each
chunk has the width size
. If size
does not evenly divide the slice,
then the remainder is not part of the iteration, and can be accessed
separately with the .remainder()
method.
Parameters
&mut self
size
: The width of each chunk.
Returns
An iterator which yields consecutive mutable chunks of the slice.
Panics
This function panics if size
is zero.
ⓘImportant traits for RChunks<'a, C, T>pub fn rchunks(&self, size: usize) -> RChunks<C, T>
[src]
Produces a galloping iterator over consecutive chunks in the slice, from
the back to the front. Each chunk, except possibly the front, has the
width size
. The chunks do not overlap. If the slice is shorter than
size
, then the iterator produces one item.
Parameters
&self
size
: The width of each chunk.
Returns
An iterator which yields consecutive chunks of the slice, from the back to the front.
Panics
This function panics if size
is zero.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0b0100_1011]; let bv: &BitSlice = store.into(); let mut rchunks = bv.rchunks(3); assert_eq!(rchunks.next(), Some(&bv[5 .. 8])); assert_eq!(rchunks.next(), Some(&bv[2 .. 5])); assert_eq!(rchunks.next(), Some(&bv[0 .. 2])); assert!(rchunks.next().is_none());
ⓘImportant traits for RChunksMut<'a, C, T>pub fn rchunks_mut(&mut self, size: usize) -> RChunksMut<C, T>
[src]
Produces a galloping iterator over consecutive chunks in the slice, from
the back to the front. Each chunk, except possibly the front, has the
width size
. The chunks do not overlap. If the slice is shorter than
size
, then the iterator produces one item.
Parameters
&mut self
size
: The width of each chunk.
Returns
An iterator which yields consecutive mutable chunks of the slice, from the back to the front.
Panics
This function panics if size
is zero.
ⓘImportant traits for RChunksExact<'a, C, T>pub fn rchunks_exact(&self, size: usize) -> RChunksExact<C, T>
[src]
Produces a galloping iterator over consecutive chunks in the slice, from
the back to the front. Each chunk has the width size
. If size
does
not evenly divide the slice, then the remainder is not part of the
iteration, and can be accessed separately with the .remainder()
method.
Parameters
&self
size
: The width of each chunk.
Returns
An iterator which yields consecutive chunks of the slice, from the back to the front.
Panics
This function panics if size
is zero.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0b0100_1011]; let bv: &BitSlice = store.into(); let mut rchunks_exact = bv.rchunks_exact(3); assert_eq!(rchunks_exact.next(), Some(&bv[5 .. 8])); assert_eq!(rchunks_exact.next(), Some(&bv[2 .. 5])); assert!(rchunks_exact.next().is_none()); assert_eq!(rchunks_exact.remainder(), &bv[0 .. 2]);
ⓘImportant traits for RChunksExactMut<'a, C, T>pub fn rchunks_exact_mut(&mut self, size: usize) -> RChunksExactMut<C, T>
[src]
Produces a galloping iterator over consecutive chunks in the slice, from
the back to the front. Each chunk has the width size
. If size
does
not evenly divide the slice, then the remainder is not part of the
iteration, and can be accessed separately with the .remainder()
method.
Parameters
&mut self
size
: The width of each chunk.
Returns
An iterator which yields consecutive mutable chunks of the slice, from the back to the front.
Panics
This function panics if size
is zero.
pub fn split_at(&self, mid: usize) -> (&Self, &Self)
[src]
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).
Parameters
&self
mid
: The index at which to split
Returns
- The bits up to but not including
mid
. - The bits from mid onwards.
Panics
Panics if mid > self.len()
.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0x0F]; let bv: &BitSlice = store.into(); let (l, r) = bv.split_at(0); assert!(l.is_empty()); assert_eq!(r, bv); let (l, r) = bv.split_at(4); assert_eq!(l, &bv[0 .. 4]); assert_eq!(r, &bv[4 .. 8]); let (l, r) = bv.split_at(8); assert_eq!(l, bv); assert!(r.is_empty());
pub fn split_at_mut(&mut self, mid: usize) -> (&mut Self, &mut Self)
[src]
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).
Parameters
&mut self
mid
: The index at which to split
Returns
- The bits up to but not including
mid
. - The bits from mid onwards.
Panics
Panics if mid > self.len()
.
pub fn starts_with<D, U>(&self, prefix: &BitSlice<D, U>) -> bool where
D: Cursor,
U: Bits,
[src]
D: Cursor,
U: Bits,
Tests if the slice begins with the given prefix.
Parameters
&self
prefix
: AnyBitSlice
against whichself
is tested. This is not required to have the same cursor or storage types asself
.
Returns
Whether self
begins with prefix
. This is true only if self
is at
least as long as prefix
and their bits are semantically equal.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0xA6]; let bv: &BitSlice = store.into();; assert!(bv.starts_with(&bv[.. 3])); assert!(!bv.starts_with(&bv[3 ..]));
pub fn ends_with<D, U>(&self, suffix: &BitSlice<D, U>) -> bool where
D: Cursor,
U: Bits,
[src]
D: Cursor,
U: Bits,
Tests if the slice ends with the given suffix.
Parameters
&self
suffix
: AnyBitSlice
against whichself
is tested. This is not required to have the same cursor or storage types asself
.
Returns
Whether self
ends with suffix
. This is true only if self
is at
least as long as suffix
and their bits are semantically equal.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0xA6]; let bv: &BitSlice = store.into(); assert!(bv.ends_with(&bv[5 ..])); assert!(!bv.ends_with(&bv[.. 5]));
pub fn rotate_left(&mut self, by: usize)
[src]
Rotates the slice, in place, to the left.
After calling this method, the bits from [.. by]
will be at the back
of the slice, and the bits from [by ..]
will be at the front. This
operates fully in-place.
In-place rotation of bits requires this method to take O(k × n)
time.
It is impossible to use machine intrinsics to perform galloping rotation
on bits.
Parameters
&mut self
by
: The number of bits by which to rotate left. This must be in the range0 ..= self.len()
. If it is0
orself.len()
, then this method is a no-op.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut [0xF0]; let bv: &mut BitSlice = store.into(); bv.rotate_left(2); assert_eq!(bv.as_ref()[0], 0xC3);
pub fn rotate_right(&mut self, by: usize)
[src]
Rotates the slice, in place, to the right.
After calling this method, the bits from [self.len() - by ..]
will be
at the front of the slice, and the bits from [.. self.len() - by]
will
be at the back. This operates fully in-place.
In-place rotation of bits requires this method to take O(k × n)
time.
It is impossible to use machine intrinsics to perform galloping rotation
on bits.
Parameters
&mut self
by
: The number of bits by which to rotate right. This must be in the range0 ..= self.len()
. If it is0
orself.len
, then this method is a no-op.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut [0xF0]; let bv: &mut BitSlice = store.into(); bv.rotate_right(2); assert_eq!(bv.as_ref()[0], 0x3C);
pub fn all(&self) -> bool
[src]
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.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0xFD]; let bv: &BitSlice = store.into(); assert!(bv[.. 4].all()); assert!(!bv[4 ..].all());
pub fn any(&self) -> bool
[src]
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.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0x40]; let bv: &BitSlice = store.into(); assert!(bv[.. 4].any()); assert!(!bv[4 ..].any());
pub fn not_all(&self) -> bool
[src]
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 store: &[u8] = &[0xFD]; let bv: &BitSlice = store.into(); assert!(!bv[.. 4].not_all()); assert!(bv[4 ..].not_all());
pub fn not_any(&self) -> bool
[src]
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 store: &[u8] = &[0x40]; let bv: &BitSlice = store.into(); assert!(!bv[.. 4].not_any()); assert!(bv[4 ..].not_any());
pub fn some(&self) -> bool
[src]
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.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0b111_000_10]; let bv: &BitSlice = store.into(); assert!(!bv[0 .. 3].some()); assert!(!bv[3 .. 6].some()); assert!(bv[6 ..].some());
pub fn count_ones(&self) -> usize
[src]
Counts how many bits are set high.
Parameters
&self
Returns
The number of high bits in the slice domain.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0xFD, 0x25]; let bv: &BitSlice = store.into(); assert_eq!(bv.count_ones(), 10);
pub fn count_zeros(&self) -> usize
[src]
Counts how many bits are set low.
Parameters
&self
Returns
The number of low bits in the slice domain.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0xFD, 0x25]; let bv: &BitSlice = store.into(); assert_eq!(bv.count_zeros(), 6);
pub fn set_all(&mut self, value: bool)
[src]
Set 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 store: &mut [u8] = &mut [0]; let bv: &mut BitSlice = store.into(); bv[2 .. 6].set_all(true); assert_eq!(bv.as_ref(), &[0b0011_1100]); bv[3 .. 5].set_all(false); assert_eq!(bv.as_ref(), &[0b0010_0100]); bv[.. 1].set_all(true); assert_eq!(bv.as_ref(), &[0b1010_0100]);
pub fn for_each<F>(&mut self, func: F) where
F: Fn(usize, bool) -> bool,
[src]
F: Fn(usize, bool) -> bool,
Provides mutable traversal of the collection.
It is impossible to implement IndexMut
on BitSlice
, because bits do
not have addresses, so there can be no &mut u1
. This method allows the
client to receive an enumerated bit, and provide a new bit to set at
each index.
Parameters
&mut self
func
: A function which receives a(usize, bool)
pair of index and value, and returns a bool. It receives the bit at each position, and the return value is written back at that position.
Examples
use bitvec::prelude::*;
pub fn as_slice(&self) -> &[T]
[src]
Accesses the backing storage of the BitSlice
as a slice of its
elements.
This is different from .body()
in that it always includes the head
and tail elements, even if they are partial.
Parameters
&self
Returns
A slice of all the elements that the BitSlice
uses for storage.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0b0000_0001, 0b0100_0000]; let bs: &BitSlice = store.into(); let mut accum = 0; for elt in bs.as_slice() { accum += elt.count_ones(); } assert_eq!(accum, 2);
pub fn as_mut_slice(&mut self) -> &mut [T]
[src]
Accesses the underlying store.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut [0b0000_0001, 0b0100_0000]; let bs: &mut BitSlice = store.into(); for elt in bs.as_mut_slice() { *elt += 2; } assert_eq!(&[3, 66], bs.as_slice());
pub fn change_cursor<D>(&self) -> &BitSlice<D, T> where
D: Cursor,
[src]
D: Cursor,
Changes the cursor type of the slice handle.
Parameters
&self
Returns
An equivalent slice handle with a new cursor type.
Type Parameters
D: Cursor
The new cursor type to use for the handle.
pub fn change_cursor_mut<D>(&mut self) -> &mut BitSlice<D, T> where
D: Cursor,
[src]
D: Cursor,
Changes the cursor type of the slice handle.
Parameters
&mut self
Returns
An equivalent slice handle with a new cursor type.
Type Parameters
D: Cursor
The new cursor type to use for the handle.
pub fn bitptr(&self) -> BitPtr<T>
[src]
Accesses the underlying pointer structure.
Parameters
&self
Returns
The BitPtr
structure of the slice handle.
Trait Implementations
impl<C, T> AsRef<[T]> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
Provides read access to all elements in the underlying storage, including the partial head and tail elements if present.
fn as_ref(&self) -> &[T]
[src]
Accesses the underlying store.
Parameters
&self
Returns
An immutable slice of all storage elements.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0, 128]; let bv: &BitSlice = store.into(); let bv = &bv[1 .. 9]; assert_eq!(&[0, 128], bv.as_ref());
impl<C, T> AsRef<BitSlice<C, T>> for BitBox<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<C, T> AsRef<BitSlice<C, T>> for BitVec<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<C, T> Sync for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
BitSlice
is safe to share between multiple threads.
impl<C, T> Ord for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
fn cmp(&self, rhs: &Self) -> Ordering
[src]
fn max(self, other: Self) -> Self
1.21.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
Compares and returns the minimum of two values. Read more
fn clamp(self, min: Self, max: Self) -> Self
[src]
clamp
)Restrict a value to a certain interval. Read more
impl<C, T> Send for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
BitSlice
is safe to move across thread boundaries.
impl<'a, C, T> From<&'a T> for &'a BitSlice<C, T> where
C: Cursor,
T: 'a + Bits,
[src]
C: Cursor,
T: 'a + Bits,
impl<'a, C, T> From<&'a [T]> for &'a BitSlice<C, T> where
C: Cursor,
T: 'a + Bits,
[src]
C: Cursor,
T: 'a + Bits,
Builds a BitSlice
from a slice of elements. The resulting BitSlice
will
always completely fill the original slice.
impl<'a, C, T> From<&'a mut T> for &'a mut BitSlice<C, T> where
C: Cursor,
T: 'a + Bits,
[src]
C: Cursor,
T: 'a + Bits,
impl<'a, C, T> From<&'a mut [T]> for &'a mut BitSlice<C, T> where
C: Cursor,
T: 'a + Bits,
[src]
C: Cursor,
T: 'a + Bits,
Builds a mutable BitSlice
from a slice of mutable elements. The resulting
BitSlice
will always completely fill the original slice.
fn from(src: &'a mut [T]) -> Self
[src]
impl<C, T, '_> From<&'_ BitSlice<C, T>> for BitBox<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<C, T, '_> From<&'_ BitSlice<C, T>> for BitVec<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<C, T> Eq for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<A, B, C, D> PartialOrd<BitSlice<C, D>> for BitSlice<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
Compares two BitSlice
s by semantic — not bitwise — ordering.
The comparison sorts by testing each index for one slice to have a set bit where the other has an unset bit. If the slices are different, the slice with the set bit sorts greater than the slice with the unset bit.
If one of the slices is exhausted before they differ, the longer slice is greater.
fn partial_cmp(&self, rhs: &BitSlice<C, D>) -> Option<Ordering>
[src]
Performs a comparison by <
or >
.
Parameters
&self
rhs
: AnotherBitSlice
against which to compare. This slice can have different cursor or storage types.
Returns
The relative ordering of self
against rhs
. self
is greater if it
has a true
bit at an index where rhs
has a false
; self
is lesser
if it has a false
bit at an index where rhs
has a true
; if the two
slices do not disagree then they are compared by length.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0x45]; let slice: &BitSlice = store.into(); let a = &slice[0 .. 3]; // 010 let b = &slice[0 .. 4]; // 0100 let c = &slice[0 .. 5]; // 01000 let d = &slice[4 .. 8]; // 0101 assert!(a < b); assert!(b < c); assert!(c < d);
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<A, B, C, D> PartialOrd<BitVec<C, D>> for BitSlice<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
fn partial_cmp(&self, rhs: &BitVec<C, D>) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<A, B, C, D, '_> PartialOrd<BitVec<C, D>> for &'_ BitSlice<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
fn partial_cmp(&self, rhs: &BitVec<C, D>) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<A, B, C, D> PartialOrd<BitSlice<C, D>> for BitBox<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
fn partial_cmp(&self, rhs: &BitSlice<C, D>) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<A, B, C, D> PartialOrd<BitBox<C, D>> for BitSlice<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
fn partial_cmp(&self, rhs: &BitBox<C, D>) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<A, B, C, D> PartialOrd<BitSlice<C, D>> for BitVec<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
fn partial_cmp(&self, rhs: &BitSlice<C, D>) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<A, B, C, D, '_> PartialOrd<&'_ BitSlice<C, D>> for BitVec<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
fn partial_cmp(&self, rhs: &&BitSlice<C, D>) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<C, T> AsMut<[T]> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
Provides write access to all elements in the underlying storage, including the partial head and tail elements if present.
fn as_mut(&mut self) -> &mut [T]
[src]
Accesses the underlying store.
Parameters
&mut self
Returns
A mutable slice of all storage elements.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut [0, 128]; let bv: &mut BitSlice = store.into(); let bv = &mut bv[1 .. 9]; for elt in bv.as_mut() { *elt += 2; } assert_eq!(&[2, 130], bv.as_ref());
impl<C, T> AsMut<BitSlice<C, T>> for BitBox<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<C, T> AsMut<BitSlice<C, T>> for BitVec<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<C, T> ToOwned for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
Creates an owned BitVec<C, T>
from a borrowed BitSlice<C, T>
.
type Owned = BitVec<C, T>
The resulting type after obtaining ownership.
fn to_owned(&self) -> Self::Owned
[src]
Clones a borrowed BitSlice
into an owned BitVec
.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0; 2]; let src: &BitSlice = store.into(); let dst = src.to_owned(); assert_eq!(src, dst);
fn clone_into(&self, target: &mut Self::Owned)
[src]
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<A, B, C, D> PartialEq<BitSlice<C, D>> for BitSlice<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
Tests if two BitSlice
s are semantically — not bitwise — equal.
It is valid to compare two slices of different endianness or element types.
The equality condition requires that they have the same number of total bits and that each pair of bits in semantic order are identical.
fn eq(&self, rhs: &BitSlice<C, D>) -> bool
[src]
Performas a comparison by ==
.
Parameters
&self
rhs
: AnotherBitSlice
against which to compare. This slice can have different cursor or storage types.
Returns
If the two slices are equal, by comparing the lengths and bit values at each semantic index.
Examples
use bitvec::prelude::*; let lstore: &[u8] = &[8, 16, 32, 0]; let rstore: &[u32] = &[0x10080400]; let lbv: &BitSlice<LittleEndian, u8> = lstore.into(); let rbv: &BitSlice<BigEndian, u32> = rstore.into(); assert_eq!(lbv, rbv);
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<A, B, C, D> PartialEq<BitVec<C, D>> for BitSlice<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
Allow comparison against the allocated form.
fn eq(&self, rhs: &BitVec<C, D>) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<A, B, C, D, '_> PartialEq<BitVec<C, D>> for &'_ BitSlice<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
fn eq(&self, rhs: &BitVec<C, D>) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<A, B, C, D> PartialEq<BitSlice<C, D>> for BitBox<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
fn eq(&self, rhs: &BitSlice<C, D>) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<A, B, C, D> PartialEq<BitBox<C, D>> for BitSlice<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
fn eq(&self, rhs: &BitBox<C, D>) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<A, B, C, D> PartialEq<BitSlice<C, D>> for BitVec<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
fn eq(&self, rhs: &BitSlice<C, D>) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<A, B, C, D, '_> PartialEq<&'_ BitSlice<C, D>> for BitVec<A, B> where
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
[src]
A: Cursor,
B: Bits,
C: Cursor,
D: Bits,
fn eq(&self, rhs: &&BitSlice<C, D>) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<'a, C, T> IntoIterator for &'a BitSlice<C, T> where
C: Cursor,
T: 'a + Bits,
[src]
C: Cursor,
T: 'a + Bits,
Produces a read-only iterator over all the bits in the BitSlice
.
This iterator follows the ordering in the BitSlice
type, and implements
ExactSizeIterator
as BitSlice
has a known, fixed, length, and
DoubleEndedIterator
as it has known ends.
type Item = bool
The type of the elements being iterated over.
type IntoIter = Iter<'a, C, T>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
Iterates over the slice.
Parameters
self
Returns
An iterator over the slice domain.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0b1010_1100]; let bits: &BitSlice = store.into(); let mut count = 0; for bit in bits { if bit { count += 1; } } assert_eq!(count, 4);
impl<C, T> Debug for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
Prints the BitSlice
for debugging.
The output is of the form BitSlice<C, T> [ELT, *]
where <C, T>
is the
cursor and element type, with square brackets on each end of the bits and
all the elements of the array printed in binary. The printout is always in
semantic order, and may not reflect the underlying buffer. To see the
underlying buffer, use .as_ref()
.
The alternate character {:#?}
prints each element on its own line, rather
than having all elements on the same line.
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Renders the BitSlice
type header and contents for debug.
Examples
use bitvec::prelude::*; let bits: &BitSlice<LittleEndian, u16> = &bitvec![ LittleEndian, u16; 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1 ]; assert_eq!( "BitSlice<LittleEndian, u16> [0101000011110101, 01]", &format!("{:?}", bits) );
impl<C, T> Display for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
Prints the BitSlice
for displaying.
This prints each element in turn, formatted in binary in semantic order (so the first bit seen is printed first and the last bit seen is printed last). Each element of storage is separated by a space for ease of reading.
The alternate character {:#}
prints each element on its own line.
To see the in-memory representation, use .as_ref()
to get access to the
raw elements and print that slice instead.
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Renders the BitSlice
contents for display.
Parameters
&self
f
: The formatter into whichself
is written.
Returns
The result of the formatting operation.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0b01001011, 0b0100_0000]; let bits: &BitSlice = store.into(); assert_eq!("[01001011, 01]", &format!("{}", &bits[.. 10]));
impl<'a, C, T> Neg for &'a mut BitSlice<C, T> where
C: Cursor,
T: 'a + Bits,
[src]
C: Cursor,
T: 'a + Bits,
Performs fixed-width 2’s-complement negation of a BitSlice
.
Unlike the !
operator (Not
trait), the unary -
operator treats the
BitSlice
as if it represents a signed 2’s-complement integer of fixed
width. The negation of a number in 2’s complement is defined as its
inversion (using !
) plus one, and on fixed-width numbers has the following
discontinuities:
- A slice whose bits are all zero is considered to represent the number zero which negates as itself.
- A slice whose bits are all one is considered to represent the most negative number, which has no correpsonding positive number, and thus negates as zero.
This behavior was chosen so that all possible values would have some
output, and so that repeated application converges at idempotence. The most
negative input can never be reached by negation, but --MOST_NEG
converges
at the least unreasonable fallback value, 0.
Because BitSlice
cannot move, the negation is performed in place.
type Output = Self
The resulting type after applying the -
operator.
fn neg(self) -> Self::Output
[src]
Perform 2’s-complement fixed-width negation.
Negation is accomplished by inverting the bits and adding one. This has
one edge case: 1000…
, the most negative number for its width, will
negate to zero instead of itself. It thas no corresponding positive
number to which it can negate.
Parameters
self
Examples
The contortions shown here are a result of this operator applying to a
mutable reference, and this example balancing access to the original
BitVec
for comparison with aquiring a mutable borrow as a slice to
ensure that the BitSlice
implementation is used, not the BitVec
.
Negate an arbitrary positive number (first bit unset).
use bitvec::prelude::*; let store: &mut [u8] = &mut [0b0110_1010]; let bv: &mut BitSlice = store.into(); eprintln!("{:?}", bv.split_at(4)); let num = &mut bv[.. 4]; -num; eprintln!("{:?}", bv.split_at(4)); assert_eq!(&bv[.. 4], &bv[4 ..]);
Negate an arbitrary negative number. This example will use the above result to demonstrate round-trip correctness.
use bitvec::prelude::*; let store: &mut [u8] = &mut [0b1010_0110]; let bv: &mut BitSlice = store.into(); let num = &mut bv[.. 4]; -num; assert_eq!(&bv[.. 4], &bv[4 ..]);
Negate the most negative number, which will become zero, and show convergence at zero.
use bitvec::prelude::*; let store: &mut [u8] = &mut [128]; let bv: &mut BitSlice = store.into(); let num = &mut bv[..]; -num; assert!(bv.not_any()); let num = &mut bv[..]; -num; assert!(bv.not_any());
impl<C, T, I> AddAssign<I> for BitSlice<C, T> where
C: Cursor,
T: Bits,
I: IntoIterator<Item = bool>,
I::IntoIter: DoubleEndedIterator,
[src]
C: Cursor,
T: Bits,
I: IntoIterator<Item = bool>,
I::IntoIter: DoubleEndedIterator,
Performs unsigned addition in place on a BitSlice
.
If the addend bitstream is shorter than self
, the addend is zero-extended
at the left (so that its final bit matches with self
’s final bit). If the
addend is longer, the excess front length is unused.
Addition proceeds from the right ends of each slice towards the left. Because this trait is forbidden from returning anything, the final carry-out bit is discarded.
Note that, unlike BitVec
, there is no subtraction implementation until I
find a subtraction algorithm that does not require modifying the subtrahend.
Subtraction can be implemented by negating the intended subtrahend yourself
and then using addition, or by using BitVec
s instead of BitSlice
s.
Type Parameters
I: IntoIterator<Item=bool, IntoIter: DoubleEndedIterator>
: The bitstream to add intoself
. It must be finite and double-ended, since addition operates in reverse.
fn add_assign(&mut self, addend: I)
[src]
Performs unsigned wrapping addition in place.
Examples
This example shows addition of a slice wrapping from max to zero.
use bitvec::prelude::*; let store: &mut [u8] = &mut [0b1110_1111, 0b0000_0001]; let bv: &mut BitSlice = store.into(); let (nums, one) = bv.split_at_mut(12); let (accum, steps) = nums.split_at_mut(4); *accum += &*one; assert_eq!(accum, &steps[.. 4]); *accum += &*one; assert_eq!(accum, &steps[4 ..]);
impl<'a, C, T> Not for &'a mut BitSlice<C, T> where
C: Cursor,
T: 'a + Bits,
[src]
C: Cursor,
T: 'a + Bits,
Flips all bits in the slice, in place.
type Output = Self
The resulting type after applying the !
operator.
fn not(self) -> Self::Output
[src]
Inverts all bits in the slice.
This will not affect bits outside the slice in slice storage elements.
Parameters
self
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut [0; 2]; let bv: &mut BitSlice = store.into(); let bits = &mut bv[2 .. 14]; let new_bits = !bits; // The `bits` binding is consumed by the `!` operator, and a new reference // is returned. // assert_eq!(bits.as_ref(), &[!0, !0]); assert_eq!(new_bits.as_ref(), &[0x3F, 0xFC]);
impl<C, T, I> BitAndAssign<I> for BitSlice<C, T> where
C: Cursor,
T: Bits,
I: IntoIterator<Item = bool>,
[src]
C: Cursor,
T: Bits,
I: IntoIterator<Item = bool>,
Performs the Boolean AND
operation against another bitstream and writes
the result into self
. If the other bitstream ends before self,
, the
remaining bits of self
are cleared.
Type Parameters
I: IntoIterator<Item=bool>
: A stream of bits, which may be aBitSlice
or some other bit producer as desired.
fn bitand_assign(&mut self, rhs: I)
[src]
AND
s a bitstream into a slice.
Parameters
&mut self
rhs
: The bitstream toAND
intoself
.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut [0b0101_0100]; let other: & [u8] = & [0b0011_0000]; let lhs: &mut BitSlice = store.into(); let rhs: & BitSlice = other.into(); lhs[.. 6] &= &rhs[.. 4]; assert_eq!(store[0], 0b0001_0000);
impl<C, T, I> BitOrAssign<I> for BitSlice<C, T> where
C: Cursor,
T: Bits,
I: IntoIterator<Item = bool>,
[src]
C: Cursor,
T: Bits,
I: IntoIterator<Item = bool>,
Performs the Boolean OR
operation against another bitstream and writes the
result into self
. If the other bitstream ends before self
, the remaining
bits of self
are not affected.
Type Parameters
I: IntoIterator<Item=bool>
: A stream of bits, which may be aBitSlice
or some other bit producer as desired.
fn bitor_assign(&mut self, rhs: I)
[src]
OR
s a bitstream into a slice.
Parameters
&mut self
rhs
: The bitstream toOR
intoself
.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut [0b0101_0100]; let other: & [u8] = & [0b0011_0000]; let lhs: &mut BitSlice = store.into(); let rhs: & BitSlice = other.into(); lhs[.. 6] |= &rhs[.. 4]; assert_eq!(store[0], 0b0111_0100);
impl<C, T, I> BitXorAssign<I> for BitSlice<C, T> where
C: Cursor,
T: Bits,
I: IntoIterator<Item = bool>,
[src]
C: Cursor,
T: Bits,
I: IntoIterator<Item = bool>,
Performs the Boolean XOR
operation against another bitstream and writes
the result into self
. If the other bitstream ends before self
, the
remaining bits of self
are not affected.
Type Parameters
I: IntoIterator<Item=bool>
: A stream of bits, which may be aBitSlice
or some other bit producer as desired.
fn bitxor_assign(&mut self, rhs: I)
[src]
XOR
s a bitstream into a slice.
Parameters
&mut self
rhs
: The bitstream toXOR
intoself
.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut [0b0101_0100]; let other: & [u8] = & [0b0011_0000]; let lhs: &mut BitSlice = store.into(); let rhs: & BitSlice = other.into(); lhs[.. 6] ^= &rhs[.. 4]; assert_eq!(store[0], 0b0110_0100);
impl<C, T> ShlAssign<usize> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
Shifts all bits in the array to the left — DOWN AND TOWARDS THE FRONT.
On primitives, the left-shift operator <<
moves bits away from the origin
and towards the ceiling. This is because we label the bits in a primitive
with the minimum on the right and the maximum on the left, which is
big-endian bit order. This increases the value of the primitive being
shifted.
THAT IS NOT HOW BitSlice
WORKS!
BitSlice
defines its layout with the minimum on the left and the maximum
on the right! Thus, left-shifting moves bits towards the minimum.
In BigEndian order, the effect in memory will be what you expect the <<
operator to do.
In LittleEndian order, the effect will be equivalent to using >>
on
the primitives in memory!
Notes
In order to preserve the effecs in memory that this operator traditionally expects, the bits that are emptied by this operation are zeroed rather than left to their old value.
The shift amount is modulated against the array length, so it is not an error to pass a shift amount greater than the array length.
A shift amount of zero is a no-op, and returns immediately.
fn shl_assign(&mut self, shamt: usize)
[src]
Shifts a slice left, in place.
Parameters
&mut self
shamt
: The shift amount. If this is greater than the length, then the slice is zeroed immediately.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut[0x4B, 0xA5]; let bv: &mut BitSlice = store.into(); let bits = &mut bv[2 .. 14]; *bits <<= 3; assert_eq!(bits.as_ref(), &[0b01_011_101, 0b001_000_01]);
impl<C, T> ShrAssign<usize> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
Shifts all bits in the array to the right — UP AND TOWARDS THE BACK.
On primitives, the right-shift operator >>
moves bits towards the origin
and away from the ceiling. This is because we label the bits in a primitive
with the minimum on the right and the maximum on the left, which is
big-endian bit order. This decreases the value of the primitive being
shifted.
THAT IS NOT HOW BitSlice
WORKS!
BitSlice
defines its layout with the minimum on the left and the maximum
on the right! Thus, right-shifting moves bits towards the maximum.
In Big-Endian order, the effect in memory will be what you expect the >>
operator to do.
In LittleEndian order, the effect will be equivalent to using <<
on
the primitives in memory!
Notes
In order to preserve the effects in memory that this operator traditionally expects, the bits that are emptied by this operation are zeroed rather than left to their old value.
The shift amount is modulated against the array length, so it is not an error to pass a shift amount greater than the array length.
A shift amount of zero is a no-op, and returns immediately.
fn shr_assign(&mut self, shamt: usize)
[src]
Shifts a slice right, in place.
Parameters
&mut self
shamt
: The shift amount. If this is greater than the length, then the slice is zeroed immediately.
Examples
use bitvec::prelude::*; let store: &mut [u8] = &mut[0x4B, 0xA5]; let bv: &mut BitSlice = store.into(); let bits = &mut bv[2 .. 14]; *bits >>= 3; assert_eq!(bits.as_ref(), &[0b01_000_00_1, 0b011_101_01])
impl<C, T> Index<usize> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
Indexes a single bit by semantic count. The index must be less than the
length of the BitSlice
.
type Output = bool
The returned type after indexing.
fn index(&self, index: usize) -> &Self::Output
[src]
Looks up a single bit by semantic index.
Parameters
&self
index
: The semantic index of the bit to look up.
Returns
The value of the bit at the requested index.
Examples
use bitvec::prelude::*; let store: &[u8] = &[0b0010_0000]; let bits: &BitSlice = store.into(); assert!(bits[2]); assert!(!bits[3]);
impl<C, T> Index<Range<usize>> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
type Output = Self
The returned type after indexing.
fn index(&self, Range { start: start, end: end }: Range<usize>) -> &Self::Output
[src]
impl<C, T> Index<RangeInclusive<usize>> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
type Output = Self
The returned type after indexing.
fn index(&self, index: RangeInclusive<usize>) -> &Self::Output
[src]
impl<C, T> Index<RangeFrom<usize>> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
type Output = Self
The returned type after indexing.
fn index(&self, RangeFrom { start: start }: RangeFrom<usize>) -> &Self::Output
[src]
impl<C, T> Index<RangeFull> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
type Output = Self
The returned type after indexing.
fn index(&self, _: RangeFull) -> &Self::Output
[src]
impl<C, T> Index<RangeTo<usize>> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
type Output = Self
The returned type after indexing.
fn index(&self, RangeTo { end: end }: RangeTo<usize>) -> &Self::Output
[src]
impl<C, T> Index<RangeToInclusive<usize>> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
type Output = Self
The returned type after indexing.
fn index(
&self,
RangeToInclusive { end: end }: RangeToInclusive<usize>
) -> &Self::Output
[src]
&self,
RangeToInclusive { end: end }: RangeToInclusive<usize>
) -> &Self::Output
impl<C, T> IndexMut<Range<usize>> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<C, T> IndexMut<RangeInclusive<usize>> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
fn index_mut(&mut self, index: RangeInclusive<usize>) -> &mut Self::Output
[src]
impl<C, T> IndexMut<RangeFrom<usize>> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<C, T> IndexMut<RangeFull> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<C, T> IndexMut<RangeTo<usize>> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<C, T> IndexMut<RangeToInclusive<usize>> for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
fn index_mut(
&mut self,
RangeToInclusive { end: end }: RangeToInclusive<usize>
) -> &mut Self::Output
[src]
&mut self,
RangeToInclusive { end: end }: RangeToInclusive<usize>
) -> &mut Self::Output
impl<C, T> Hash for BitSlice<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
Writes the contents of the BitSlice
, in semantic bit order, into a hasher.
fn hash<H>(&self, hasher: &mut H) where
H: Hasher,
[src]
H: Hasher,
Writes each bit of the BitSlice
, as a full bool
, into the hasher.
Parameters
&self
hasher
: The hashing state into which the slice will be written.
Type Parameters
H: Hasher
: The type of the hashing algorithm which receives the bits ofself
.
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<C, T> BorrowMut<BitSlice<C, T>> for BitBox<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
fn borrow_mut(&mut self) -> &mut BitSlice<C, T>
[src]
impl<C, T> BorrowMut<BitSlice<C, T>> for BitVec<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
Signifies that BitSlice
is the borrowed form of BitVec
.
fn borrow_mut(&mut self) -> &mut BitSlice<C, T>
[src]
Mutably borrows the BitVec
as a BitSlice
.
Parameters
&mut self
Returns
A mutably borrowed BitSlice
of the vector.
Examples
use bitvec::prelude::*; use std::borrow::BorrowMut; let mut bv = bitvec![0; 13]; let bs: &mut BitSlice = bv.borrow_mut(); assert!(!bs[10]); bs.set(10, true); assert!(bs[10]);
impl<C, T> Borrow<BitSlice<C, T>> for BitBox<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
impl<C, T> Borrow<BitSlice<C, T>> for BitVec<C, T> where
C: Cursor,
T: Bits,
[src]
C: Cursor,
T: Bits,
Signifies that BitSlice
is the borrowed form of BitVec
.
fn borrow(&self) -> &BitSlice<C, T>
[src]
Borrows the BitVec
as a BitSlice
.
Parameters
&self
Returns
A borrowed BitSlice
of the vector.
Examples
use bitvec::prelude::*; use std::borrow::Borrow; let bv = bitvec![0; 13]; let bs: &BitSlice = bv.borrow(); assert!(!bs[10]);
impl<C, T> Serialize for BitSlice<C, T> where
C: Cursor,
T: Bits + Serialize,
[src]
C: Cursor,
T: Bits + Serialize,
Blanket Implementations
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,