pub struct Builder<'p> { /* private fields */ }Expand description
Owns one pooled buffer. Build a message with root(), then:
finished(): borrow it until the nextroot()(no atomics);seal()…take_frames(): batch length-prefixed frames into oneBytes(one refcount per batch — the fast path for senders);flush(): one ownedBytesper message (one refcount per message);finish(): likeflush(), 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>
impl<'p> Builder<'p>
pub fn new(pool: &'p dyn BufPool) -> Self
Sourcepub fn new_enveloped(pool: &'p dyn BufPool) -> Self
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.
Sourcepub fn root(&mut self, fixed_len: u16) -> TableW<'_>
pub fn root(&mut self, fixed_len: u16) -> TableW<'_>
Start the root table of a new message (an unsealed previous message is discarded).
Sourcepub fn root_fixed<const N: usize>(&mut self) -> FixedW<'_, N>
pub fn root_fixed<const N: usize>(&mut self) -> FixedW<'_, N>
Root table with only scalar fields (no pointers) and compile-time size N.
Sourcepub fn finished(&self) -> Result<&[u8], Error>
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().
Sourcepub fn seal(&mut self) -> Result<(), Error>
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.
Sourcepub fn sealed_len(&self) -> usize
pub fn sealed_len(&self) -> usize
Bytes of sealed frames pending in the batch (to ship by size).
Sourcepub fn take_frames(&mut self) -> Bytes
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.
Sourcepub fn flush(&mut self) -> Result<Bytes, Error>
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).
Sourcepub fn seal_envelope(&mut self, schema_id: u64) -> Result<(), Error>
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.
Sourcepub fn flush_envelope(&mut self, schema_id: u64) -> Result<Bytes, Error>
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).