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§
- Decode
Error - Malformed binary data.
- Decoder
- Reads the binary form. The step-by-step reads (
Decoder::int,Decoder::map…) answerNonefor anything but whatcrate::Schema::encodewrites 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 (keythen 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 inarena— nothing is copied. - encode
valuein the binary form.- encode_
into valuein the binary form, appended toout(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::Streamis the list of its items, as it is read anywhere without a schema).