From c52c5f4d6b5eb88354e51d8f63baaef94fb4da27 Mon Sep 17 00:00:00 2001 From: DragonDev1906 <8270201+DragonDev1906@users.noreply.github.com> Date: Thu, 5 Dec 2024 05:19:29 +0000 Subject: [PATCH] Remove derivative dependency (#37) * Remove derivative dependency * Tweak doc comments * Test hash impl * Test variable list Hash impl --------- Co-authored-by: Michael Sproul --- Cargo.toml | 1 - src/fixed_vector.rs | 32 +++++++++++++++++++++++++++++--- src/variable_list.rs | 32 +++++++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2d92c5a..bb653a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ ethereum_ssz = "0.8.0" serde = "1.0.0" serde_derive = "1.0.0" typenum = "1.12.0" -derivative = "2.1.1" smallvec = "1.8.0" arbitrary = { version = "1.0", features = ["derive"], optional = true } itertools = "0.13.0" diff --git a/src/fixed_vector.rs b/src/fixed_vector.rs index 76e182d..f4a2029 100644 --- a/src/fixed_vector.rs +++ b/src/fixed_vector.rs @@ -1,6 +1,5 @@ use crate::tree_hash::vec_tree_hash_root; use crate::Error; -use derivative::Derivative; use serde_derive::{Deserialize, Serialize}; use std::marker::PhantomData; use std::ops::{Deref, DerefMut, Index, IndexMut}; @@ -45,14 +44,26 @@ pub use typenum; /// let long: FixedVector<_, typenum::U5> = FixedVector::from(base); /// assert_eq!(&long[..], &[1, 2, 3, 4, 0]); /// ``` -#[derive(Debug, Clone, Serialize, Deserialize, Derivative)] -#[derivative(PartialEq, Eq, Hash(bound = "T: std::hash::Hash"))] +#[derive(Debug, Clone, Serialize, Deserialize)] #[serde(transparent)] pub struct FixedVector { vec: Vec, _phantom: PhantomData, } +// Implement comparison functions even if N doesn't implement PartialEq +impl PartialEq for FixedVector { + fn eq(&self, other: &Self) -> bool { + self.vec == other.vec + } +} +impl Eq for FixedVector {} +impl std::hash::Hash for FixedVector { + fn hash(&self, state: &mut H) { + self.vec.hash(state); + } +} + impl FixedVector { /// Returns `Ok` if the given `vec` equals the fixed length of `Self`. Otherwise returns /// `Err`. @@ -347,6 +358,7 @@ impl<'a, T: arbitrary::Arbitrary<'a>, N: 'static + Unsigned> arbitrary::Arbitrar mod test { use super::*; use ssz::*; + use std::collections::HashSet; use tree_hash::{merkle_root, TreeHash}; use tree_hash_derive::TreeHash; use typenum::*; @@ -502,4 +514,18 @@ mod test { merkle_root(&repeat(a.tree_hash_root().as_slice(), 16), 0) ); } + + #[test] + fn std_hash() { + let x: FixedVector = FixedVector::from(vec![3; 16]); + let y: FixedVector = FixedVector::from(vec![4; 16]); + let mut hashset = HashSet::new(); + + for value in [x.clone(), y.clone()] { + assert!(hashset.insert(value.clone())); + assert!(!hashset.insert(value.clone())); + assert!(hashset.contains(&value)); + } + assert_eq!(hashset.len(), 2); + } } diff --git a/src/variable_list.rs b/src/variable_list.rs index 9816839..7c1140b 100644 --- a/src/variable_list.rs +++ b/src/variable_list.rs @@ -1,6 +1,5 @@ use crate::tree_hash::vec_tree_hash_root; use crate::Error; -use derivative::Derivative; use serde_derive::{Deserialize, Serialize}; use std::marker::PhantomData; use std::ops::{Deref, DerefMut, Index, IndexMut}; @@ -47,14 +46,26 @@ pub use typenum; /// // Push a value to if it _does_ exceed the maximum. /// assert!(long.push(6).is_err()); /// ``` -#[derive(Debug, Clone, Serialize, Deserialize, Derivative)] -#[derivative(PartialEq, Eq, Hash(bound = "T: std::hash::Hash"))] +#[derive(Debug, Clone, Serialize, Deserialize)] #[serde(transparent)] pub struct VariableList { vec: Vec, _phantom: PhantomData, } +// Implement comparison functions even if N doesn't implement PartialEq +impl PartialEq for VariableList { + fn eq(&self, other: &Self) -> bool { + self.vec == other.vec + } +} +impl Eq for VariableList {} +impl std::hash::Hash for VariableList { + fn hash(&self, state: &mut H) { + self.vec.hash(state); + } +} + /// Maximum number of elements to pre-allocate in `try_from_iter`. /// /// Some variable lists have *very long* maximum lengths such that we can't actually fit them @@ -321,6 +332,7 @@ impl<'a, T: arbitrary::Arbitrary<'a>, N: 'static + Unsigned> arbitrary::Arbitrar mod test { use super::*; use ssz::*; + use std::collections::HashSet; use tree_hash::{merkle_root, TreeHash}; use tree_hash_derive::TreeHash; use typenum::*; @@ -548,4 +560,18 @@ mod test { List::try_from_iter(wonky_iter).unwrap() ); } + + #[test] + fn std_hash() { + let x: VariableList = VariableList::from(vec![3; 16]); + let y: VariableList = VariableList::from(vec![4; 16]); + let mut hashset = HashSet::new(); + + for value in [x.clone(), y.clone()] { + assert!(hashset.insert(value.clone())); + assert!(!hashset.insert(value.clone())); + assert!(hashset.contains(&value)); + } + assert_eq!(hashset.len(), 2); + } }