[][src]Trait xactor::Actor

pub trait Actor: Sized + Send + 'static {
    fn started<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        _ctx: &'life1 Context<Self>
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        'life1: 'async_trait,
        Self: 'async_trait
, { ... }
fn start_default() -> Addr<Self>
    where
        Self: Default
, { ... }
fn start(self) -> Addr<Self> { ... } }

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 Handle<T> implementation for this message. All messages are statically typed.

Provided methods

fn started<'life0, 'life1, 'async_trait>(
    &'life0 mut self,
    _ctx: &'life1 Context<Self>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where
    'life0: 'async_trait,
    'life1: 'async_trait,
    Self: 'async_trait, 

Called when the actor is first started

fn start_default() -> Addr<Self> 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.

fn start(self) -> Addr<Self>

Start a new actor, returning its address.

Examples

use xactor::*;

struct MyActor;

impl Actor for MyActor {}

struct MyMsg(i32);

impl Message for MyMsg {
    type Result = i32;
}

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

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

    // Call 'MyMsg' to actor via addr
    let res = addr.call(MyMsg(10)).await?;
    assert_eq!(res, 100);
    Ok(())
}
Loading content...

Implementors

Loading content...