Macro static_assertions::assert_fields
source · macro_rules! assert_fields {
($($xs:tt)+) => { ... };
}
Expand description
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,
}
// Requires a unique label in module scope
assert_fields!(windows; Ty, val1);
fn main() {
// Always have `val2` regardless of OS
assert_fields!(Ty, val2);
}
The labeling limitation is not necessary if
compiling on nightly Rust with the nightly
feature enabled:
ⓘ
#![feature(underscore_const_names)]
use std::ops::Range;
assert_fields!(Range<u32>, start, end);
Range does not have a field named middle
:
ⓘ
assert_fields!(Range<u32>, middle);