pub struct ZeroCopyBitSlice<'a> { /* private fields */ }
Expand description
A zero copy container that helps read from and write to the bits in a &[u8]
.
Implementations§
Source§impl<'a> ZeroCopyBitSlice<'a>
impl<'a> ZeroCopyBitSlice<'a>
Sourcepub fn intialize_in<'o>(
num_bits: u32,
bytes: &'o mut &'a mut [u8],
) -> ZeroCopyBitSlice<'a>
pub fn intialize_in<'o>( num_bits: u32, bytes: &'o mut &'a mut [u8], ) -> ZeroCopyBitSlice<'a>
Initializes a ZeroCopyBitSlice
with num_bits
bits within the buffer provided.
Requires space for the bit length in addition to bytes for all bits.
We also update the position of the reference to just after the end of the bitslice.
Panics if buffer is not large enough.
Examples found in repository?
2fn main() {
3 use zerocopy_bitslice::ZeroCopyBitSlice;
4 const MOCK_OFFSET: usize = 100;
5 const MOCK_END_BUFFER: usize = 100;
6 const NUM_ITEMS: u32 = 100;
7
8 // Calculate required space for the `ZeroCopyBitSlice`
9 let required_space: usize = ZeroCopyBitSlice::required_space(NUM_ITEMS);
10
11 // Initalize some buffer w/ bytes on either end of where the `ZeroCopyBitSlice` will live
12 let mut mock_minter: Vec<u8> = vec![0; MOCK_OFFSET + required_space + MOCK_END_BUFFER];
13
14 // Get reference to the bit bytes and build `ZeroCopyBitSlice`
15 let mut zcbs_bytes: &mut [u8] = &mut mock_minter[MOCK_OFFSET..];
16 let mut zcbs: ZeroCopyBitSlice = ZeroCopyBitSlice::intialize_in(NUM_ITEMS, &mut zcbs_bytes);
17
18 // Build random sequence
19 let mut sequence: [u32; NUM_ITEMS as usize] = std::array::from_fn(|_a_new_minter_appears| {
20 // Mock seed ingredients
21 let mock_time: i64 = 123456890;
22 let mock_time_bytes: [u8; 8] = mock_time.to_le_bytes();
23 let mock_time_seed: [u8; 32] = core::array::from_fn(|i| mock_time_bytes[i % 8]);
24 let mock_slothash: [u8; 32] = core::array::from_fn(|i| i as u8);
25 let mock_minter_pubkey: [u8; 32] = core::array::from_fn(|i| i as u8);
26
27 // Build seed
28 let seed: [u8; 32] =
29 core::array::from_fn(|i| mock_time_seed[i] ^ mock_slothash[i] ^ mock_minter_pubkey[i]);
30
31 // Get element in sequence
32 zcbs.choose_random_zero(seed)
33 .expect("we are minting exactly the right number of items")
34 });
35
36 // Show sequence and sorted sequence
37 println!("random sequence is {sequence:?}");
38 sequence.sort();
39 println!("sorted sequence is {sequence:?}");
40}
Sourcepub fn from_bytes(bytes: &'a mut [u8]) -> ZeroCopyBitSlice<'a>
pub fn from_bytes(bytes: &'a mut [u8]) -> ZeroCopyBitSlice<'a>
Given a pointer to a &mut [u8]
with a previously initialized `ZeropCopyBitSlice<’a>,
perform zero copy deserialization on the byte array containing the bits.
Sourcepub fn from_bytes_update<'o>(
bytes: &'o mut &'a mut [u8],
) -> ZeroCopyBitSlice<'a>
pub fn from_bytes_update<'o>( bytes: &'o mut &'a mut [u8], ) -> ZeroCopyBitSlice<'a>
Given a pointer to a &mut [u8]
with a previously initialized `ZeropCopyBitSlice<’a>,
perform zero copy deserialization on the byte array containing the bits.
Sourcepub fn set(&mut self, idx: u32, value: bool)
pub fn set(&mut self, idx: u32, value: bool)
Sets the value of the bit at position idx
.
Panics if out of bounds.
Sourcepub fn get(&self, idx: u32) -> bool
pub fn get(&self, idx: u32) -> bool
Retrieves the value of the bit at position idx
.
Panics if out of bounds.
Sourcepub fn choose_random_zero(&mut self, seed: impl AsRef<[u8]>) -> Option<u32>
pub fn choose_random_zero(&mut self, seed: impl AsRef<[u8]>) -> Option<u32>
Chooses a random zero bit from within the bitslice, setting the bit to 1 and returning the bit’s index.
Returns None
if all bits are set to 1.
Examples found in repository?
2fn main() {
3 use zerocopy_bitslice::ZeroCopyBitSlice;
4 const MOCK_OFFSET: usize = 100;
5 const MOCK_END_BUFFER: usize = 100;
6 const NUM_ITEMS: u32 = 100;
7
8 // Calculate required space for the `ZeroCopyBitSlice`
9 let required_space: usize = ZeroCopyBitSlice::required_space(NUM_ITEMS);
10
11 // Initalize some buffer w/ bytes on either end of where the `ZeroCopyBitSlice` will live
12 let mut mock_minter: Vec<u8> = vec![0; MOCK_OFFSET + required_space + MOCK_END_BUFFER];
13
14 // Get reference to the bit bytes and build `ZeroCopyBitSlice`
15 let mut zcbs_bytes: &mut [u8] = &mut mock_minter[MOCK_OFFSET..];
16 let mut zcbs: ZeroCopyBitSlice = ZeroCopyBitSlice::intialize_in(NUM_ITEMS, &mut zcbs_bytes);
17
18 // Build random sequence
19 let mut sequence: [u32; NUM_ITEMS as usize] = std::array::from_fn(|_a_new_minter_appears| {
20 // Mock seed ingredients
21 let mock_time: i64 = 123456890;
22 let mock_time_bytes: [u8; 8] = mock_time.to_le_bytes();
23 let mock_time_seed: [u8; 32] = core::array::from_fn(|i| mock_time_bytes[i % 8]);
24 let mock_slothash: [u8; 32] = core::array::from_fn(|i| i as u8);
25 let mock_minter_pubkey: [u8; 32] = core::array::from_fn(|i| i as u8);
26
27 // Build seed
28 let seed: [u8; 32] =
29 core::array::from_fn(|i| mock_time_seed[i] ^ mock_slothash[i] ^ mock_minter_pubkey[i]);
30
31 // Get element in sequence
32 zcbs.choose_random_zero(seed)
33 .expect("we are minting exactly the right number of items")
34 });
35
36 // Show sequence and sorted sequence
37 println!("random sequence is {sequence:?}");
38 sequence.sort();
39 println!("sorted sequence is {sequence:?}");
40}
Sourcepub const fn required_space(num_bits: u32) -> usize
pub const fn required_space(num_bits: u32) -> usize
Calculates the requires number of bytes to initialize a ZeroCopyBitSlice
, e.g. via
initialize_in
.
Examples found in repository?
2fn main() {
3 use zerocopy_bitslice::ZeroCopyBitSlice;
4 const MOCK_OFFSET: usize = 100;
5 const MOCK_END_BUFFER: usize = 100;
6 const NUM_ITEMS: u32 = 100;
7
8 // Calculate required space for the `ZeroCopyBitSlice`
9 let required_space: usize = ZeroCopyBitSlice::required_space(NUM_ITEMS);
10
11 // Initalize some buffer w/ bytes on either end of where the `ZeroCopyBitSlice` will live
12 let mut mock_minter: Vec<u8> = vec![0; MOCK_OFFSET + required_space + MOCK_END_BUFFER];
13
14 // Get reference to the bit bytes and build `ZeroCopyBitSlice`
15 let mut zcbs_bytes: &mut [u8] = &mut mock_minter[MOCK_OFFSET..];
16 let mut zcbs: ZeroCopyBitSlice = ZeroCopyBitSlice::intialize_in(NUM_ITEMS, &mut zcbs_bytes);
17
18 // Build random sequence
19 let mut sequence: [u32; NUM_ITEMS as usize] = std::array::from_fn(|_a_new_minter_appears| {
20 // Mock seed ingredients
21 let mock_time: i64 = 123456890;
22 let mock_time_bytes: [u8; 8] = mock_time.to_le_bytes();
23 let mock_time_seed: [u8; 32] = core::array::from_fn(|i| mock_time_bytes[i % 8]);
24 let mock_slothash: [u8; 32] = core::array::from_fn(|i| i as u8);
25 let mock_minter_pubkey: [u8; 32] = core::array::from_fn(|i| i as u8);
26
27 // Build seed
28 let seed: [u8; 32] =
29 core::array::from_fn(|i| mock_time_seed[i] ^ mock_slothash[i] ^ mock_minter_pubkey[i]);
30
31 // Get element in sequence
32 zcbs.choose_random_zero(seed)
33 .expect("we are minting exactly the right number of items")
34 });
35
36 // Show sequence and sorted sequence
37 println!("random sequence is {sequence:?}");
38 sequence.sort();
39 println!("sorted sequence is {sequence:?}");
40}
Auto Trait Implementations§
impl<'a> Freeze for ZeroCopyBitSlice<'a>
impl<'a> RefUnwindSafe for ZeroCopyBitSlice<'a>
impl<'a> Send for ZeroCopyBitSlice<'a>
impl<'a> Sync for ZeroCopyBitSlice<'a>
impl<'a> Unpin for ZeroCopyBitSlice<'a>
impl<'a> !UnwindSafe for ZeroCopyBitSlice<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more