I want to match, e.g. an ident
's type to implement a certain trait, how would I do that?
Here the basic idea in (incomplete) code:
macro_rules! has_trait {
($ ($t : ty), ($x : ident),) => {
}
}
fn trait_test() {
let a = vec![1, 2, 3];
let b = 42;
let a_iteratable = has_trait!(IntoIterator, a);
let b_iteratable = has_trait!(IntoIterator, b);
println!("{:?} iterable? {}", a, a_iteratable);
println!("{:?} iterable? {}", b, b_iteratable);
}
I cannot wrap my head around how to say "any type which has trait Foo
".
I see 2 options how to tackle the problem:
- Find a match expression which matches any type with trait
$t
and simply return true on match, else (how works else?) false. - In the body of the match of any type, use some code to determine if trait
$t
is implemented by the type of$x
.
I cannot see how to do either of both options.
Can this even be done?