I have a trait which manages conversions to different trait objects.
The trait looks like this: (Boo
and Gee
are both different Traits)
trait Foo {
fn as_boo(&mut self) -> Option<&mut Boo> {
None
}
fn as_gee(&mut self) -> Option<&mut Gee> {
None
}
}
To reduce the amount of boilerplate code I now want to automatically change this implementation to this in case the struct implements Boo
/Gee
:
#[derive(Boo)]
struct W {}
impl Foo for W {
fn as_boo(&mut self) -> Option<&mut Boo> {
Some(self)
}
}
I could do this if there is only one other trait I have to convert to by using generics:
impl<T: Boo> Foo for T {
fn as_boo(&mut self) -> Option<&mut Boo> {
Some(self)
}
}
But in case I also want to automatically implement Foo
for Gee
it doesn't work this way due to the fact that I could not implement Foo
twice, which is not supported by rust as far as I know.
// This does not compile because Foo might get implemented twice.
impl<T: Boo> Foo for T {
fn as_boo(&mut self) -> Option<&mut Boo> {
Some(self)
}
}
impl<T: Gee> Foo for T {
fn as_gee(&mut self) -> Option<&mut Gee> {
Some(self)
}
}
It might be possible to achieve this using Procedural Macros
but I could not find any in depth explanation of them so I am kind of stuck now.