Closed bobdebuildr closed 2 years ago
I can think of multiple ways to solve this:
Handler
for that.MessageChannel
s, one per message. MessageChannel
s are cheap to clone.Do these Address
es point the same actor type or different ones?
Thanks for your comments, that makes sense. I would have different actor types, in the example above e.g. DogActor
and CatActor
, both implementing the two Handler
s. In my case, the types to be handled are already enums with lots of variants, so I think I would prefer option 2.
I have personally used (2) before and it worked well. You can also do something like this which cuts some of the boilerplate:
struct Channels {
ch1: MessageChannel<Foo>,
ch2: MessageChannel<Bar>
}
impl Channels {
fn new<A: Handler<Foo> + Handler<Bar>>(addr: Address<A>) -> Self {
Self {
ch1: MessageChannel::new(addr.clone()),
ch2: MessageChannel::new(addr)
}
}
}
That would avoid needing to pass the address to the same actor multiple times but allow for different actors serving as the backing one for the channels.
Hope this helps!
Overall, I'm not 100% sure if it's possible for xtra to provide some kind of combination message channel like this, unfortunately (how would this be expressible through the rust typesystem?). I think that the solution to the actual problems themselves have been discussed here though.
Hello I have been trying out the master branch of xtra for a small project, and I have a question regarding the Handler implementations.
Is it somehow possible to define a trait that requires a certain set of messages, e.g.
A
andB
to be handled, by requiring implementations ofHandler<A>
,Handler<B>
to exist? Ideally I would be able to write something like this:I guess this is similar to a
MessageChannel
, but for multiple handlers. Does this even make sense or is there some other, better way to do this?