alloy_trie/mask.rs
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
use core::fmt;
use derive_more::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Deref, From, Not};
/// A struct representing a mask of 16 bits, used for Ethereum trie operations.
///
/// Masks in a trie are used to efficiently represent and manage information about the presence or
/// absence of certain elements, such as child nodes, within a trie. Masks are usually implemented
/// as bit vectors, where each bit represents the presence (1) or absence (0) of a corresponding
/// element.
#[derive(
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Deref,
From,
BitAnd,
BitAndAssign,
BitOr,
BitOrAssign,
Not,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(derive_arbitrary::Arbitrary, proptest_derive::Arbitrary))]
pub struct TrieMask(u16);
impl fmt::Debug for TrieMask {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "TrieMask({:016b})", self.0)
}
}
impl TrieMask {
/// Creates a new `TrieMask` from the given inner value.
#[inline]
pub const fn new(inner: u16) -> Self {
Self(inner)
}
/// Returns the inner value of the `TrieMask`.
#[inline]
pub const fn get(self) -> u16 {
self.0
}
/// Creates a new `TrieMask` from the given nibble.
#[inline]
pub const fn from_nibble(nibble: u8) -> Self {
Self(1u16 << nibble)
}
/// Returns `true` if the current `TrieMask` is a subset of `other`.
#[inline]
pub fn is_subset_of(self, other: Self) -> bool {
self & other == self
}
/// Returns `true` if a given bit is set in a mask.
#[inline]
pub const fn is_bit_set(self, index: u8) -> bool {
self.0 & (1u16 << index) != 0
}
/// Returns `true` if the mask is empty.
#[inline]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
/// Returns the number of bits set in the mask.
#[inline]
pub const fn count_bits(self) -> u8 {
self.0.count_ones() as u8
}
/// Returns the index of the first bit set in the mask, or `None` if the mask is empty.
#[inline]
pub const fn first_set_bit_index(self) -> Option<u8> {
if self.is_empty() {
None
} else {
Some(self.0.trailing_zeros() as u8)
}
}
/// Set bit at a specified index.
#[inline]
pub fn set_bit(&mut self, index: u8) {
self.0 |= 1u16 << index;
}
/// Unset bit at a specified index.
#[inline]
pub fn unset_bit(&mut self, index: u8) {
self.0 &= !(1u16 << index);
}
}