Thanks to visit codestin.com
Credit goes to docs.rs

Crate hashmark

Crate hashmark 

Source
Expand description

Serde-based Merkle tree hashing for efficient change detection.

This crate provides tree hashing built on top of serde and bevy_reflect, enabling:

  • Field name tracking for readable diffs
  • Compatibility with any Serialize type
  • Runtime field reference access via Reflect
  • Support for all serde attributes

§Basic Example (Path-based diff)

use serde::Serialize;
use hashmark::{tree_hash, merkle_diff, Diff};

#[derive(Serialize)]
struct User {
    name: String,
    age: u32,
}

let user1 = User { name: "Alice".into(), age: 30 };
let user2 = User { name: "Alice".into(), age: 31 };

let m1 = tree_hash(&user1).unwrap();
let m2 = tree_hash(&user2).unwrap();

match merkle_diff(&m1, &m2) {
    Diff::Same => println!("No changes"),
    Diff::Different(detail) => {
        for (path, _) in detail.changed_paths() {
            println!("Changed: {}", path);
        }
    }
}

§Advanced Example (With field references)

use serde::Serialize;
use bevy_reflect::Reflect;
use hashmark::{diff_reflect, ChangeType};

#[derive(Serialize, Reflect)]
struct User {
    name: String,
    age: u32,
}

let old = User { name: "Alice".into(), age: 30 };
let new = User { name: "Alice".into(), age: 31 };

let changes = diff_reflect(&old, &new).unwrap();

for change in &changes {
    println!("Changed: {} ({:?})", change.path, change.change_type);

    // Access actual values via PartialReflect
    if let (Some(old_val), Some(new_val)) = (&change.old, &change.new) {
        if let (Some(old_n), Some(new_n)) = (
            old_val.try_downcast_ref::<u32>(),
            new_val.try_downcast_ref::<u32>()
        ) {
            println!("  {} -> {}", old_n, new_n);
        }
    }
}

Structs§

EnumNode
An enum node containing variant information.
FieldPath
A path to a field in a data structure.
LocalChange
A change detected when comparing local data against a remote Merkle tree.
MapNode
A map node containing key-value pairs.
Merkle
A node in the Merkle tree with its computed hash.
ReflectedChange
A detected change with references to the actual field values.
SeqNode
A sequence node containing ordered elements.
StructNode
A struct node containing named fields.

Enums§

ChangeType
Type of change detected.
Diff
Result of comparing two Merkle trees.
DiffDetail
Details about how two Merkle trees differ.
LeafType
The type of a leaf node (primitives).
Node
The type of node in the Merkle tree.
PathSegment
A segment in a field path.
TreeHashError

Traits§

PartialReflect
The foundational trait of bevy_reflect, used for accessing and modifying data dynamically.

Functions§

diff_reflect
Diff two values and return references to changed fields.
merkle_diff
Compare two Merkle trees and return their differences.
tree_hash
Compute the Merkle tree hash of a value.

Type Aliases§

Digest