Fix clippy lints, impl treehash for slices

This commit is contained in:
Paul Hauner 2019-04-29 15:32:41 +10:00
parent 240d1e197a
commit f20314bd87
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
4 changed files with 64 additions and 54 deletions

View File

@ -4,9 +4,6 @@ version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"] authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018" edition = "2018"
[profile.release]
debug = true
[[bench]] [[bench]]
name = "benches" name = "benches"
harness = false harness = false

View File

@ -1,4 +1,3 @@
use cached_tree_hash::TreeHashCache;
use ethereum_types::H256 as Hash256; use ethereum_types::H256 as Hash256;
use tree_hash::TreeHash; use tree_hash::TreeHash;

View File

@ -2,7 +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 for Vec<T> macro_rules! impl_for_list {
($type: ty) => {
impl<T> CachedTreeHash for $type
where where
T: CachedTreeHash + TreeHash, T: CachedTreeHash + TreeHash,
{ {
@ -28,7 +30,7 @@ where
cache.chunk_index += 1; cache.chunk_index += 1;
// Update the cache, returning the new overlay. // Update the cache, returning the new overlay.
let new_overlay = update_tree_hash_cache(self, cache)?; let new_overlay = update_tree_hash_cache(&self, cache)?;
// Mix in length // Mix in length
cache.mix_in_length(new_overlay.chunk_range(), self.len())?; cache.mix_in_length(new_overlay.chunk_range(), self.len())?;
@ -39,9 +41,14 @@ where
Ok(()) Ok(())
} }
} }
};
}
impl_for_list!(Vec<T>);
impl_for_list!(&[T]);
pub fn new_tree_hash_cache<T: CachedTreeHash>( pub fn new_tree_hash_cache<T: CachedTreeHash>(
vec: &Vec<T>, vec: &[T],
depth: usize, depth: usize,
) -> Result<(TreeHashCache, BTreeSchema), Error> { ) -> Result<(TreeHashCache, BTreeSchema), Error> {
let schema = vec.tree_hash_cache_schema(depth); let schema = vec.tree_hash_cache_schema(depth);
@ -58,7 +65,7 @@ pub fn new_tree_hash_cache<T: CachedTreeHash>(
.map(|item| TreeHashCache::new(item, depth + 1)) .map(|item| TreeHashCache::new(item, depth + 1))
.collect::<Result<Vec<TreeHashCache>, _>>()?; .collect::<Result<Vec<TreeHashCache>, _>>()?;
TreeHashCache::from_leaves_and_subtrees(vec, subtrees, depth) TreeHashCache::from_leaves_and_subtrees(&vec, subtrees, depth)
} }
}?; }?;
@ -91,11 +98,11 @@ pub fn produce_schema<T: CachedTreeHash>(vec: &[T], depth: usize) -> BTreeSchema
#[allow(clippy::range_plus_one)] // Minor readability lint requiring structural changes; not worth it. #[allow(clippy::range_plus_one)] // Minor readability lint requiring structural changes; not worth it.
pub fn update_tree_hash_cache<T: CachedTreeHash>( pub fn update_tree_hash_cache<T: CachedTreeHash>(
vec: &Vec<T>, vec: &[T],
cache: &mut TreeHashCache, cache: &mut TreeHashCache,
) -> Result<BTreeOverlay, Error> { ) -> Result<BTreeOverlay, Error> {
let old_overlay = cache.get_overlay(cache.schema_index, cache.chunk_index)?; let old_overlay = cache.get_overlay(cache.schema_index, cache.chunk_index)?;
let new_overlay = BTreeOverlay::new(vec, cache.chunk_index, old_overlay.depth); let new_overlay = BTreeOverlay::new(&vec, cache.chunk_index, old_overlay.depth);
cache.replace_overlay(cache.schema_index, cache.chunk_index, new_overlay.clone())?; cache.replace_overlay(cache.schema_index, cache.chunk_index, new_overlay.clone())?;

View File

@ -87,7 +87,9 @@ impl TreeHash for H256 {
} }
} }
impl<T> TreeHash for Vec<T> macro_rules! impl_for_list {
($type: ty) => {
impl<T> TreeHash for $type
where where
T: TreeHash, T: TreeHash,
{ {
@ -111,6 +113,11 @@ where
hash(&root_and_len) hash(&root_and_len)
} }
} }
};
}
impl_for_list!(Vec<T>);
impl_for_list!(&[T]);
pub fn vec_tree_hash_root<T>(vec: &[T]) -> Vec<u8> pub fn vec_tree_hash_root<T>(vec: &[T]) -> Vec<u8>
where where