[−][src]Struct xactor::Broker
Message broker is used to support publishing and subscribing to messages.
Examples
use xactor::*; use std::time::Duration; #[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: &mut Context<Self>) -> Result<()> { ctx.subscribe::<MyMsg>().await; Ok(()) } } #[async_trait::async_trait] impl Handler<MyMsg> for MyActor { async fn handle(&mut self, _ctx: &mut Context<Self>, msg: MyMsg) { self.0 += msg.0; } } #[async_trait::async_trait] impl Handler<GetValue> for MyActor { async fn handle(&mut self, _ctx: &mut Context<Self>, _msg: GetValue) -> String { self.0.clone() } } #[xactor::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")); 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]
#[must_use]pub fn started<'life0, 'life1, 'async_trait>(
&'life0 mut self,
ctx: &'life1 mut Context<Self>
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
[src]
&'life0 mut self,
ctx: &'life1 mut Context<Self>
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
#[must_use]pub fn stopped<'life0, 'life1, 'async_trait>(
&'life0 mut self,
ctx: &'life1 mut 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 mut Context<Self>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
#[must_use]pub fn start_default<'async_trait>(
) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>> where
Self: Default,
Self: 'async_trait,
[src]
) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>> where
Self: Default,
Self: 'async_trait,
#[must_use]pub fn start<'async_trait>(
self
) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>> where
Self: 'async_trait,
[src]
self
) -> Pin<Box<dyn Future<Output = Result<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>
[src]
impl<T> Send for Broker<T>
[src]
impl<T> !Sync for Broker<T>
[src]
impl<T> Unpin for Broker<T> where
T: Unpin,
[src]
T: Unpin,
impl<T> !UnwindSafe for Broker<T>
[src]
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,
pub 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.
pub 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>,