pub trait Buffer {
Show 16 methods
// Required methods
fn new(initial_capacity: usize) -> Self;
fn head(&self) -> usize;
fn head_mut(&mut self) -> &mut usize;
fn buffer(&self) -> &[u8] ⓘ;
fn buffer_mut(&mut self) -> &mut [u8] ⓘ;
fn share_vtable(&mut self, vtable: &[u8], table_slot: usize);
fn grow(&mut self, new_cap: usize);
fn load<T>(bytes: &[u8]) -> Self
where T: Table;
fn clear_vtables(&mut self);
// Provided methods
fn reset(&mut self) { ... }
fn len(&self) -> usize { ... }
fn ensure_capacity(&mut self, additional_size: usize) { ... }
fn slot(&self) -> usize { ... }
fn align(&mut self, alignment: usize) { ... }
fn bytes(&self) -> &[u8] ⓘ { ... }
fn finish(&mut self, slot: usize) -> &[u8] ⓘ { ... }
}Expand description
Abstraction over a backward-growing byte buffer used during serialization and merging.
The contract: data is written from len() toward 0. head() is the
current write frontier. slot() = len() - head() is a stable handle
to the last object written — it remains valid even if grow() is called
and the entire allocation is shifted.
All provided methods are implemented in terms of the required primitives;
implementors only need to supply new, head/head_mut,
buffer/buffer_mut, grow, clear_vtables, share_vtable, and
load.
Required Methods§
Sourcefn new(initial_capacity: usize) -> Self
fn new(initial_capacity: usize) -> Self
Construct a new buffer with at least initial_capacity bytes available
for writing.
Sourcefn head_mut(&mut self) -> &mut usize
fn head_mut(&mut self) -> &mut usize
Return a mutable reference to the write frontier so callers can
advance it with *buf.head_mut() -= n.
Sourcefn buffer(&self) -> &[u8] ⓘ
fn buffer(&self) -> &[u8] ⓘ
Return the full backing byte slice (including the unwritten low region).
Sourcefn buffer_mut(&mut self) -> &mut [u8] ⓘ
fn buffer_mut(&mut self) -> &mut [u8] ⓘ
Return a mutable view of the full backing byte slice.
Write or look up a vtable and patch the jump field of the table object
at table_slot. See DefaultBuffer::share_vtable for the layout.
Sourcefn grow(&mut self, new_cap: usize)
fn grow(&mut self, new_cap: usize)
Grow the backing allocation to exactly new_cap bytes, shifting all
existing written data toward the high end. Must update head by
new_cap - old_cap.
Sourcefn load<T>(bytes: &[u8]) -> Selfwhere
T: Table,
fn load<T>(bytes: &[u8]) -> Selfwhere
T: Table,
Deserialize a finished flatbuffer into this buffer type.
Sourcefn clear_vtables(&mut self)
fn clear_vtables(&mut self)
Clear the vtable deduplication cache. Called by reset.
Provided Methods§
Sourcefn reset(&mut self)
fn reset(&mut self)
Reset the buffer for reuse without freeing the backing allocation.
Resets head to len() (as if the buffer were freshly allocated) and
clears the vtable cache. The bytes below head are now garbage — the
Cook-Mertz contract guarantees they will not be read before being
overwritten.
Called automatically by merge_into on entry so the caller only needs
to allocate once and reuse across many merge calls.
Sourcefn ensure_capacity(&mut self, additional_size: usize)
fn ensure_capacity(&mut self, additional_size: usize)
Ensure additional_size bytes are available below head, growing the
buffer if necessary.
Sourcefn slot(&self) -> usize
fn slot(&self) -> usize
Distance from the end of the buffer to the current write frontier.
This is the stable “slot” address of the most-recently-written object.
It remains valid across grow() calls because growing shifts data
right by exactly the amount head is incremented.
Sourcefn align(&mut self, alignment: usize)
fn align(&mut self, alignment: usize)
Align head downward to alignment bytes (must be a power of two).
Sourcefn bytes(&self) -> &[u8] ⓘ
fn bytes(&self) -> &[u8] ⓘ
The finished, readable byte slice starting at head.
Only valid after finish() has been called to write the root prefix.
Sourcefn finish(&mut self, slot: usize) -> &[u8] ⓘ
fn finish(&mut self, slot: usize) -> &[u8] ⓘ
Write the 4-byte root prefix and return the finished buffer slice.
The root prefix is a u32 at the lowest address that encodes
table_pos - prefix_pos, i.e. the byte distance from the prefix to
the root table object. Readers call read_root to find the table.
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<B> Buffer for &mut Bwhere
B: Buffer,
Forwarding impl so that merge_inline_list and friends, which are generic
over B: Buffer and receive out: &mut B, can call Buffer methods
without an extra wrapper type.
impl<B> Buffer for &mut Bwhere
B: Buffer,
Forwarding impl so that merge_inline_list and friends, which are generic
over B: Buffer and receive out: &mut B, can call Buffer methods
without an extra wrapper type.
new and load are not meaningful on a mutable reference and will panic
if called — callers must always allocate the owned B first and pass a
&mut to the helpers.