Skip to main content

Packable

Trait Packable 

Source
pub trait Packable<B: Buffer> {
    const SIZE: u32;

    // Required methods
    fn pack(&self) -> B;
    fn unpack(buffer: B) -> Self;
}
Expand description

A type that can be packed into and unpacked from a Buffer of type B.

The #[packable(B)] attribute macro derives this trait for structs and enums automatically. Implement it manually for primitive types or for custom encodings.

§Manual implementation contract

  • pack must return a value where bits at and above position SIZE are zero. Debug builds verify this with assert!; release builds silently mask oversized values.
  • unpack receives a value where only the lower SIZE bits are meaningful; higher bits are not guaranteed to be zero.

§Example

A 5×6 board coordinate packed into 5 bits — fewer than the 6 bits a naive “3 bits for x + 3 bits for y” layout would use:

use bitcram::Packable;

struct Coord { x: u8, y: u8 }

impl Packable<u16> for Coord {
    const SIZE: u32 = 5;
    fn pack(&self) -> u16 {
        (self.y * 5 + self.x) as u16
    }
    fn unpack(buffer: u16) -> Self {
        let i = buffer as u8;
        Self { x: i % 5, y: i / 5 }
    }
}

Required Associated Constants§

Source

const SIZE: u32

The number of bits this type occupies when packed.

Required Methods§

Source

fn pack(&self) -> B

Packs self into the lower SIZE bits of a buffer.

Source

fn unpack(buffer: B) -> Self

Reconstructs a value from the lower SIZE bits of buffer.

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.

Implementations on Foreign Types§

Source§

impl<B: Buffer> Packable<B> for bool

Source§

const SIZE: u32 = 1

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, O: Packable<B>, P: Packable<B>, Q: Packable<B>, R: Packable<B>, S: Packable<B>, T: Packable<B>, U: Packable<B>, V: Packable<B>, W: Packable<B>, X: Packable<B>, Y: Packable<B>, Z: Packable<B>> Packable<B> for (O, P, Q, R, S, T, U, V, W, X, Y, Z)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, P: Packable<B>, Q: Packable<B>, R: Packable<B>, S: Packable<B>, T: Packable<B>, U: Packable<B>, V: Packable<B>, W: Packable<B>, X: Packable<B>, Y: Packable<B>, Z: Packable<B>> Packable<B> for (P, Q, R, S, T, U, V, W, X, Y, Z)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, Q: Packable<B>, R: Packable<B>, S: Packable<B>, T: Packable<B>, U: Packable<B>, V: Packable<B>, W: Packable<B>, X: Packable<B>, Y: Packable<B>, Z: Packable<B>> Packable<B> for (Q, R, S, T, U, V, W, X, Y, Z)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, R: Packable<B>, S: Packable<B>, T: Packable<B>, U: Packable<B>, V: Packable<B>, W: Packable<B>, X: Packable<B>, Y: Packable<B>, Z: Packable<B>> Packable<B> for (R, S, T, U, V, W, X, Y, Z)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, S: Packable<B>, T: Packable<B>, U: Packable<B>, V: Packable<B>, W: Packable<B>, X: Packable<B>, Y: Packable<B>, Z: Packable<B>> Packable<B> for (S, T, U, V, W, X, Y, Z)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, T: Packable<B>> Packable<B> for Option<T>

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, T: Packable<B>, U: Packable<B>, V: Packable<B>, W: Packable<B>, X: Packable<B>, Y: Packable<B>, Z: Packable<B>> Packable<B> for (T, U, V, W, X, Y, Z)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, T: Packable<B>, const N: usize> Packable<B> for [T; N]

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, U: Packable<B>, V: Packable<B>, W: Packable<B>, X: Packable<B>, Y: Packable<B>, Z: Packable<B>> Packable<B> for (U, V, W, X, Y, Z)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, V: Packable<B>, W: Packable<B>, X: Packable<B>, Y: Packable<B>, Z: Packable<B>> Packable<B> for (V, W, X, Y, Z)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, W: Packable<B>, X: Packable<B>, Y: Packable<B>, Z: Packable<B>> Packable<B> for (W, X, Y, Z)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, X: Packable<B>, Y: Packable<B>, Z: Packable<B>> Packable<B> for (X, Y, Z)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, Y: Packable<B>, Z: Packable<B>> Packable<B> for (Y, Z)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Source§

impl<B: Buffer, Z: Packable<B>> Packable<B> for (Z,)

Source§

const SIZE: u32

Source§

fn pack(&self) -> B

Source§

fn unpack(buffer: B) -> Self

Implementors§