I have a trait with one function:
type Arg = (); // Not relevant for this example.
type Res = (); // Not relevant for this example.
trait Call {
fn call(&self, arg: Arg) -> Option<Res>;
}
Now I want to implement this trait for different structs.
Some structs may not want to handle specific Arg
and so they return None
.
Instead of having return type of Option<Res>
I would like to enforce this rule in compile time, with something like this:
trait Call {
type Token;
fn can_call(&self, arg: &Arg) -> Option<Token>;
fn call(&self, token: Self::Token, arg: Arg) -> Res;
}
It doesn't have to be token-based approach, but it seems reasonable.
The problem is when I want to build a pipeline of implementations.
Obviously, I can't have
struct Callables {
callables: Vec<Box<dyn Call>>,
}
since Call
needs to know the type of associated Token
.
I suppose there is a way to do this via layering Call
implementations in each other:
struct Stack<A, AToken, B, BToken> {
callable: A<Token = AToken>,
next: B<Token = BToken>,
}
But I don't exactly know how to build such Stack
-s and then store them.
Can anyone tell me if this is even possible?