Closed idanarye closed 3 years ago
Consider three actors, each holding the addresses of the other two. With the closure&context style creating them can get quite unsightly:
let builder = factory.instantiate();
builder.new_actor(|ctx1| {
ctx1.connect_signals::<Signal1>();
let mut addr3_ = None;
let addr2 = builder.new_actor(|ctx2| {
ctx2.connect_signals::<Signal2>();
let addr3 = builder.new_actor(|ctx3| {
ctx3.connect_signals::<Signal3>();
Actor3 {
actor1: ctx1.address(),
actor2: ctx2.address(),
}
})
addr3_ = Some(addr3.clone())
Actor2 {
actor1: ctx1.address(),
actor3: addr3,
}
});
Actor1 {
actor2: addr2
actor3: addr3_.unwrap(),
}
});
But with the style I'm proposing here we can have this:
let builder = factory.instantiate();
let actor1 = builder.actor().connect_signals(Signal1::conector());
let actor2 = builder.actor().connect_signals(Signal2::conector());
let actor3 = builder.actor().connect_signals(Signal3::conector());
// actorN are actor builders, but they contain the actor contexts so we can easily get the
// addresses from them.
let addr1 = actor1.address();
let addr2 = actor2.address();
let addr3 = actor3.address();
actor1.run(Actor1 {
actor2: addr2.clone(),
actor3: addr3.clone(),
});
actor2.run(Actor2 {
actor1: addr1.clone(),
actor3: addr3,
});
actor3.run(Actor3 {
actor1: addr1,
actor2: addr2,
});
Is this a common use case? (I haven't built any actix actors that interact with each other yet.) But I see the point.
I don't think one can realize two actors that cross-reference each other using the current situation. I added a fn context(&self) -> &A::Context
to ActorBuilder
to get it working but I'm not sure if that is a good solution API wise.
See #4 for background. Tl;DR: we want to support this style: