I am building a Raft consensus algorithm middleware for Rust with a similar architecture to copycat. I wrote a macro for developers to define a scheme for commands. The macro generates a trait A
with a dispatch
function for user defined commands, which requires the developer to implement a struct for its behavior and an encoder/decoder which is irrelevant to this topic.
The trait objects register with a Server
object to make it work. Because trait A
is dynamically generated, I have to define another trait B
as parent trait for A
. Server
works with B
to invoke the dispatch
function defined in A
.
I tried different combination but none of them works.
trait B {
fn dispatch(&mut self, fn_id: u64) -> Vec<u8>;
}
// macro generated trait
trait A: B {
fn a(&self) -> Vec<u8>;
fn b(&self) -> Vec<u8>;
fn dispatch(&mut self, fn_id: u64) -> Vec<u8> {
match fn_id {
1 => a(),
2 => b(),
_ => {}
}
}
}
Unfortunately, the dispatch
function in A
will not implement dispatch
in B
. When I implement the structure for A
, the compiler still asked to implement dispatch
for B
.
I also tried to move dispatch
in trait B
to another trait as the parent trait for B
, but the trait cannot implement for A
.
I have also reviewed https://github.com/rust-lang/rfcs/issues/1024. It seems the issue is open but have been ditched. Is there any alternative way to make this pattern work?
B
at the same time you are expanding the macro that creates the traitA
? – Shepmaster