Skip to content

Commit

Permalink
Don't panic when dropping StaticSnapshot, and implement Clone
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Sep 15, 2023
1 parent e2e0d33 commit fe7fc2f
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/merk/snapshot.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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(),
})
}

Expand All @@ -133,10 +127,29 @@ impl StaticSnapshot {
ManuallyDrop::drop(&mut ss);
std::mem::forget(self);
}

fn clone_tree(&self) -> Cell<Option<Tree>> {
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,
}
}
}

0 comments on commit fe7fc2f

Please sign in to comment.