From 648d6b6034df211f2ccab13956e2d01a87e309cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Fri, 26 Nov 2021 22:05:29 +0100 Subject: [PATCH] Use reference --- trie-db/src/triedbmut.rs | 45 +++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/trie-db/src/triedbmut.rs b/trie-db/src/triedbmut.rs index 7ba3611b..0639054a 100644 --- a/trie-db/src/triedbmut.rs +++ b/trie-db/src/triedbmut.rs @@ -35,12 +35,19 @@ use log::trace; use crate::rstd::fmt::{self, Debug}; -// For lookups into the Node storage buffer. +/// For lookups into the Node storage buffer. // This is deliberately non-copyable. #[cfg_attr(feature = "std", derive(Debug))] struct StorageHandle(usize); -// Handles to nodes in the trie. +impl StorageHandle { + /// Clone, but should only be used when you know what your doing. + fn clone_unsafe(&self) -> Self { + Self(self.0) + } +} + +/// Handles to nodes in the trie. #[cfg_attr(feature = "std", derive(Debug))] enum NodeHandle { /// Loaded into memory. @@ -203,19 +210,19 @@ where } // TODO: parallelize - fn into_encoded(self, mut child_cb: F) -> Vec + fn into_encoded(&self, mut child_cb: F) -> Vec where C: NodeCodec, - F: FnMut(NodeHandle, Option<&NibbleSlice>, Option) -> ChildReference, + F: FnMut(&NodeHandle, Option<&NibbleSlice>, Option) -> ChildReference, H: Hasher, { match self { Node::Empty => C::empty_node().to_vec(), - Node::Leaf(partial, value) => { + Node::Leaf(ref partial, ref value) => { let pr = NibbleSlice::new_offset(&partial.1[..], partial.0); C::leaf_node(pr.right(), &value) }, - Node::Extension(partial, child) => { + Node::Extension(ref partial, ref child) => { let pr = NibbleSlice::new_offset(&partial.1[..], partial.0); let it = pr.right_iter(); let c = child_cb(child, Some(&pr), None); @@ -225,27 +232,27 @@ where c, ) }, - Node::Branch(mut children, value) => { + Node::Branch(ref children, ref value) => { C::branch_node( // map the `NodeHandle`s from the Branch to `ChildReferences` - children.iter_mut() - .map(Option::take) + children.iter() + .map(Option::as_ref) .enumerate() .map(|(i, maybe_child)| { maybe_child.map(|child| child_cb(child, None, Some(i as u8))) }), - value.as_ref().map(|v| &v[..]) + value.as_deref(), ) }, - Node::NibbledBranch(partial, mut children, value) => { + Node::NibbledBranch(ref partial, ref children, ref value) => { let pr = NibbleSlice::new_offset(&partial.1[..], partial.0); let it = pr.right_iter(); C::branch_node_nibbled( it, pr.len(), // map the `NodeHandle`s from the Branch to `ChildReferences` - children.iter_mut() - .map(Option::take) + children.iter() + .map(Option::as_ref) .enumerate() .map(|(i, maybe_child)| { //let branch_index = [i as u8]; @@ -254,7 +261,7 @@ where child_cb(child, Some(&pr), Some(i as u8)) }) }), - value.as_ref().map(|v| &v[..]) + value.as_deref(), ) }, } @@ -1454,23 +1461,23 @@ where /// `into_encoded` method of `Node`. fn commit_child( &mut self, - handle: NodeHandle>, + handle: &NodeHandle>, prefix: &mut NibbleVec, ) -> ChildReference> { match handle { - NodeHandle::Hash(hash) => ChildReference::Hash(hash), + NodeHandle::Hash(ref hash) => ChildReference::Hash(*hash), NodeHandle::InMemory(storage_handle) => { - match self.storage.destroy(storage_handle) { + match self.storage.destroy(storage_handle.clone_unsafe()) { Stored::Cached(_, hash) => ChildReference::Hash(hash), Stored::New(node) => { let encoded = { let commit_child = | - node_handle, + node_handle: &NodeHandle>, o_slice: Option<&NibbleSlice>, o_index: Option | { let mov = prefix.append_optional_slice_and_nibble(o_slice, o_index); - let cr = self.commit_child(node_handle, prefix); + let cr = self.commit_child(&node_handle, prefix); prefix.drop_lasts(mov); cr };