diff --git a/serio/src/sink.rs b/serio/src/sink.rs index 060c7e0..8a3a948 100644 --- a/serio/src/sink.rs +++ b/serio/src/sink.rs @@ -176,7 +176,7 @@ pub trait SinkExt: Sink { where Self: Sized, { - IntoSink::new(self) + assert_futures_sink(IntoSink::new(self)) } } @@ -290,63 +290,67 @@ impl Future for Feed<'_, Si, Item> { mod compat { use super::*; - /// Wraps a sink and provides a `futures::Sink` implementation. - pub struct IntoSink(Si, PhantomData); + pin_project_lite::pin_project! { + /// Wraps a sink and provides a `futures::Sink` implementation. + pub struct IntoSink { + #[pin] + sink: Si, + _pd: PhantomData, + } + } impl IntoSink { pub(super) fn new(sink: Si) -> Self { - Self(sink, PhantomData) + Self { + sink, + _pd: PhantomData, + } } /// Returns a reference to the inner sink. pub fn sink(&self) -> &Si { - &self.0 + &self.sink } /// Returns a mutable reference to the inner sink. pub fn sink_mut(&mut self) -> &mut Si { - &mut self.0 + &mut self.sink } /// Returns the inner sink. pub fn into_inner(self) -> Si { - self.0 + self.sink } } impl futures_sink::Sink for IntoSink where - Si: Sink + Unpin, + Si: Sink, Item: Serialize, { - type Error = std::io::Error; + type Error = Si::Error; - fn poll_ready( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll> { - Pin::new(&mut self.0).poll_ready(cx) + fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + self.project().sink.poll_ready(cx) } - fn start_send(mut self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> { - Pin::new(&mut self.0).start_send(item) + fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> { + self.project().sink.start_send(item) } - fn poll_flush( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll> { - Pin::new(&mut self.0).poll_flush(cx) + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + self.project().sink.poll_flush(cx) } - fn poll_close( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll> { - Pin::new(&mut self.0).poll_close(cx) + fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + self.project().sink.poll_close(cx) } } } #[cfg(feature = "compat")] pub use compat::IntoSink; + +pub(crate) fn assert_futures_sink, Item>(sink: S) -> S { + sink +}