#struct-fields #fields #compare #macro #struct #field

cmp

Simple, convenience macros and function for comparing

1 unstable release

0.1.0 Jun 6, 2024

#1075 in Development tools

Download history 37/week @ 2024-07-23 39/week @ 2024-07-30 15/week @ 2024-08-06 570/week @ 2024-08-13 961/week @ 2024-08-20 656/week @ 2024-08-27 683/week @ 2024-09-03 882/week @ 2024-09-10 875/week @ 2024-09-17 1235/week @ 2024-09-24 890/week @ 2024-10-01 737/week @ 2024-10-08 924/week @ 2024-10-15 1264/week @ 2024-10-22 948/week @ 2024-10-29 836/week @ 2024-11-05

4,088 downloads per month

MIT license

7KB
99 lines

cmp

cmp is a Rust crate that provides a macro for comparing fields in structs, which is particularly useful when writing assert! tests.

Usage

To use this crate, add the following to your Cargo.toml:

[dependencies]
cmp = "0.1.0"

Then, in your Rust file:

use cmp::compare_structs;

compare_structs!

The compare_structs! macro compares specified fields of two structs. If the fields do not match, the macro will panic and output the fields that do not match.

Example

use cmp::compare_structs;

struct A<'a> {
    a: i32,
    b: &'a str,
    c: [(f64, f32); 2],
}

struct B<'a> {
    a: i32,
    b: &'a str,
    c: [(f64, f32); 2],
}

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);

In this example, the compare_structs! macro compares the a and c fields of struct_a and struct_b. If they do not match, the macro will panic and output the fields that do not match.

Output

The output of the compare_structs! macro singles out the fields in the structs that do not match. For example:

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

In this output, the c field of the two structs do not match, and the macro outputs the differing values.

No runtime deps