Skip to main content

multipart_write/write/
flush.rs

1use std::fmt::{self, Debug, Formatter};
2use std::marker::PhantomData;
3use std::pin::Pin;
4use std::task::{Context, Poll};
5
6use futures_core::future::{FusedFuture, Future};
7
8use crate::{FusedMultipartWrite, MultipartWrite};
9
10/// Future for [`flush`](super::MultipartWriteExt::flush).
11#[must_use = "futures do nothing unless polled"]
12pub struct Flush<'a, Wr: ?Sized, Part> {
13    writer: &'a mut Wr,
14    _p: PhantomData<fn(Part)>,
15}
16
17impl<Wr: ?Sized + Unpin, Part> Unpin for Flush<'_, Wr, Part> {}
18
19impl<'a, Wr: ?Sized + MultipartWrite<Part> + Unpin, Part> Flush<'a, Wr, Part> {
20    pub(super) fn new(writer: &'a mut Wr) -> Self {
21        Self { writer, _p: std::marker::PhantomData }
22    }
23}
24
25impl<Wr, Part> FusedFuture for Flush<'_, Wr, Part>
26where
27    Wr: FusedMultipartWrite<Part> + Unpin,
28{
29    fn is_terminated(&self) -> bool {
30        self.writer.is_terminated()
31    }
32}
33
34impl<Wr: MultipartWrite<Part> + Unpin, Part> Future for Flush<'_, Wr, Part> {
35    type Output = Result<(), Wr::Error>;
36
37    fn poll(
38        mut self: Pin<&mut Self>,
39        cx: &mut Context<'_>,
40    ) -> Poll<Self::Output> {
41        Pin::new(&mut self.writer).poll_flush(cx)
42    }
43}
44
45impl<Wr: Debug, Part> Debug for Flush<'_, Wr, Part> {
46    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
47        f.debug_struct("Flush").field("writer", &self.writer).finish()
48    }
49}