CanonicalWriter

Struct CanonicalWriter 

Source
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-v0 says 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

Source

pub fn new(magic: &[u8; 4], kind: u8, format_version: u8) -> Self

Start an object: magic, kind tag, format version.

Source

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.

Source

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.

Source

pub fn u32(&mut self, value: u32) -> &mut Self

A member count. Always written before the members it counts.

Source

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.

Source

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.

Source

pub fn i64(&mut self, value: i64) -> &mut Self

A 64-bit integer, for timestamps.

Source

pub fn address(&mut self, value: &Address) -> &mut Self

A child address, by its raw digest rather than its tagged string: the Merkle rule is over bytes, and the tag is presentation.

Source

pub fn finish(self) -> (Address, Vec<u8>)

Finish, returning the object’s address and its canonical bytes.

Trait Implementations§

Source§

impl Debug for CanonicalWriter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.