Skip to main content

Module codec

Module codec 

Source
Expand description

The byte codec both surfaces share. One writer, one reader — the host links these functions and so does every guest, so a field cannot be encoded one way and decoded another (LAW 5, by construction rather than by a guard watching two copies agree).

§Why not JSON

znippy’s wasm_loader::parse_json_to_row splits on , and : before stripping quotes, so a value containing either separator silently corrupts the row, and it has no null representation at all — an absent column and an empty one are the same bytes on the wire. Both are properties of the ad-hoc format, not bugs that can be patched out of the parser.

This codec is length-prefixed, so a value is copied by length and its contents are never scanned for structure: ,, :, ", a NUL and an arbitrary UTF-8 sequence all survive a round trip. Option carries an explicit 1-byte tag, so None and Some("") are distinct on the wire. codec_tests.rs asserts exactly those two properties.

§Layout

typebytes
u32 / u16 / u64 / i64little-endian, fixed width
bool1 byte, 0 or 1
bytes / Stringu32 length, then that many bytes
Option<T>0u8, or 1u8 then T
Vec<T>u32 count, then that many T
Result<T, String>0u8 then T, or 1u8 then the message

Structs§

Reader
Cursor over a byte string.
Writer
Append-only byte writer.

Enums§

DecodeError
Why a byte string could not be decoded. Every variant names the field being read, because a decode failure on the host side is otherwise indistinguishable from a plugin that returned nothing.

Functions§

read_result
Decode a Result<T, String> written by write_result.
write_result
Encode a Result<T, String>: tag 0 = ok, tag 1 = the error message.