use std::{
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
};
use futures::{ready, sink::Sink};
use pin_project::pin_project;
pub trait SinkExt<Item>: Sink<Item> {
fn with_fn<F, T, E>(self, func: F) -> WithFn<Self, F, T, E>
where
Self: Sized,
F: FnMut(T) -> Result<Item, E>,
E: From<Self::Error>;
}
impl<Item, S> SinkExt<Item> for S
where
S: Sink<Item>,
{
fn with_fn<F, T, E>(self, func: F) -> WithFn<Self, F, T, E>
where
Self: Sized,
F: FnMut(T) -> Result<Item, E>,
E: From<Self::Error>,
{
WithFn {
sink: self,
func,
_phantom: PhantomData,
}
}
}
#[pin_project]
pub struct WithFn<S, F, T, E> {
#[pin]
sink: S,
func: F,
_phantom: PhantomData<fn() -> Result<T, E>>,
}
impl<S, Item, F, T, E> Sink<T> for WithFn<S, F, T, E>
where
S: Sink<Item>,
F: FnMut(T) -> Result<Item, E>,
E: From<S::Error>,
{
type Error = E;
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
ready!(self.project().sink.poll_ready(cx))?;
Poll::Ready(Ok(()))
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
ready!(self.project().sink.poll_flush(cx))?;
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
ready!(self.project().sink.poll_close(cx))?;
Poll::Ready(Ok(()))
}
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
let this = self.project();
let item = (this.func)(item)?;
this.sink.start_send(item).map_err(E::from)
}
}