thrifty/writer_highlevel
Types
Immutable builder for encoding Thrift Compact structs.
The writer keeps track of the last emitted field id so that subsequent field headers can use the compact delta encoding.
pub type StructWriter {
StructWriter(last_field_id: Int, buffer: writer.Buffer)
}
Constructors
-
StructWriter(last_field_id: Int, buffer: writer.Buffer)
Values
pub fn finish(builder: StructWriter) -> BitArray
Finalise the StructWriter returning a compact protocol encoded BitArray.
Outputs
- A
BitArraycontaining the concatenated headers and payloads for the struct, with a stop field appended automatically.
pub fn new() -> StructWriter
Create a new empty StructWriter.
Outputs
- A
StructWriterinitialised with last field id0and an empty buffer.
Semantics
- The writer tracks
last_field_idto emit compact delta-encoded headers.
pub fn write_binary(
builder: StructWriter,
field_id: Int,
bytes: BitArray,
) -> StructWriter
Append a binary/blob field to the struct under construction.
Inputs
builder: currentStructWriterstate.field_id: field identifier.bytes: raw payload asBitArray.
Outputs
- Returns an updated
StructWriterwith header and length-prefixed payload appended.
pub fn write_bool(
builder: StructWriter,
field_id: Int,
value: Bool,
) -> StructWriter
Append a boolean field to the struct under construction.
Inputs
builder: currentStructWriterstate.field_id: field identifier.value: boolean value to encode.
Outputs
- Returns an updated
StructWriterwith the boolean field header appended.
Semantics
- The boolean is encoded inline in the field header; no payload bytes are appended beyond the header itself.
pub fn write_double(
builder: StructWriter,
field_id: Int,
value: Float,
) -> StructWriter
Append a double (64-bit float) field to the struct under construction.
Inputs
builder: currentStructWriterstate.field_id: field identifier.value: floating point value to store.
Outputs
- Returns an updated
StructWriterwith the encoded field appended.
pub fn write_field_bytes(
builder: StructWriter,
field_id: Int,
field_type: types.FieldType,
payload: BitArray,
) -> StructWriter
Write an arbitrary field by supplying the field type and the payload bytes.
This is useful for struct fields encoded by custom logic (e.g. nested structs) while still benefiting from automatic delta handling.
pub fn write_i16(
builder: StructWriter,
field_id: Int,
value: Int,
) -> StructWriter
Append an i16 field to the struct under construction.
Inputs
builder: currentStructWriterstate.field_id: field identifier.value: signed integer to encode as i16 (via zigzag + varint semantics).
Outputs
- Returns an updated
StructWriterwith header and encoded payload appended.
pub fn write_i32(
builder: StructWriter,
field_id: Int,
value: Int,
) -> Result(StructWriter, zigzag.ZigzagRangeError)
Append an i32 field to the struct under construction.
Inputs
builder: currentStructWriterstate.field_id: field identifier.value: signed integer to encode as i32.
Outputs
Ok(StructWriter)with the new state on success.Error(ZigzagRangeError)whenvaluedoes not fit in i32 range.
pub fn write_i64(
builder: StructWriter,
field_id: Int,
value: Int,
) -> Result(StructWriter, zigzag.ZigzagRangeError)
Append an i64 field to the struct under construction.
Inputs
builder: currentStructWriterstate.field_id: field identifier.value: signed integer to encode as i64.
Outputs
Ok(StructWriter)with the new state on success.Error(ZigzagRangeError)whenvaluedoes not fit in i64 range.
pub fn write_i8(
builder: StructWriter,
field_id: Int,
value: Int,
) -> StructWriter
Append an i8 field to the struct under construction.
Inputs
builder: currentStructWriterstate.field_id: field identifier.value: 8-bit signed integer to encode.
Outputs
- Returns an updated
StructWriterwith the field header and payload appended.
pub fn write_list(
builder: StructWriter,
field_id: Int,
size: Int,
element_type: container.ElementType,
payload: BitArray,
) -> StructWriter
Write a list field using a pre-encoded payload of elements.
The payload must contain the concatenated element encodings. The size is used to emit the list header with the correct short/long encoding.
pub fn write_map(
builder: StructWriter,
field_id: Int,
size: Int,
key_type: container.ElementType,
value_type: container.ElementType,
payload: BitArray,
) -> StructWriter
Write a map field using a pre-encoded payload of key/value pairs.
The payload must contain the concatenated encodings for keys and values in sequence.
pub fn write_string(
builder: StructWriter,
field_id: Int,
value: String,
) -> StructWriter
Append a UTF-8 string field to the struct under construction.
Inputs
builder: currentStructWriterstate.field_id: field identifier.value: UTF-8 string to encode.
Outputs
- Returns an updated
StructWriterwith the encoded string appended.