Macro xpct::fields

source ·
macro_rules! fields {
    (
        $struct_type:ty {
            $(
                $field_name:tt: $matcher:expr
            ),+
            $(,)?
        }
    ) => { ... };
}
Expand description

Apply matchers to multiple struct fields.

This macro is meant to be used with the match_fields and match_any_fields matchers. It provides a Rust-like syntax for mapping struct fields to matchers.

This macro returns an opaque FieldsSpec value that can be passed to match_fields or match_any_fields.

The syntax of this macro looks like this:

use xpct::{fields, equal, be_ge};

struct Person {
    name: String,
    age: u32,
}

fields!(Person {
    name: equal("Jean Vicquemare"),
    age: be_ge(34),
});

// You can omit fields.
fields!(Person {
    name: equal("Jean Vicquemare"),
});

This macro also supports tuple structs; the syntax is identical, except you replace the field names with indices.

use xpct::{fields, equal};

struct Point(u64, u64);

fields!(Point {
    0: equal(41),
    1: equal(57),
});

The syntax for tuple structs looks different from the Rust syntax because it makes it easy to skip fields:

fields!(Point {
    1: equal(57),
});