[][src]Trait actix_web::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

Loading content...

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 its 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 });
    });
}
Loading content...

Implementations on Foreign Types

impl Actor for Server[src]

type Context = Context<Server>

fn started(&mut self, ctx: &mut Self::Context)[src]

fn stopping(&mut self, ctx: &mut Self::Context) -> Running[src]

fn stopped(&mut self, ctx: &mut Self::Context)[src]

fn start(self) -> Addr<Self> where
    Self: Actor<Context = Context<Self>>, 
[src]

fn start_default() -> Addr<Self> where
    Self: Actor<Context = Context<Self>> + Default
[src]

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

Loading content...

Implementors

impl Actor for Resolver[src]

type Context = Context<Resolver>

fn stopping(&mut self, ctx: &mut Self::Context) -> Running[src]

fn stopped(&mut self, ctx: &mut Self::Context)[src]

fn start(self) -> Addr<Self> where
    Self: Actor<Context = Context<Self>>, 
[src]

fn start_default() -> Addr<Self> where
    Self: Actor<Context = Context<Self>> + Default
[src]

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

impl Actor for DefaultSignalsHandler[src]

type Context = Context<DefaultSignalsHandler>

fn stopping(&mut self, ctx: &mut Self::Context) -> Running[src]

fn stopped(&mut self, ctx: &mut Self::Context)[src]

fn start(self) -> Addr<Self> where
    Self: Actor<Context = Context<Self>>, 
[src]

fn start_default() -> Addr<Self> where
    Self: Actor<Context = Context<Self>> + Default
[src]

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

impl Actor for ProcessSignals[src]

type Context = Context<ProcessSignals>

fn started(&mut self, ctx: &mut Self::Context)[src]

fn stopping(&mut self, ctx: &mut Self::Context) -> Running[src]

fn stopped(&mut self, ctx: &mut Self::Context)[src]

fn start(self) -> Addr<Self> where
    Self: Actor<Context = Context<Self>>, 
[src]

fn start_default() -> Addr<Self> where
    Self: Actor<Context = Context<Self>> + Default
[src]

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

impl Actor for Arbiter[src]

type Context = Context<Arbiter>

fn started(&mut self, ctx: &mut Self::Context)[src]

fn stopping(&mut self, ctx: &mut Self::Context) -> Running[src]

fn stopped(&mut self, ctx: &mut Self::Context)[src]

fn start(self) -> Addr<Self> where
    Self: Actor<Context = Context<Self>>, 
[src]

fn start_default() -> Addr<Self> where
    Self: Actor<Context = Context<Self>> + Default
[src]

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

impl Actor for ClientConnector[src]

type Context = Context<ClientConnector>

fn stopping(&mut self, ctx: &mut Self::Context) -> Running[src]

fn stopped(&mut self, ctx: &mut Self::Context)[src]

fn start(self) -> Addr<Self> where
    Self: Actor<Context = Context<Self>>, 
[src]

fn start_default() -> Addr<Self> where
    Self: Actor<Context = Context<Self>> + Default
[src]

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

impl<A> Actor for SyncArbiter<A> where
    A: Actor<Context = SyncContext<A>>, 
[src]

type Context = Context<SyncArbiter<A>>

fn started(&mut self, ctx: &mut Self::Context)[src]

fn stopping(&mut self, ctx: &mut Self::Context) -> Running[src]

fn stopped(&mut self, ctx: &mut Self::Context)[src]

fn start(self) -> Addr<Self> where
    Self: Actor<Context = Context<Self>>, 
[src]

fn start_default() -> Addr<Self> where
    Self: Actor<Context = Context<Self>> + Default
[src]

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

impl<T> Actor for Mocker<T> where
    T: 'static, 
[src]

type Context = Context<Mocker<T>>

fn started(&mut self, ctx: &mut Self::Context)[src]

fn stopping(&mut self, ctx: &mut Self::Context) -> Running[src]

fn stopped(&mut self, ctx: &mut Self::Context)[src]

fn start(self) -> Addr<Self> where
    Self: Actor<Context = Context<Self>>, 
[src]

fn start_default() -> Addr<Self> where
    Self: Actor<Context = Context<Self>> + Default
[src]

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

Loading content...