/rust/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.42.0/src/util/bit.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use std::fmt; |
2 | | |
3 | | #[derive(Clone, Copy, PartialEq)] |
4 | | pub(crate) struct Pack { |
5 | | mask: usize, |
6 | | shift: u32, |
7 | | } |
8 | | |
9 | | impl Pack { |
10 | | /// Value is packed in the `width` least-significant bits. |
11 | 0 | pub(crate) const fn least_significant(width: u32) -> Pack { |
12 | 0 | let mask = mask_for(width); |
13 | 0 |
|
14 | 0 | Pack { mask, shift: 0 } |
15 | 0 | } Unexecuted instantiation: <tokio::util::bit::Pack>::least_significant Unexecuted instantiation: <tokio::util::bit::Pack>::least_significant Unexecuted instantiation: <tokio::util::bit::Pack>::least_significant Unexecuted instantiation: <tokio::util::bit::Pack>::least_significant Unexecuted instantiation: <tokio::util::bit::Pack>::least_significant |
16 | | |
17 | | /// Value is packed in the `width` more-significant bits. |
18 | 0 | pub(crate) const fn then(&self, width: u32) -> Pack { |
19 | 0 | let shift = usize::BITS - self.mask.leading_zeros(); |
20 | 0 | let mask = mask_for(width) << shift; |
21 | 0 |
|
22 | 0 | Pack { mask, shift } |
23 | 0 | } Unexecuted instantiation: <tokio::util::bit::Pack>::then Unexecuted instantiation: <tokio::util::bit::Pack>::then Unexecuted instantiation: <tokio::util::bit::Pack>::then Unexecuted instantiation: <tokio::util::bit::Pack>::then Unexecuted instantiation: <tokio::util::bit::Pack>::then |
24 | | |
25 | | /// Width, in bits, dedicated to storing the value. |
26 | 48.6k | pub(crate) const fn width(&self) -> u32 { |
27 | 48.6k | usize::BITS - (self.mask >> self.shift).leading_zeros() |
28 | 48.6k | } Unexecuted instantiation: <tokio::util::bit::Pack>::width <tokio::util::bit::Pack>::width Line | Count | Source | 26 | 48.6k | pub(crate) const fn width(&self) -> u32 { | 27 | 48.6k | usize::BITS - (self.mask >> self.shift).leading_zeros() | 28 | 48.6k | } |
Unexecuted instantiation: <tokio::util::bit::Pack>::width Unexecuted instantiation: <tokio::util::bit::Pack>::width Unexecuted instantiation: <tokio::util::bit::Pack>::width |
29 | | |
30 | | /// Max representable value. |
31 | 48.6k | pub(crate) const fn max_value(&self) -> usize { |
32 | 48.6k | (1 << self.width()) - 1 |
33 | 48.6k | } Unexecuted instantiation: <tokio::util::bit::Pack>::max_value <tokio::util::bit::Pack>::max_value Line | Count | Source | 31 | 48.6k | pub(crate) const fn max_value(&self) -> usize { | 32 | 48.6k | (1 << self.width()) - 1 | 33 | 48.6k | } |
Unexecuted instantiation: <tokio::util::bit::Pack>::max_value Unexecuted instantiation: <tokio::util::bit::Pack>::max_value Unexecuted instantiation: <tokio::util::bit::Pack>::max_value |
34 | | |
35 | 48.6k | pub(crate) fn pack(&self, value: usize, base: usize) -> usize { |
36 | 48.6k | assert!(value <= self.max_value()); |
37 | 48.6k | (base & !self.mask) | (value << self.shift) |
38 | 48.6k | } Unexecuted instantiation: <tokio::util::bit::Pack>::pack <tokio::util::bit::Pack>::pack Line | Count | Source | 35 | 48.6k | pub(crate) fn pack(&self, value: usize, base: usize) -> usize { | 36 | 48.6k | assert!(value <= self.max_value()); | 37 | 48.6k | (base & !self.mask) | (value << self.shift) | 38 | 48.6k | } |
Unexecuted instantiation: <tokio::util::bit::Pack>::pack Unexecuted instantiation: <tokio::util::bit::Pack>::pack Unexecuted instantiation: <tokio::util::bit::Pack>::pack |
39 | | |
40 | 314k | pub(crate) fn unpack(&self, src: usize) -> usize { |
41 | 314k | unpack(src, self.mask, self.shift) |
42 | 314k | } Unexecuted instantiation: <tokio::util::bit::Pack>::unpack <tokio::util::bit::Pack>::unpack Line | Count | Source | 40 | 314k | pub(crate) fn unpack(&self, src: usize) -> usize { | 41 | 314k | unpack(src, self.mask, self.shift) | 42 | 314k | } |
Unexecuted instantiation: <tokio::util::bit::Pack>::unpack Unexecuted instantiation: <tokio::util::bit::Pack>::unpack Unexecuted instantiation: <tokio::util::bit::Pack>::unpack |
43 | | } |
44 | | |
45 | | impl fmt::Debug for Pack { |
46 | 0 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
47 | 0 | write!( |
48 | 0 | fmt, |
49 | 0 | "Pack {{ mask: {:b}, shift: {} }}", |
50 | 0 | self.mask, self.shift |
51 | 0 | ) |
52 | 0 | } Unexecuted instantiation: <tokio::util::bit::Pack as core::fmt::Debug>::fmt Unexecuted instantiation: <tokio::util::bit::Pack as core::fmt::Debug>::fmt Unexecuted instantiation: <tokio::util::bit::Pack as core::fmt::Debug>::fmt Unexecuted instantiation: <tokio::util::bit::Pack as core::fmt::Debug>::fmt Unexecuted instantiation: <tokio::util::bit::Pack as core::fmt::Debug>::fmt |
53 | | } |
54 | | |
55 | | /// Returns a `usize` with the right-most `n` bits set. |
56 | 0 | pub(crate) const fn mask_for(n: u32) -> usize { |
57 | 0 | let shift = 1usize.wrapping_shl(n - 1); |
58 | 0 | shift | (shift - 1) |
59 | 0 | } Unexecuted instantiation: tokio::util::bit::mask_for Unexecuted instantiation: tokio::util::bit::mask_for Unexecuted instantiation: tokio::util::bit::mask_for Unexecuted instantiation: tokio::util::bit::mask_for Unexecuted instantiation: tokio::util::bit::mask_for |
60 | | |
61 | | /// Unpacks a value using a mask & shift. |
62 | 314k | pub(crate) const fn unpack(src: usize, mask: usize, shift: u32) -> usize { |
63 | 314k | (src & mask) >> shift |
64 | 314k | } Unexecuted instantiation: tokio::util::bit::unpack Line | Count | Source | 62 | 314k | pub(crate) const fn unpack(src: usize, mask: usize, shift: u32) -> usize { | 63 | 314k | (src & mask) >> shift | 64 | 314k | } |
Unexecuted instantiation: tokio::util::bit::unpack Unexecuted instantiation: tokio::util::bit::unpack Unexecuted instantiation: tokio::util::bit::unpack |