use std::future::Future;
use std::pin::Pin;
use super::source::Block;
pub trait EventSource: Send + Sync + 'static {
type Event: Send + 'static;
fn take(&mut self) -> impl Future<Output = Vec<Self::Event>> + Send;
fn announce(&self, event: &Self::Event) -> Block;
}
trait EventSourceImpl: Send + Sync {
fn deliver<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = Vec<Block>> + Send + 'a>>;
}
impl<S: EventSource> EventSourceImpl for S {
fn deliver<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = Vec<Block>> + Send + 'a>> {
Box::pin(async move {
let events = self.take().await;
events.iter().map(|event| self.announce(event)).collect()
})
}
}
pub struct AnyEventSource {
inner: Box<dyn EventSourceImpl>,
}
impl std::fmt::Debug for AnyEventSource {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("AnyEventSource")
.finish_non_exhaustive()
}
}
impl AnyEventSource {
#[must_use]
pub fn new<S: EventSource>(source: S) -> Self {
Self {
inner: Box::new(source),
}
}
pub(crate) fn deliver(&mut self) -> impl Future<Output = Vec<Block>> + Send + '_ {
self.inner.deliver()
}
}