Macro cmp::compare_structs

source ·
macro_rules! compare_structs {
    ($expected:expr, $actual:expr, $($field:ident),+) => { ... };
}
Expand description

Macro which is mostly useful when writing assert! tests on structs.

use cmp::compare_structs;
let struct_a = A {
    a: 10,
    b: "str",
    c: [(1.0, 1.0), (2.0, 2.0)],
};
let struct_b = B {
    a: 10,
    b: "diff str",
    c: [(1.0, 1.0), (2.0, 2.0)],
};
compare_structs!(struct_a, struct_b, a, c);

Output singles-out fields in the struct which do not match:

thread 'tests::compare_different_structs' panicked at src/lib.rs:135:9:
c: [
    (
        1.0,
        1.0,
    ),
    (
        2.0,
        3.0,
    ),
] != [
    (
        1.0,
        1.0,
    ),
    (
        2.0,
        2.0,
    ),
]

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The main motivation behind this macro is for structs with many fields, where assert_eq!(struct_a, struct_b)’s output is difficult to read.

/// # Panics

Panics if any of the fields do not have partial equality.