vector_common/
event_data_eq.rs

1/// A related trait to `PartialEq`, `EventDataEq` tests if two events
2/// contain the same data, exclusive of the metadata. This is used to
3/// test for events having the same values but potentially different
4/// parts of the metadata that not fixed between runs, without removing
5/// the ability to compare them for exact equality.
6pub trait EventDataEq<Rhs: ?Sized = Self> {
7    fn event_data_eq(&self, other: &Rhs) -> bool;
8}
9
10#[allow(clippy::module_name_repetitions)] // full name is better since this macro is typically imported
11#[macro_export]
12macro_rules! assert_event_data_eq {
13    ($left:expr, $right:expr, $message:expr) => {{
14        use $crate::EventDataEq as _;
15        match (&($left), &($right)) {
16            (left, right) => {
17                assert!(
18                    left.event_data_eq(right),
19                    "assertion failed: {}\n\n{}\n",
20                    $message,
21                    similar_asserts::SimpleDiff::from_str(
22                        format!("{:#?}", left).as_str(),
23                        format!("{:#?}", right).as_str(),
24                        "left",
25                        "right"
26                    ),
27                );
28            }
29        }
30    }};
31    ($left:expr, $right:expr,) => {
32        $crate::assert_event_data_eq!($left, $right)
33    };
34    ($left:expr, $right:expr) => {
35        $crate::assert_event_data_eq!($left, $right, "`left.event_data_eq(right)`")
36    };
37}
38
39#[allow(clippy::module_name_repetitions)] // full name is better since this macro is typically imported
40#[macro_export]
41macro_rules! impl_event_data_eq {
42    ($type:ty) => {
43        impl $crate::EventDataEq for $type {
44            fn event_data_eq(&self, other: &Self) -> bool {
45                self == other
46            }
47        }
48    };
49}
50
51impl<T: EventDataEq> EventDataEq for &[T] {
52    fn event_data_eq(&self, other: &Self) -> bool {
53        self.len() == other.len()
54            && self
55                .iter()
56                .zip(other.iter())
57                .all(|(a, b)| a.event_data_eq(b))
58    }
59}
60
61impl<T: EventDataEq> EventDataEq for Vec<T> {
62    fn event_data_eq(&self, other: &Self) -> bool {
63        self.as_slice().event_data_eq(&other.as_slice())
64    }
65}
66
67impl<T: EventDataEq> EventDataEq for Option<T> {
68    fn event_data_eq(&self, other: &Self) -> bool {
69        match (self, other) {
70            (None, None) => true,
71            (Some(left), Some(right)) => left.event_data_eq(right),
72            _ => false,
73        }
74    }
75}
76
77impl<R: EventDataEq, E: EventDataEq> EventDataEq for Result<R, E> {
78    fn event_data_eq(&self, other: &Self) -> bool {
79        match (self, other) {
80            (Ok(left), Ok(right)) => left.event_data_eq(right),
81            (Err(left), Err(right)) => left.event_data_eq(right),
82            _ => false,
83        }
84    }
85}