pub struct Context<ActorId = DefaultActorId> { /* private fields */ }Implementations§
Source§impl<ActorId> Context<ActorId>
impl<ActorId> Context<ActorId>
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
More examples
examples/dynamic.rs (line 40)
39async fn main() {
40 let ctx = Context::new();
41
42 MyActor::run(ctx.clone(), "actor 1", 1);
43 let actor_2_task = MyActor::run(ctx.clone(), "actor 2", 2);
44
45 let sender_1 = ctx.actor_sender::<Value>("actor 1");
46 sender_1.send(Value(11)).await.ok();
47 sender_1.send(Value(12)).await.ok();
48
49 let sender_2 = ctx.actor_sender::<Value>("actor 2");
50 sender_2.send(Value(21)).await.ok();
51 sender_2.send(Value(22)).await.ok();
52
53 sender_2.send(Value(0)).await.ok();
54 ctx.join(&actor_2_task).await.ok();
55
56 MyActor::run(ctx.clone(), "actor 2", 2);
57
58 let sender_2 = ctx.actor_sender::<Value>("actor 2");
59 sender_2.send(Value(23)).await.ok();
60
61 ctx.shutdown().await;
62}pub fn from_system(system: System<ActorId>) -> Self
Sourcepub fn spawn<T>(&self, future: T) -> TaskId
pub fn spawn<T>(&self, future: T) -> TaskId
Examples found in repository?
examples/dynamic.rs (lines 19-29)
14 fn run(ctx: Context, actor_id: &'static str, value: u32) -> TaskId {
15 let mut value_in = ctx.actor_receiver::<Value>(actor_id);
16 let mut actor = MyActor { value };
17
18 println!("run the {actor_id}");
19 ctx.clone().spawn(async move {
20 truba::event_loop!(ctx, {
21 Some(msg) = value_in.recv() => {
22 actor.handle_value(msg);
23 if actor.value == 0 {
24 break;
25 }
26 },
27 });
28 println!("stop the {actor_id}");
29 })
30 }pub fn extract_channel<M: Message>(&self) -> Option<M::Channel>
pub fn get_sender<M: Message>(&self) -> Option<<M::Channel as Channel>::Sender>
pub fn sender_of_custom_channel<M: Message>( &self, constructor: impl FnOnce() -> M::Channel, ) -> <M::Channel as Channel>::Sender
pub fn receiver_of_custom_channel<M: Message>( &self, constructor: impl FnOnce() -> M::Channel, ) -> <M::Channel as Channel>::Receiver
Sourcepub fn sender<M: Message>(&self) -> <M::Channel as Channel>::Sender
pub fn sender<M: Message>(&self) -> <M::Channel as Channel>::Sender
Examples found in repository?
More examples
Sourcepub fn receiver<M: Message>(&self) -> <M::Channel as Channel>::Receiver
pub fn receiver<M: Message>(&self) -> <M::Channel as Channel>::Receiver
Examples found in repository?
More examples
pub fn is_channel_closed<M: Message>(&self) -> Option<bool>
pub fn system(&self) -> MutexGuard<'_, System<ActorId>>
Sourcepub async fn shutdown(&self)
pub async fn shutdown(&self)
Examples found in repository?
More examples
examples/dynamic.rs (line 61)
39async fn main() {
40 let ctx = Context::new();
41
42 MyActor::run(ctx.clone(), "actor 1", 1);
43 let actor_2_task = MyActor::run(ctx.clone(), "actor 2", 2);
44
45 let sender_1 = ctx.actor_sender::<Value>("actor 1");
46 sender_1.send(Value(11)).await.ok();
47 sender_1.send(Value(12)).await.ok();
48
49 let sender_2 = ctx.actor_sender::<Value>("actor 2");
50 sender_2.send(Value(21)).await.ok();
51 sender_2.send(Value(22)).await.ok();
52
53 sender_2.send(Value(0)).await.ok();
54 ctx.join(&actor_2_task).await.ok();
55
56 MyActor::run(ctx.clone(), "actor 2", 2);
57
58 let sender_2 = ctx.actor_sender::<Value>("actor 2");
59 sender_2.send(Value(23)).await.ok();
60
61 ctx.shutdown().await;
62}pub async fn join_all(&self)
Sourcepub async fn join(&self, id: &TaskId) -> Result<(), JoinError>
pub async fn join(&self, id: &TaskId) -> Result<(), JoinError>
Examples found in repository?
examples/dynamic.rs (line 54)
39async fn main() {
40 let ctx = Context::new();
41
42 MyActor::run(ctx.clone(), "actor 1", 1);
43 let actor_2_task = MyActor::run(ctx.clone(), "actor 2", 2);
44
45 let sender_1 = ctx.actor_sender::<Value>("actor 1");
46 sender_1.send(Value(11)).await.ok();
47 sender_1.send(Value(12)).await.ok();
48
49 let sender_2 = ctx.actor_sender::<Value>("actor 2");
50 sender_2.send(Value(21)).await.ok();
51 sender_2.send(Value(22)).await.ok();
52
53 sender_2.send(Value(0)).await.ok();
54 ctx.join(&actor_2_task).await.ok();
55
56 MyActor::run(ctx.clone(), "actor 2", 2);
57
58 let sender_2 = ctx.actor_sender::<Value>("actor 2");
59 sender_2.send(Value(23)).await.ok();
60
61 ctx.shutdown().await;
62}pub fn handles(&self) -> &TaskHandles
Source§impl<ActorId: Eq + Hash> Context<ActorId>
impl<ActorId: Eq + Hash> Context<ActorId>
pub fn extract_actor_channel<M: Message>( &self, actor_id: &ActorId, ) -> Option<M::Channel>
pub fn get_actor_sender<M: Message>( &self, actor_id: &ActorId, ) -> Option<<M::Channel as Channel>::Sender>
pub fn actor_sender_of_custom_channel<M: Message>( &self, actor_id: ActorId, constructor: impl FnOnce() -> M::Channel, ) -> <M::Channel as Channel>::Sender
pub fn actor_receiver_of_custom_channel<M: Message>( &self, actor_id: ActorId, constructor: impl FnOnce() -> M::Channel, ) -> <M::Channel as Channel>::Receiver
Source§impl<ActorId: Eq + Hash + Display> Context<ActorId>
impl<ActorId: Eq + Hash + Display> Context<ActorId>
Sourcepub fn actor_sender<M: Message>(
&self,
actor_id: impl Into<ActorId>,
) -> <M::Channel as Channel>::Sender
pub fn actor_sender<M: Message>( &self, actor_id: impl Into<ActorId>, ) -> <M::Channel as Channel>::Sender
Examples found in repository?
examples/dynamic.rs (line 45)
39async fn main() {
40 let ctx = Context::new();
41
42 MyActor::run(ctx.clone(), "actor 1", 1);
43 let actor_2_task = MyActor::run(ctx.clone(), "actor 2", 2);
44
45 let sender_1 = ctx.actor_sender::<Value>("actor 1");
46 sender_1.send(Value(11)).await.ok();
47 sender_1.send(Value(12)).await.ok();
48
49 let sender_2 = ctx.actor_sender::<Value>("actor 2");
50 sender_2.send(Value(21)).await.ok();
51 sender_2.send(Value(22)).await.ok();
52
53 sender_2.send(Value(0)).await.ok();
54 ctx.join(&actor_2_task).await.ok();
55
56 MyActor::run(ctx.clone(), "actor 2", 2);
57
58 let sender_2 = ctx.actor_sender::<Value>("actor 2");
59 sender_2.send(Value(23)).await.ok();
60
61 ctx.shutdown().await;
62}Sourcepub fn actor_receiver<M: Message>(
&self,
actor_id: impl Into<ActorId>,
) -> <M::Channel as Channel>::Receiver
pub fn actor_receiver<M: Message>( &self, actor_id: impl Into<ActorId>, ) -> <M::Channel as Channel>::Receiver
Examples found in repository?
examples/dynamic.rs (line 15)
14 fn run(ctx: Context, actor_id: &'static str, value: u32) -> TaskId {
15 let mut value_in = ctx.actor_receiver::<Value>(actor_id);
16 let mut actor = MyActor { value };
17
18 println!("run the {actor_id}");
19 ctx.clone().spawn(async move {
20 truba::event_loop!(ctx, {
21 Some(msg) = value_in.recv() => {
22 actor.handle_value(msg);
23 if actor.value == 0 {
24 break;
25 }
26 },
27 });
28 println!("stop the {actor_id}");
29 })
30 }pub fn is_actor_channel_closed<M: Message>( &self, actor_id: impl Borrow<ActorId>, ) -> Option<bool>
Trait Implementations§
Auto Trait Implementations§
impl<ActorId> Freeze for Context<ActorId>
impl<ActorId = String> !RefUnwindSafe for Context<ActorId>
impl<ActorId> Send for Context<ActorId>where
ActorId: Send,
impl<ActorId> Sync for Context<ActorId>where
ActorId: Send,
impl<ActorId> Unpin for Context<ActorId>
impl<ActorId = String> !UnwindSafe for Context<ActorId>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more