Trait hannibal::Actor

source ·
pub trait Actor: Sized + Send + 'static {
    const NAME: &'static str = "hannibal::Actor";

    // Provided methods
    fn started(
        &mut self,
        ctx: &mut Context<Self>
    ) -> impl Future<Output = Result<()>> + Send { ... }
    fn stopped(
        &mut self,
        ctx: &mut Context<Self>
    ) -> impl Future<Output = ()> + Send { ... }
    fn start_default() -> impl Future<Output = Result<Addr<Self>>> + Send
       where Self: Default { ... }
    fn start(self) -> impl Future<Output = Result<Addr<Self>>> + Send { ... }
    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.

Provided Associated Constants§

source

const NAME: &'static str = "hannibal::Actor"

Provided Methods§

source

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

Called when the actor is first started.

source

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

Called after an actor is stopped.

source

fn start_default() -> impl Future<Output = Result<Addr<Self>>> + Send
where Self: Default,

Construct and start a new actor, returning its address.

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

source

fn start(self) -> impl Future<Output = Result<Addr<Self>>> + Send

Start a new actor, returning its address.

§Examples
use hannibal::*;

struct MyActor;

impl Actor for MyActor {}

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

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(())
}
source

fn name(&self) -> Cow<'static, str>

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T: Message<Result = ()>> Actor for Broker<T>