#[repr(C, align(8))]pub struct BitRef<'a, M = Const, T = usize, O = Lsb0>where
M: Mutability,
T: BitStore,
O: BitOrder,{ /* private fields */ }
Expand description
Proxy Bit-Reference
This structure simulates &/mut bool
within BitSlice
regions. It is analogous
to the C++ type std::bitset<N>::reference
.
This type wraps a BitPtr
and caches a bool
in one of the remaining padding
bytes. It is then able to freely give out references to its cached bool
, and
commits the cached value back to the proxied location when dropped.
Original
This is semantically equivalent to &'a bool
or &'a mut bool
.
Quirks
Because this type has both a lifetime and a destructor, it can introduce an
uncommon syntax error condition in Rust. When an expression that produces this
type is in the final expression of a block, including if that expression is used
as a condition in a match
, if let
, or if
, then the compiler will attempt
to extend the drop scope of this type to the outside of the block. This causes a
lifetime mismatch error if the source region from which this proxy is produced
begins its lifetime inside the block.
If you get a compiler error that this type causes something to be dropped while borrowed, you can end the borrow by putting any expression-ending syntax element after the offending expression that produces this type, including a semicolon or an item definition.
Examples
use bitvec::prelude::*;
let bits = bits![mut 0; 2];
let (left, right) = bits.split_at_mut(1);
let mut first = left.get_mut(0).unwrap();
let second = right.get_mut(0).unwrap();
// Writing through a dereference requires a `mut` binding.
*first = true;
// Writing through the explicit method call does not.
second.commit(true);
drop(first); // It’s not a reference, so NLL does not apply!
assert_eq!(bits, bits![1; 2]);
Implementations§
source§impl<M, T, O> BitRef<'_, M, T, O>where
M: Mutability,
T: BitStore,
O: BitOrder,
impl<M, T, O> BitRef<'_, M, T, O>where M: Mutability, T: BitStore, O: BitOrder,
sourcepub unsafe fn from_bitptr(bitptr: BitPtr<M, T, O>) -> Self
pub unsafe fn from_bitptr(bitptr: BitPtr<M, T, O>) -> Self
Converts a bit-pointer into a proxy bit-reference.
This reads through the pointer in order to cache the current bit value in the proxy.
Original
The syntax unsafe { &* ptr }
.
Safety
This is equivalent to (and is!) dereferencing a raw pointer. The pointer must be well-constructed, refer to a live memory location in the program context, and not be aliased beyond its typing indicators.
sourcepub fn into_bitptr(self) -> BitPtr<M, T, O>
pub fn into_bitptr(self) -> BitPtr<M, T, O>
source§impl<T, O> BitRef<'_, Mut, T, O>where
T: BitStore,
O: BitOrder,
impl<T, O> BitRef<'_, Mut, T, O>where T: BitStore, O: BitOrder,
sourcepub fn swap<T2, O2>(&mut self, other: &mut BitRef<'_, Mut, T2, O2>)where
T2: BitStore,
O2: BitOrder,
pub fn swap<T2, O2>(&mut self, other: &mut BitRef<'_, Mut, T2, O2>)where T2: BitStore, O2: BitOrder,
Trait Implementations§
source§impl<M, T, O> AsRef<bool> for BitRef<'_, M, T, O>where
M: Mutability,
T: BitStore,
O: BitOrder,
impl<M, T, O> AsRef<bool> for BitRef<'_, M, T, O>where M: Mutability, T: BitStore, O: BitOrder,
source§impl<'a, M, T1, T2, O1, O2> Extend<BitRef<'a, M, T2, O2>> for BitVec<T1, O1>where
M: Mutability,
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<'a, M, T1, T2, O1, O2> Extend<BitRef<'a, M, T2, O2>> for BitVec<T1, O1>where M: Mutability, T1: BitStore, T2: BitStore, O1: BitOrder, O2: BitOrder,
Bit-Vector Extension by Proxy References
DO NOT use this. You clearly have a bit-slice. Use
.extend_from_bitslice()
instead!
Iterating over a bit-slice requires loading from memory and constructing a proxy reference for each bit. This is needlessly slow; the specialized method is able to avoid this per-bit cost and possibly even use batched operations.
source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = BitRef<'a, M, T2, O2>>,
fn extend<I>(&mut self, iter: I)where I: IntoIterator<Item = BitRef<'a, M, T2, O2>>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<'a, M, T1, T2, O1, O2> FromIterator<BitRef<'a, M, T2, O2>> for BitVec<T1, O1>where
M: Mutability,
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<'a, M, T1, T2, O1, O2> FromIterator<BitRef<'a, M, T2, O2>> for BitVec<T1, O1>where M: Mutability, T1: BitStore, T2: BitStore, O1: BitOrder, O2: BitOrder,
Bit-Vector Collection from Proxy References
DO NOT use this. You clearly have a bit-slice. Use
::from_bitslice()
instead!
Iterating over a bit-slice requires loading from memory and constructing a proxy reference for each bit. This is needlessly slow; the specialized method is able to avoid this per-bit cost and possibly even use batched operations.
source§impl<M, T, O> Ord for BitRef<'_, M, T, O>where
M: Mutability,
T: BitStore,
O: BitOrder,
impl<M, T, O> Ord for BitRef<'_, M, T, O>where M: Mutability, T: BitStore, O: BitOrder,
source§impl<M, T, O> PartialEq<&bool> for BitRef<'_, M, T, O>where
M: Mutability,
T: BitStore,
O: BitOrder,
impl<M, T, O> PartialEq<&bool> for BitRef<'_, M, T, O>where M: Mutability, T: BitStore, O: BitOrder,
source§impl<M, T, O> PartialEq<BitRef<'_, M, T, O>> for &boolwhere
M: Mutability,
T: BitStore,
O: BitOrder,
impl<M, T, O> PartialEq<BitRef<'_, M, T, O>> for &boolwhere M: Mutability, T: BitStore, O: BitOrder,
source§impl<M, T, O> PartialEq<BitRef<'_, M, T, O>> for boolwhere
M: Mutability,
T: BitStore,
O: BitOrder,
impl<M, T, O> PartialEq<BitRef<'_, M, T, O>> for boolwhere M: Mutability, T: BitStore, O: BitOrder,
source§impl<M1, M2, O1, O2, T1, T2> PartialEq<BitRef<'_, M2, T2, O2>> for BitRef<'_, M1, T1, O1>where
M1: Mutability,
M2: Mutability,
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<M1, M2, O1, O2, T1, T2> PartialEq<BitRef<'_, M2, T2, O2>> for BitRef<'_, M1, T1, O1>where M1: Mutability, M2: Mutability, T1: BitStore, T2: BitStore, O1: BitOrder, O2: BitOrder,
source§impl<M, T, O> PartialEq<bool> for BitRef<'_, M, T, O>where
M: Mutability,
T: BitStore,
O: BitOrder,
impl<M, T, O> PartialEq<bool> for BitRef<'_, M, T, O>where M: Mutability, T: BitStore, O: BitOrder,
source§impl<M, T, O> PartialOrd<&bool> for BitRef<'_, M, T, O>where
M: Mutability,
T: BitStore,
O: BitOrder,
impl<M, T, O> PartialOrd<&bool> for BitRef<'_, M, T, O>where M: Mutability, T: BitStore, O: BitOrder,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<M1, M2, O1, O2, T1, T2> PartialOrd<BitRef<'_, M2, T2, O2>> for BitRef<'_, M1, T1, O1>where
M1: Mutability,
M2: Mutability,
T1: BitStore,
T2: BitStore,
O1: BitOrder,
O2: BitOrder,
impl<M1, M2, O1, O2, T1, T2> PartialOrd<BitRef<'_, M2, T2, O2>> for BitRef<'_, M1, T1, O1>where M1: Mutability, M2: Mutability, T1: BitStore, T2: BitStore, O1: BitOrder, O2: BitOrder,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<M, T, O> PartialOrd<bool> for BitRef<'_, M, T, O>where
M: Mutability,
T: BitStore,
O: BitOrder,
impl<M, T, O> PartialOrd<bool> for BitRef<'_, M, T, O>where M: Mutability, T: BitStore, O: BitOrder,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl<M, T, O> Eq for BitRef<'_, M, T, O>where M: Mutability, T: BitStore, O: BitOrder,
impl<M, T, O> Send for BitRef<'_, M, T, O>where M: Mutability, T: BitStore + Sync, O: BitOrder,
impl<M, T, O> Sync for BitRef<'_, M, T, O>where M: Mutability, T: BitStore + Sync, O: BitOrder,
Auto Trait Implementations§
impl<'a, M = Const, T = usize, O = Lsb0> !RefUnwindSafe for BitRef<'a, M, T, O>
impl<'a, M, T, O> Unpin for BitRef<'a, M, T, O>where M: Unpin, O: Unpin,
impl<'a, M = Const, T = usize, O = Lsb0> !UnwindSafe for BitRef<'a, M, T, O>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,
self
to use its Display
implementation when
Debug
-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
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 ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
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) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_mut()
into the pipe
function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.