[][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 stopped<'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<'async_trait>(
    ) -> Pin<Box<dyn Future<Output = Addr<Self>> + Send + 'async_trait>>
    where
        Self: Default,
        Self: 'async_trait
, { ... }
fn start<'async_trait>(
        self
    ) -> Pin<Box<dyn Future<Output = Addr<Self>> + Send + 'async_trait>>
    where
        Self: 'async_trait
, { ... } }

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 stopped<'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 after an actor is stopped.

fn start_default<'async_trait>(
) -> Pin<Box<dyn Future<Output = Addr<Self>> + Send + 'async_trait>> where
    Self: Default,
    Self: 'async_trait, 

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<'async_trait>(
    self
) -> Pin<Box<dyn Future<Output = Addr<Self>> + Send + 'async_trait>> where
    Self: 'async_trait, 

Start a new actor, returning its address.

Examples

use xactor::*;

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: &Context<Self>, msg: MyMsg) -> i32 {
        msg.0 * msg.0
    }
}

#[async_std::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(())
}
Loading content...

Implementors

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

Loading content...