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

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

    pub trait Actor: 'static {
        type Context: ActorContext;
        fn started(&mut self, ctx: &mut Self::Context) { ... }
        fn stopping(&mut self, ctx: &mut Self::Context) -> Running { ... }
        fn stopped(&mut self, ctx: &mut Self::Context) { ... }
        fn start(self) -> Addr<Self>
        where
            Self: Actor<Context = Context<Self>>,
        { ... }
        fn start_default() -> Addr<Self>
        where
            Self: Actor<Context = Context<Self>> + Default,
        { ... }
        fn create<F>(f: F) -> Addr<Self>
        where
            F: FnOnce(&mut Context<Self>) -> Self + 'static,
            Self: Actor<Context = Context<Self>>,
        { ... }
    }

Actors are objects which encapsulate state and behavior.

Actors run within specific execution context Context. Context object is available only during execution. Each actor has separate execution context. Also execution context controls lifecycle of an actor.

Actors communicate exclusively by exchanging messages. Sender actor can wait for response. Actors are not referenced directly, but by address `Addr` To be able to handle specific message actor has to provide `Handler<M>` implementation for this message. All messages are statically typed. Message could be handled in asynchronous fashion. Actor can spawn other actors or add futures or streams to execution context. Actor trait provides several methods that allow to control actor lifecycle.

# Actor lifecycle

## Started

Actor starts in `Started` state, during this state `started` method get called.

## Running

After Actor's method `started` get called, actor transitions to `Running` state. Actor can stay in `running` state indefinitely long.

## Stopping

Actor execution state changes to `stopping` state in following situations,

- `Context::stop` get called by actor itself
- all addresses to the actor get dropped
- no evented objects are registered in context.

Actor could restore from `stopping` state to `running` state by creating new address or adding evented object, like future or stream, in `Actor::stopping` method.

If actor changed state to a `stopping` state because of `Context::stop()` get called then context immediately stops processing incoming messages and calls `Actor::stopping()` method. If actor does not restore back to a `running` state, all unprocessed messages get dropped.

## Stopped

If actor does not modify execution context during stopping state actor state changes to `Stopped`. This state is considered final and at this point actor get dropped.

## Associated Types

### `type Context: ActorContext`

Actor execution context type

## Provided Methods

### `fn started(&mut self, ctx: &mut Self::Context)`

Method is called when actor get polled first time.

### `fn stopping(&mut self, ctx: &mut Self::Context) -> Running`

Method is called after an actor is in `Actor::Stopping` state. There could be several reasons for stopping. `Context::stop` get called by the actor itself. All addresses to current actor get dropped and no more evented objects left in the context.

Actor could restore from stopping state by returning `Running::Continue` value.

### `fn stopped(&mut self, ctx: &mut Self::Context)`

Method is called after an actor is stopped, it can be used to perform any needed cleanup work or spawning more actors. This is final state, after this call actor get dropped.

### `fn start(self) -> Addr<Self> where
Self: Actor<Context = Context<Self>>,`

Start new asynchronous actor, returns address of newly created actor.

# Examples

    use actix::*;

    struct MyActor;
    impl Actor for MyActor {
        type Context = Context<Self>;
    }

    fn main() {
        // initialize system
        System::run(|| {
            let addr = MyActor.start(); // <- start actor and get it's address
        });
    }

### `fn start_default() -> Addr<Self> where
Self: Actor<Context = Context<Self>> + Default,`

Start new asynchronous actor, returns address of newly created actor.

### `fn create<F>(f: F) -> Addr<Self> where
F: FnOnce(&mut Context<Self>) -> Self + 'static,
Self: Actor<Context = Context<Self>>,`

Use `create` method, if you need `Context` object during actor initialization.

# Examples

    use actix::*;

    struct MyActor {
        val: usize,
    }
    impl Actor for MyActor {
        type Context = Context<Self>;
    }

    fn main() {
        // initialize system
        System::run(|| {
            let addr = MyActor::create(|ctx: &mut Context<MyActor>| MyActor { val: 10 });
        });
    }

## Implementors

- `impl Actor for ProcessSignals type Context = Context<ProcessSignals>;` |

- `impl Actor for DefaultSignalsHandler type Context = Context<DefaultSignalsHandler>;` |

- `impl<A> Actor for SyncArbiter<A> where A: Actor<Context = SyncContext<A>>, type Context = Context<SyncArbiter<A>>;` |

- `impl Actor for Resolver type Context = Context<Resolver>;` |

- `impl Actor for Arbiter type Context = Context<Arbiter>;` |

- `impl Actor for ClientConnector type Context = Context<ClientConnector>;` | [src]

- `impl<H> Actor for HttpServer<H> where H: IntoHttpHandler, type Context = 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`)