0083. Codec wire
rules and vec.to_bytes
- Status: Accepted. Binary header superseded by ADR-0188.
- Date: 2026-11-01
- Plan: M9.6
- Review: Data, schemas and wire formats
Context
Codec generators need deterministic wire rules and a way to construct arbitrary bytes. String literals cannot construct a byte >= 0x80; existing bytes operations only expose, slice or concatenate bytes that already exist.
Decision
Add the kernel method
vec<u8>.to_bytes() -> bytes. It copies every
element into fresh storage in O(n), including NULs, high bytes and
empty inputs. It performs no UTF-8 validation. Other element types
retain the existing method-not-found error. The IR intrinsic has
class Fresh, uses ordinary destination/region placement and
charges its byte buffer to the interpreter sandbox heap budget.
The C helper is feature-gated and enabled only when the intrinsic
occurs in emitted functions; programs without it retain
byte-identical emitted C.
M9.6's path-dependent codec package will implement these wire rules:
- JSON uses decimal integers in their declared widths, lowercase
hex bytes, required option keys with null for none, and enums as
"Empty"or{"Circle":[3]}. Fused parsers reject duplicate/unknown/missing keys, invalid UTF-8, trailing input, fractions/exponents, leading zeros, -0 and overflow. Decode escapes before comparing keys; reject malformed Unicode surrogates. - Binary uses unsigned LEB128, declared-width zigzag signed LEB128, and raw u8. Strings/bytes carry byte lengths, vectors/maps element counts; fields/tags follow declaration order, options use 0/1. Prefix an eight-byte signature fingerprint. Reject schema mismatches, overlong/out-of-width/truncated values, invalid tags/UTF-8 and trailing data; validate lengths before allocating.
- Both return ordinary error values for malformed input, without trapping.
Alternatives considered
- Construct bytes through str — cannot express arbitrary bytes or invalid UTF-8.
- Reinterpret the vector as a view — complicates ownership and snapshot guarantees across representations; a fresh copy follows existing allocation rules.
- Permissive codec decoders — duplicate keys and alternate integer encodings obscure canonical representations and schema mismatches.
Consequences
Binary output and Unicode escape decoding become expressible in Harness. The primitive adds no diagnostic code or dependency. Step 6.1 documents the method in the vector reference; M9.5 Step 5.8 owns the LANGUAGE.md budget move. Later M9.6 steps implement and test the codec rules. IDs 0081/0082 remain reserved by ADR-0078.