Skip to main content

Buffer

Trait Buffer 

Source
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§

Source

fn new(initial_capacity: usize) -> Self

Construct a new buffer with at least initial_capacity bytes available for writing.

Source

fn head(&self) -> usize

Return the current write frontier: the index of the first valid byte.

Source

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.

Source

fn buffer(&self) -> &[u8]

Return the full backing byte slice (including the unwritten low region).

Source

fn buffer_mut(&mut self) -> &mut [u8]

Return a mutable view of the full backing byte slice.

Source

fn share_vtable(&mut self, vtable: &[u8], table_slot: usize)

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.

Source

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.

Source

fn load<T>(bytes: &[u8]) -> Self
where T: Table,

Deserialize a finished flatbuffer into this buffer type.

Source

fn clear_vtables(&mut self)

Clear the vtable deduplication cache. Called by reset.

Provided Methods§

Source

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.

Source

fn len(&self) -> usize

Total capacity of the backing allocation in bytes.

Source

fn ensure_capacity(&mut self, additional_size: usize)

Ensure additional_size bytes are available below head, growing the buffer if necessary.

Source

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.

Source

fn align(&mut self, alignment: usize)

Align head downward to alignment bytes (must be a power of two).

Source

fn bytes(&self) -> &[u8]

The finished, readable byte slice starting at head.

Only valid after finish() has been called to write the root prefix.

Source

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 B
where 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.

Source§

fn new(_: usize) -> &mut B

Source§

fn head(&self) -> usize

Source§

fn head_mut(&mut self) -> &mut usize

Source§

fn buffer(&self) -> &[u8]

Source§

fn buffer_mut(&mut self) -> &mut [u8]

Source§

fn grow(&mut self, n: usize)

Source§

fn clear_vtables(&mut self)

Source§

fn share_vtable(&mut self, vt: &[u8], slot: usize)

Source§

fn load<T>(_: &[u8]) -> &mut B
where T: Table,

Implementors§