Trait Storable

Source
pub trait Storable: Sized + Copy {
    // Required methods
    fn to_word(self) -> u64;
    fn from_word(word: u64) -> Self;
}
Expand description

A trait for types that can be stored in a variable-length compressed vector.

This trait provides a bidirectional, lossless conversion between a user-facing element type (e.g., i32, u16) and the u64 representation required by the underlying compression codecs.

§Zig-Zag Encoding for Signed Integers

For signed integer types, this trait’s implementation automatically applies Zig-Zag encoding. This is a transformation that maps signed integers to unsigned integers in a way that is efficient for variable-length compression.

It works by mapping small positive and negative numbers to small unsigned numbers, as shown below:

Original SignedZig-Zag Unsigned
00
-11
12
-23
24

This ensures that values close to zero, whether positive or negative, are represented by small unsigned integers, which can then be compressed into very few bits by the variable-length codecs.

Required Methods§

Source

fn to_word(self) -> u64

Converts the element into its u64 storage representation.

For unsigned types, this is a simple cast. For signed types, this applies Zig-Zag encoding.

Source

fn from_word(word: u64) -> Self

Converts a u64 storage word back into the element type.

For unsigned types, this is a simple cast. For signed types, this decodes the Zig-Zag encoded value.

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 Storable for i8

Source§

fn to_word(self) -> u64

Source§

fn from_word(word: u64) -> Self

Source§

impl Storable for i16

Source§

fn to_word(self) -> u64

Source§

fn from_word(word: u64) -> Self

Source§

impl Storable for i32

Source§

fn to_word(self) -> u64

Source§

fn from_word(word: u64) -> Self

Source§

impl Storable for i64

Source§

fn to_word(self) -> u64

Source§

fn from_word(word: u64) -> Self

Source§

impl Storable for u8

Source§

fn to_word(self) -> u64

Source§

fn from_word(word: u64) -> Self

Source§

impl Storable for u16

Source§

fn to_word(self) -> u64

Source§

fn from_word(word: u64) -> Self

Source§

impl Storable for u32

Source§

fn to_word(self) -> u64

Source§

fn from_word(word: u64) -> Self

Source§

impl Storable for u64

Source§

fn to_word(self) -> u64

Source§

fn from_word(word: u64) -> Self

Implementors§