pub struct Context<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
sourceimpl<A: Actor> Context<A>
impl<A: Actor> Context<A>
sourcepub fn new(message_cap: Option<usize>) -> (Address<A>, Self)
pub fn new(message_cap: Option<usize>) -> (Address<A>, Self)
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;
sourcepub fn attach(&mut self, actor: A) -> impl Future<Output = ()>
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.
sourcepub fn stop(&mut self)
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.
sourcepub fn address(&self) -> Result<Address<A>, ActorShutdown>
pub fn address(&self) -> Result<Address<A>, ActorShutdown>
Get an address to the current actor if there are still external addresses to the actor.
sourcepub async fn run(self, actor: A)
pub async fn run(self, actor: A)
Run the given actor’s main loop, handling incoming messages to its mailbox.
sourcepub async fn yield_once(&mut self, act: &mut A)
pub async fn yield_once(&mut self, act: &mut A)
Yields to the manager to handle one message.
sourcepub async fn handle_while<F, R>(&mut self, actor: &mut A, fut: F) -> R where
F: Future<Output = R>,
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
sourcepub fn notify<M>(&mut self, msg: M) where
M: Message,
A: Handler<M>,
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).
sourcepub fn notify_all<M>(&mut self, msg: M) where
M: Message + Clone + Sync,
A: Handler<M>,
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.
sourcepub 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>,
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 Address
es). This does not take priority over other messages.
sourcepub fn notify_after<M>(
&mut self,
duration: Duration,
notification: M
) -> Result<impl Future<Output = ()>, ActorShutdown> where
M: Message,
A: Handler<M>,
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> !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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more