CompactStorage

Trait CompactStorage 

Source
pub trait CompactStorage: Copy + 'static {
    type InlineType: Copy + Default + 'static;

    const CONVERSION_INFALLIBLE: bool;

    // Required methods
    fn to_inline(value: Self) -> Result<Self::InlineType, Self>;
    fn from_inline(value: Self::InlineType) -> Self;
    fn to_inline_unchecked(value: Self) -> Self::InlineType;
}
Expand description

A trait for types that can be stored compactly by attempting to fit them into smaller storage types.

This trait is designed for scenarios where values are often, but not always, small enough to fit in a more compact representation (e.g., storing i32 values that typically fit in i8 to save memory).

Required Associated Constants§

Source

const CONVERSION_INFALLIBLE: bool

Whether conversion to inline type is guaranteed to never fail

Required Associated Types§

Source

type InlineType: Copy + Default + 'static

The compact storage type used for inline representation

Required Methods§

Source

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Attempts to convert the value to its compact inline representation. Returns Err(value) if the value cannot fit in the inline type.

Source

fn from_inline(value: Self::InlineType) -> Self

Converts from the compact inline representation back to the original type. This conversion is always infallible as we’re expanding to a larger type.

Source

fn to_inline_unchecked(value: Self) -> Self::InlineType

Converts to inline representation without bounds checking.

§Safety

The caller must guarantee that the value fits within the bounds of InlineType. Violating this contract may result in data truncation or undefined behavior.

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

Source§

const CONVERSION_INFALLIBLE: bool = true

Source§

type InlineType = i8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Source§

impl CompactStorage for i16

Source§

const CONVERSION_INFALLIBLE: bool = false

Source§

type InlineType = i8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Source§

impl CompactStorage for i32

Source§

const CONVERSION_INFALLIBLE: bool = false

Source§

type InlineType = i8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Source§

impl CompactStorage for i64

Source§

const CONVERSION_INFALLIBLE: bool = false

Source§

type InlineType = i8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Source§

impl CompactStorage for i128

Source§

const CONVERSION_INFALLIBLE: bool = false

Source§

type InlineType = i8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Source§

impl CompactStorage for isize

Source§

const CONVERSION_INFALLIBLE: bool = false

Source§

type InlineType = i8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Source§

impl CompactStorage for u8

Source§

const CONVERSION_INFALLIBLE: bool = true

Source§

type InlineType = u8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Source§

impl CompactStorage for u16

Source§

const CONVERSION_INFALLIBLE: bool = false

Source§

type InlineType = u8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Source§

impl CompactStorage for u32

Source§

const CONVERSION_INFALLIBLE: bool = false

Source§

type InlineType = u8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Source§

impl CompactStorage for u64

Source§

const CONVERSION_INFALLIBLE: bool = false

Source§

type InlineType = u8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Source§

impl CompactStorage for u128

Source§

const CONVERSION_INFALLIBLE: bool = false

Source§

type InlineType = u8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Source§

impl CompactStorage for usize

Source§

const CONVERSION_INFALLIBLE: bool = false

Source§

type InlineType = u8

Source§

fn to_inline(value: Self) -> Result<Self::InlineType, Self>

Source§

fn from_inline(value: Self::InlineType) -> Self

Source§

fn to_inline_unchecked(value: Self) -> Self::InlineType

Implementors§