Builder

Struct Builder 

Source
pub struct Builder<'p> { /* private fields */ }
Expand description

Owns one pooled buffer. Build a message with root(), then:

  • finished(): borrow it until the next root() (no atomics);
  • seal() … take_frames(): batch length-prefixed frames into one Bytes (one refcount per batch — the fast path for senders);
  • flush(): one owned Bytes per message (one refcount per message);
  • finish(): like flush(), and the buffer goes back to the pool.

Every message is written after a 4-byte slot that seal() fills with its length, so frames are [u32 len][message] with no extra copy.

A builder made with Builder::new_enveloped reserves a HEADER_LEN-byte slot instead, which flush_envelope() / seal_envelope() fill with an envelope header (the CRC is appended after the message) — again without copying the payload. finished() / flush() still return the bare message in either mode; seal() only works in the default mode and seal_envelope() / flush_envelope() only in the enveloped one (otherwise: panic “builder mode mismatch”).

Implementations§

Source§

impl<'p> Builder<'p>

Source

pub fn new(pool: &'p dyn BufPool) -> Self

Source

pub fn new_enveloped(pool: &'p dyn BufPool) -> Self

A builder whose messages can be emitted as envelopes (crate::envelope) with flush_envelope() / seal_envelope() without copying: a HEADER_LEN-byte slot is reserved before each one.

Source

pub fn root(&mut self, fixed_len: u16) -> TableW<'_>

Start the root table of a new message (an unsealed previous message is discarded).

Source

pub fn root_fixed<const N: usize>(&mut self) -> FixedW<'_, N>

Root table with only scalar fields (no pointers) and compile-time size N.

Source

pub fn finished(&self) -> Result<&[u8], Error>

Borrow the current message without freezing it (no refcount traffic). Valid until the next root() — the equivalent of FlatBuffers’ finished_data().

Source

pub fn seal(&mut self) -> Result<(), Error>

Seal the current message as a frame [u32 len][message] in the pending batch. No atomics; a no-op when there is no current message.

Source

pub fn sealed_len(&self) -> usize

Bytes of sealed frames pending in the batch (to ship by size).

Source

pub fn take_frames(&mut self) -> Bytes

Freeze every sealed frame as one Bytes (one refcount for the whole batch) and start an empty batch. An unsealed message is discarded. Split the result back into messages with crate::frames.

Source

pub fn flush(&mut self) -> Result<Bytes, Error>

Freeze the current message alone (without its length slot) and keep the builder. One refcount per message. Panics if sealed frames are pending (call take_frames first).

Source

pub fn seal_envelope(&mut self, schema_id: u64) -> Result<(), Error>

Seal the current message as an envelope in the pending batch, so take_frames() returns concatenated envelopes (split them with envelope::envelopes). A no-op when there is no current message. Panics with “builder mode mismatch” unless built with new_enveloped.

Source

pub fn flush_envelope(&mut self, schema_id: u64) -> Result<Bytes, Error>

Freeze the current message as one envelope (header + message + CRC) and keep the builder; the payload is not copied. Returns an empty Bytes when there is no current message. Panics with “builder mode mismatch” unless built with new_enveloped, and if sealed frames are pending (call take_frames first).

Source

pub fn finish(self) -> Result<Bytes, Error>

Freeze the message; the buffer goes back to the pool.

Trait Implementations§

Source§

impl Drop for Builder<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'p> Freeze for Builder<'p>

§

impl<'p> !RefUnwindSafe for Builder<'p>

§

impl<'p> !Send for Builder<'p>

§

impl<'p> !Sync for Builder<'p>

§

impl<'p> Unpin for Builder<'p>

§

impl<'p> !UnwindSafe for Builder<'p>

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.