Actor

Trait Actor 

Source
pub trait Actor: Send + 'static {
    // Provided methods
    fn started<'life0, 'async_trait>(
        &'life0 mut self,
        _addr: Addr<Self>,
    ) -> Pin<Box<dyn Future<Output = ActorResult<()>> + Send + 'async_trait>>
       where Self: Sized + 'async_trait,
             'life0: 'async_trait { ... }
    fn error<'life0, 'async_trait>(
        &'life0 mut self,
        error: ActorError,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

Trait implemented by all actors. This trait is defined using the #[async_trait] attribute:

#[async_trait]
pub trait Actor: Send + 'static {
    /// Called automatically when an actor is started. Actors can use this
    /// to store their own address for future use.
    async fn started(&mut self, _addr: Addr<Self>) -> ActorResult<()>
    where
        Self: Sized,
    {
        Ok(())
    }

    /// Called when any actor method returns an error. If this method
    /// returns `true`, the actor will stop.
    /// The default implementation logs the error using the `log` crate
    /// and then stops the actor.
    async fn error(&mut self, error: ActorError) -> bool {
        error!("{}", error);
        true
    }
}

In order to use a trait object with the actor system, such as with Addr<dyn Trait>, the trait must extend this Actor trait.

Provided Methods§

Source

fn started<'life0, 'async_trait>( &'life0 mut self, _addr: Addr<Self>, ) -> Pin<Box<dyn Future<Output = ActorResult<()>> + Send + 'async_trait>>
where Self: Sized + 'async_trait, 'life0: 'async_trait,

Called automatically when an actor is started. Actors can use this to store their own address for future use.

Source

fn error<'life0, 'async_trait>( &'life0 mut self, error: ActorError, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Called when any actor method returns an error. If this method returns true, the actor will stop. The default implementation logs the error using the log crate and then stops the actor.

Implementors§