multipart_write/io/
multi_io_writer.rs1use crate::MultipartWrite;
2
3use std::io::Write;
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7pin_project_lite::pin_project! {
8 #[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 Ret = usize;
23 type Output = W;
24 type Error = std::io::Error;
25
26 fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
27 Poll::Ready(Ok(()))
28 }
29
30 fn start_send(self: Pin<&mut Self>, part: &[u8]) -> Result<Self::Ret, Self::Error> {
31 self.get_mut().inner.write(part)
32 }
33
34 fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
35 Poll::Ready(self.get_mut().inner.flush())
36 }
37
38 fn poll_complete(
39 mut self: Pin<&mut Self>,
40 _cx: &mut Context<'_>,
41 ) -> Poll<Result<Self::Output, Self::Error>> {
42 Poll::Ready(Ok(std::mem::take(&mut self.inner)))
43 }
44}