Skip to main content

BlockWriter

Struct BlockWriter 

Source
pub struct BlockWriter { /* private fields */ }
Expand description

TLV field serializer for block bodies.

BlockWriter accumulates tag-length-value encoded fields into an internal byte buffer. It mirrors the field encoding convention from bcp_types::fields but wraps it in a stateful builder that tracks the buffer and provides a clean finish() hand-off.

This is an internal implementation detail of the encoder — it is not part of the public API. Each block type’s serialization calls into BlockWriter to produce the body bytes that get framed by BlockFrame.

Wire format per field:

┌─────────────────┬──────────────────┬────────────────────────┐
│ field_id (varint)│ wire_type (varint)│ payload (varies)      │
├─────────────────┼──────────────────┼────────────────────────┤
│                 │ 0 (Varint)       │ value (varint)         │
│                 │ 1 (Bytes)        │ length (varint) + data │
│                 │ 2 (Nested)       │ length (varint) + data │
└─────────────────┴──────────────────┴────────────────────────┘

Implementations§

Source§

impl BlockWriter

Source

pub fn new() -> Self

Create a new writer with an empty buffer.

Source

pub fn with_capacity(capacity: usize) -> Self

Create a new writer with a pre-allocated buffer capacity.

Use this when you can estimate the final body size to avoid intermediate reallocations.

Source

pub fn write_varint_field(&mut self, field_id: u64, value: u64)

Write a varint field (wire type 0).

Encodes: field_id (varint) | 0 (varint) | value (varint)

Source

pub fn write_bytes_field(&mut self, field_id: u64, value: &[u8])

Write a bytes field (wire type 1).

Encodes: field_id (varint) | 1 (varint) | length (varint) | data [length]

Strings are encoded as bytes fields with UTF-8 content — there is no distinct string wire type.

Source

pub fn write_nested_field(&mut self, field_id: u64, nested: &[u8])

Write a nested field (wire type 2).

Encodes: field_id (varint) | 2 (varint) | length (varint) | nested [length]

The nested bytes are themselves a sequence of TLV-encoded fields, pre-serialized by the caller. This enables recursive structures like FileEntry children and DiffHunk sequences.

Source

pub fn finish(self) -> Vec<u8>

Consume the writer and return the accumulated bytes.

After calling finish(), the writer is consumed. The returned Vec<u8> is the complete TLV-encoded body ready to be wrapped in a BlockFrame.

Trait Implementations§

Source§

impl Default for BlockWriter

Source§

fn default() -> Self

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.