macro_rules! field {
    ($($t:tt)*) => { ... };
}
Expand description

Matches a structure or enum with a given field which is matched by a given matcher.

This takes two arguments:

  • a specification of the field against which to match, and
  • an inner Matcher to apply to that field.

For example:

#[derive(Debug)]
struct IntField {
  int: i32
}
verify_that!(IntField{int: 32}, field!(IntField.int, eq(32)))?;

Tuple structs are also supported via the index syntax:

#[derive(Debug)]
struct IntField(i32);
verify_that!(IntField(32), field!(IntField.0, eq(32)))?;

Enums are also supported, in which case only the specified variant is matched:

#[derive(Debug)]
enum MyEnum {
    A(i32),
    B,
}
verify_that!(MyEnum::A(32), field!(MyEnum::A.0, eq(32)))?; // Passes
verify_that!(MyEnum::B, field!(MyEnum::A.0, eq(32)))?; // Fails: wrong enum variant

The structure or enum may also be referenced from a separate module:

mod a_module {
    #[derive(Debug)]
    pub struct AStruct(pub i32);
}
verify_that!(a_module::AStruct(32), field!(a_module::AStruct.0, eq(32)))?;

Nested structures are not supported, however:

#[derive(Debug)]
struct InnerStruct(i32);
#[derive(Debug)]
struct OuterStruct {
    inner: InnerStruct,
}
verify_that!(value, field!(OuterStruct.inner.0, eq(32)))?; // Does not compile

See also the macro property for an analogous mechanism to extract a datum by invoking a method.