Skip to main content

Overview

The Hasher trait defines the interface for hash functions in RotorTree. It supports streaming hash operations through an associated State type.

Trait Definition

pub trait Hasher: Clone + Send + Sync + 'static {
    type State: HashState;
    
    fn new_state(&self) -> Self::State;
}

Trait Bounds

Implementors must satisfy:
  • Clone - Can be cloned cheaply
  • Send - Can be sent across threads
  • Sync - Can be shared across threads
  • 'static - Contains no non-static references

Associated Types

State
HashState
The streaming hash state type. Must implement the HashState trait.

Methods

new_state

Create a fresh hashing state for streaming operations.
fn new_state(&self) -> Self::State
returns
Self::State
A new hash state ready to accept data via update()

HashState Trait

The HashState trait defines the streaming interface:
pub trait HashState {
    fn update(&mut self, data: &[u8]);
    fn finalize(self) -> Hash;
}

update

Feed bytes into the hash state.
data
&[u8]
Bytes to hash

finalize

Finalize the hash and return the digest. Consumes the state.
returns
Hash
32-byte hash digest ([u8; 32])

Implementing a Custom Hasher

Here’s how to implement Hasher for a custom hash function:
use rotortree::{Hash, HashState, Hasher};

// 1. Define your hash state
struct MyHashState {
    // Your state fields
}

impl HashState for MyHashState {
    fn update(&mut self, data: &[u8]) {
        // Feed data into your hash
    }
    
    fn finalize(self) -> Hash {
        // Return the 32-byte digest
        [0u8; 32]
    }
}

// 2. Define your hasher
#[derive(Clone)]
struct MyHasher;

impl Hasher for MyHasher {
    type State = MyHashState;
    
    fn new_state(&self) -> Self::State {
        MyHashState {
            // Initialize state
        }
    }
}

Usage Example

use rotortree::{Hasher, Blake3Hasher};

let hasher = Blake3Hasher::new();

// Create a new streaming state
let mut state = hasher.new_state();

// Feed data in chunks
state.update(b"hello ");
state.update(b"world");

// Get the final hash
let hash = state.finalize();
assert_eq!(hash.len(), 32);

TreeHasher

Domain-separated wrapper for Merkle tree operations

Blake3Hasher

Built-in BLAKE3 implementation

Type Alias

pub type Hash = [u8; 32];
All hash functions in RotorTree produce 32-byte digests.

Build docs developers (and LLMs) love