From f20314bd8710a3c2fb90d78b3d0cc2473fd0ea9f Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 29 Apr 2019 15:32:41 +1000 Subject: [PATCH] Fix clippy lints, impl treehash for slices --- eth2/utils/cached_tree_hash/Cargo.toml | 3 - .../examples/8k_hashes_standard.rs | 1 - eth2/utils/cached_tree_hash/src/impls/vec.rs | 69 ++++++++++--------- eth2/utils/tree_hash/src/impls.rs | 45 +++++++----- 4 files changed, 64 insertions(+), 54 deletions(-) diff --git a/eth2/utils/cached_tree_hash/Cargo.toml b/eth2/utils/cached_tree_hash/Cargo.toml index c33f20add..7b331ad68 100644 --- a/eth2/utils/cached_tree_hash/Cargo.toml +++ b/eth2/utils/cached_tree_hash/Cargo.toml @@ -4,9 +4,6 @@ version = "0.1.0" authors = ["Paul Hauner "] edition = "2018" -[profile.release] -debug = true - [[bench]] name = "benches" harness = false diff --git a/eth2/utils/cached_tree_hash/examples/8k_hashes_standard.rs b/eth2/utils/cached_tree_hash/examples/8k_hashes_standard.rs index e5df7ea95..bcbb392e2 100644 --- a/eth2/utils/cached_tree_hash/examples/8k_hashes_standard.rs +++ b/eth2/utils/cached_tree_hash/examples/8k_hashes_standard.rs @@ -1,4 +1,3 @@ -use cached_tree_hash::TreeHashCache; use ethereum_types::H256 as Hash256; use tree_hash::TreeHash; diff --git a/eth2/utils/cached_tree_hash/src/impls/vec.rs b/eth2/utils/cached_tree_hash/src/impls/vec.rs index b04884636..89f8b85db 100644 --- a/eth2/utils/cached_tree_hash/src/impls/vec.rs +++ b/eth2/utils/cached_tree_hash/src/impls/vec.rs @@ -2,46 +2,53 @@ use super::*; use crate::btree_overlay::LeafNode; use crate::merkleize::{merkleize, num_sanitized_leaves, sanitise_bytes}; -impl CachedTreeHash for Vec -where - T: CachedTreeHash + TreeHash, -{ - fn new_tree_hash_cache(&self, depth: usize) -> Result { - let (mut cache, schema) = new_tree_hash_cache(self, depth)?; +macro_rules! impl_for_list { + ($type: ty) => { + impl CachedTreeHash for $type + where + T: CachedTreeHash + TreeHash, + { + fn new_tree_hash_cache(&self, depth: usize) -> Result { + let (mut cache, schema) = new_tree_hash_cache(self, depth)?; - cache.add_length_nodes(schema.into_overlay(0).chunk_range(), self.len())?; + cache.add_length_nodes(schema.into_overlay(0).chunk_range(), self.len())?; - Ok(cache) - } + Ok(cache) + } - fn num_tree_hash_cache_chunks(&self) -> usize { - // Add two extra nodes to cater for the node before and after to allow mixing-in length. - BTreeOverlay::new(self, 0, 0).num_chunks() + 2 - } + fn num_tree_hash_cache_chunks(&self) -> usize { + // Add two extra nodes to cater for the node before and after to allow mixing-in length. + BTreeOverlay::new(self, 0, 0).num_chunks() + 2 + } - fn tree_hash_cache_schema(&self, depth: usize) -> BTreeSchema { - produce_schema(self, depth) - } + fn tree_hash_cache_schema(&self, depth: usize) -> BTreeSchema { + produce_schema(self, depth) + } - fn update_tree_hash_cache(&self, cache: &mut TreeHashCache) -> Result<(), Error> { - // Skip the length-mixed-in root node. - cache.chunk_index += 1; + fn update_tree_hash_cache(&self, cache: &mut TreeHashCache) -> Result<(), Error> { + // Skip the length-mixed-in root node. + cache.chunk_index += 1; - // Update the cache, returning the new overlay. - let new_overlay = update_tree_hash_cache(self, cache)?; + // Update the cache, returning the new overlay. + let new_overlay = update_tree_hash_cache(&self, cache)?; - // Mix in length - cache.mix_in_length(new_overlay.chunk_range(), self.len())?; + // Mix in length + cache.mix_in_length(new_overlay.chunk_range(), self.len())?; - // Skip an extra node to clear the length node. - cache.chunk_index += 1; + // Skip an extra node to clear the length node. + cache.chunk_index += 1; - Ok(()) - } + Ok(()) + } + } + }; } +impl_for_list!(Vec); +impl_for_list!(&[T]); + pub fn new_tree_hash_cache( - vec: &Vec, + vec: &[T], depth: usize, ) -> Result<(TreeHashCache, BTreeSchema), Error> { let schema = vec.tree_hash_cache_schema(depth); @@ -58,7 +65,7 @@ pub fn new_tree_hash_cache( .map(|item| TreeHashCache::new(item, depth + 1)) .collect::, _>>()?; - TreeHashCache::from_leaves_and_subtrees(vec, subtrees, depth) + TreeHashCache::from_leaves_and_subtrees(&vec, subtrees, depth) } }?; @@ -91,11 +98,11 @@ pub fn produce_schema(vec: &[T], depth: usize) -> BTreeSchema #[allow(clippy::range_plus_one)] // Minor readability lint requiring structural changes; not worth it. pub fn update_tree_hash_cache( - vec: &Vec, + vec: &[T], cache: &mut TreeHashCache, ) -> Result { 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())?; diff --git a/eth2/utils/tree_hash/src/impls.rs b/eth2/utils/tree_hash/src/impls.rs index 56eb7dbf4..42ea9add0 100644 --- a/eth2/utils/tree_hash/src/impls.rs +++ b/eth2/utils/tree_hash/src/impls.rs @@ -87,31 +87,38 @@ impl TreeHash for H256 { } } -impl TreeHash for Vec -where - T: TreeHash, -{ - fn tree_hash_type() -> TreeHashType { - TreeHashType::List - } +macro_rules! impl_for_list { + ($type: ty) => { + impl TreeHash for $type + where + T: TreeHash, + { + fn tree_hash_type() -> TreeHashType { + TreeHashType::List + } - fn tree_hash_packed_encoding(&self) -> Vec { - unreachable!("List should never be packed.") - } + fn tree_hash_packed_encoding(&self) -> Vec { + unreachable!("List should never be packed.") + } - fn tree_hash_packing_factor() -> usize { - unreachable!("List should never be packed.") - } + fn tree_hash_packing_factor() -> usize { + unreachable!("List should never be packed.") + } - fn tree_hash_root(&self) -> Vec { - let mut root_and_len = Vec::with_capacity(HASHSIZE * 2); - root_and_len.append(&mut vec_tree_hash_root(self)); - root_and_len.append(&mut int_to_bytes32(self.len() as u64)); + fn tree_hash_root(&self) -> Vec { + let mut root_and_len = Vec::with_capacity(HASHSIZE * 2); + root_and_len.append(&mut vec_tree_hash_root(self)); + root_and_len.append(&mut int_to_bytes32(self.len() as u64)); - hash(&root_and_len) - } + hash(&root_and_len) + } + } + }; } +impl_for_list!(Vec); +impl_for_list!(&[T]); + pub fn vec_tree_hash_root(vec: &[T]) -> Vec where T: TreeHash,