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§
Sourceconst CONVERSION_INFALLIBLE: bool
const CONVERSION_INFALLIBLE: bool
Whether conversion to inline type is guaranteed to never fail
Required Associated Types§
Sourcetype InlineType: Copy + Default + 'static
type InlineType: Copy + Default + 'static
The compact storage type used for inline representation
Required Methods§
Sourcefn to_inline(value: Self) -> Result<Self::InlineType, Self>
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.
Sourcefn from_inline(value: Self::InlineType) -> Self
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.
Sourcefn to_inline_unchecked(value: Self) -> Self::InlineType
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.