Trait actix::AsyncContext[][src]

pub trait AsyncContext<A>: ActorContext where
    A: Actor<Context = Self>, 
{ 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; 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

Returns the address of the context.

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.

Spawns a future into the context, waiting for it to resolve.

This stops processing any incoming events until the future resolves.

Checks if the context is paused (waiting for future completion or stopping).

Cancels a spawned future.

The handle is a value returned by the spawn method.

Provided methods

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

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

fn main() {
   System::new().block_on(async {
       let addr = MyActor.start();
   });
}

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.

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.

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.

Spawns a job to execute the given closure periodically, at a specified fixed interval.

Implementors