Struct ZeroCopyBitSlice

Source
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>

Source

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?
examples/minter.rs (line 16)
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}
Source

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.

Source

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.

Source

pub fn bit_len(&self) -> u32

The number of bits contained within this bit slice

Source

pub fn byte_len(&self) -> usize

The number bytes used to store these bits

Source

pub fn set(&mut self, idx: u32, value: bool)

Sets the value of the bit at position idx.

Panics if out of bounds.

Source

pub fn get(&self, idx: u32) -> bool

Retrieves the value of the bit at position idx.

Panics if out of bounds.

Source

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?
examples/minter.rs (line 32)
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}
Source

pub fn num_zeros(&self) -> u32

Calculates the number of zero bits in the bitslice.

Source

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?
examples/minter.rs (line 9)
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> AbiExample for T

Source§

default fn example() -> T

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V