[−][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>(); } } #[async_trait::async_trait] impl Handler<MyMsg> for MyActor { async fn handle(&mut self, _ctx: &Context<Self>, msg: MyMsg) { self.0 += msg.0; println!("add {}", self.0); } } #[async_trait::async_trait] impl Handler<GetValue> for MyActor { async fn handle(&mut self, _ctx: &Context<Self>, _msg: GetValue) -> String { println!("get {}", self.0); self.0.clone() } } #[async_std::main] async fn main() -> Result<()> { let mut addr1 = MyActor::start_default(); let mut addr2 = MyActor::start_default(); Broker::from_registry().publish(MyMsg("a")); Broker::from_registry().publish(MyMsg("b")); task::sleep(Duration::from_secs(1)).await; // Wait for the actors started assert_eq!(addr1.call(GetValue).await?, "ab"); assert_eq!(addr2.call(GetValue).await?, "ab"); Ok(()) }
Trait Implementations
impl Actor for Broker
[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() -> Addr<Self> where
Self: Default,
[src]
Self: Default,
fn start(self) -> Addr<Self>
[src]
impl Default for Broker
[src]
impl Service for Broker
[src]
fn from_registry() -> Addr<Self>
[src]
Auto Trait Implementations
impl !RefUnwindSafe for Broker
impl Send for Broker
impl !Sync for Broker
impl Unpin for Broker
impl !UnwindSafe for Broker
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>,