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
Serializetype - 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§
- Enum
Node - An enum node containing variant information.
- Field
Path - A path to a field in a data structure.
- Local
Change - 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.
- Reflected
Change - A detected change with references to the actual field values.
- SeqNode
- A sequence node containing ordered elements.
- Struct
Node - A struct node containing named fields.
Enums§
- Change
Type - Type of change detected.
- Diff
- Result of comparing two Merkle trees.
- Diff
Detail - Details about how two Merkle trees differ.
- Leaf
Type - The type of a leaf node (primitives).
- Node
- The type of node in the Merkle tree.
- Path
Segment - A segment in a field path.
- Tree
Hash Error
Traits§
- Partial
Reflect - 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.