diff --git a/Cargo.toml b/Cargo.toml index 0384d55..60463ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,3 +25,7 @@ itertools = "0.13.0" [dev-dependencies] serde_json = "1.0.0" tree_hash_derive = "0.8.0" + +[patch.crates-io] +tree_hash = { git = "https://github.com/sigp/tree_hash", branch = "self-by-value" } +tree_hash_derive = { git = "https://github.com/sigp/tree_hash", branch = "self-by-value" } diff --git a/src/bitfield.rs b/src/bitfield.rs index dedaf7a..8ddeb2e 100644 --- a/src/bitfield.rs +++ b/src/bitfield.rs @@ -643,12 +643,12 @@ impl<'de, N: Unsigned + Clone> Deserialize<'de> for Bitfield> { } } -impl tree_hash::TreeHash for Bitfield> { +impl tree_hash::TreeHash for &Bitfield> { fn tree_hash_type() -> tree_hash::TreeHashType { tree_hash::TreeHashType::List } - fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding { + fn tree_hash_packed_encoding(self) -> tree_hash::PackedEncoding { unreachable!("List should never be packed.") } @@ -656,7 +656,7 @@ impl tree_hash::TreeHash for Bitfield> { unreachable!("List should never be packed.") } - fn tree_hash_root(&self) -> Hash256 { + fn tree_hash_root(self) -> Hash256 { // Note: we use `as_slice` because it does _not_ have the length-delimiting bit set (or // present). let root = bitfield_bytes_tree_hash_root::(self.as_slice()); @@ -664,12 +664,12 @@ impl tree_hash::TreeHash for Bitfield> { } } -impl tree_hash::TreeHash for Bitfield> { +impl tree_hash::TreeHash for &Bitfield> { fn tree_hash_type() -> tree_hash::TreeHashType { tree_hash::TreeHashType::Vector } - fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding { + fn tree_hash_packed_encoding(self) -> tree_hash::PackedEncoding { unreachable!("Vector should never be packed.") } @@ -677,7 +677,7 @@ impl tree_hash::TreeHash for Bitfield> { unreachable!("Vector should never be packed.") } - fn tree_hash_root(&self) -> Hash256 { + fn tree_hash_root(self) -> Hash256 { bitfield_bytes_tree_hash_root::(self.as_slice()) } } diff --git a/src/fixed_vector.rs b/src/fixed_vector.rs index 939fbd8..cdd1a44 100644 --- a/src/fixed_vector.rs +++ b/src/fixed_vector.rs @@ -177,15 +177,15 @@ impl IntoIterator for FixedVector { } } -impl tree_hash::TreeHash for FixedVector +impl<'a, T, N: Unsigned> tree_hash::TreeHash for &'a FixedVector where - T: tree_hash::TreeHash, + &'a T: tree_hash::TreeHash, { fn tree_hash_type() -> tree_hash::TreeHashType { tree_hash::TreeHashType::Vector } - fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding { + fn tree_hash_packed_encoding(self) -> tree_hash::PackedEncoding { unreachable!("Vector should never be packed.") } @@ -193,7 +193,7 @@ where unreachable!("Vector should never be packed.") } - fn tree_hash_root(&self) -> Hash256 { + fn tree_hash_root(self) -> Hash256 { vec_tree_hash_root::(&self.vec) } } diff --git a/src/tree_hash.rs b/src/tree_hash.rs index d1509c3..d84df8c 100644 --- a/src/tree_hash.rs +++ b/src/tree_hash.rs @@ -3,15 +3,16 @@ use typenum::Unsigned; /// A helper function providing common functionality between the `TreeHash` implementations for /// `FixedVector` and `VariableList`. -pub fn vec_tree_hash_root(vec: &[T]) -> Hash256 +pub fn vec_tree_hash_root<'a, T: 'a, N>(vec: &'a [T]) -> Hash256 where - T: TreeHash, + &'a T: TreeHash, N: Unsigned, { - match T::tree_hash_type() { + match <&T>::tree_hash_type() { TreeHashType::Basic => { let mut hasher = MerkleHasher::with_leaves( - (N::to_usize() + T::tree_hash_packing_factor() - 1) / T::tree_hash_packing_factor(), + (N::to_usize() + <&T>::tree_hash_packing_factor() - 1) + / <&T>::tree_hash_packing_factor(), ); for item in vec { diff --git a/src/variable_list.rs b/src/variable_list.rs index 9816839..f0a6101 100644 --- a/src/variable_list.rs +++ b/src/variable_list.rs @@ -193,15 +193,15 @@ impl IntoIterator for VariableList { } } -impl tree_hash::TreeHash for VariableList +impl<'a, T, N: Unsigned> tree_hash::TreeHash for &'a VariableList where - T: tree_hash::TreeHash, + &'a T: tree_hash::TreeHash, { fn tree_hash_type() -> tree_hash::TreeHashType { tree_hash::TreeHashType::List } - fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding { + fn tree_hash_packed_encoding(self) -> tree_hash::PackedEncoding { unreachable!("List should never be packed.") } @@ -209,7 +209,7 @@ where unreachable!("List should never be packed.") } - fn tree_hash_root(&self) -> Hash256 { + fn tree_hash_root(self) -> Hash256 { let root = vec_tree_hash_root::(&self.vec); tree_hash::mix_in_length(&root, self.len())