[][src]Macro static_assertions::assert_fields

macro_rules! assert_fields {
    ($t:ident::$v:ident: $($f:ident),+) => { ... };
    ($t:path: $($f:ident),+) => { ... };
}

Asserts that the type has the given fields.

Examples

One common use case is when types have fields defined multiple times as a result of #[cfg]. This can be an issue when exposing a public API.

pub struct Ty {
    #[cfg(windows)]
    pub val1: u8,
    #[cfg(not(windows))]
    pub val1: usize,

    #[cfg(unix)]
    pub val2: u32,
    #[cfg(not(unix))]
    pub val2: usize,
}

// Always have `val2` regardless of OS
assert_fields!(Ty: val2);

This macro even works with enum variants:

enum Data {
    Val {
        id: i32,
        name: String,
        bytes: [u8; 128],
    },
    Ptr(*const u8),
}

assert_fields!(Data::Val: id, bytes);

The following example fails to compile because Range does not have a field named middle:

This example deliberately fails to compile
use std::ops::Range;

assert_fields!(Range<u32>: middle);