pub trait Actor: Sized + Send + 'static {
    const NAME: &'static str;
    fn started<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        ctx: &'life1 mut Context<Self>
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        'life1: 'async_trait,
        Self: 'async_trait
, { ... }
fn stopped<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        ctx: &'life1 mut Context<Self>
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        'life1: 'async_trait,
        Self: 'async_trait
, { ... }
fn start_default<'async_trait>(
    ) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>>
    where
        Self: Default,
        Self: 'async_trait
, { ... }
fn start<'async_trait>(
        self
    ) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>>
    where
        Self: 'async_trait
, { ... }
fn name(&self) -> Cow<'static, str> { ... } }
Expand description

Actors are objects which encapsulate state and behavior. Actors run within a specific execution context Context<A>. The context object is available only during execution. Each actor has a separate execution context.

Roles communicate by exchanging messages. The requester can wait for a response. By Addr referring to the actors, the actors must provide an Handler<T> implementation for this message. All messages are statically typed.

Associated Constants

Provided methods

Called when the actor is first started.

Called after an actor is stopped.

Construct and start a new actor, returning its address.

This is constructs a new actor using the Default trait, and invokes its start method.

Start a new actor, returning its address.

Examples
use hannibal::*;

struct MyActor;

impl Actor for MyActor {}

#[message(result = "i32")]
struct MyMsg(i32);

#[async_trait::async_trait]
impl Handler<MyMsg> for MyActor {
    async fn handle(&mut self, _ctx: &mut Context<Self>, msg: MyMsg) -> i32 {
        msg.0 * msg.0
    }
}

#[hannibal::main]
async fn main() -> Result<()> {
    // Start actor and get its address
    let mut addr = MyActor.start().await?;

    // Send message `MyMsg` to actor via addr
    let res = addr.call(MyMsg(10)).await?;
    assert_eq!(res, 100);
    Ok(())
}

Implementors