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
impl BlockWriter
Sourcepub fn with_capacity(capacity: usize) -> Self
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.
Sourcepub fn write_varint_field(&mut self, field_id: u64, value: u64)
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)
Sourcepub fn write_bytes_field(&mut self, field_id: u64, value: &[u8])
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.
Sourcepub fn write_nested_field(&mut self, field_id: u64, nested: &[u8])
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.