Macro variadics::variadic_trait

source ·
macro_rules! variadic_trait {
    (
        $( #[$( $attrs:tt )*] )*
        $vis:vis variadic<$item:ident> $name:ident $( $clause:tt )*
    ) => { ... };
}
Expand description

This macro generates a basic variadic trait where each element must fulfill the where clause.

use variadics::{var_expr, variadic_trait};

variadic_trait! {
    /// A variadic list of `Debug` items.
    pub variadic<Item> DebugList where Item: std::fmt::Debug {}
}

let x = &var_expr!(1, "hello", 5.6);
let _: &dyn DebugList = x;
println!("{:?}", x);

This uses a special syntax similar to traits, but with the trait keyword replaced with variadic<T> where T is the generic parameter name for each item in the variadic list. T can be changed to any valid generic identifier. The bounds on T must be put in the where clause; they cannot be expressed directly– variadic<T: Clone> is invalid.

For now this can only create traits which bounds the Items and cannot have associated methods. This means the body of the variadic trait must be empty. But in the future this declarative macro may be converted into a more powerful procedural macro with associated method support.