## Proposed Changes Clippy 1.67.0 put us on blast for the size of some of our errors, most of them written by me ( 👀 ). This PR shrinks the size of `BeaconChainError` by dropping some extraneous info and boxing an inner error which should only occur infrequently anyway. For the `AttestationSlashInfo` and `BlockSlashInfo` I opted to ignore the lint as they are always used in a `Result<A, Info>` where `A` is a similar size. This means they don't bloat the size of the `Result`, so it's a bit annoying for Clippy to report this as an issue. I also chose to ignore `clippy::uninlined-format-args` because I think the benefit-to-churn ratio is too low. E.g. sometimes we have long identifiers in `format!` args and IMO the non-inlined form is easier to read: ```rust // I prefer this... format!( "{} did {} to {}", REALLY_LONG_CONSTANT_NAME, ANOTHER_REALLY_LONG_CONSTANT_NAME, regular_long_identifier_name ); // To this format!("{REALLY_LONG_CONSTANT_NAME} did {ANOTHER_REALLY_LONG_CONSTANT_NAME} to {regular_long_identifier_name}"); ``` I tried generating an automatic diff with `cargo clippy --fix` but it came out at: ``` 250 files changed, 1209 insertions(+), 1469 deletions(-) ``` Which seems like a bad idea when we'd have to back-merge it to `capella` and `eip4844` 😱
176 lines
4.5 KiB
Rust
176 lines
4.5 KiB
Rust
//! 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);
|
|
//!
|
|
//!
|
|
//!
|
|
//! // TODO:
|
|
//! ```
|
|
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 {
|
|
let mut children = vec![];
|
|
|
|
for i in 0..std::cmp::max(a.len(), b.len()) {
|
|
children.push(FieldComparison::new(format!("{i}"), &a.get(i), &b.get(i)));
|
|
}
|
|
|
|
Self::parent(field_name, a == b, 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()
|
|
}
|
|
}
|