messagebus-next/examples/demo.rs
Andrey Tkachenko 4dd6a7a2af handler context
2023-11-23 10:17:58 +04:00

87 lines
1.7 KiB
Rust

#![feature(return_position_impl_trait_in_trait)]
#![feature(async_fn_in_trait)]
use std::sync::Arc;
use anyhow::Error;
use messagebus::{Builder, Bus, Context, Handler, IntoMessages, Message};
#[derive(Debug, Clone)]
pub struct Msg(pub i32);
impl Message for Msg {}
pub struct Processor {
_state: i32,
}
impl Handler<Msg> for Processor {
type Result = ();
type Error = Error;
async fn handle(
&mut self,
_msg: Msg,
_ctx: Context,
_bus: Bus,
) -> Result<impl IntoMessages<Self::Result, Self::Error>, Self::Error> {
Ok(())
}
async fn handle_error(
&mut self,
_err: messagebus::Error,
_ctx: Context,
_bus: messagebus::Bus,
) -> Result<impl IntoMessages<Self::Result, Self::Error> + Send + '_, Self::Error> {
Ok(None)
}
async fn finalize(self, _bus: Bus) -> Result<(), Self::Error> {
Ok(())
}
}
struct ProcSpawner;
impl Builder<Msg> for ProcSpawner {
type Context = Processor;
async fn build(
&self,
stream_id: u32,
_task_id: u32,
) -> Result<Self::Context, messagebus::Error> {
Ok(Processor {
_state: stream_id as _,
})
}
}
impl Processor {
pub async fn spawn(_sid: u32) -> Result<(usize, Self), Error> {
Ok((4, Self { _state: 0 }))
}
pub async fn handler_msg(
self: Arc<Self>,
_sid: u32,
_tid: u32,
_msg: Msg,
) -> Result<(), Error> {
Ok(())
}
pub async fn finalize_msg_handler(self: Arc<Self>, _sid: u32) -> Result<(), Error> {
Ok(())
}
}
async fn run() {
let bus = Bus::new();
bus.register(ProcSpawner).await;
}
#[tokio::main]
async fn main() {
run().await
}