Skip to main content

Serialize

Trait Serialize 

Source
pub trait Serialize: Sized {
    const MODE: DataType;
    const SIZE: usize = _;
    const ALIGN: usize = _;
    const ALIGNR: usize = _;
    const ALIGN_MASK: usize = _;

    // Required methods
    fn size_hint(&self) -> usize;
    fn write_to<B>(&self, buffer: &mut B) -> usize
       where B: Buffer;
    fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
       where B: Buffer;
    fn is_absent(&self) -> bool;

    // Provided method
    fn tag(&self) -> u8 { ... }
}
Expand description

Trait for values that can be written into a Buffer.

The proc-macro generates implementations for every #[derive(Table)] and #[derive(Flat)] struct; built-in implementations cover all primitive scalars, String, &str, Vec<T>, &[T], and ListView<T>.

Required Associated Constants§

Source

const MODE: DataType

Whether this value is written inline at the field position (Inline) or whether a 32-bit forward offset to the value is written instead (Offset). This constant is inspected by parent arrays and tables at compile time to choose the correct write strategy.

Provided Associated Constants§

Source

const SIZE: usize = _

Source

const ALIGN: usize = _

Source

const ALIGNR: usize = _

Source

const ALIGN_MASK: usize = _

Required Methods§

Source

fn size_hint(&self) -> usize

Upper bound on the number of bytes this value and its alignment padding will consume. Used by write_to and parent tables to call ensure_capacity before entering the unchecked fast path.

Source

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Write this value into buffer, calling ensure_capacity first. Returns the slot of the outermost written unit (length prefix for arrays, value start for scalars).

Source

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Write this value into buffer without checking capacity first.

§Safety

The caller must guarantee that buffer.head() > size_hint() before calling this method. Violating this will cause a panic on the slice indexing inside, or — if debug_assert is disabled — silent memory corruption.

Source

fn is_absent(&self) -> bool

Return true if this value is the default/zero/absent state.

The proc-macro uses this to skip writing absent fields to the table object and leave their vtable entry as 0 (absent marker). Scalars are absent when equal to 0; strings/arrays when empty; tables when all their own fields are absent.

Provided Methods§

Source

fn tag(&self) -> u8

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 Serialize for &str

Source§

fn size_hint(&self) -> usize

len + 11 — 4 bytes for length prefix, 4 bytes alignment, 3 bytes worst-case padding before the u32 align step.

Source§

const SIZE: usize = 4

Source§

const ALIGN: usize = 4

Source§

const MODE: DataType = DataType::Offset

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for f32

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for f64

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for i8

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for i16

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for i32

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for i64

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for i128

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for u8

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for u16

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for u32

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for u64

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for u128

Source§

fn size_hint(&self) -> usize

size_of + align_of - 1 — worst-case bytes including alignment padding.

Source§

const MODE: DataType = DataType::Inline

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl Serialize for String

Source§

fn size_hint(&self) -> usize

len + 11 — 4 bytes for length prefix, 4 bytes alignment, 3 bytes worst-case padding before the u32 align step.

Source§

const SIZE: usize = 4

Source§

const ALIGN: usize = 4

Source§

const MODE: DataType = DataType::Offset

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl<T> Serialize for &[T]
where T: Serialize,

Serialize for &[T].

Arrays are always DataType::Offset from the parent table’s perspective — the table writes a forward offset to the array regardless of whether the elements are inline or offset themselves.

§Case A — Inline elements (T::MODE == Inline)

All elements are written as a single packed memcpy (using from_raw_parts to reinterpret the slice as bytes), followed by a u32 length prefix. The element size and alignment are determined at compile time via size_of and align_of.

§Case B — Offset elements (T::MODE == Offset)

Elements are written in reverse order (last element first, at the high end) so that element 0 ends up at the lowest address, matching iteration order. A forward-offset table (n × u32) is then written pointing to each element’s slot. Finally the u32 length prefix is written before the offset table.

Source§

const SIZE: usize = 4

Source§

const ALIGN: usize = 4

Source§

const MODE: DataType = DataType::Offset

Source§

fn size_hint(&self) -> usize

Source§

fn write_to<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, buffer: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Source§

impl<T> Serialize for Vec<T>
where T: Serialize,

Serialize for Vec<T> — delegates to the &[T] implementation.

Source§

const SIZE: usize = 4

Source§

const MODE: DataType = DataType::Offset

Source§

fn size_hint(&self) -> usize

Source§

fn write_to<B>(&self, b: &mut B) -> usize
where B: Buffer,

Source§

fn write_to_unchecked<B>(&self, b: &mut B) -> usize
where B: Buffer,

Source§

fn is_absent(&self) -> bool

Implementors§

Source§

impl<'a, T> Serialize for FileBlobView<'a, T>
where T: FileKind,

Source§

const SIZE: usize = 4

Source§

const ALIGN: usize = 4

Source§

const MODE: DataType = DataType::Offset

Source§

impl<'a, T> Serialize for ListView<'a, T>
where T: ReadAt<'a>, <T as ReadAt<'a>>::ReadOutput: Serialize,

Serialize for ListView — enables zero-copy re-serialization of a view directly into a new buffer without materializing a Vec.

For inline element types, the source bytes are memcopied directly from the view’s backing buffer slice — no element-by-element dispatch. For offset types, the offset table must be rewritten because absolute positions change in the destination buffer; element values are re-serialized individually.

Source§

const SIZE: usize = 4

Source§

const MODE: DataType = DataType::Offset

Source§

impl<T> Serialize for FileBlob<T>
where T: FileKind,

Source§

const SIZE: usize = 4

Source§

const ALIGN: usize = 4

Source§

const MODE: DataType = DataType::Offset