Skip to main content

Module binary

Module binary 

Source
Expand description

The binary form — self-describing, compact: what a build compiles content to, what saves and caches are kept in, what a reader that doesn’t know the schema can still walk. docs/format.md (“Binary form”) has the layout: a header (SDAT, a version byte), then the root value, a tag byte and its payload. A string is written where it’s first used and referred to after that, so repeated keys and ids cost one varint each and the writer never goes back over what it wrote.

Nothing is aligned, and decoding checks every length against the input, so malformed bytes are an error, never a panic or a huge allocation. A crate::Schema writes itself here straight (crate::Schema::encode) and reads itself back the same way (crate::Schema::decode) — no value tree on the way.

Modules§

tag
A value’s first byte: what it is.

Structs§

DecodeError
Malformed binary data.
Decoder
Reads the binary form. The step-by-step reads (Decoder::int, Decoder::map …) answer None for anything but what crate::Schema::encode writes there — the schema then reads the slow way, which says what’s wrong.
Writer
Writes the binary form piece by piece, from data that isn’t a Value — nothing is built or copied on the way. A map or list is given its length, then that many entries (key then a value) or values.

Constants§

INLINE_INT_MAX
The largest int a tag of its own holds.
INLINE_INT_MIN
The smallest int a tag of its own holds (crate::binary::tag::INT_INLINE).
LEGACY_VERSION
The first version, still read and never written: a v1 document is a v2 one that uses only the tags 0..=14 (the reader checks that it does).
MAGIC
What a binary file starts with.
MAX_DEPTH
Nested deeper than this is malformed.
NEW_STRING
A string’s varint: a new one’s (odd, its length) or a reference to the table (even, its index).
STREAM_LEN_BYTES
A stream’s length: fixed width, so the writer fills it in after its items.
STR_INDEX_COUNT
How many table strings a tag of their own can name (crate::binary::tag::STR_INDEX).
VERSION
The version of the form after the magic that every writer writes.

Functions§

decode
Reads the binary form.
decode_ref
Reads the binary form borrowed: every string points into bytes, lists and maps go in arena — nothing is copied.
encode
value in the binary form.
encode_into
value in the binary form, appended to out (a whole document, header first): a buffer that is kept and reused grows once.
upgrade
A document of version 1 or 2 as a version 2 one: what reading an old file and saving it again does. Nothing is lost: the value read is the value written, and every value version 1 holds has a version 2 form (a crate::Stream is the list of its items, as it is read anywhere without a schema).