Trait actix::AsyncContext

source ·
pub trait AsyncContext<A>: ActorContext
where 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 { ... } }
Expand description

Asynchronous execution context.

Required Methods§

source

fn address(&self) -> Addr<A>

Returns the address of the context.

source

fn spawn<F>(&mut self, fut: F) -> SpawnHandle
where 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.

source

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.

source

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

Cancels a spawned future.

The handle is a value returned by the spawn method.

Provided Methods§

source

fn add_stream<S>(&mut self, fut: S) -> SpawnHandle
where S: Stream + 'static, A: StreamHandler<S::Item>,

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();
 }
source

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.

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();
source

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.

source

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.

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.

source

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.

The closure gets passed the same actor and its context. Execution gets cancelled if the context’s stop method gets called.

source

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.

Examples found in repository?
examples/weak_addr.rs (line 51)
49
50
51
52
    fn started(&mut self, ctx: &mut Self::Context) {
        println!("⏰ starting TimeService");
        ctx.run_interval(Duration::from_millis(1_000), Self::send_tick);
    }
More examples
Hide additional examples
examples/weak_recipient.rs (line 51)
49
50
51
52
    fn started(&mut self, ctx: &mut Self::Context) {
        println!("⏰ starting TimeService");
        ctx.run_interval(Duration::from_millis(1_000), Self::send_tick);
    }

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<A> AsyncContext<A> for Context<A>
where A: Actor<Context = Self>,