2021-06-28 17:41:00 +04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-06-24 13:32:19 +04:00
|
|
|
use async_trait::async_trait;
|
2021-07-02 20:21:53 +04:00
|
|
|
use messagebus::{error, AsyncBatchHandler, BatchHandler, Bus, Message};
|
2021-06-28 17:41:00 +04:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[derive(Debug, Error, Clone)]
|
|
|
|
enum Error {
|
|
|
|
#[error("Error({0})")]
|
2021-07-02 20:21:53 +04:00
|
|
|
Error(Arc<anyhow::Error>),
|
2021-06-28 17:41:00 +04:00
|
|
|
}
|
2021-06-24 13:32:19 +04:00
|
|
|
|
2021-06-28 17:41:00 +04:00
|
|
|
impl<M: Message> From<error::Error<M>> for Error {
|
|
|
|
fn from(err: error::Error<M>) -> Self {
|
|
|
|
Self::Error(Arc::new(err.into()))
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 13:32:19 +04:00
|
|
|
struct TmpReceiver;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl AsyncBatchHandler<i32> for TmpReceiver {
|
|
|
|
type Error = Error;
|
|
|
|
type Response = ();
|
2021-07-02 20:21:53 +04:00
|
|
|
type InBatch = Vec<i32>;
|
|
|
|
type OutBatch = Vec<()>;
|
2021-06-24 13:32:19 +04:00
|
|
|
|
|
|
|
async fn handle(&self, msg: Vec<i32>, _bus: &Bus) -> Result<Vec<Self::Response>, Self::Error> {
|
|
|
|
println!("---> [i32; {}] {:?}", msg.len(), msg);
|
|
|
|
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BatchHandler<i16> for TmpReceiver {
|
|
|
|
type Error = Error;
|
|
|
|
type Response = ();
|
2021-07-02 20:21:53 +04:00
|
|
|
type InBatch = Vec<i16>;
|
|
|
|
type OutBatch = Vec<()>;
|
2021-06-24 13:32:19 +04:00
|
|
|
|
|
|
|
fn handle(&self, msg: Vec<i16>, _bus: &Bus) -> Result<Vec<Self::Response>, Self::Error> {
|
|
|
|
println!("---> [i16; {}] {:?}", msg.len(), msg);
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let (b, poller) = Bus::build()
|
|
|
|
.register(TmpReceiver)
|
2021-07-02 20:21:53 +04:00
|
|
|
.subscribe_batch_async::<i32>(16, Default::default())
|
|
|
|
.subscribe_batch_sync::<i16>(16, Default::default())
|
2021-06-24 13:32:19 +04:00
|
|
|
.done()
|
|
|
|
.build();
|
|
|
|
|
2021-06-28 17:41:00 +04:00
|
|
|
for i in 1..100i32 {
|
2021-06-24 13:32:19 +04:00
|
|
|
b.send(i).await.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
b.send(1i16).await.unwrap();
|
|
|
|
b.send(2i16).await.unwrap();
|
|
|
|
b.send(3i16).await.unwrap();
|
|
|
|
b.send(4i16).await.unwrap();
|
|
|
|
b.send(5i16).await.unwrap();
|
|
|
|
b.send(6i16).await.unwrap();
|
|
|
|
b.send(7i16).await.unwrap();
|
|
|
|
|
|
|
|
println!("flush");
|
|
|
|
b.flush().await;
|
|
|
|
|
|
|
|
println!("close");
|
|
|
|
b.close().await;
|
|
|
|
|
|
|
|
println!("closed");
|
|
|
|
|
|
|
|
poller.await;
|
|
|
|
println!("[done]");
|
|
|
|
}
|