Skip to main content

assert_not_impl

Macro assert_not_impl 

Source
macro_rules! assert_not_impl {
    {[$rule:ident $( < $($gens:tt)* )? ] $t:ty : $trait:path } => { ... };
}
Expand description

Give a compile time error if TYPE implements TRAIT

Includes the identifier $rule in the error message, to help the user diagnose the problem (unlike the similar macro in static_assertions.

Supports generics (also, unlike the one in static_assertions`).

§Input syntaxes

assert_not_impl! { [RULE_IDENTIFIER] TYPE: TRAIT }
assert_not_impl! { [RULE_IDENTIFIER <TYPE_GENERICS>] TYPE: TRAIT }
  • RULE_IDENTIFIER is an arbitrary identifier; it will appear in the error message. (There is no way to include arbitrary explanatory text.)
  • TYPE_GENERICS are generic bindings needed for TYPE. (Generics on the trait are not supported.)

§Examples

use std::cell::Cell;
use tor_basic_utils::assert_not_impl;

// No error will occur; Cell is not Sync
assert_not_impl! {
    [cell_must_not_be_sync] Cell<u32>: Sync
}
assert_not_impl! {
    [cell_must_not_be_sync <T: Copy>]
    Cell<T>: Sync
}
// Compile-time error _is_ given; String implements Clone.
assert_not_impl! {
    [clone_is_forbidden_here] String: Clone
}