Skip to main content

Actor

Trait Actor 

Source
pub trait Actor:
    Sized
    + Send
    + 'static {
    type Context: ActorContext<Self>;
    type Error: Into<Box<dyn Error + Send + Sync>> + Send + 'static;

    // Provided methods
    fn pre_start(&mut self, ctx: &mut Self::Context) -> Result<(), Self::Error> { ... }
    fn post_start(
        &mut self,
        ctx: &mut Self::Context,
    ) -> impl Future<Output = Result<(), Self::Error>> + Send { ... }
    fn stopping(
        &mut self,
        ctx: &mut Self::Context,
    ) -> impl Future<Output = Result<Stopping, Self::Error>> + Send { ... }
    fn post_stop(
        &mut self,
        ctx: &mut Self::Context,
    ) -> impl Future<Output = Result<(), Self::Error>> + Send { ... }
    fn run<S>(
        self,
        label: S,
    ) -> Result<(Address<Self>, JoinHandle<()>), Self::Error>
       where S: AsRef<str> { ... }
    fn create<S, F>(
        label: S,
        f: F,
    ) -> Result<(Address<Self>, JoinHandle<()>), Self::Error>
       where S: AsRef<str>,
             F: FnOnce(&mut Self::Context) -> Result<Self, Self::Error> { ... }
}
Expand description

Describes an actor.

Required Associated Types§

Source

type Context: ActorContext<Self>

The execution context type for this actor.

Source

type Error: Into<Box<dyn Error + Send + Sync>> + Send + 'static

The error type returned by lifecycle hooks and message handlers.

Provided Methods§

Source

fn pre_start(&mut self, ctx: &mut Self::Context) -> Result<(), Self::Error>

Invoked before an actor is spawned into the tokio runtime. The actor should be in Unstarted state.

This method is used to perform initialization tasks or spawn child actors. In the default Context implementation, it is not spawned into the tokio runtime and it is outside of the processing loop. Thus it will be invoked only once synchronously. The actor will enter the Starting state after this method returns.

Panics in this method will prevent the actor being spawned into the runtime. run will return an error to the caller in this case.

Source

fn post_start( &mut self, ctx: &mut Self::Context, ) -> impl Future<Output = Result<(), Self::Error>> + Send

Invoked after an actor is spawned into the tokio runtime. The actor should be in Starting state.

This method is used to perform addtional initialization. In the default Context implementation, it is spawned into the tokio runtime and it is outside of the processing loop. Thus it will be invoked only once asynchronously. The actor will enter the Running state after this method returns.

Panics in this method will be notified to the supervisor if there is one.

Source

fn stopping( &mut self, ctx: &mut Self::Context, ) -> impl Future<Output = Result<Stopping, Self::Error>> + Send

Invoked when an actor is being stopped. The actor should be in Stopping state.

This method is used to make decisions about whether to stop or to restart the actor.

Source

fn post_stop( &mut self, ctx: &mut Self::Context, ) -> impl Future<Output = Result<(), Self::Error>> + Send

Invoked after an actor is stopped. The actor should be in Stopped state.

This method is used to perform cleanup tasks or spawn new actors.

Source

fn run<S>( self, label: S, ) -> Result<(Address<Self>, JoinHandle<()>), Self::Error>
where S: AsRef<str>,

Starts an actor and spawns it to the tokio runtime, returns its actor address and the join handle.

Source

fn create<S, F>( label: S, f: F, ) -> Result<(Address<Self>, JoinHandle<()>), Self::Error>
where S: AsRef<str>, F: FnOnce(&mut Self::Context) -> Result<Self, Self::Error>,

Constructs a new actor, starts it and spawns it to the tokio runtime, returns its actor address and the join handle.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§