pub unsafe trait BitBlock: Copy + Not<Output = Self> + BitOr<Output = Self> + BitOrAssign + BitAndAssign + BitAnd<Output = Self> + Ord {
const BITS: usize;
const ZERO: Self;
const UPSTREAM_ONE: Self;
// Required methods
fn wrapping_shift_downstream(self, n: usize) -> Self;
fn wrapping_shift_upstream(self, n: usize) -> Self;
fn leading_zeros(self) -> usize;
fn leading_ones(self) -> usize;
// Provided methods
fn get_block_index(idx: usize) -> usize { ... }
fn bit_index_from_block_index(idx: usize) -> usize { ... }
fn wrapping_test_bit(self, idx: usize) -> bool { ... }
fn wrapping_with_bit(self, idx: usize) -> Self { ... }
fn wrapping_without_bit(self, idx: usize) -> Self { ... }
fn ones_mask(position: usize, length: NonZeroUsize) -> Self { ... }
}
Expand description
Represents a copyable container of bits (usually an unsigned integer).
Safety
Self::ZERO
must becore::mem::zeroed()
.2^BITS_LOG2
must be equal to BITS.- The provided methods can only be overridden with equivalent methods.
Required Associated Constants§
sourceconst BITS: usize
const BITS: usize
Represents the number of bits in a block. It is generally a good idea for this number to be a power of two.
const ZERO: Self
sourceconst UPSTREAM_ONE: Self
const UPSTREAM_ONE: Self
As of now, this value is not 1
for unsigned integers UPSTREAM_ONE.wrapping_shift_upstream(1)
should be 0
.
For example, for u8, this value is 0x80
(0b10000000
).
Required Methods§
sourcefn wrapping_shift_downstream(self, n: usize) -> Self
fn wrapping_shift_downstream(self, n: usize) -> Self
Shifts self
n
bits in the indexing direction.
sourcefn wrapping_shift_upstream(self, n: usize) -> Self
fn wrapping_shift_upstream(self, n: usize) -> Self
Shifts self
n
bits against the indexing direction.
sourcefn leading_zeros(self) -> usize
fn leading_zeros(self) -> usize
Gets the leading zeros in the indexing direction.
sourcefn leading_ones(self) -> usize
fn leading_ones(self) -> usize
Gets the leading ones in the indexing direction.
Provided Methods§
sourcefn get_block_index(idx: usize) -> usize
fn get_block_index(idx: usize) -> usize
Returns the index of the block holding the bit at the specified location in a collection (eg. for a u8
, get_block_index(9) == 2
).
sourcefn bit_index_from_block_index(idx: usize) -> usize
fn bit_index_from_block_index(idx: usize) -> usize
Returns the index of the first bit in the block at idx
..
sourcefn wrapping_test_bit(self, idx: usize) -> bool
fn wrapping_test_bit(self, idx: usize) -> bool
Returns true if the bit at idx
is 1
.
sourcefn wrapping_with_bit(self, idx: usize) -> Self
fn wrapping_with_bit(self, idx: usize) -> Self
Returns self
with the bit at idx
set to 1
.
sourcefn wrapping_without_bit(self, idx: usize) -> Self
fn wrapping_without_bit(self, idx: usize) -> Self
Returns self
with the bit at idx
set to 0
.
sourcefn ones_mask(position: usize, length: NonZeroUsize) -> Self
fn ones_mask(position: usize, length: NonZeroUsize) -> Self
Returns a bit mask of ones at position
(wrapped) with a length of length
(saturated).
Example
assert_eq!(u8::ones_mask(2, NonZeroUsize::new(3).unwrap()), 0b00111000);
assert_eq!(u8::ones_mask(6, NonZeroUsize::new(10).unwrap()), 0b00000011);
assert_eq!(u8::ones_mask(12, NonZeroUsize::new(2).unwrap()), 0b00001100);