[][src]Struct xactor::Broker

pub struct Broker<T: Message<Result = ()>> { /* fields omitted */ }

Message broker is used to support publishing and subscribing to messages.

Examples

use xactor::*;
use std::time::Duration;
use async_std::task;

#[message]
#[derive(Clone)]
struct MyMsg(&'static str);

#[message(result = "String")]
struct GetValue;

#[derive(Default)]
struct MyActor(String);

#[async_trait::async_trait]
impl Actor for MyActor {
    async fn started(&mut self, ctx: &Context<Self>)  {
        ctx.subscribe::<MyMsg>().await;
    }
}

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

#[async_trait::async_trait]
impl Handler<GetValue> for MyActor {
    async fn handle(&mut self, _ctx: &Context<Self>, _msg: GetValue) -> String {
        self.0.clone()
    }
}

#[async_std::main]
async fn main() -> Result<()> {
    let mut addr1 = MyActor::start_default().await;
    let mut addr2 = MyActor::start_default().await;

    Broker::from_registry().await.publish(MyMsg("a"));
    Broker::from_registry().await.publish(MyMsg("b"));

    task::sleep(Duration::from_secs(1)).await; // Wait for the messages

    assert_eq!(addr1.call(GetValue).await?, "ab");
    assert_eq!(addr2.call(GetValue).await?, "ab");
    Ok(())
}

Trait Implementations

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

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

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

Auto Trait Implementations

impl<T> !RefUnwindSafe for Broker<T>

impl<T> Send for Broker<T>

impl<T> !Sync for Broker<T>

impl<T> Unpin for Broker<T> where
    T: Unpin

impl<T> !UnwindSafe for Broker<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.