Sha256: 9c5baeac0215be557213eec9e80fc00de10a721d9b2c369fece743fcc6ccdc8e

Contents?: true

Size: 1.82 KB

Versions: 34

Compression:

Stored size: 1.82 KB

Contents

/// 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.
///
/// ```
/// # #[macro_use] extern crate static_assertions;
/// 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:
///
/// ```
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// 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`:
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// use std::ops::Range;
///
/// assert_fields!(Range<u32>: middle);
/// ```
///
/// [`Range`]: https://doc.rust-lang.org/std/ops/struct.Range.html
#[macro_export]
macro_rules! assert_fields {
    ($t:ident::$v:ident: $($f:ident),+) => {
        #[allow(unknown_lints, unneeded_field_pattern)]
        const _: fn() = || {
            #[allow(dead_code, unreachable_patterns)]
            fn assert(value: $t) {
                match value {
                    $($t::$v { $f: _, .. } => {},)+
                    _ => {}
                }
            }
        };
    };
    ($t:path: $($f:ident),+) => {
        #[allow(unknown_lints, unneeded_field_pattern)]
        const _: fn() = || {
            $(let $t { $f: _, .. };)+
        };
    };
}

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-29.0.0 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-28.0.0 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-27.0.0 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-26.0.0 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-25.0.2 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-25.0.1 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-25.0.0 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-24.0.0 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-23.0.2 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-22.0.0 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-21.0.1 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-20.0.2 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-20.0.0 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-18.0.3 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-17.0.1 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-17.0.0 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-16.0.0 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-15.0.1 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs
wasmtime-15.0.0 ./ext/cargo-vendor/static_assertions-1.1.0/src/assert_fields.rs