multipart_write/io/
multi_io_writer.rs

1use crate::MultipartWrite;
2
3use std::io::Write;
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7pin_project_lite::pin_project! {
8    /// `MultiIoWriter` implements [`MultipartWrite`] for a synchronous
9    /// [`Write`](std::io::Write).
10    ///
11    /// [`MultipartWrite`]: crate::MultipartWrite
12    #[derive(Debug, Default)]
13    pub struct MultiIoWriter<W: Write> {
14        inner: W,
15    }
16}
17
18impl<W: Write> MultiIoWriter<W> {
19    pub(super) fn new(inner: W) -> Self {
20        Self { inner }
21    }
22}
23
24impl<W: Write + Default> MultipartWrite<&[u8]> for MultiIoWriter<W> {
25    type Ret = usize;
26    type Output = W;
27    type Error = std::io::Error;
28
29    fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
30        Poll::Ready(Ok(()))
31    }
32
33    fn start_send(self: Pin<&mut Self>, part: &[u8]) -> Result<Self::Ret, Self::Error> {
34        self.get_mut().inner.write(part)
35    }
36
37    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
38        Poll::Ready(self.get_mut().inner.flush())
39    }
40
41    fn poll_complete(
42        mut self: Pin<&mut Self>,
43        _cx: &mut Context<'_>,
44    ) -> Poll<Result<Self::Output, Self::Error>> {
45        Poll::Ready(Ok(std::mem::take(&mut self.inner)))
46    }
47}