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§
Provided Associated Constants§
const SIZE: usize = _
const ALIGN: usize = _
const ALIGNR: usize = _
const ALIGN_MASK: usize = _
Required Methods§
Sourcefn size_hint(&self) -> usize
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.
Sourcefn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
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).
Sourcefn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
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.
Sourcefn is_absent(&self) -> bool
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§
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
impl Serialize for &str
Source§fn size_hint(&self) -> usize
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.
const SIZE: usize = 4
const ALIGN: usize = 4
const MODE: DataType = DataType::Offset
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for f32
impl Serialize for f32
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for f64
impl Serialize for f64
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for i8
impl Serialize for i8
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for i16
impl Serialize for i16
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for i32
impl Serialize for i32
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for i64
impl Serialize for i64
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for i128
impl Serialize for i128
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for u8
impl Serialize for u8
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for u16
impl Serialize for u16
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for u32
impl Serialize for u32
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for u64
impl Serialize for u64
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for u128
impl Serialize for u128
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
size_of + align_of - 1 — worst-case bytes including alignment padding.
const MODE: DataType = DataType::Inline
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl Serialize for String
impl Serialize for String
Source§fn size_hint(&self) -> usize
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.
const SIZE: usize = 4
const ALIGN: usize = 4
const MODE: DataType = DataType::Offset
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl<T> Serialize for &[T]where
T: Serialize,
Serialize for &[T].
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.
const SIZE: usize = 4
const ALIGN: usize = 4
const MODE: DataType = DataType::Offset
fn size_hint(&self) -> usize
fn write_to<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn write_to_unchecked<B>(&self, buffer: &mut B) -> usizewhere
B: Buffer,
fn is_absent(&self) -> bool
Source§impl<T> Serialize for Vec<T>where
T: Serialize,
Serialize for Vec<T> — delegates to the &[T] implementation.
impl<T> Serialize for Vec<T>where
T: Serialize,
Serialize for Vec<T> — delegates to the &[T] implementation.
Implementors§
Source§impl<'a, T> Serialize for FileBlobView<'a, T>where
T: FileKind,
impl<'a, T> Serialize for FileBlobView<'a, T>where
T: FileKind,
Source§impl<'a, T> Serialize for ListView<'a, T>
Serialize for ListView — enables zero-copy re-serialization of a
view directly into a new buffer without materializing a Vec.
impl<'a, T> Serialize for ListView<'a, T>
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.