pub struct Context<A>{ /* private fields */ }Expand description
An actor execution context.
Implementations§
Source§impl<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§
Source§impl<A> ActorContext for Context<A>where
    A: Actor<Context = Self>,
 
impl<A> ActorContext for Context<A>where
    A: Actor<Context = Self>,
Source§fn 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.Source§fn 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.Source§fn state(&self) -> ActorState
 
fn state(&self) -> ActorState
Retrieve the current Actor execution state.
Source§impl<A> AsyncContext<A> for Context<A>where
    A: Actor<Context = Self>,
 
impl<A> AsyncContext<A> for Context<A>where
    A: Actor<Context = Self>,
Source§fn spawn<F>(&mut self, fut: F) -> SpawnHandlewhere
    F: ActorFuture<A, Output = ()> + 'static,
 
fn spawn<F>(&mut self, fut: F) -> SpawnHandlewhere
    F: ActorFuture<A, Output = ()> + 'static,
Spawns a future into the context. Read more
Source§fn 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
Source§fn waiting(&self) -> bool
 
fn waiting(&self) -> bool
Checks if the context is paused (waiting for future completion or stopping).
Source§fn cancel_future(&mut self, handle: SpawnHandle) -> bool
 
fn cancel_future(&mut self, handle: SpawnHandle) -> bool
Cancels a spawned future. Read more
Source§fn add_stream<S>(&mut self, fut: S) -> SpawnHandle
 
fn add_stream<S>(&mut self, fut: S) -> SpawnHandle
Registers a stream with the context. Read more
Source§fn add_message_stream<S>(&mut self, fut: S)
 
fn add_message_stream<S>(&mut self, fut: S)
Registers a stream with the context, ignoring errors. Read more
Source§fn notify<M>(&mut self, msg: M)
 
fn notify<M>(&mut self, msg: M)
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.Source§fn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandle
 
fn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandle
Sends the message 
msg to self after a specified period of time. Read moreSource§fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
 
fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
Executes a closure after a specified period of time. Read more
Source§fn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
 
fn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
Spawns a job to execute the given closure periodically, at a
specified fixed interval.
Source§fn run_interval_at<F>(
    &mut self,
    start: Instant,
    interval: Duration,
    task: F,
) -> SpawnHandle
 
fn run_interval_at<F>( &mut self, start: Instant, interval: Duration, task: F, ) -> SpawnHandle
Spawns a periodic 
task function to begin executing at the given start time, and with the
given interval duration.Source§impl<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> 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> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more