Skip to main content

PermutationStrategy

Trait PermutationStrategy 

Source
pub unsafe trait PermutationStrategy<const NBITS: usize> {
    // Required methods
    fn bytes(count: usize) -> usize;
    unsafe fn pack(s: &mut [u8], i: usize, value: u8);
    unsafe fn unpack(s: &[u8], i: usize) -> u8;
}
Expand description

A enable the dimensions within a BitSlice to be permuted in an arbitrary way.

§Safety

This provides the computation for the number of bytes required to store a given number of NBITS bit-packed values. Improper implementation will result in out-of-bounds accesses being made.

The following must hold:

For all counts values c, let b = Self::bytes(c) be requested number of bytes for c for this permutation strategy and s be a slice of bytes with length c. Then, for all i < c, Self::pack(s, i, _) and Self::unpack(s, i) must only access s in-bounds.

This implementation must be such that unsafe code can rely on this property holding.

Required Methods§

Source

fn bytes(count: usize) -> usize

Return the number of bytes required to store count values of with NBITS.

Source

unsafe fn pack(s: &mut [u8], i: usize, value: u8)

Pack the lower NBITS bits of value into s at logical index i.

§Safety

This is a tricky function to call with several subtle requirements.

  • Let s be a slice of length c where c = Self::bytes(b) for some b. Then this function is safe to call if i is in [0, b).
Source

unsafe fn unpack(s: &[u8], i: usize) -> u8

Unpack the value stored at logical index i and return it as the lower NBITS bits in the return value.

§Safety

This is a tricky function to call with several subtle requirements.

  • Let s be a slice of length c where c = Self::bytes(b) for some b. Then this function is safe to call if i is in [0, b).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl PermutationStrategy<4> for BitTranspose

Safety: We ask for bytes in multiples of 32. Furthermore, the accesses to the packed data in pack and unpack use checked accesses, so out-of-bounds reads will panic.

Source§

impl<const NBITS: usize> PermutationStrategy<NBITS> for Dense

Safety: For all 0 <= i < count, NBITS * i <= 8 * ceil((NBITS * count) / 8).