Skip to main content

DefaultBuffer

Struct DefaultBuffer 

Source
pub struct DefaultBuffer {
    pub buffer: Vec<u8>,
    pub head: usize,
    pub vtables: HashMap<Box<[u8]>, usize, BuildVTableHasher>,
}
Expand description

The standard Buffer implementation.

§Fields

  • buffer — the raw backing allocation. Data grows from the high end toward the low end; bytes below head are uninitialized or stale and must never be read.
  • head — index of the first byte of valid data. Always satisfies head <= buffer.len().
  • vtables — deduplication map from vtable bytes to the slot where that vtable was written. Prevents writing the same vtable multiple times when many table elements share identical schemas (the common case).

Fields§

§buffer: Vec<u8>§head: usize§vtables: HashMap<Box<[u8]>, usize, BuildVTableHasher>

Trait Implementations§

Source§

impl Buffer for DefaultBuffer

Source§

fn new(initial_size: usize) -> DefaultBuffer

Allocate an uninitialized buffer of initial_size bytes.

Uses with_capacity + set_len rather than vec![0; n] — see the module-level Cook-Mertz discussion for why zero-init is safe to skip here.

Source§

fn grow(&mut self, new_cap: usize)

Grow the buffer to new_cap bytes, shifting all existing written data to the right so that slot-based offsets remain valid.

Uses reserve + set_len rather than resize(n, 0) for the same reason as new: the new bytes in the low region are never read before being written.

Source§

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

Write vtable to the buffer if not already present, then patch the 4-byte vtable-jump field at the start of the table object.

The vtable jump is a signed 32-bit integer stored at t_pos: jump = vtable_pos - table_pos (negative when vtable is before table)

Multiple table elements with the same schema share one physical vtable copy, which is why the dedup map is essential for space efficiency.

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 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 clear_vtables(&mut self)

Clear the vtable deduplication cache. Called by reset.
Source§

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

Deserialize a finished flatbuffer into this buffer type.
Source§

fn reset(&mut self)

Reset the buffer for reuse without freeing the backing allocation. Read more
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. Read more
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. Read more
Source§

fn finish(&mut self, slot: usize) -> &[u8]

Write the 4-byte root prefix and return the finished buffer slice. Read more
Source§

impl Default for DefaultBuffer

Source§

fn default() -> DefaultBuffer

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.