[][src]Macro assert_bound::assert_bound

macro_rules! assert_bound {
    (
        $e:expr =>
        // One trait bound, e.g. `std::fmt::Debug`, `PartialEq<()>`
        $head:ident $( :: $tail:ident )* $( < $param:ty $(, $p:ty)* > )?
        // Zero or more trait bounds splited by `+`
        $(+ $head2:ident $( :: $tail2:ident )* $( < $param2:ty $(, $p2:ty)* > )?)*
    ) => { ... };
}

Assert that expression implements trait(s) at compile-time.

Examples

use assert_bound::assert_bound;
use std::fmt::Debug;

assert_bound!(() => Debug + Ord + PartialEq<()>);
trait T {}
impl T for () {}

assert_bound!(() => T);

Note: expression is not executed:

let mut var = 0;
assert_bound!({ var = 1; } => Eq);
assert_eq!(var, 0);

However you can execute it by calling result of the macro:

let mut var = 0;
assert_bound!({ var = 1; } => Eq)();
assert_eq!(var, 1);
This example deliberately fails to compile
// f32/f64 doesn't implement `Eq`,
// so rustc will fail to compile that.
assert_bound!(0.1; Eq);

Expand

Following code:

assert_bound!({ println!("Hewwo?") } => Ord + PartialEq<()>);

expands to something like this:

|| {
    fn assert_bound<T>(_: &T)
    where
        T: Ord,
        T: PartialEq<()>,
    {}

    let expr = { println!("Hewwo?") };
    assert_bound(&expr);
    expr
};