Skip to main content

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    /// The writer returned by [`io_writer`](super::io_writer).
9    #[derive(Debug, Default)]
10    pub struct MultiIoWriter<W: Write> {
11        inner: W,
12    }
13}
14
15impl<W: Write> MultiIoWriter<W> {
16    pub(super) fn new(inner: W) -> Self {
17        Self { inner }
18    }
19}
20
21impl<W: Write + Default> MultipartWrite<&[u8]> for MultiIoWriter<W> {
22    type Error = std::io::Error;
23    type Output = W;
24    type Recv = usize;
25
26    fn poll_ready(
27        self: Pin<&mut Self>,
28        _cx: &mut Context<'_>,
29    ) -> Poll<Result<(), Self::Error>> {
30        Poll::Ready(Ok(()))
31    }
32
33    fn start_send(
34        self: Pin<&mut Self>,
35        part: &[u8],
36    ) -> Result<Self::Recv, Self::Error> {
37        self.get_mut().inner.write(part)
38    }
39
40    fn poll_flush(
41        self: Pin<&mut Self>,
42        _cx: &mut Context<'_>,
43    ) -> Poll<Result<(), Self::Error>> {
44        Poll::Ready(self.get_mut().inner.flush())
45    }
46
47    fn poll_complete(
48        mut self: Pin<&mut Self>,
49        _cx: &mut Context<'_>,
50    ) -> Poll<Result<Self::Output, Self::Error>> {
51        Poll::Ready(Ok(std::mem::take(&mut self.inner)))
52    }
53}