pub struct Addr<A> { /* private fields */ }Expand description
The address of an actor.
When all references to Addr<A> are dropped, the actor ends.
You can use Clone trait to create multiple copies of Addr<A>.
Implementations§
Source§impl<A: Actor> Addr<A>
impl<A: Actor> Addr<A>
Sourcepub async fn call<T: Message>(&self, msg: T) -> Result<T::Result>where
A: Handler<T>,
pub async fn call<T: Message>(&self, msg: T) -> Result<T::Result>where
A: Handler<T>,
Send a message msg to the actor and wait for the return value.
Sourcepub fn send<T: Message<Result = ()>>(&self, msg: T) -> Result<()>where
A: Handler<T>,
pub fn send<T: Message<Result = ()>>(&self, msg: T) -> Result<()>where
A: Handler<T>,
Send a message msg to the actor without waiting for the return value.
Examples found in repository?
examples/subscriber.rs (line 36)
34 async fn started(&mut self, ctx: &mut Context<Self>) -> Result<()> {
35 println!("Subscriber Parent Started");
36 let _ = ctx.address().send(InitializeChildSubscribers);
37 Ok(())
38 }
39}
40
41#[message]
42struct InitializeChildSubscribers;
43
44#[async_trait::async_trait]
45impl Handler<InitializeChildSubscribers> for SubscriberParent {
46 async fn handle(&mut self, _ctx: &mut Context<Self>, _msg: InitializeChildSubscribers) {
47 let message_producer_addr = self.message_producer.clone();
48 let dummy_ids: Vec<i32> = vec![1, 2, 3, 4, 5];
49 let children_unstarted_actors_vec = dummy_ids.into_iter().map(move |id| {
50 let id = id.clone();
51 let addr = message_producer_addr.clone();
52
53 Subscriber::new(id, addr)
54 });
55
56 let children_addr_vec = children_unstarted_actors_vec
57 .into_iter()
58 .map(|actor| async { actor.start().await.unwrap() });
59
60 let children_addr_vec = futures::future::join_all(children_addr_vec).await;
61
62 self.children_subscribers = children_addr_vec;
63 }
64}
65
66#[message]
67struct ClearChildSubscribers;
68
69#[async_trait::async_trait]
70impl Handler<ClearChildSubscribers> for SubscriberParent {
71 async fn handle(&mut self, _ctx: &mut Context<Self>, _msg: ClearChildSubscribers) {
72 self.children_subscribers = Vec::new();
73 }
74}
75
76// (Child) Subscriber - B
77
78struct Subscriber {
79 id: i32,
80 message_producer_addr: Addr<MessageProducer>,
81}
82
83impl Subscriber {
84 fn new(id: i32, message_producer_addr: Addr<MessageProducer>) -> Subscriber {
85 Subscriber {
86 id,
87 message_producer_addr,
88 }
89 }
90}
91
92#[async_trait::async_trait]
93impl Actor for Subscriber {
94 async fn started(&mut self, ctx: &mut Context<Self>) -> Result<()> {
95 // Send subscription request message to the Message Producer
96 println!("Child Subscriber Started - id {:?}", self.id);
97 let self_sender = ctx.address().sender();
98 let _ = self.message_producer_addr.send(SubscribeToProducer {
99 sender: self_sender,
100 });
101 Ok(())
102 }More examples
examples/supervisor_clear_send_later.rs (line 53)
43async fn main() -> Result<(), Box<dyn std::error::Error>> {
44 let service_supervisor = xactor::Supervisor::start(PingLater::default).await?;
45 let service_addr = service_supervisor.clone();
46
47 let supervisor_task = xactor::spawn(async {
48 service_supervisor.wait_for_stop().await;
49 });
50
51 let send_ping = async {
52 println!(" main :: sending Ping");
53 service_addr.send(Ping("before halt")).unwrap();
54 };
55
56 let send_halt = async {
57 xactor::sleep(Duration::from_millis(1_000)).await;
58 println!(" main :: sending Halt");
59 service_addr.send(Halt).unwrap();
60 };
61
62 let _ = futures::join!(supervisor_task, send_halt, send_ping);
63
64 Ok(())
65}examples/supervisor_clear_interval.rs (line 79)
68async fn main() -> Result<(), Box<dyn std::error::Error>> {
69 let service_supervisor = xactor::Supervisor::start(PingTimer::default).await?;
70 let service_addr = service_supervisor.clone();
71
72 let supervisor_task = xactor::spawn(async {
73 service_supervisor.wait_for_stop().await;
74 });
75
76 let send_halt = async {
77 xactor::sleep(Duration::from_millis(5_200)).await;
78 println!(" main :: sending Halt");
79 service_addr.send(Halt).unwrap();
80 };
81
82 let _ = futures::join!(supervisor_task, send_halt);
83 // run this to see that the interval is not properly stopped if the ctx is stopped
84 // futures::join!(supervisor_task, send_panic); // there is no panic recovery
85
86 Ok(())
87}Sourcepub fn caller<T: Message>(&self) -> Caller<T>where
A: Handler<T>,
pub fn caller<T: Message>(&self) -> Caller<T>where
A: Handler<T>,
Create a Caller<T> for a specific message type
Sourcepub fn sender<T: Message<Result = ()>>(&self) -> Sender<T>where
A: Handler<T>,
pub fn sender<T: Message<Result = ()>>(&self) -> Sender<T>where
A: Handler<T>,
Create a Sender<T> for a specific message type
Sourcepub async fn wait_for_stop(self)
pub async fn wait_for_stop(self)
Wait for an actor to finish, and if the actor has finished, the function returns immediately.
Examples found in repository?
More examples
examples/supervisor_clear_send_later.rs (line 48)
43async fn main() -> Result<(), Box<dyn std::error::Error>> {
44 let service_supervisor = xactor::Supervisor::start(PingLater::default).await?;
45 let service_addr = service_supervisor.clone();
46
47 let supervisor_task = xactor::spawn(async {
48 service_supervisor.wait_for_stop().await;
49 });
50
51 let send_ping = async {
52 println!(" main :: sending Ping");
53 service_addr.send(Ping("before halt")).unwrap();
54 };
55
56 let send_halt = async {
57 xactor::sleep(Duration::from_millis(1_000)).await;
58 println!(" main :: sending Halt");
59 service_addr.send(Halt).unwrap();
60 };
61
62 let _ = futures::join!(supervisor_task, send_halt, send_ping);
63
64 Ok(())
65}examples/supervisor_clear_interval.rs (line 73)
68async fn main() -> Result<(), Box<dyn std::error::Error>> {
69 let service_supervisor = xactor::Supervisor::start(PingTimer::default).await?;
70 let service_addr = service_supervisor.clone();
71
72 let supervisor_task = xactor::spawn(async {
73 service_supervisor.wait_for_stop().await;
74 });
75
76 let send_halt = async {
77 xactor::sleep(Duration::from_millis(5_200)).await;
78 println!(" main :: sending Halt");
79 service_addr.send(Halt).unwrap();
80 };
81
82 let _ = futures::join!(supervisor_task, send_halt);
83 // run this to see that the interval is not properly stopped if the ctx is stopped
84 // futures::join!(supervisor_task, send_panic); // there is no panic recovery
85
86 Ok(())
87}Trait Implementations§
Auto Trait Implementations§
impl<A> Freeze for Addr<A>
impl<A> !RefUnwindSafe for Addr<A>
impl<A> Send for Addr<A>
impl<A> Sync for Addr<A>
impl<A> Unpin for Addr<A>
impl<A> !UnwindSafe for Addr<A>
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