mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-18 21:38:31 +00:00
Remove compare_fields and import from crates.io (#8189)
Use the recently published `compare_fields` and remove it from Lighthouse https://crates.io/crates/compare_fields Co-Authored-By: Mac L <mjladson@pm.me>
This commit is contained in:
12
Cargo.lock
generated
12
Cargo.lock
generated
@@ -1658,15 +1658,19 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "compare_fields"
|
||||
version = "0.2.0"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05162add7c8618791829528194a271dca93f69194d35b19db1ca7fbfb8275278"
|
||||
dependencies = [
|
||||
"compare_fields_derive",
|
||||
"itertools 0.10.5",
|
||||
"itertools 0.14.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "compare_fields_derive"
|
||||
version = "0.2.0"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f5ee468b2e568b668e2a686112935e7bbe9a81bf4fa6b9f6fc3410ea45fb7ce"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
@@ -2539,7 +2543,6 @@ dependencies = [
|
||||
"beacon_chain",
|
||||
"bls",
|
||||
"compare_fields",
|
||||
"compare_fields_derive",
|
||||
"context_deserialize",
|
||||
"derivative",
|
||||
"eth2_network_config",
|
||||
@@ -9903,7 +9906,6 @@ dependencies = [
|
||||
"beacon_chain",
|
||||
"bls",
|
||||
"compare_fields",
|
||||
"compare_fields_derive",
|
||||
"context_deserialize",
|
||||
"criterion",
|
||||
"derivative",
|
||||
|
||||
@@ -19,8 +19,6 @@ members = [
|
||||
"boot_node",
|
||||
"common/account_utils",
|
||||
"common/clap_utils",
|
||||
"common/compare_fields",
|
||||
"common/compare_fields_derive",
|
||||
"common/deposit_contract",
|
||||
"common/directory",
|
||||
"common/eip_3076",
|
||||
@@ -121,8 +119,7 @@ c-kzg = { version = "2.1", default-features = false }
|
||||
cargo_metadata = "0.19"
|
||||
clap = { version = "4.5.4", features = ["derive", "cargo", "wrap_help"] }
|
||||
clap_utils = { path = "common/clap_utils" }
|
||||
compare_fields = { path = "common/compare_fields" }
|
||||
compare_fields_derive = { path = "common/compare_fields_derive" }
|
||||
compare_fields = "0.1"
|
||||
console-subscriber = "0.4"
|
||||
context_deserialize = { path = "consensus/context_deserialize/context_deserialize", features = [
|
||||
"all",
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
[package]
|
||||
name = "compare_fields"
|
||||
version = "0.2.0"
|
||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
edition = { workspace = true }
|
||||
|
||||
[package.metadata.cargo-udeps.ignore]
|
||||
development = ["compare_fields_derive"] # used in doc-tests
|
||||
|
||||
[dependencies]
|
||||
itertools = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
compare_fields_derive = { workspace = true }
|
||||
@@ -1,197 +0,0 @@
|
||||
//! Provides field-by-field comparisons for structs and vecs.
|
||||
//!
|
||||
//! Returns comparisons as data, without making assumptions about the desired equality (e.g.,
|
||||
//! does not `panic!` on inequality).
|
||||
//!
|
||||
//! Note: `compare_fields_derive` requires `PartialEq` and `Debug` implementations.
|
||||
//!
|
||||
//! ## Example
|
||||
//!
|
||||
//! ```rust
|
||||
//! use compare_fields::{CompareFields, Comparison, FieldComparison};
|
||||
//! use compare_fields_derive::CompareFields;
|
||||
//!
|
||||
//! #[derive(PartialEq, Debug, CompareFields)]
|
||||
//! pub struct Bar {
|
||||
//! a: u64,
|
||||
//! b: u16,
|
||||
//! #[compare_fields(as_slice)]
|
||||
//! c: Vec<Foo>
|
||||
//! }
|
||||
//!
|
||||
//! #[derive(Clone, PartialEq, Debug, CompareFields)]
|
||||
//! pub struct Foo {
|
||||
//! d: String
|
||||
//! }
|
||||
//!
|
||||
//! let cat = Foo {d: "cat".to_string()};
|
||||
//! let dog = Foo {d: "dog".to_string()};
|
||||
//! let chicken = Foo {d: "chicken".to_string()};
|
||||
//!
|
||||
//! let mut bar_a = Bar {
|
||||
//! a: 42,
|
||||
//! b: 12,
|
||||
//! c: vec![ cat.clone(), dog.clone() ],
|
||||
//! };
|
||||
//!
|
||||
//! let mut bar_b = Bar {
|
||||
//! a: 42,
|
||||
//! b: 99,
|
||||
//! c: vec![ chicken.clone(), dog.clone()]
|
||||
//! };
|
||||
//!
|
||||
//! let cat_dog = Comparison::Child(FieldComparison {
|
||||
//! field_name: "d".to_string(),
|
||||
//! equal: false,
|
||||
//! a: "\"cat\"".to_string(),
|
||||
//! b: "\"dog\"".to_string(),
|
||||
//! });
|
||||
//! assert_eq!(cat.compare_fields(&dog), vec![cat_dog]);
|
||||
//!
|
||||
//! let bar_a_b = vec![
|
||||
//! Comparison::Child(FieldComparison {
|
||||
//! field_name: "a".to_string(),
|
||||
//! equal: true,
|
||||
//! a: "42".to_string(),
|
||||
//! b: "42".to_string(),
|
||||
//! }),
|
||||
//! Comparison::Child(FieldComparison {
|
||||
//! field_name: "b".to_string(),
|
||||
//! equal: false,
|
||||
//! a: "12".to_string(),
|
||||
//! b: "99".to_string(),
|
||||
//! }),
|
||||
//! Comparison::Parent{
|
||||
//! field_name: "c".to_string(),
|
||||
//! equal: false,
|
||||
//! children: vec![
|
||||
//! FieldComparison {
|
||||
//! field_name: "0".to_string(),
|
||||
//! equal: false,
|
||||
//! a: "Some(Foo { d: \"cat\" })".to_string(),
|
||||
//! b: "Some(Foo { d: \"chicken\" })".to_string(),
|
||||
//! },
|
||||
//! FieldComparison {
|
||||
//! field_name: "1".to_string(),
|
||||
//! equal: true,
|
||||
//! a: "Some(Foo { d: \"dog\" })".to_string(),
|
||||
//! b: "Some(Foo { d: \"dog\" })".to_string(),
|
||||
//! }
|
||||
//! ]
|
||||
//! }
|
||||
//! ];
|
||||
//! assert_eq!(bar_a.compare_fields(&bar_b), bar_a_b);
|
||||
//! ```
|
||||
use itertools::{EitherOrBoth, Itertools};
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Comparison {
|
||||
Child(FieldComparison),
|
||||
Parent {
|
||||
field_name: String,
|
||||
equal: bool,
|
||||
children: Vec<FieldComparison>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Comparison {
|
||||
pub fn child<T: Debug + PartialEq<T>>(field_name: String, a: &T, b: &T) -> Self {
|
||||
Comparison::Child(FieldComparison::new(field_name, a, b))
|
||||
}
|
||||
|
||||
pub fn parent(field_name: String, equal: bool, children: Vec<FieldComparison>) -> Self {
|
||||
Comparison::Parent {
|
||||
field_name,
|
||||
equal,
|
||||
children,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_slice<T: Debug + PartialEq<T>>(field_name: String, a: &[T], b: &[T]) -> Self {
|
||||
Self::from_iter(field_name, a.iter(), b.iter())
|
||||
}
|
||||
|
||||
pub fn from_into_iter<'a, T: Debug + PartialEq + 'a>(
|
||||
field_name: String,
|
||||
a: impl IntoIterator<Item = &'a T>,
|
||||
b: impl IntoIterator<Item = &'a T>,
|
||||
) -> Self {
|
||||
Self::from_iter(field_name, a.into_iter(), b.into_iter())
|
||||
}
|
||||
|
||||
pub fn from_iter<'a, T: Debug + PartialEq + 'a>(
|
||||
field_name: String,
|
||||
a: impl Iterator<Item = &'a T>,
|
||||
b: impl Iterator<Item = &'a T>,
|
||||
) -> Self {
|
||||
let mut children = vec![];
|
||||
let mut all_equal = true;
|
||||
|
||||
for (i, entry) in a.zip_longest(b).enumerate() {
|
||||
let comparison = match entry {
|
||||
EitherOrBoth::Both(x, y) => {
|
||||
FieldComparison::new(format!("{i}"), &Some(x), &Some(y))
|
||||
}
|
||||
EitherOrBoth::Left(x) => FieldComparison::new(format!("{i}"), &Some(x), &None),
|
||||
EitherOrBoth::Right(y) => FieldComparison::new(format!("{i}"), &None, &Some(y)),
|
||||
};
|
||||
all_equal = all_equal && comparison.equal();
|
||||
children.push(comparison);
|
||||
}
|
||||
|
||||
Self::parent(field_name, all_equal, children)
|
||||
}
|
||||
|
||||
pub fn retain_children<F>(&mut self, f: F)
|
||||
where
|
||||
F: FnMut(&FieldComparison) -> bool,
|
||||
{
|
||||
match self {
|
||||
Comparison::Child(_) => (),
|
||||
Comparison::Parent { children, .. } => children.retain(f),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn equal(&self) -> bool {
|
||||
match self {
|
||||
Comparison::Child(fc) => fc.equal,
|
||||
Comparison::Parent { equal, .. } => *equal,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn not_equal(&self) -> bool {
|
||||
!self.equal()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct FieldComparison {
|
||||
pub field_name: String,
|
||||
pub equal: bool,
|
||||
pub a: String,
|
||||
pub b: String,
|
||||
}
|
||||
|
||||
pub trait CompareFields {
|
||||
fn compare_fields(&self, b: &Self) -> Vec<Comparison>;
|
||||
}
|
||||
|
||||
impl FieldComparison {
|
||||
pub fn new<T: Debug + PartialEq<T>>(field_name: String, a: &T, b: &T) -> Self {
|
||||
Self {
|
||||
field_name,
|
||||
equal: a == b,
|
||||
a: format!("{a:?}"),
|
||||
b: format!("{b:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn equal(&self) -> bool {
|
||||
self.equal
|
||||
}
|
||||
|
||||
pub fn not_equal(&self) -> bool {
|
||||
!self.equal()
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
[package]
|
||||
name = "compare_fields_derive"
|
||||
version = "0.2.0"
|
||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
edition = { workspace = true }
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
quote = { workspace = true }
|
||||
syn = { workspace = true }
|
||||
@@ -1,70 +0,0 @@
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{DeriveInput, parse_macro_input};
|
||||
|
||||
fn is_iter(field: &syn::Field) -> bool {
|
||||
field.attrs.iter().any(|attr| {
|
||||
attr.path.is_ident("compare_fields")
|
||||
&& (attr.tokens.to_string().replace(' ', "") == "(as_slice)"
|
||||
|| attr.tokens.to_string().replace(' ', "") == "(as_iter)")
|
||||
})
|
||||
}
|
||||
|
||||
#[proc_macro_derive(CompareFields, attributes(compare_fields))]
|
||||
pub fn compare_fields_derive(input: TokenStream) -> TokenStream {
|
||||
let item = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
let name = &item.ident;
|
||||
let (impl_generics, ty_generics, where_clause) = &item.generics.split_for_impl();
|
||||
|
||||
let syn::Data::Struct(struct_data) = &item.data else {
|
||||
panic!("compare_fields_derive only supports structs.");
|
||||
};
|
||||
|
||||
let mut quotes = vec![];
|
||||
|
||||
for field in struct_data.fields.iter() {
|
||||
let Some(ident_a) = &field.ident else {
|
||||
panic!("compare_fields_derive only supports named struct fields.");
|
||||
};
|
||||
let field_name = ident_a.to_string();
|
||||
let ident_b = ident_a.clone();
|
||||
|
||||
let quote = if is_iter(field) {
|
||||
quote! {
|
||||
comparisons.push(compare_fields::Comparison::from_into_iter(
|
||||
#field_name.to_string(),
|
||||
&self.#ident_a,
|
||||
&b.#ident_b
|
||||
));
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
comparisons.push(
|
||||
compare_fields::Comparison::child(
|
||||
#field_name.to_string(),
|
||||
&self.#ident_a,
|
||||
&b.#ident_b
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
quotes.push(quote);
|
||||
}
|
||||
|
||||
let output = quote! {
|
||||
impl #impl_generics compare_fields::CompareFields for #name #ty_generics #where_clause {
|
||||
fn compare_fields(&self, b: &Self) -> Vec<compare_fields::Comparison> {
|
||||
let mut comparisons = vec![];
|
||||
|
||||
#(
|
||||
#quotes
|
||||
)*
|
||||
|
||||
comparisons
|
||||
}
|
||||
}
|
||||
};
|
||||
output.into()
|
||||
}
|
||||
@@ -29,7 +29,6 @@ alloy-rlp = { version = "0.3.4", features = ["derive"] }
|
||||
arbitrary = { workspace = true, features = ["derive"], optional = true }
|
||||
bls = { workspace = true }
|
||||
compare_fields = { workspace = true }
|
||||
compare_fields_derive = { workspace = true }
|
||||
context_deserialize = { workspace = true }
|
||||
derivative = { workspace = true }
|
||||
eth2_interop_keypairs = { path = "../../common/eth2_interop_keypairs" }
|
||||
|
||||
@@ -5,7 +5,6 @@ use crate::historical_summary::HistoricalSummary;
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::*;
|
||||
use compare_fields::CompareFields;
|
||||
use compare_fields_derive::CompareFields;
|
||||
use derivative::Derivative;
|
||||
use ethereum_hashing::hash;
|
||||
use int_to_bytes::{int_to_bytes4, int_to_bytes8};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::context_deserialize;
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::{BeaconState, EthSpec, ForkName, Hash256};
|
||||
use compare_fields_derive::CompareFields;
|
||||
use compare_fields::CompareFields;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
|
||||
@@ -16,7 +16,6 @@ alloy-primitives = { workspace = true }
|
||||
beacon_chain = { workspace = true }
|
||||
bls = { workspace = true }
|
||||
compare_fields = { workspace = true }
|
||||
compare_fields_derive = { workspace = true }
|
||||
context_deserialize = { workspace = true }
|
||||
derivative = { workspace = true }
|
||||
eth2_network_config = { workspace = true }
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
use crate::case_result::compare_result_detailed;
|
||||
use crate::decode::{ssz_decode_file, ssz_decode_state, yaml_decode_file};
|
||||
use compare_fields_derive::CompareFields;
|
||||
use compare_fields::CompareFields;
|
||||
use serde::Deserialize;
|
||||
use ssz::four_byte_option_impl;
|
||||
use ssz_derive::{Decode, Encode};
|
||||
|
||||
Reference in New Issue
Block a user