Skip to main content

ContentHasher

Struct ContentHasher 

Source
pub struct ContentHasher {
    state: u128,
}
Expand description

An incremental builder for a deterministic ContentHash over a sequence of typed fields (design note §4).

Construct with new (which stamps the engine version), fold a domain tag plus the boundary’s fields with field / u32, then read the result with finish. The field sequence is the boundary’s contract: keep it fixed per domain, and lead with a domain tag so two boundaries that fold identical bytes still differ.

§Examples

use mos_core::ContentHasher;

// Same fields in the same order hash equal; any change diverges.
let mut a = ContentHasher::new();
a.field(b"example/v1").u32(7).field(b"payload");

let mut b = ContentHasher::new();
b.field(b"example/v1").u32(7).field(b"payload");

assert_eq!(a.finish(), b.finish());

let mut c = ContentHasher::new();
c.field(b"example/v1").u32(8).field(b"payload");
assert_ne!(a.finish(), c.finish());

Fields§

§state: u128

Implementations§

Source§

impl ContentHasher

Source

pub fn new() -> Self

Start a hasher, stamping the engine version (§5 rule 2) so callers cannot forget it.

§Examples
use mos_core::ContentHasher;

// A fresh hasher already carries the engine-version stamp, so two
// freshly-constructed hashers agree before any field is folded.
assert_eq!(ContentHasher::new().finish(), ContentHasher::new().finish());
Source

pub fn field(&mut self, bytes: &[u8]) -> &mut Self

Fold one variable-length field, length-prefixed so field boundaries stay unambiguous.

The u64 length prefix is fixed-width (not usize) so the hash is identical on 32- and 64-bit targets.

§Examples
use mos_core::ContentHasher;

// Length framing keeps `("a", "bc")` distinct from `("ab", "c")`.
let mut split_a = ContentHasher::new();
split_a.field(b"a").field(b"bc");
let mut split_b = ContentHasher::new();
split_b.field(b"ab").field(b"c");
assert_ne!(split_a.finish(), split_b.finish());
Source

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

Fold a fixed-width u32 (little-endian). No length prefix is needed: the width is constant, so the field boundary is implicit.

§Examples
use mos_core::ContentHasher;

let mut a = ContentHasher::new();
let mut b = ContentHasher::new();
a.u32(1);
b.u32(2);
assert_ne!(a.finish(), b.finish());
Source

pub fn finish(&self) -> ContentHash

The accumulated ContentHash.

§Examples
use mos_core::{ContentHash, ContentHasher};

let hash: ContentHash = ContentHasher::new().field(b"x").finish();
// It is a real 128-bit value, not the default.
assert_ne!(hash, ContentHash::default());
Source

fn fold(&mut self, bytes: &[u8])

Fold raw bytes into the running FNV-1a state (XOR-then-multiply).

Trait Implementations§

Source§

impl Clone for ContentHasher

Source§

fn clone(&self) -> ContentHasher

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ContentHasher

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for ContentHasher

Source§

fn default() -> Self

Returns the “default value” for a type. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.