Expand description
An actor execution context.
Implementations
sourceimpl<A> Context<A> where
A: Actor<Context = Self>,
impl<A> Context<A> where
A: Actor<Context = Self>,
sourcepub fn new() -> Self
pub fn new() -> Self
Create a context without spawning it.
The context can be spawned into an actor using its run
method.
struct Actor1 {
actor2_addr: Addr<Actor2>,
}
struct Actor2 {
actor1_addr: Addr<Actor1>,
}
let ctx1 = Context::<Actor1>::new();
let ctx2 = Context::<Actor2>::new();
let actor1 = Actor1 { actor2_addr: ctx2.address() };
let actor2 = Actor2 { actor1_addr: ctx1.address() };
ctx1.run(actor1);
ctx2.run(actor2);
pub fn with_receiver(rx: AddressReceiver<A>) -> Self
pub fn run(self, act: A) -> Addr<A>
pub fn into_future(self, act: A) -> ContextFut<A, Self>
sourcepub fn handle(&self) -> SpawnHandle
pub fn handle(&self) -> SpawnHandle
Returns a handle to the running future.
This is the handle returned by the AsyncContext::spawn()
method.
sourcepub fn set_mailbox_capacity(&mut self, cap: usize)
pub fn set_mailbox_capacity(&mut self, cap: usize)
Sets the mailbox capacity.
The default mailbox capacity is 16 messages. #Examples
struct MyActor;
impl Actor for MyActor {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
ctx.set_mailbox_capacity(1);
System::current().stop();
}
}
let addr = sys.block_on(async { MyActor.start() });
sys.run();
Trait Implementations
sourceimpl<A> ActorContext for Context<A> where
A: Actor<Context = Self>,
impl<A> ActorContext for Context<A> where
A: Actor<Context = Self>,
sourcefn stop(&mut self)
fn stop(&mut self)
Immediately stop processing incoming messages and switch to a
stopping
state. This only affects actors that are currently
running
. Future attempts to queue messages will fail. Read more
sourcefn terminate(&mut self)
fn terminate(&mut self)
Terminate actor execution unconditionally. This sets the actor
into the stopped
state. This causes future attempts to queue
messages to fail. Read more
sourcefn state(&self) -> ActorState
fn state(&self) -> ActorState
Retrieve the current Actor execution state.
sourceimpl<A> AsyncContext<A> for Context<A> where
A: Actor<Context = Self>,
impl<A> AsyncContext<A> for Context<A> where
A: Actor<Context = Self>,
sourcefn spawn<F>(&mut self, fut: F) -> SpawnHandle where
F: ActorFuture<A, Output = ()> + 'static,
fn spawn<F>(&mut self, fut: F) -> SpawnHandle where
F: ActorFuture<A, Output = ()> + 'static,
Spawns a future into the context. Read more
sourcefn wait<F>(&mut self, fut: F) where
F: ActorFuture<A, Output = ()> + 'static,
fn wait<F>(&mut self, fut: F) where
F: ActorFuture<A, Output = ()> + 'static,
Spawns a future into the context, waiting for it to resolve. Read more
sourcefn waiting(&self) -> bool
fn waiting(&self) -> bool
Checks if the context is paused (waiting for future completion or stopping).
sourcefn cancel_future(&mut self, handle: SpawnHandle) -> bool
fn cancel_future(&mut self, handle: SpawnHandle) -> bool
Cancels a spawned future. Read more
sourcefn add_stream<S>(&mut self, fut: S) -> SpawnHandle where
S: Stream + 'static,
A: StreamHandler<S::Item>,
fn add_stream<S>(&mut self, fut: S) -> SpawnHandle where
S: Stream + 'static,
A: StreamHandler<S::Item>,
Registers a stream with the context. Read more
sourcefn add_message_stream<S>(&mut self, fut: S) where
S: Stream + 'static,
S::Item: Message,
A: Handler<S::Item>,
fn add_message_stream<S>(&mut self, fut: S) where
S: Stream + 'static,
S::Item: Message,
A: Handler<S::Item>,
Registers a stream with the context, ignoring errors. Read more
sourcefn notify<M>(&mut self, msg: M) where
A: Handler<M>,
M: Message + 'static,
fn notify<M>(&mut self, msg: M) where
A: Handler<M>,
M: Message + 'static,
Sends the message msg
to self. This bypasses the mailbox capacity, and
will always queue the message. If the actor is in the stopped
state, an
error will be raised. Read more
sourcefn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandle where
A: Handler<M>,
M: Message + 'static,
fn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandle where
A: Handler<M>,
M: Message + 'static,
Sends the message msg
to self after a specified period of time. Read more
sourcefn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle where
F: FnOnce(&mut A, &mut A::Context) + 'static,
fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle where
F: FnOnce(&mut A, &mut A::Context) + 'static,
Executes a closure after a specified period of time. Read more
sourcefn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandle where
F: FnMut(&mut A, &mut A::Context) + 'static,
fn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandle where
F: FnMut(&mut A, &mut A::Context) + 'static,
Spawns a job to execute the given closure periodically, at a specified fixed interval. Read more
sourceimpl<A> AsyncContextParts<A> for Context<A> where
A: Actor<Context = Self>,
impl<A> AsyncContextParts<A> for Context<A> where
A: Actor<Context = Self>,
fn parts(&mut self) -> &mut ContextParts<A>
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 · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more