Hey, I'm doing a lot of rpc/socket/protocol things and I keep hitting the same problem every time and I feel like there should be a better solution. So I was wondering if anyone here would have a suggestion or even just a link to a lib so it can help me think outside the box
the idea is, I always end up with an enum like this
// all the actions the receiver can answer to
enum Message {
SetA(A),
GetA,
}
enum Response {
Unit, // the action doesn't return anything
TheA(A), // the response that `GetA` returns
}
The problem here is that although this makes the receiver code quite simple, just match on the message enum and dispatch
It makes the "caller's" job quite awkward
fn get_a() -> A {
match SOME_TRANSPORT.send(Message::GetA) {
Response::TheA(a) => a,
x => panic!("Unexpected response: {x:?}"),
}
}
So my question is, is there a way to create an association between "question types" and "response types" in the type system so that I don't have to runtime check? I have experimented with traits and associated types but it makes the receiver code quite messy
trait Message {
type Response;
}
struct SetA(A);
impl Message for SetA {
type Response = ();
}