From f20b9f85502e2aa65ae23b000e8ed727038a17a5 Mon Sep 17 00:00:00 2001 From: Dzejkop Date: Fri, 12 Apr 2024 02:07:54 +0200 Subject: [PATCH] Rename constructors + `new` --- benches/cascading_merkle_tree.rs | 3 ++- examples/abort_test/main.rs | 2 +- src/cascading_merkle_tree.rs | 14 +++++++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/benches/cascading_merkle_tree.rs b/benches/cascading_merkle_tree.rs index 728671f..a9e89bc 100644 --- a/benches/cascading_merkle_tree.rs +++ b/benches/cascading_merkle_tree.rs @@ -173,7 +173,8 @@ fn bench_cascading_restore_dense_mmap_tree(criterion: &mut Criterion) { let storage = unsafe { MmapVec::open_create(format!("./testfile{}", id)).unwrap() }; let _tree: CascadingMerkleTree = - CascadingMerkleTree::new(storage, value.depth, &value.empty_value).unwrap(); + CascadingMerkleTree::restore(storage, value.depth, &value.empty_value) + .unwrap(); let _root = _tree.root(); }); }, diff --git a/examples/abort_test/main.rs b/examples/abort_test/main.rs index fb77f1a..efa4c14 100644 --- a/examples/abort_test/main.rs +++ b/examples/abort_test/main.rs @@ -46,7 +46,7 @@ fn main() -> Result<()> { let mmap_vec: MmapVec<::Hash> = unsafe { MmapVec::new(tempfile)? }; println!("restoring"); - let mut tree = CascadingMerkleTree::::new(mmap_vec, 30, &1)?; + let mut tree = CascadingMerkleTree::::restore(mmap_vec, 30, &1)?; tree.push(2).unwrap(); println!("tree length: {}", tree.num_leaves()); diff --git a/src/cascading_merkle_tree.rs b/src/cascading_merkle_tree.rs index af288d6..6bf4e0b 100644 --- a/src/cascading_merkle_tree.rs +++ b/src/cascading_merkle_tree.rs @@ -51,14 +51,14 @@ where S: StorageOps, { /// Use to open a previously initialized tree - pub fn new( + pub fn restore( storage: S, depth: usize, empty_value: &H::Hash, ) -> Result> { // # Safety // Safe because we're calling `.validate` on the tree later - let tree = unsafe { Self::new_unchecked(storage, depth, empty_value)? }; + let tree = unsafe { Self::restore_unchecked(storage, depth, empty_value)? }; tree.validate()?; @@ -71,7 +71,7 @@ where /// This method is unsafe as it does not validate the contents of storage. /// Use this only if you're sure that the contents of storage are valid - /// i.e. have not been modified since last use. - pub unsafe fn new_unchecked( + pub unsafe fn restore_unchecked( storage: S, depth: usize, empty_value: &H::Hash, @@ -95,6 +95,14 @@ where Ok(tree) } + /// Create and initialize a tree in the provided storage + /// + /// initializes an empty tree + #[must_use] + pub fn new(storage: S, depth: usize, empty_value: &H::Hash) -> CascadingMerkleTree { + Self::new_with_leaves(storage, depth, empty_value, &[]) + } + /// Create and initialize a tree in the provided storage #[must_use] pub fn new_with_leaves(