Skip to content

Commit

Permalink
Improve SnapshotBuilder ergonomics
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-D-coder committed Mar 26, 2024
1 parent eebc3cb commit 88c86b4
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 110 deletions.
91 changes: 90 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ overflow-checks = true
[dependencies]
sha2 = "0.10"
bumpalo = "3"
ouroboros = "0.18"

proptest-derive = { version = "0.4", optional = true }
proptest = { version = "1", optional = true }

Expand Down
74 changes: 74 additions & 0 deletions src/stored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub mod merkle;

use core::fmt::Display;

use alloc::{rc::Rc, sync::Arc};

use crate::{
transaction::nodes::{Branch, Leaf, Node},
NodeHash,
Expand Down Expand Up @@ -32,6 +34,34 @@ impl<V, S: Store<V>> Store<V> for &S {
}
}

impl<V, S: Store<V>> Store<V> for Rc<S> {
type Error = S::Error;

#[inline(always)]
fn calc_subtree_hash(&self, hash_idx: Idx) -> Result<NodeHash, Self::Error> {
(**self).calc_subtree_hash(hash_idx)
}

#[inline(always)]
fn get_node(&self, hash_idx: Idx) -> Result<Node<&Branch<Idx>, &Leaf<V>>, Self::Error> {
(**self).get_node(hash_idx)
}
}

impl<V, S: Store<V>> Store<V> for Arc<S> {
type Error = S::Error;

#[inline(always)]
fn calc_subtree_hash(&self, hash_idx: Idx) -> Result<NodeHash, Self::Error> {
(**self).calc_subtree_hash(hash_idx)
}

#[inline(always)]
fn get_node(&self, hash_idx: Idx) -> Result<Node<&Branch<Idx>, &Leaf<V>>, Self::Error> {
(**self).get_node(hash_idx)
}
}

pub trait DatabaseGet<V> {
type GetError: Display;

Expand Down Expand Up @@ -69,3 +99,47 @@ impl<V, D: DatabaseSet<V>> DatabaseSet<V> for &D {
(**self).set(hash, node)
}
}

impl<V, D: DatabaseGet<V>> DatabaseGet<V> for Rc<D> {
type GetError = D::GetError;

#[inline]
fn get(&self, hash: &NodeHash) -> Result<Node<Branch<NodeHash>, Leaf<V>>, Self::GetError> {
(**self).get(hash)
}
}

impl<V, D: DatabaseSet<V>> DatabaseSet<V> for Rc<D> {
type SetError = D::SetError;

#[inline]
fn set(
&self,
hash: NodeHash,
node: Node<Branch<NodeHash>, Leaf<V>>,
) -> Result<(), Self::GetError> {
(**self).set(hash, node)
}
}

impl<V, D: DatabaseGet<V>> DatabaseGet<V> for Arc<D> {
type GetError = D::GetError;

#[inline]
fn get(&self, hash: &NodeHash) -> Result<Node<Branch<NodeHash>, Leaf<V>>, Self::GetError> {
(**self).get(hash)
}
}

impl<V, D: DatabaseSet<V>> DatabaseSet<V> for Arc<D> {
type SetError = D::SetError;

#[inline]
fn set(
&self,
hash: NodeHash,
node: Node<Branch<NodeHash>, Leaf<V>>,
) -> Result<(), Self::GetError> {
(**self).set(hash, node)
}
}
Loading

0 comments on commit 88c86b4

Please sign in to comment.