use crate::bus::{Response, RequestEnvelope}; use async_std::sync::{Arc, Mutex}; use std::fmt::{Formatter, Debug, Error}; use futures::Stream; #[async_trait] pub trait BusConnection: Send + Stream + Unpin { async fn close(&mut self); async fn ack(&mut self, id: u64); async fn send(&mut self, id: u64, message: Response); } #[derive(Clone)] pub struct Receiver(Arc>>, String); impl Debug for Receiver { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { write!(f, "Receiver {{ {} }}", self.1) } } impl Receiver { pub fn new(receiver: Box) -> Receiver { let name = receiver.name(); Receiver(Arc::new(Mutex::new(receiver)), name.to_string()) } pub async fn get(&mut self) -> Option> { self.0.lock().await.get().await } } #[async_trait] pub trait RawReceiver: Send { fn name<'s>(&self) -> &'s str; async fn disconnect(&mut self); async fn get(&mut self) -> Option>; }