Addr

Struct Addr 

Source
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>

Source

pub async fn send<M>(&self, msg: M) -> Result<S::Response, S::Error>
where S: Handler<M> + Send + Sync + 'static, M: 'static + Send + Sync, S::Error: Send + Sync, S::Response: Send + Sync,

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}
Source

pub fn do_send<M>(&self, msg: M)
where S: Handler<M> + Send + Sync + 'static, M: 'static + Send + Sync, S::Error: Send + Sync, S::Response: Send + Sync,

Just send a message, without waiting for a response.

Be aware that by using this function you allow desynchonization to happen, as this function doesn’t wait for the response.

If you don’t want desyncs to happen, use send.

Trait Implementations§

Source§

impl<S: Service> Clone for Addr<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.