[−][src]Struct xactor::Broker
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]
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,
[src]
&'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,
[src]
) -> 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,
[src]
self
) -> Pin<Box<dyn Future<Output = Addr<Self>> + Send + 'async_trait>> where
Self: 'async_trait,
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,
T: Unpin,
impl<T> !UnwindSafe for Broker<T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,