From fe7fc2ff8ef9878d842183af24cd79a7c5d52508 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Fri, 15 Sep 2023 14:44:26 -0500 Subject: [PATCH] Don't panic when dropping StaticSnapshot, and implement Clone --- src/merk/snapshot.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/merk/snapshot.rs b/src/merk/snapshot.rs index a6f3394..8d6aedc 100644 --- a/src/merk/snapshot.rs +++ b/src/merk/snapshot.rs @@ -1,7 +1,5 @@ use std::{cell::Cell, mem::ManuallyDrop}; -use ed::{Decode, Encode}; - use crate::{ proofs::{query::QueryItem, Query}, tree::{Fetch, RefWalker, Tree, NULL_HASH}, @@ -118,13 +116,9 @@ impl StaticSnapshot { }; let db_ss: rocksdb::Snapshot<'a> = std::mem::transmute(db_ss); - let tree = self.tree.take().unwrap(); - let tree_clone = Tree::decode(tree.key().to_vec(), tree.encode().as_slice()); - self.tree.set(Some(tree)); - ManuallyDrop::new(Snapshot { ss: db_ss, - tree: Cell::new(Some(tree_clone)), + tree: self.clone_tree(), }) } @@ -133,10 +127,29 @@ impl StaticSnapshot { ManuallyDrop::drop(&mut ss); std::mem::forget(self); } + + fn clone_tree(&self) -> Cell> { + let tree = self.tree.take().unwrap(); + let tree_clone = Cell::new(Some(Tree::decode( + tree.key().to_vec(), + tree.encode().as_slice(), + ))); + self.tree.set(Some(tree)); + tree_clone + } } impl Drop for StaticSnapshot { fn drop(&mut self) { - panic!("StaticSnapshot must be manually dropped"); + log::debug!("StaticSnapshot must be manually dropped"); + } +} + +impl Clone for StaticSnapshot { + fn clone(&self) -> Self { + Self { + tree: self.clone_tree(), + inner: self.inner, + } } }