File: BitRef.md

package info (click to toggle)
rust-coreutils 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 505,620 kB
  • sloc: ansic: 103,594; asm: 28,570; sh: 8,910; python: 5,581; makefile: 472; cpp: 97; javascript: 72
file content (49 lines) | stat: -rw-r--r-- 1,764 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Proxy Bit-Reference

This structure simulates `&/mut bool` within `BitSlice` regions. It is analogous
to the C++ type [`std::bitset<N>::reference`][0].

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

```rust
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]);
```

[0]: https://en.cppreference.com/w/cpp/utility/bitset/reference