A library for tree traversal over annotated datastructures

26 releases (11 breaking)

new 0.13.0-rc.1 Jan 12, 2022
0.12.0-rc.5 Dec 14, 2021
0.11.0-rc.0 Nov 4, 2021
0.10.3 Jan 18, 2022
0.5.3 Nov 17, 2020

#85 in Data structures

Download history 29/week @ 2021-09-29 8/week @ 2021-10-06 61/week @ 2021-10-13 30/week @ 2021-10-20 17/week @ 2021-10-27 63/week @ 2021-11-03 54/week @ 2021-11-10 93/week @ 2021-11-17 65/week @ 2021-11-24 59/week @ 2021-12-01 168/week @ 2021-12-08 37/week @ 2021-12-15 13/week @ 2021-12-22 18/week @ 2021-12-29 48/week @ 2022-01-05 128/week @ 2022-01-12

207 downloads per month
Used in 9 crates (5 directly)

MPL-2.0 and maybe GPL-3.0


Dusk CI codecov


Library for creating and searching through annotated merkle trees.

Built on the canonical serialization and FFI library.

Compound trait

/// A type that can recursively contain itself and leaves.
pub trait Compound<A>: Sized + Canon {
    /// The leaf type of the Compound collection
    type Leaf;

    /// Returns a reference to a possible child at specified offset
    fn child(&self, ofs: usize) -> Child<Self, A>
        A: Annotation<Self::Leaf>;

    /// Returns a mutable reference to a possible child at specified offset
    fn child_mut(&mut self, ofs: usize) -> ChildMut<Self, A>
        A: Annotation<Self::Leaf>;

The Compound trait defines a type as a collection type. This means that it can be searched and have branches constructed pointing to its elements.

Annotation/Combine trait

/// The trait defining an annotation type over a leaf
pub trait Annotation<Leaf>: Default + Clone {
    /// Creates an annotation from the leaf type
    fn from_leaf(leaf: &Leaf) -> Self;
/// Trait for defining how to combine Annotations
pub trait Combine<C, A>: Annotation<C::Leaf>
    C: Compound<A>,
    /// Combines multiple annotations
    fn combine(node: &C) -> Self;

The annotation and combine traits keep an automatically updated annotation of subtrees, for example total leaf amount (Cardinality in reference implementation), or which leaf compares the greatest (Max in reference implementation)

Branch walking

This is ane example of walking a recursive structure to yield a branch pointing at the nth leaf of the collection, if any.

It is automatically implemented on all types implementing Compound whose annotation can be borrowed as Cardinality. Giving this capability to any such structure.

impl<'a, C, A> Nth<'a, A> for C
    C: Compound<A> + Clone,
    A: Annotation<Self::Leaf> + Borrow<Cardinality>,
    fn nth(
        &'a self,
        mut remainder: u64,
    ) -> Result<Option<Branch<'a, Self, A>>, CanonError> {
        // Return the first that satisfies the walk
        Branch::<_, A>::walk(self, |w| nth(w, &mut remainder))

    fn nth_mut(
        &'a mut self,
        mut remainder: u64,
    ) -> Result<Option<BranchMut<'a, Self, A>>, CanonError> {
        // Return the first mutable branch that satisfies the walk
        BranchMut::<_, A>::walk(self, |w| nth(w, &mut remainder))


Please check out the nstack implementation of a stack/vector type for a more advanced example.


~31K SLoC