pub struct Addr<S: Service> { /* private fields */ }Expand description
A handle to a running Service, allowing to send messages to it.
Implementations§
Source§impl<S: Service<Context = Context<S>>> Addr<S>
impl<S: Service<Context = Context<S>>> Addr<S>
Sourcepub async fn send<M>(&self, msg: M) -> Result<S::Response, S::Error>
pub async fn send<M>(&self, msg: M) -> Result<S::Response, S::Error>
Send a message to the service pointed to by this address.
Examples found in repository?
examples/ping.rs (line 69)
66 async fn call(&mut self, _ping: Ping, cx: &mut Self::Context) -> Result<Pong, Self::Error> {
67 cx.singleton::<Printer>()
68 .await
69 .send(format!("ping #{}", self.count))
70 .await?;
71
72 self.count += 1;
73
74 Ok(Pong(self.count))
75 }
76}
77impl acril::Handler<GetOutput> for Pinger {
78 type Response = u32;
79 async fn call(
80 &mut self,
81 _request: GetOutput,
82 _cx: &mut Self::Context,
83 ) -> Result<Self::Response, Self::Error> {
84 Ok(self.count)
85 }
86}
87
88#[tokio::main(flavor = "current_thread")]
89async fn main() {
90 // make sure the runtime can spawn !Send tasks
91 let local_set = tokio::task::LocalSet::new();
92 let _guard = local_set.enter();
93
94 let runtime = acril_rt::Runtime::new();
95
96 local_set.run_until(async move {
97 #[cfg(debug_assertions)]
98 let printer = runtime.spawn(Printer::ToStdout).await;
99 #[cfg(not(debug_assertions))]
100 let printer = runtime.spawn(Printer::ToString(String::new())).await;
101 let pinger = runtime.spawn(Pinger { count: 0 }).await;
102
103 printer.send("Liftoff!".to_string()).await.unwrap();
104
105 for i in 0..10000 {
106 let pong = pinger.send(Ping).await.unwrap();
107
108 // i is 0-based and pong is 1-based because the pinger increments before returning
109 assert_eq!(pong.0, i + 1);
110
111 printer.send(format!("pong #{}", pong.0)).await.unwrap();
112 }
113
114 assert_eq!(pinger.send(GetOutput).await.unwrap(), 10000);
115 #[cfg(not(debug_assertions))]
116 // assert that the output is Ok(Some(non_empty_string))
117 assert!(!printer.send(GetOutput).await.unwrap().unwrap().is_empty());
118
119 printer.send("We're done!".to_string()).await.unwrap();
120 }).await;
121}Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for Addr<S>
impl<S> RefUnwindSafe for Addr<S>where
S: RefUnwindSafe,
impl<S> Send for Addr<S>where
S: Send,
impl<S> Sync for Addr<S>where
S: Sync,
impl<S> Unpin for Addr<S>where
S: Unpin,
impl<S> UnwindSafe for Addr<S>where
S: UnwindSafe,
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