writeOption<T> method

void writeOption<T>(
  1. T? value,
  2. void writeValue(
    1. T
    )
)

Encodes an optional value

Format: u8 sum-variant tag + value if Some.

Matches SpacetimeDB's canonical Option encoding: some is variant index 0 (tag byte 0x00) and carries the inner payload; none is variant index 1 (tag byte 0x01) with no payload. See crates/sats/src/ser/impls.rs in the upstream repo.

Example:

// Encode Some(42)
encoder.writeOption<int>(42, (v) => encoder.writeU32(v));

// Encode None
encoder.writeOption<int>(null, (v) => encoder.writeU32(v));

Implementation

void writeOption<T>(T? value, void Function(T) writeValue) {
  if (value == null) {
    writeU8(1);
  } else {
    writeU8(0);
    writeValue(value);
  }
}