Trait actix::prelude::actix::Actor
[−]
[src]
pub trait Actor: Sized + 'static { type Context: ActorContext + ToEnvelope<Self>; fn started(&mut self, ctx: &mut Self::Context) { ... } fn stopping(&mut self, ctx: &mut Self::Context) { ... } fn stopped(&mut self, ctx: &mut Self::Context) { ... } fn start<Addr>(self) -> Addr
where
Self: Actor<Context = Context<Self>> + ActorAddress<Self, Addr>, { ... } fn start_default<Addr>() -> Addr
where
Self: Default + Actor<Context = Context<Self>> + ActorAddress<Self, Addr>, { ... } fn create<Addr, F>(f: F) -> Addr
where
Self: Actor<Context = Context<Self>> + ActorAddress<Self, Addr>,
F: FnOnce(&mut Context<Self>) -> Self + 'static, { ... } fn reply<M>(val: M::Item) -> Response<Self, M>
where
M: ResponseType, { ... } fn async_reply<T, M>(fut: T) -> Response<Self, M>
where
M: ResponseType,
T: ActorFuture<Item = M::Item, Error = M::Error, Actor = Self> + Sized + 'static, { ... } fn empty<M>() -> Response<Self, M>
where
M: ResponseType<Item = ()>, { ... } fn reply_error<M>(err: M::Error) -> Response<Self, M>
where
M: ResponseType, { ... } }
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
non thread safe Address
or thread safe address
SyncAddress<A>
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.
Stopped
If actor does not modify execution context during stooping state actor state changes
to Stopped
. This state is considered final and at this point actor get dropped.
Associated Types
type Context: ActorContext + ToEnvelope<Self>
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)
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 to running state
by creating new address or adding future or stream to current content.
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<Addr>(self) -> Addr where
Self: Actor<Context = Context<Self>> + ActorAddress<Self, Addr>,
Self: Actor<Context = Context<Self>> + ActorAddress<Self, Addr>,
Start new asynchronous actor, returns address of newly created actor.
Examples
use actix::*; // initialize system System::new("test"); struct MyActor; impl Actor for MyActor { type Context = Context<Self>; } let addr: Address<_> = MyActor.start();
fn start_default<Addr>() -> Addr where
Self: Default + Actor<Context = Context<Self>> + ActorAddress<Self, Addr>,
Self: Default + Actor<Context = Context<Self>> + ActorAddress<Self, Addr>,
Start new asynchronous actor, returns address of newly created actor.
fn create<Addr, F>(f: F) -> Addr where
Self: Actor<Context = Context<Self>> + ActorAddress<Self, Addr>,
F: FnOnce(&mut Context<Self>) -> Self + 'static,
Self: Actor<Context = Context<Self>> + ActorAddress<Self, Addr>,
F: FnOnce(&mut Context<Self>) -> Self + 'static,
Use create
method, if you need Context
object during actor initialization.
Examples
use actix::*; // initialize system System::new("test"); struct MyActor{val: usize}; impl Actor for MyActor { type Context = Context<Self>; } let addr: Address<_> = MyActor::create(|ctx: &mut Context<MyActor>| { MyActor{val: 10} });
fn reply<M>(val: M::Item) -> Response<Self, M> where
M: ResponseType,
M: ResponseType,
Create static response.
fn async_reply<T, M>(fut: T) -> Response<Self, M> where
M: ResponseType,
T: ActorFuture<Item = M::Item, Error = M::Error, Actor = Self> + Sized + 'static,
M: ResponseType,
T: ActorFuture<Item = M::Item, Error = M::Error, Actor = Self> + Sized + 'static,
Create async response process.
fn empty<M>() -> Response<Self, M> where
M: ResponseType<Item = ()>,
M: ResponseType<Item = ()>,
Create unit response, for case when ResponseType::Item = ()
fn reply_error<M>(err: M::Error) -> Response<Self, M> where
M: ResponseType,
M: ResponseType,
Create error response
Implementors
impl Actor for Arbiter type Context = Context<Self>;
impl Actor for System type Context = Context<Self>;
impl Actor for ProcessSignals type Context = Context<Self>;
impl Actor for DefaultSignalsHandler type Context = Context<Self>;
impl<A> Actor for SyncArbiter<A> where
A: Actor<Context = SyncContext<A>>, type Context = Context<Self>;