microkelvin

Crates.iomicrokelvin
lib.rsmicrokelvin
version0.17.0
sourcesrc
created_at2020-10-20 14:03:39.491057
updated_at2022-10-19 12:43:54.565573
descriptionA crate for tree traversal over annotated data structures
homepage
repositoryhttps://github.com/dusk-network/microkelvin
max_upload_size
id303493
size61,517
kristoffer (krl)

documentation

README

microkelvin

Repository Build Status codecov Documentation

Crate for creating and traversing recursively annotated structures.

Compound trait

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

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

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

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.

Branch walking

The Walker trait can be implemented for walking the tree in a user defined way. As an example, here's AllLeaves - an implementation used internally:

/// Walker that visits all leaves
pub struct AllLeaves;

impl<C, A> Walker<C, A> for AllLeaves
where
    C: Compound<A>,
{
    fn walk(&mut self, walk: Walk<C, A>) -> Step {
        for i in 0.. {
            match walk.child(i) {
                Child::Leaf(_) => return Step::Found(i),
                Child::Node(_) => return Step::Into(i),
                Child::Empty => (),
                Child::EndOfNode => return Step::Advance,
            }
        }
        unreachable!()
    }
}

Usage

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

Commit count: 235

cargo fmt