pub trait FromBytes<'a> {
const SLICE_COUNT: usize;
// Required method
fn from_bytes(bytes: &mut impl Iterator<Item = &'a [u8]>) -> Self;
// Provided methods
fn from_store(store: &DecodedStore<'a>, offset: &mut usize) -> Self
where Self: Sized { ... }
fn element_sizes(sizes: &mut Vec<usize>) -> Result<(), String> { ... }
fn validate(slices: &[(&[u64], u8)]) -> Result<(), String>
where Self: Sized { ... }
}Expand description
A type that can be reconstituted from byte slices with lifetime 'a.
Implementors of this trait almost certainly reference the lifetime 'a themselves,
unless they actively deserialize the bytes (vs sit on the slices, as if zero-copy).
§Overriding methods
The only required method is from_bytes. However, the default
implementation of from_store falls back through from_bytes,
which contains panicking operations that prevent LLVM from eliminating unused fields.
Implementors should override from_store and element_sizes
to ensure optimal codegen. Missing overrides are functionally correct but silently
degrade performance. The #[derive(Columnar)] macro generates all overrides
automatically.
Required Associated Constants§
Sourceconst SLICE_COUNT: usize
const SLICE_COUNT: usize
The number of byte slices this type consumes when reconstructed.
Required Methods§
Sourcefn from_bytes(bytes: &mut impl Iterator<Item = &'a [u8]>) -> Self
fn from_bytes(bytes: &mut impl Iterator<Item = &'a [u8]>) -> Self
Reconstructs self from a sequence of correctly aligned and sized bytes slices.
The implementation is expected to consume the right number of items from the iterator,
which may go on to be used by other implementations of FromBytes.
The implementation should aim for only doing trivial work, as it backs calls like
borrow for serialized containers.
Implementations should almost always be marked as #[inline(always)] to ensure that
they are inlined. A single non-inlined function on a tree of from_bytes calls
can cause the performance to drop significantly.
Provided Methods§
Sourcefn from_store(store: &DecodedStore<'a>, offset: &mut usize) -> Selfwhere
Self: Sized,
fn from_store(store: &DecodedStore<'a>, offset: &mut usize) -> Selfwhere
Self: Sized,
Reconstructs self from a DecodedStore,
using direct random access at a given offset.
Each field indexes directly into the store at its compile-time-known offset, with no iterator state or sequential dependency. This enables LLVM to fully eliminate unused fields.
Sourcefn element_sizes(sizes: &mut Vec<usize>) -> Result<(), String>
fn element_sizes(sizes: &mut Vec<usize>) -> Result<(), String>
Reports the element sizes (in bytes) for each slice this type consumes.
Implementors should override this to report their actual element sizes.
For example, &[u32] pushes 4, while a tuple delegates to each field.
The default returns Err, so that validate rejects
data for types that have not implemented this method.
Sourcefn validate(slices: &[(&[u64], u8)]) -> Result<(), String>where
Self: Sized,
fn validate(slices: &[(&[u64], u8)]) -> Result<(), String>where
Self: Sized,
Validates that the given slices are compatible with this type.
The input provides (&[u64], u8) pairs: each is a word slice
and trailing byte count. This type consumes Self::SLICE_COUNT entries and checks
that each slice’s byte length is a multiple of its element size.
Built from Self::element_sizes; generally should not need to be overridden.
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.