Context

Struct Context 

Source
pub struct Context<A>
where A: Actor,
{ /* private fields */ }
Expand description

Context is used to control how the actor is managed and to get the actor’s address from inside of a message handler.

Implementations§

Source§

impl<A> Context<A>
where A: Actor,

Source

pub fn new(message_cap: Option<usize>) -> (Address<A>, Context<A>)

Creates a new actor context with a given mailbox capacity, returning an address to the actor and the context. This can be used as a builder to add more actors to an address before any have started.

§Example
let (addr, mut ctx) = Context::new(Some(32));
for n in 0..3 {
    smol::spawn(ctx.attach(MyActor::new(n))).detach();
}
ctx.run(MyActor::new(4)).await;
Source

pub fn attach(&mut self, actor: A) -> impl Future<Output = ()>

Attaches an actor of the same type listening to the same address as this actor is. They will operate in a message-stealing fashion, with no message handled by two actors.

Source

pub fn stop(&mut self)

Stop the actor as soon as it has finished processing current message. This will mean that the Actor::stopping method will be called.

Source

pub fn address(&self) -> Result<Address<A>, ActorShutdown>

Get an address to the current actor if there are still external addresses to the actor.

Source

pub async fn run(self, actor: A)

Run the given actor’s main loop, handling incoming messages to its mailbox.

Source

pub async fn yield_once(&mut self, act: &mut A)

Yields to the manager to handle one message.

Source

pub async fn handle_while<F, R>(&mut self, actor: &mut A, fut: F) -> R
where F: Future<Output = R>,

Handle any incoming messages for the actor while running a given future.

§Example
Source

pub fn notify<M>(&mut self, msg: M)
where M: Message, A: Handler<M>,

Notify this actor with a message that is handled before any other messages from the general queue are processed (therefore, immediately). If multiple notify messages are queued, they will still be processed in the order that they are queued (i.e the immediate priority is only over other messages).

Source

pub fn notify_all<M>(&mut self, msg: M)
where M: Message + Clone + Sync, A: Handler<M>,

Notify all actors on this address actor with a message that is handled after any other messages from the general queue are processed.

Source

pub fn notify_interval<F, M>( &mut self, duration: Duration, constructor: F, ) -> Result<impl Future<Output = ()>, ActorShutdown>
where F: Send + 'static + Fn() -> M, M: Message, A: Handler<M>,

Notify the actor with a synchronously handled message every interval until it is stopped (either directly with Context::stop, or for a lack of strong Addresses). This does not take priority over other messages.

Source

pub fn notify_after<M>( &mut self, duration: Duration, notification: M, ) -> Result<impl Future<Output = ()>, ActorShutdown>
where M: Message, A: Handler<M>,

Notify the actor with a synchronously handled message after a certain duration has elapsed. This does not take priority over other messages.

Auto Trait Implementations§

§

impl<A> Freeze for Context<A>

§

impl<A> !RefUnwindSafe for Context<A>

§

impl<A> Send for Context<A>

§

impl<A> !Sync for Context<A>

§

impl<A> Unpin for Context<A>

§

impl<A> !UnwindSafe for Context<A>

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.