Skip to main content

BitPacker

Trait BitPacker 

Source
pub trait BitPacker: Sealed {
    const BLOCK_LEN: usize;

    // Required methods
    fn num_bits(decompressed: &[u32]) -> u8;
    fn compress(
        decompressed: &[u32],
        compressed: &mut [u8],
        num_bits: u8,
    ) -> usize;
    fn decompress(
        compressed: &[u8],
        decompressed: &mut [u32],
        num_bits: u8,
    ) -> usize;
}
Expand description

A fixed-block-size integer bit-packing codec.

Packs u32 values into a tightly-bit-packed byte buffer at a caller-chosen bit width, and unpacks them back out. Every implementation operates on exactly BLOCK_LEN values at a time — arbitrary-length input is handled by the pack/unpack wrapper, not by this trait directly.

This trait is sealed: it can only be implemented by types defined in this crate, so adding a new required method here is never a breaking change for downstream users.

§Examples

use wasm_bitpack::{BitPacker, Scalar};

let values = vec![7u32; Scalar::BLOCK_LEN];
let num_bits = Scalar::num_bits(&values);

let mut compressed = vec![0u8; Scalar::BLOCK_LEN * num_bits as usize / 8];
let written = Scalar::compress(&values, &mut compressed, num_bits);

let mut decompressed = vec![0u32; Scalar::BLOCK_LEN];
let read = Scalar::decompress(&compressed[..written], &mut decompressed, num_bits);
assert_eq!(read, written);
assert_eq!(decompressed, values);

Required Associated Constants§

Source

const BLOCK_LEN: usize

The number of u32 values a single block holds. 128 for every v0.1 implementation.

Required Methods§

Source

fn num_bits(decompressed: &[u32]) -> u8

Returns the minimum number of bits needed to represent every value in decompressed.

Source

fn compress(decompressed: &[u32], compressed: &mut [u8], num_bits: u8) -> usize

Packs exactly BLOCK_LEN values from decompressed into compressed at num_bits bits per value, returning the number of bytes written.

§Panics

Panics if decompressed.len() != Self::BLOCK_LEN, if compressed is too small to hold Self::BLOCK_LEN * num_bits bits, or if num_bits is 0 or greater than 32.

Source

fn decompress( compressed: &[u8], decompressed: &mut [u32], num_bits: u8, ) -> usize

Unpacks exactly BLOCK_LEN values from compressed into decompressed, returning the number of bytes read.

§Panics

Panics if decompressed.len() != Self::BLOCK_LEN, if compressed is too small to hold Self::BLOCK_LEN * num_bits bits, or if num_bits is 0 or greater than 32.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§