I have a rust codebase that has a lot of code that looks like this:
struct FooA {}
impl struct FooA {
pub fn run(&self) {}
}
struct FooB {}
impl struct FooB {
pub fn run(&self) {}
}
enum Bars {
BarA(FooA),
BarB(FooB),
}
impl Bars {
pub fn run(&self) {
match self {
BarA(wrapped: &FooA) => wrapped.run(),
BarB(wrapped: &FooB) => wrapped.run(),
}
}
}
It's pretty clear that there's a trait here, that might look like this:
trait Runner {
fn run(&self)
}
Both FooA
and FooB
could implement that, boom, easy peasy. However, it's the implementation of Runner
in Bars
that I'm hoping to simplify. As Bars
gets bigger, we have to account for every single variant, but they will all essentially return wrapped.run()
because they will all implement Runner
.
Maybe I just am not using the right search term incantation, but I haven't found a right spelling that might make it so that the Runner
implementation of Bars
can just say "You'll always wrap an implementation of Runner
, so call run
on the unwrapped value. The best I can think of is maybe a macro.