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