thrifty/writer

Types

Buffer accumulator used by the high-level writer for struct assembly.

pub type Buffer {
  Buffer(parts: List(BitArray))
}

Constructors

  • Buffer(parts: List(BitArray))

Values

pub fn buffer_append(buffer: Buffer, part: BitArray) -> Buffer

Append a part to an existing Buffer accumulator.

Inputs

  • buffer: existing Buffer.
  • part: BitArray to append.

Outputs

  • New Buffer with part added to the internal list.
pub fn buffer_new() -> Buffer

Create a new empty buffer accumulator for high-level writer assembly.

pub fn buffer_to_bitarray(buffer: Buffer) -> BitArray

Convert an accumulated Buffer into a contiguous BitArray.

Outputs

  • Concatenated BitArray in original append order.
pub fn write_binary(bytes: BitArray) -> BitArray

Write a length-prefixed binary blob.

pub fn write_bool(
  field_id: Int,
  value: Bool,
  last_field_id: Int,
) -> BitArray

Write a boolean field using the inline field header encoding.

pub fn write_bool_inline(
  field_id: Int,
  value: Bool,
  last_field_id: Int,
) -> BitArray

Write a boolean field using the inline header helper.

pub fn write_double(value: Float) -> BitArray

Write a 64-bit little-endian IEEE‑754 float value.

Inputs

  • value: floating point number to encode.

Outputs

  • BitArray of 8 bytes containing the IEEE‑754 little-endian encoding.
pub fn write_field_header(
  field_id: Int,
  field_type: types.FieldType,
  last_field_id: Int,
) -> BitArray

Encode a field header with compact delta encoding logic.

pub fn write_i16(value: Int) -> BitArray

Write an i16 value using zigzag encoding and varint bytes.

Inputs

  • value: signed integer in -32768..32767.

Outputs

  • Returns a BitArray with the encoded bytes (zigzag + varint).

Error modes

  • Panics when value is outside the i16 range.
pub fn write_i32(value: Int) -> BitArray

Write an i32 value using zigzag encoding and varint bytes.

Inputs

  • value: signed integer expected to fit in i32 range.

Outputs

  • Returns a BitArray containing the encoded bytes or panics on overflow.
pub fn write_i32_checked(
  value: Int,
) -> Result(BitArray, zigzag.ZigzagRangeError)

Checked i32 writer that returns an error on overflow.

Inputs

  • value: signed integer to write.

Outputs

  • Ok(BitArray) with encoded bytes on success.
  • Error(ZigzagRangeError) when the value is out of range.
pub fn write_i64(value: Int) -> BitArray

Write an i64 value using zigzag encoding and varint bytes.

Inputs

  • value: signed integer expected to fit in i64 range.

Outputs

  • Returns a BitArray with the encoded bytes or panics on overflow.
pub fn write_i64_checked(
  value: Int,
) -> Result(BitArray, zigzag.ZigzagRangeError)

Checked i64 writer that returns an error on overflow.

Inputs

  • value: signed integer to write.

Outputs

  • Ok(BitArray) with encoded bytes on success.
  • Error(ZigzagRangeError) when the value is out of range.
pub fn write_i8(value: Int) -> BitArray

Write a single signed byte value.

Inputs

  • value: integer in -128..127 that will be represented as a single byte.

Outputs

  • Returns a BitArray of length 1 containing the byte.

Error modes

  • Panics when value is outside -128..127. Use a range check before calling if the value origin is untrusted.
pub fn write_list(
  size: Int,
  element_type: container.ElementType,
  payload: BitArray,
) -> BitArray

Encode a list by combining its header and payload.

pub fn write_list_header(
  size: Int,
  element_type: container.ElementType,
) -> BitArray

Encode a list header.

pub fn write_map(
  size: Int,
  key_type: container.ElementType,
  value_type: container.ElementType,
  payload: BitArray,
) -> BitArray

Encode a map by combining its header and payload.

pub fn write_map_header(
  size: Int,
  key_type: container.ElementType,
  value_type: container.ElementType,
) -> BitArray

Encode a map header.

pub fn write_message_header(
  header: message.MessageHeader,
) -> BitArray

Encode a message header.

pub fn write_string(value: String) -> BitArray

Write a UTF-8 string as length-prefixed binary.

pub fn write_varint(value: Int) -> BitArray

Encode a raw unsigned integer as varint bytes.

Inputs

  • value: non-negative integer to encode.

Outputs

  • Returns a BitArray containing 1..10 bytes of varint-encoded data.
pub fn write_zigzag_i32(value: Int) -> BitArray

Encode an i32 using ZigZag followed by varint encoding.

Inputs

  • value: signed integer expected to fit in 32-bit signed range.

Outputs

  • Returns a BitArray containing the zigzag-then-varint encoding.

Error modes

  • Panics if value does not fit in the i32 range. Prefer write_zigzag_i32_checked/1 to handle overflow explicitly.
pub fn write_zigzag_i32_checked(
  value: Int,
) -> Result(BitArray, zigzag.ZigzagRangeError)

Checked i32 ZigZag encoder that returns the resulting bytes or an error.

Inputs

  • value: signed integer to encode.

Outputs

  • Ok(BitArray) containing the encoded bytes on success.
  • Error(ZigzagRangeError) when value is out of the i32 range.
pub fn write_zigzag_i64(value: Int) -> BitArray

Encode an i64 using ZigZag followed by varint encoding.

Inputs

  • value: signed integer expected to fit in 64-bit signed range.

Outputs

  • Returns a BitArray containing the zigzag-then-varint encoding.

Error modes

  • Panics if value does not fit in the i64 range. Prefer write_zigzag_i64_checked/1 to handle overflow explicitly.
pub fn write_zigzag_i64_checked(
  value: Int,
) -> Result(BitArray, zigzag.ZigzagRangeError)

Checked i64 ZigZag encoder that returns the resulting bytes or an error.

Inputs

  • value: signed integer to encode.

Outputs

  • Ok(BitArray) containing the encoded bytes on success.
  • Error(ZigzagRangeError) when value is out of the i64 range.
Search Document