diff --git a/storage/src/nodestore.rs b/storage/src/nodestore.rs index 2d7c0c8ac..d182c8737 100644 --- a/storage/src/nodestore.rs +++ b/storage/src/nodestore.rs @@ -50,6 +50,7 @@ use std::io::{Error, ErrorKind, Write}; use std::iter::once; use std::mem::offset_of; use std::num::NonZeroU64; +use std::ops::Deref; use std::sync::Arc; use crate::hashednode::hash_node; @@ -244,7 +245,7 @@ pub trait Parentable { impl Parentable for Arc { fn as_nodestore_parent(&self) -> NodeStoreParent { - NodeStoreParent::Proposed(self.clone()) + NodeStoreParent::Proposed(Arc::clone(&self)) } fn root_hash(&self) -> Option { self.root_hash.clone() @@ -587,56 +588,46 @@ pub trait HashedNodeReader: TrieReader { } } -impl RootReader for Arc { - fn root_node(&self) -> Option> { - R::root_node(self) - } -} - -impl TrieReader for Arc where T: TrieReader {} - -impl NodeReader for Arc +impl HashedNodeReader for T where - T: NodeReader, + T: Deref, + T::Target: HashedNodeReader, { - fn read_node(&self, addr: LinearAddress) -> Result, Error> { - T::read_node(self, addr) - } -} - -impl HashedNodeReader for Arc { fn root_address_and_hash(&self) -> Result, Error> { - H::root_address_and_hash(self) - } - fn root_hash(&self) -> Result, Error> { - H::root_hash(self) + self.deref().root_address_and_hash() } } /// Reads nodes and the root address from a merkle trie. pub trait TrieReader: NodeReader + RootReader {} +impl TrieReader for T where T: NodeReader + RootReader {} + +/// Reads nodes from a merkle trie. +pub trait NodeReader { + /// Returns the node at `addr`. + fn read_node(&self, addr: LinearAddress) -> Result, Error>; +} -impl TrieReader for &NodeStore {} -impl NodeReader for &NodeStore { +impl NodeReader for T +where + T: Deref, + T::Target: NodeReader, +{ fn read_node(&self, addr: LinearAddress) -> Result, Error> { - self.read_node_from_disk(addr) + self.deref().read_node(addr) } } -impl RootReader for &NodeStore { +impl RootReader for T +where + T: Deref, + T::Target: RootReader, +{ fn root_node(&self) -> Option> { - self.header - .root_address - .map(|addr| self.read_node_from_disk(addr).unwrap()) + self.deref().root_node() } } -/// Reads nodes from a merkle trie. -pub trait NodeReader { - /// Returns the node at `addr`. - fn read_node(&self, addr: LinearAddress) -> Result, Error>; -} - /// Reads the root of a merkle trie. pub trait RootReader { /// Returns the root of the trie. @@ -703,7 +694,7 @@ impl ImmutableProposal { } } -impl ReadInMemoryNode for Arc { +impl ReadInMemoryNode for ImmutableProposal { fn read_in_memory_node(&self, addr: LinearAddress) -> Option> { // Check if the node being requested was created in this proposal. if let Some((_, node)) = self.new.get(&addr) { @@ -730,6 +721,16 @@ pub trait ReadInMemoryNode { fn read_in_memory_node(&self, addr: LinearAddress) -> Option>; } +impl ReadInMemoryNode for T +where + T: Deref, + T::Target: ReadInMemoryNode, +{ + fn read_in_memory_node(&self, addr: LinearAddress) -> Option> { + self.deref().read_in_memory_node(addr) + } +} + /// Contains the state of a revision of a merkle trie. /// The first generic parameter is the type of the revision, which supports reading nodes from parent proposals. /// The second generic parameter is the type of the storage used, either @@ -1052,11 +1053,6 @@ impl RootReader for NodeStore< } } -impl TrieReader for NodeStore where - NodeStore: RootReader -{ -} - impl HashedNodeReader for NodeStore where NodeStore: TrieReader,