I have the following code from a larger project.
trait Wanted {
fn wanted() -> bool;
}
struct S<'a, T> {
x: &'a T,
}
impl Wanted for u8 {
fn wanted() -> bool {
true
}
}
impl Wanted for u16 {
fn wanted() -> bool {
true
}
}
type A = extern "C" fn(x: S<u8>);
type B = extern "C" fn(x: S<u16>);
// How to change **only this impl** to make both `a` and `b` work at the same time?
impl<'a, T> Wanted for extern "C" fn(x: S<'a, T>)
where
T: Wanted,
{
fn wanted() -> bool {
T::wanted()
}
}
fn main() {
let a = A::wanted();
let b = B::wanted();
}
Due to the way I use proc macros I want to generically implement a trait Wanted
for classes of function pointers if those function pointers use parameters which also implement that trait.
I also happen to have type aliases A
and B
used elsewhere.
I now want to generate a function that, similar to main, can give meta information whether A
or B
has the wanted
property.
The only code I would like to change right now is (due to architectural reasons) is the impl<'a, T> Wanted for ...
section. In particular I would not like having to write explicit impl ... for A
.
Questions:
-
How can I make
main()
compile? -
Also, could someone ELI5 why the existing impl doesn't work? Naively I'd assume
impl<'a, T>
means that for any lifetime'a
and typeT
traitWanted
will be implemented as given. Therefore, when using typeA
which (if I understand correctly) has a hidden for<'a> that should still be qualified by the impl block. Clearly I'm misunderstanding something here, but it would be nice to understand what.
Thanks!