|
| 1 | +use futures_core::task::{Context, Poll}; |
| 2 | +use futures_io::AsyncWrite; |
| 3 | +use futures_sink::Sink; |
| 4 | +use std::fmt; |
| 5 | +use std::io; |
| 6 | +use std::pin::Pin; |
| 7 | +use std::marker::Unpin; |
| 8 | +use pin_utils::{unsafe_pinned, unsafe_unpinned}; |
| 9 | + |
| 10 | +/// Sink for the [`into_sink`](super::AsyncWriteExt::into_sink) method. |
| 11 | +#[must_use = "sinks do nothing unless polled"] |
| 12 | +pub struct IntoSink<W> { |
| 13 | + writer: W, |
| 14 | + /// An outstanding block for us to push into the underlying writer, along with an index of how |
| 15 | + /// far into the block we have written already. |
| 16 | + buffer: Option<(usize, Box<dyn AsRef<[u8]>>)>, |
| 17 | +} |
| 18 | + |
| 19 | +impl<W: Unpin> Unpin for IntoSink<W> {} |
| 20 | + |
| 21 | +impl<W: AsyncWrite> IntoSink<W> { |
| 22 | + unsafe_pinned!(writer: W); |
| 23 | + unsafe_unpinned!(buffer: Option<(usize, Box<dyn AsRef<[u8]>>)>); |
| 24 | + |
| 25 | + pub(super) fn new(writer: W) -> Self { |
| 26 | + IntoSink { writer, buffer: None } |
| 27 | + } |
| 28 | + |
| 29 | + fn project<'a>(self: Pin<&'a mut Self>) -> (Pin<&'a mut W>, &'a mut Option<(usize, Box<dyn AsRef<[u8]>>)>) { |
| 30 | + unsafe { |
| 31 | + let this = self.get_unchecked_mut(); |
| 32 | + (Pin::new_unchecked(&mut this.writer), &mut this.buffer) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// If we have an outstanding block in `buffer` attempt to push it into the writer, does _not_ |
| 37 | + /// flush the writer after it succeeds in pushing the block into it. |
| 38 | + fn poll_flush_buffer( |
| 39 | + self: Pin<&mut Self>, |
| 40 | + cx: &mut Context<'_>, |
| 41 | + ) -> Poll<Result<(), io::Error>> |
| 42 | + { |
| 43 | + let (mut writer, buffer) = self.project(); |
| 44 | + if let Some((index, block)) = buffer { |
| 45 | + loop { |
| 46 | + let bytes = (**block).as_ref(); |
| 47 | + let written = ready!(writer.as_mut().poll_write(cx, &bytes[*index..]))?; |
| 48 | + *index += written; |
| 49 | + if *index == bytes.len() { |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + *buffer = None; |
| 54 | + } |
| 55 | + Poll::Ready(Ok(())) |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +impl<W: AsyncWrite, Item: AsRef<[u8]> + 'static> Sink<Item> for IntoSink<W> { |
| 61 | + type SinkError = io::Error; |
| 62 | + |
| 63 | + fn poll_ready( |
| 64 | + mut self: Pin<&mut Self>, |
| 65 | + cx: &mut Context<'_>, |
| 66 | + ) -> Poll<Result<(), Self::SinkError>> |
| 67 | + { |
| 68 | + ready!(self.as_mut().poll_flush_buffer(cx))?; |
| 69 | + Poll::Ready(Ok(())) |
| 70 | + } |
| 71 | + |
| 72 | + fn start_send( |
| 73 | + mut self: Pin<&mut Self>, |
| 74 | + item: Item, |
| 75 | + ) -> Result<(), Self::SinkError> |
| 76 | + { |
| 77 | + debug_assert!(self.as_mut().buffer().is_none()); |
| 78 | + *self.as_mut().buffer() = Some((0, Box::new(item))); |
| 79 | + Ok(()) |
| 80 | + } |
| 81 | + |
| 82 | + fn poll_flush( |
| 83 | + mut self: Pin<&mut Self>, |
| 84 | + cx: &mut Context<'_>, |
| 85 | + ) -> Poll<Result<(), Self::SinkError>> |
| 86 | + { |
| 87 | + ready!(self.as_mut().poll_flush_buffer(cx))?; |
| 88 | + ready!(self.as_mut().writer().poll_flush(cx))?; |
| 89 | + Poll::Ready(Ok(())) |
| 90 | + } |
| 91 | + |
| 92 | + fn poll_close( |
| 93 | + mut self: Pin<&mut Self>, |
| 94 | + cx: &mut Context<'_>, |
| 95 | + ) -> Poll<Result<(), Self::SinkError>> |
| 96 | + { |
| 97 | + ready!(self.as_mut().poll_flush_buffer(cx))?; |
| 98 | + ready!(self.as_mut().writer().poll_close(cx))?; |
| 99 | + Poll::Ready(Ok(())) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl<W: fmt::Debug> fmt::Debug for IntoSink<W> { |
| 104 | + |
| 105 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 106 | + let buffer = self.buffer.as_ref().map(|(size, block)| { |
| 107 | + (size, format!("[... {} bytes ...]", (**block).as_ref().len())) |
| 108 | + }); |
| 109 | + f.debug_struct("IntoSink") |
| 110 | + .field("writer", &self.writer) |
| 111 | + .field("buffer", &buffer) |
| 112 | + .finish()?; |
| 113 | + Ok(()) |
| 114 | + } |
| 115 | +} |
0 commit comments