# actix_web::actix::actix::AsyncContext - Rust
API documentation for the Rust `AsyncContext` trait in crate `actix_web`.
Keywords: rust, rustlang, rust-lang, AsyncContext

# Trait actix_web::actix::actix::AsyncContext [−]

    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<Item = (), Error = (), Actor = A> + 'static;
        fn wait<F>(&mut self, fut: F)
        where
            F: ActorFuture<Item = (), Error = (), Actor = A> + 'static;
        fn waiting(&self) -> bool;
        fn cancel_future(&mut self, handle: SpawnHandle) -> bool;

        fn add_stream<S>(&mut self, fut: S) -> SpawnHandle
        where
            A: StreamHandler<<S as Stream>::Item, <S as Stream>::Error>,
            S: Stream + 'static,
        { ... }
        fn add_message_stream<S>(&mut self, fut: S)
        where
            A: Handler<<S as Stream>::Item>,
            S: Stream<Error = ()> + 'static,
            <S as Stream>::Item: Message,
        { ... }
        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 as Actor>::Context) + 'static,
        { ... }
        fn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
        where
            F: FnMut(&mut A, &mut <A as Actor>::Context) + 'static,
        { ... }
    }

Asynchronous execution context

## Required Methods

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

Return `Address` of the context

### `fn spawn<F>(&mut self, fut: F) -> SpawnHandle where
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,`

Spawn async future into context. Returns handle of the item, could be used for cancelling execution.

All futures cancel during actor stopping stage.

### `fn wait<F>(&mut self, fut: F) where
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,`

Spawn future into the context. Stop processing any of incoming events until this future resolves.

### `fn waiting(&self) -> bool`

Check if context is paused (waiting for future completion or stopping)

### `fn cancel_future(&mut self, handle: SpawnHandle) -> bool`

Cancel future. handle is a value returned by `spawn` method.

## Provided Methods

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

This method register stream to an actor context and allows to handle `Stream` in similar way as normal actor messages.

    use actix::prelude::*;
    use futures::stream::once;

    #[derive(Message)]
    struct Ping;

    struct MyActor;

    impl StreamHandler<Ping, io::Error> for MyActor {

        fn handle(&mut self, item: Ping, ctx: &mut Context<MyActor>) {
            println!("PING");
        }

        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::<Ping, io::Error>(Ok(Ping)));
       }
    }

### `fn add_message_stream<S>(&mut self, fut: S) where
A: Handler<<S as Stream>::Item>,
S: Stream<Error = ()> + 'static,
<S as Stream>::Item: Message,`

This method is similar to `add_stream` but it skips stream errors.

    use actix::prelude::*;
    use futures::stream::once;

    #[derive(Message)]
    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(Ok(Ping)));
        }
    }

### `fn notify<M>(&mut self, msg: M) where
A: Handler<M>,
M: Message + 'static,`

Send message `msg` to self.

### `fn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandle where
A: Handler<M>,
M: Message + 'static,`

Send message `msg` to self after specified period of time. Returns spawn handle which could be used for cancellation. Notification get cancelled if context's stop method get called.

### `fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle where
F: FnOnce(&mut A, &mut <A as Actor>::Context) + 'static,`

Execute closure after specified period of time within same Actor and Context. Execution get cancelled if context's stop method get called.

### `fn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandle where
F: FnMut(&mut A, &mut <A as Actor>::Context) + 'static,`

Spawns job to execute closure with specified interval

## Implementors

- `impl<A, S> AsyncContext<A> for HttpContext<A, S> where A: Actor<Context = Self>,` | [src]

- `impl<A> AsyncContext<A> for Context<A> where A: Actor<Context = Context<A>>,` |

- `impl<A, S> AsyncContext<A> for WebsocketContext<A, S> where A: Actor<Context = Self>,` | [src]

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)