Expand description
Message broker is used to support publishing and subscribing to messages.
Examples
use hannibal::*;
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()
}
}
#[hannibal::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
sourceimpl<T: Message<Result = ()>> Actor for Broker<T>
impl<T: Message<Result = ()>> Actor for Broker<T>
const NAME: &'static str
sourcefn 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,
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,
Called when the actor is first started.
sourcefn 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,
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,
Called after an actor is stopped.
sourcefn start_default<'async_trait>(
) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>> where
Self: Default,
Self: 'async_trait,
fn start_default<'async_trait>(
) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>> where
Self: Default,
Self: 'async_trait,
Construct and start a new actor, returning its address. Read more
sourcefn start<'async_trait>(
self
) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>> where
Self: 'async_trait,
fn start<'async_trait>(
self
) -> Pin<Box<dyn Future<Output = Result<Addr<Self>>> + Send + 'async_trait>> where
Self: 'async_trait,
Start a new actor, returning its address. Read more
fn name(&self) -> Cow<'static, str>
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more