use std::{any::type_name, error::Error as StdError, future::Future, pin::Pin};
use crate::Broker;
pub(crate) type BoxError = Box<dyn StdError + Send + Sync>;
pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
pub(crate) trait BrokerLifecycle: Send + Sync {
fn connect(&self) -> BoxFuture<'_, Result<(), BoxError>>;
fn shutdown(&self) -> BoxFuture<'_, Result<(), BoxError>>;
fn name(&self) -> &'static str;
}
impl<B: Broker> BrokerLifecycle for B {
fn name(&self) -> &'static str {
type_name::<B>()
}
fn connect(&self) -> BoxFuture<'_, Result<(), BoxError>> {
Box::pin(async move {
Broker::connect(self)
.await
.map_err(|e| Box::new(e) as BoxError)
})
}
fn shutdown(&self) -> BoxFuture<'_, Result<(), BoxError>> {
Box::pin(async move {
Broker::shutdown(self)
.await
.map_err(|e| Box::new(e) as BoxError)
})
}
}