| 1 |
# Proxy Bit-Reference |
| 2 |
|
| 3 |
This structure simulates `&/mut bool` within `BitSlice` regions. It is analogous |
| 4 |
to the C++ type [`std::bitset<N>::reference`][0]. |
| 5 |
|
| 6 |
This type wraps a [`BitPtr`] and caches a `bool` in one of the remaining padding |
| 7 |
bytes. It is then able to freely give out references to its cached `bool`, and |
| 8 |
commits the cached value back to the proxied location when dropped. |
| 9 |
|
| 10 |
## Original |
| 11 |
|
| 12 |
This is semantically equivalent to `&'a bool` or `&'a mut bool`. |
| 13 |
|
| 14 |
## Quirks |
| 15 |
|
| 16 |
Because this type has both a lifetime and a destructor, it can introduce an |
| 17 |
uncommon syntax error condition in Rust. When an expression that produces this |
| 18 |
type is in the final expression of a block, including if that expression is used |
| 19 |
as a condition in a `match`, `if let`, or `if`, then the compiler will attempt |
| 20 |
to extend the drop scope of this type to the outside of the block. This causes a |
| 21 |
lifetime mismatch error if the source region from which this proxy is produced |
| 22 |
begins its lifetime inside the block. |
| 23 |
|
| 24 |
If you get a compiler error that this type causes something to be dropped while |
| 25 |
borrowed, you can end the borrow by putting any expression-ending syntax element |
| 26 |
after the offending expression that produces this type, including a semicolon or |
| 27 |
an item definition. |
| 28 |
|
| 29 |
## Examples |
| 30 |
|
| 31 |
```rust |
| 32 |
use bitvec::prelude::*; |
| 33 |
|
| 34 |
let bits = bits![mut 0; 2]; |
| 35 |
|
| 36 |
let (left, right) = bits.split_at_mut(1); |
| 37 |
let mut first = left.get_mut(0).unwrap(); |
| 38 |
let second = right.get_mut(0).unwrap(); |
| 39 |
|
| 40 |
// Writing through a dereference requires a `mut` binding. |
| 41 |
*first = true; |
| 42 |
// Writing through the explicit method call does not. |
| 43 |
second.commit(true); |
| 44 |
|
| 45 |
drop(first); // It’s not a reference, so NLL does not apply! |
| 46 |
assert_eq!(bits, bits![1; 2]); |
| 47 |
``` |
| 48 |
|
| 49 |
[0]: https://en.cppreference.com/w/cpp/utility/bitset/reference |