merkle_search_tree/
builder.rs

1//! A configurable [`MerkleSearchTree`] constructor.
2//!
3//! [`MerkleSearchTree`]: crate::MerkleSearchTree
4
5use std::num::NonZeroU8;
6
7use crate::DEFAULT_LEVEL_BASE;
8
9/// A [`MerkleSearchTree`] builder to initialise trees with custom parameters.
10///
11/// [`MerkleSearchTree`]: crate::MerkleSearchTree
12#[derive(Debug, Clone)]
13pub struct Builder<H> {
14    pub(crate) hasher: H,
15    pub(crate) level_base: NonZeroU8,
16}
17
18impl Default for Builder<super::tree::DefaultHasher> {
19    fn default() -> Self {
20        Self {
21            hasher: Default::default(),
22            level_base: DEFAULT_LEVEL_BASE,
23        }
24    }
25}
26
27impl<H> Builder<H> {
28    /// Use the provided [`Hasher`] to compute key and value digests.
29    ///
30    /// [`Hasher`]: crate::digest::Hasher
31    pub fn with_hasher<T>(self, hasher: T) -> Builder<T> {
32        Builder {
33            hasher,
34            level_base: self.level_base,
35        }
36    }
37
38    /// Configure the value for the parameter `B` as described in the paper (the
39    /// branching factor).
40    ///
41    /// Decreasing this value increases the hight of the tree, which increases
42    /// the number of pages, and therefore decreases the average page size.
43    pub fn with_level_base(self, b: NonZeroU8) -> Self {
44        Self {
45            level_base: b,
46            ..self
47        }
48    }
49}
50
51// build() is in tree.rs