Context

Struct Context 

Source
pub struct Context<S: Service<Context = Self>> { /* private fields */ }
Expand description

A service context.

Any service that is spawned onto a Runtime needs to have this type as context.

Implementations§

Source§

impl<S: Service<Context = Self>> Context<S>

Source

pub fn this(&self) -> Addr<S>

Get the address of this service.

Beware that if you use this to send a message to yourself (with Addr::send), you will encounter a deadlock because to handle a message you need send a message, which just makes the message handler never finish.

Source

pub fn spawn<F: Future + 'static>(&self, fut: F) -> JoinHandle<F::Output>

Spawn a future onto the current arbiter.

Source

pub async fn try_singleton<T: Service<Context = Context<T>> + Any>( &self, ) -> Result<Addr<T>, SingletonError>

Try to retrieve the address of a singleton service T. If there are more than one or no instances of T running, this method will return a SingletonError.

Source

pub async fn singleton<T: Service<Context = Context<T>> + Any>(&self) -> Addr<T>

Retrieve an address to the singleton service T. This method panics if the service wasn’t running or there were more than one instance of it.

Examples found in repository?
examples/ping.rs (line 67)
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    }

Auto Trait Implementations§

§

impl<S> Freeze for Context<S>

§

impl<S> RefUnwindSafe for Context<S>
where S: RefUnwindSafe,

§

impl<S> Send for Context<S>
where S: Send,

§

impl<S> Sync for Context<S>
where S: Sync,

§

impl<S> Unpin for Context<S>
where S: Unpin,

§

impl<S> UnwindSafe for Context<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> 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, 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.