pub struct CanonicalWriter { /* private fields */ }Expand description
The canonical byte writer.
Every object kind is encoded through this, so the framing rules live in one place:
- A fixed 6-byte header: 4-byte ASCII magic, 1-byte kind tag, 1-byte
format version. A format change mints a new magic rather than reusing
this one, which is how
node-hash-v0says a successor must announce itself. - Counts precede members, so a truncated stream cannot be mistaken for a shorter valid one.
- Strings are length-prefixed, never terminated, so a string containing any byte — a separator included — cannot be read as two fields. A delimiter-based encoding has exactly this collision, and it is the defect class tracked for the TypeScript side in #8088.
No Default: new is the only constructor, so a
header-less buffer — one with no magic, kind or version — cannot be built.
(Review on #8265 also asked about pub(crate): left pub, because
optional_string and i64 have no caller yet outside tests — the adapter
and commit object that will call them are later PRs on #8262 — and
pub(crate) turns that into a dead_code warning this crate would then
have to silence rather than earn.)
Implementations§
Source§impl CanonicalWriter
impl CanonicalWriter
Sourcepub fn new(magic: &[u8; 4], kind: u8, format_version: u8) -> Self
pub fn new(magic: &[u8; 4], kind: u8, format_version: u8) -> Self
Start an object: magic, kind tag, format version.
Sourcepub fn string(&mut self, value: &str) -> &mut Self
pub fn string(&mut self, value: &str) -> &mut Self
An NFC-normalized UTF-8 string, length-prefixed with its byte length.
Normalization happens here, in the one place every string passes through, so no caller can encode a raw spelling by forgetting to.
Sourcepub fn optional_string(&mut self, value: Option<&str>) -> &mut Self
pub fn optional_string(&mut self, value: Option<&str>) -> &mut Self
An optional string. Absent and empty are distinct: absent writes a
0 tag and no payload, empty writes a 1 tag and a zero length.
Sourcepub fn u32(&mut self, value: u32) -> &mut Self
pub fn u32(&mut self, value: u32) -> &mut Self
A member count. Always written before the members it counts.
Sourcepub fn u8(&mut self, value: u8) -> &mut Self
pub fn u8(&mut self, value: u8) -> &mut Self
A single byte: a kind or boolean tag. Narrower than u32
because a tag is a closed, small set — v0’s rules (§2.5, as node.rs
keeps them) use one byte for exactly this, and a count is the only
field wide enough to need four.
Sourcepub fn f64_bits(&mut self, value: f64) -> &mut Self
pub fn f64_bits(&mut self, value: f64) -> &mut Self
An f64 as its raw IEEE-754 bit pattern, little-endian.
v0’s rule, and it avoids a decision rather than making one: encoding a float as text means choosing a rendering, and two renderings of one value hash differently. Bits have no such choice.
Two values that compare equal must not hash differently, so this
canonicalises before taking bits: -0.0 becomes 0.0 (IFC writers
emit -0. routinely, and it compares equal to 0.0 while differing in
its sign bit), and every NaN payload — signalling or quiet, any
mantissa — becomes one quiet pattern (they all compare unequal to
everything, including each other, so bits are the only identity they
have, and two authoring tools are not guaranteed to agree on one).
Canonicalising here, the one place every float passes through, means
no caller can forget to.