Remove redundant type param fom CachedTreeHash

This commit is contained in:
Paul Hauner 2019-04-29 07:48:48 +10:00
parent 6258abfa9f
commit 0599d3f1f8
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
10 changed files with 26 additions and 30 deletions

View File

@ -224,7 +224,7 @@ macro_rules! impl_ssz {
} }
} }
impl cached_tree_hash::CachedTreeHash<$type> for $type { impl cached_tree_hash::CachedTreeHash for $type {
fn new_tree_hash_cache( fn new_tree_hash_cache(
&self, &self,
depth: usize, depth: usize,

View File

@ -55,9 +55,9 @@ where
} }
} }
impl<T> CachedTreeHash<TreeHashVector<T>> for TreeHashVector<T> impl<T> CachedTreeHash for TreeHashVector<T>
where where
T: CachedTreeHash<T> + TreeHash, T: CachedTreeHash + TreeHash,
{ {
fn new_tree_hash_cache( fn new_tree_hash_cache(
&self, &self,

View File

@ -42,7 +42,7 @@ pub struct BTreeOverlay {
impl BTreeOverlay { impl BTreeOverlay {
pub fn new<T>(item: &T, initial_offset: usize, depth: usize) -> Self pub fn new<T>(item: &T, initial_offset: usize, depth: usize) -> Self
where where
T: CachedTreeHash<T>, T: CachedTreeHash,
{ {
Self::from_schema(item.tree_hash_cache_schema(depth), initial_offset) Self::from_schema(item.tree_hash_cache_schema(depth), initial_offset)
} }

View File

@ -6,7 +6,7 @@ pub mod vec;
macro_rules! impl_for_single_leaf_int { macro_rules! impl_for_single_leaf_int {
($type: ident) => { ($type: ident) => {
impl CachedTreeHash<$type> for $type { impl CachedTreeHash for $type {
fn new_tree_hash_cache(&self, _depth: usize) -> Result<TreeHashCache, Error> { fn new_tree_hash_cache(&self, _depth: usize) -> Result<TreeHashCache, Error> {
Ok(TreeHashCache::from_bytes( Ok(TreeHashCache::from_bytes(
merkleize(self.to_le_bytes().to_vec()), merkleize(self.to_le_bytes().to_vec()),
@ -37,7 +37,7 @@ impl_for_single_leaf_int!(u32);
impl_for_single_leaf_int!(u64); impl_for_single_leaf_int!(u64);
impl_for_single_leaf_int!(usize); impl_for_single_leaf_int!(usize);
impl CachedTreeHash<bool> for bool { impl CachedTreeHash for bool {
fn new_tree_hash_cache(&self, _depth: usize) -> Result<TreeHashCache, Error> { fn new_tree_hash_cache(&self, _depth: usize) -> Result<TreeHashCache, Error> {
Ok(TreeHashCache::from_bytes( Ok(TreeHashCache::from_bytes(
merkleize((*self as u8).to_le_bytes().to_vec()), merkleize((*self as u8).to_le_bytes().to_vec()),
@ -60,7 +60,7 @@ impl CachedTreeHash<bool> for bool {
} }
} }
impl CachedTreeHash<[u8; 4]> for [u8; 4] { impl CachedTreeHash for [u8; 4] {
fn new_tree_hash_cache(&self, _depth: usize) -> Result<TreeHashCache, Error> { fn new_tree_hash_cache(&self, _depth: usize) -> Result<TreeHashCache, Error> {
Ok(TreeHashCache::from_bytes( Ok(TreeHashCache::from_bytes(
merkleize(self.to_vec()), merkleize(self.to_vec()),
@ -83,7 +83,7 @@ impl CachedTreeHash<[u8; 4]> for [u8; 4] {
} }
} }
impl CachedTreeHash<H256> for H256 { impl CachedTreeHash for H256 {
fn new_tree_hash_cache(&self, _depth: usize) -> Result<TreeHashCache, Error> { fn new_tree_hash_cache(&self, _depth: usize) -> Result<TreeHashCache, Error> {
Ok(TreeHashCache::from_bytes( Ok(TreeHashCache::from_bytes(
self.as_bytes().to_vec(), self.as_bytes().to_vec(),

View File

@ -2,9 +2,9 @@ use super::*;
use crate::btree_overlay::LeafNode; use crate::btree_overlay::LeafNode;
use crate::merkleize::{merkleize, num_sanitized_leaves, sanitise_bytes}; use crate::merkleize::{merkleize, num_sanitized_leaves, sanitise_bytes};
impl<T> CachedTreeHash<Vec<T>> for Vec<T> impl<T> CachedTreeHash for Vec<T>
where where
T: CachedTreeHash<T> + TreeHash, T: CachedTreeHash + TreeHash,
{ {
fn new_tree_hash_cache(&self, depth: usize) -> Result<TreeHashCache, Error> { fn new_tree_hash_cache(&self, depth: usize) -> Result<TreeHashCache, Error> {
let (mut cache, schema) = new_tree_hash_cache(self, depth)?; let (mut cache, schema) = new_tree_hash_cache(self, depth)?;
@ -40,7 +40,7 @@ where
} }
} }
pub fn new_tree_hash_cache<T: CachedTreeHash<T>>( pub fn new_tree_hash_cache<T: CachedTreeHash>(
vec: &Vec<T>, vec: &Vec<T>,
depth: usize, depth: usize,
) -> Result<(TreeHashCache, BTreeSchema), Error> { ) -> Result<(TreeHashCache, BTreeSchema), Error> {
@ -65,7 +65,7 @@ pub fn new_tree_hash_cache<T: CachedTreeHash<T>>(
Ok((cache, schema)) Ok((cache, schema))
} }
pub fn produce_schema<T: CachedTreeHash<T>>(vec: &Vec<T>, depth: usize) -> BTreeSchema { pub fn produce_schema<T: CachedTreeHash>(vec: &Vec<T>, depth: usize) -> BTreeSchema {
let lengths = match T::tree_hash_type() { let lengths = match T::tree_hash_type() {
TreeHashType::Basic => { TreeHashType::Basic => {
// Ceil division. // Ceil division.
@ -89,7 +89,7 @@ pub fn produce_schema<T: CachedTreeHash<T>>(vec: &Vec<T>, depth: usize) -> BTree
BTreeSchema::from_lengths(depth, lengths) BTreeSchema::from_lengths(depth, lengths)
} }
pub fn update_tree_hash_cache<T: CachedTreeHash<T>>( pub fn update_tree_hash_cache<T: CachedTreeHash>(
vec: &Vec<T>, vec: &Vec<T>,
cache: &mut TreeHashCache, cache: &mut TreeHashCache,
) -> Result<BTreeOverlay, Error> { ) -> Result<BTreeOverlay, Error> {
@ -273,7 +273,7 @@ fn splice_in_new_tree<T>(
cache: &mut TreeHashCache, cache: &mut TreeHashCache,
) -> Result<(), Error> ) -> Result<(), Error>
where where
T: CachedTreeHash<T>, T: CachedTreeHash,
{ {
let (bytes, mut bools, schemas) = TreeHashCache::new(item, depth)?.into_components(); let (bytes, mut bools, schemas) = TreeHashCache::new(item, depth)?.into_components();
@ -296,7 +296,7 @@ where
fn get_packed_leaves<T>(vec: &Vec<T>) -> Result<Vec<u8>, Error> fn get_packed_leaves<T>(vec: &Vec<T>) -> Result<Vec<u8>, Error>
where where
T: CachedTreeHash<T>, T: CachedTreeHash,
{ {
let num_packed_bytes = (BYTES_PER_CHUNK / T::tree_hash_packing_factor()) * vec.len(); let num_packed_bytes = (BYTES_PER_CHUNK / T::tree_hash_packing_factor()) * vec.len();
let num_leaves = num_sanitized_leaves(num_packed_bytes); let num_leaves = num_sanitized_leaves(num_packed_bytes);

View File

@ -1,5 +1,4 @@
use hashing::hash; use hashing::hash;
use merkleize::num_unsanitized_leaves;
use std::ops::Range; use std::ops::Range;
use tree_hash::{TreeHash, TreeHashType, BYTES_PER_CHUNK, HASHSIZE}; use tree_hash::{TreeHash, TreeHashType, BYTES_PER_CHUNK, HASHSIZE};
@ -14,7 +13,7 @@ pub use btree_overlay::{BTreeOverlay, BTreeSchema};
pub use errors::Error; pub use errors::Error;
pub use tree_hash_cache::TreeHashCache; pub use tree_hash_cache::TreeHashCache;
pub trait CachedTreeHash<Item>: TreeHash { pub trait CachedTreeHash: TreeHash {
fn tree_hash_cache_schema(&self, depth: usize) -> BTreeSchema; fn tree_hash_cache_schema(&self, depth: usize) -> BTreeSchema;
fn num_tree_hash_cache_chunks(&self) -> usize { fn num_tree_hash_cache_chunks(&self) -> usize {
@ -34,7 +33,7 @@ pub struct CachedTreeHasher {
impl CachedTreeHasher { impl CachedTreeHasher {
pub fn new<T>(item: &T) -> Result<Self, Error> pub fn new<T>(item: &T) -> Result<Self, Error>
where where
T: CachedTreeHash<T>, T: CachedTreeHash,
{ {
Ok(Self { Ok(Self {
cache: TreeHashCache::new(item, 0)?, cache: TreeHashCache::new(item, 0)?,
@ -43,7 +42,7 @@ impl CachedTreeHasher {
pub fn update<T>(&mut self, item: &T) -> Result<(), Error> pub fn update<T>(&mut self, item: &T) -> Result<(), Error>
where where
T: CachedTreeHash<T>, T: CachedTreeHash,
{ {
self.cache.update(item) self.cache.update(item)
} }
@ -57,7 +56,7 @@ impl CachedTreeHasher {
#[macro_export] #[macro_export]
macro_rules! cached_tree_hash_ssz_encoding_as_vector { macro_rules! cached_tree_hash_ssz_encoding_as_vector {
($type: ident, $num_bytes: expr) => { ($type: ident, $num_bytes: expr) => {
impl cached_tree_hash::CachedTreeHash<$type> for $type { impl cached_tree_hash::CachedTreeHash for $type {
fn new_tree_hash_cache( fn new_tree_hash_cache(
&self, &self,
depth: usize, depth: usize,
@ -94,7 +93,7 @@ macro_rules! cached_tree_hash_ssz_encoding_as_vector {
#[macro_export] #[macro_export]
macro_rules! cached_tree_hash_bytes_as_list { macro_rules! cached_tree_hash_bytes_as_list {
($type: ident) => { ($type: ident) => {
impl cached_tree_hash::CachedTreeHash<$type> for $type { impl cached_tree_hash::CachedTreeHash for $type {
fn new_tree_hash_cache( fn new_tree_hash_cache(
&self, &self,
depth: usize, depth: usize,

View File

@ -33,14 +33,14 @@ impl Into<Vec<u8>> for TreeHashCache {
impl TreeHashCache { impl TreeHashCache {
pub fn new<T>(item: &T, depth: usize) -> Result<Self, Error> pub fn new<T>(item: &T, depth: usize) -> Result<Self, Error>
where where
T: CachedTreeHash<T>, T: CachedTreeHash,
{ {
item.new_tree_hash_cache(depth) item.new_tree_hash_cache(depth)
} }
pub fn update<T>(&mut self, item: &T) -> Result<(), Error> pub fn update<T>(&mut self, item: &T) -> Result<(), Error>
where where
T: CachedTreeHash<T>, T: CachedTreeHash,
{ {
if self.is_empty() { if self.is_empty() {
Err(Error::CacheNotInitialized) Err(Error::CacheNotInitialized)
@ -62,7 +62,7 @@ impl TreeHashCache {
depth: usize, depth: usize,
) -> Result<Self, Error> ) -> Result<Self, Error>
where where
T: CachedTreeHash<T>, T: CachedTreeHash,
{ {
let overlay = BTreeOverlay::new(item, 0, depth); let overlay = BTreeOverlay::new(item, 0, depth);

View File

@ -34,7 +34,7 @@ pub struct NestedStruct {
fn test_routine<T>(original: T, modified: Vec<T>) fn test_routine<T>(original: T, modified: Vec<T>)
where where
T: CachedTreeHash<T> + std::fmt::Debug, T: CachedTreeHash + std::fmt::Debug,
{ {
let mut hasher = CachedTreeHasher::new(&original).unwrap(); let mut hasher = CachedTreeHasher::new(&original).unwrap();

View File

@ -56,7 +56,7 @@ pub fn subtree_derive(input: TokenStream) -> TokenStream {
let idents_c = idents_a.clone(); let idents_c = idents_a.clone();
let output = quote! { let output = quote! {
impl cached_tree_hash::CachedTreeHash<#name> for #name { impl cached_tree_hash::CachedTreeHash for #name {
fn new_tree_hash_cache(&self, depth: usize) -> Result<cached_tree_hash::TreeHashCache, cached_tree_hash::Error> { fn new_tree_hash_cache(&self, depth: usize) -> Result<cached_tree_hash::TreeHashCache, cached_tree_hash::Error> {
let tree = cached_tree_hash::TreeHashCache::from_leaves_and_subtrees( let tree = cached_tree_hash::TreeHashCache::from_leaves_and_subtrees(
self, self,

View File

@ -10,10 +10,7 @@ pub struct Inner {
pub d: u64, pub d: u64,
} }
fn test_standard_and_cached<T>(original: &T, modified: &T) fn test_standard_and_cached<T: CachedTreeHash>(original: &T, modified: &T) {
where
T: CachedTreeHash<T>,
{
// let mut cache = original.new_tree_hash_cache().unwrap(); // let mut cache = original.new_tree_hash_cache().unwrap();
let mut hasher = CachedTreeHasher::new(original).unwrap(); let mut hasher = CachedTreeHasher::new(original).unwrap();