pub trait AsyncContext<A>: ActorContextwhere
A: Actor<Context = Self>,{
// Required methods
fn address(&self) -> Addr<A>;
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
where F: ActorFuture<A, Output = ()> + 'static;
fn wait<F>(&mut self, fut: F)
where F: ActorFuture<A, Output = ()> + 'static;
fn waiting(&self) -> bool;
fn cancel_future(&mut self, handle: SpawnHandle) -> bool;
// Provided methods
fn add_stream<S>(&mut self, fut: S) -> SpawnHandle
where S: Stream + 'static,
A: StreamHandler<S::Item> { ... }
fn add_message_stream<S>(&mut self, fut: S)
where S: Stream + 'static,
S::Item: Message,
A: Handler<S::Item> { ... }
fn notify<M>(&mut self, msg: M)
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 { ... }
fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
where F: FnOnce(&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 { ... }
fn run_interval_at<F>(
&mut self,
start: Instant,
interval: Duration,
task: F,
) -> SpawnHandle
where F: FnMut(&mut A, &mut A::Context) + 'static { ... }
}
Expand description
Asynchronous execution context.
Required Methods§
Sourcefn 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.
Returns a handle of the spawned future, which can be used for cancelling its execution.
All futures spawned into an actor’s context are cancelled during the actor’s stopping stage.
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.
This stops processing any incoming events until the future resolves.
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.
The handle
is a value returned by the spawn
method.
Provided Methods§
Sourcefn add_stream<S>(&mut self, fut: S) -> SpawnHandle
fn add_stream<S>(&mut self, fut: S) -> SpawnHandle
Registers a stream with the context.
This allows handling a Stream
in a way similar to normal
actor messages.
use actix::prelude::*;
use futures_util::stream::once;
#[derive(Message)]
#[rtype(result = "()")]
struct Ping;
struct MyActor;
impl StreamHandler<Ping> for MyActor {
fn handle(&mut self, item: Ping, ctx: &mut Context<MyActor>) {
println!("PING");
System::current().stop();
}
fn finished(&mut self, ctx: &mut Self::Context) {
println!("finished");
}
}
impl Actor for MyActor {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Context<Self>) {
// add stream
ctx.add_stream(once(async { Ping }));
}
}
fn main() {
let mut sys = System::new();
let addr = sys.block_on(async { MyActor.start() });
sys.run();
}
Sourcefn 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.
This method is similar to add_stream
but it skips stream
errors.
use actix::prelude::*;
use futures_util::stream::once;
#[derive(Message)]
#[rtype(result = "()")]
struct Ping;
struct MyActor;
impl Handler<Ping> for MyActor {
type Result = ();
fn handle(&mut self, msg: Ping, ctx: &mut Context<MyActor>) {
println!("PING");
}
}
impl Actor for MyActor {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Context<Self>) {
// add messages stream
ctx.add_message_stream(once(async { Ping }));
}
}
let addr = MyActor.start();
Sourcefn 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.
Sourcefn 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.
Returns a spawn handle which can be used for cancellation. The
notification gets cancelled if the context’s stop method gets
called. This bypasses the mailbox capacity, and
will always queue the message. If the actor is in the stopped
state, an
error will be raised.
Sourcefn 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.
The closure gets passed the same actor and its context. Execution gets cancelled if the context’s stop method gets called.
Sourcefn 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.
Sourcefn 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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.