use futures_util::sink;
use std::{
pin::Pin,
task::{Context, Poll},
};
pub use hedwig_core::message::EncodableMessage;
pub trait Publisher<M: EncodableMessage, S: sink::Sink<M> = Drain<M>> {
type PublishError;
type PublishSink: sink::Sink<M, Error = Self::PublishError>;
fn publish_sink(self, validator: M::Validator) -> Self::PublishSink
where
Self: Sized,
S: Default,
{
self.publish_sink_with_responses(validator, S::default())
}
fn publish_sink_with_responses(
self,
validator: M::Validator,
response_sink: S,
) -> Self::PublishSink;
}
#[derive(Debug)]
pub struct Drain<T>(std::marker::PhantomData<T>);
impl<T> Default for Drain<T> {
fn default() -> Self {
Self(std::marker::PhantomData)
}
}
impl<T> sink::Sink<T> for Drain<T> {
type Error = futures_util::never::Never;
fn poll_ready(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn start_send(self: Pin<&mut Self>, _: T) -> Result<(), Self::Error> {
Ok(())
}
fn poll_flush(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
}