Skip to main content

multipart_write/write/
complete.rs

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