multipart_write/write/
send.rs

1use crate::write::Feed;
2use crate::{FusedMultipartWrite, MultipartWrite};
3
4use futures::future::FusedFuture;
5use futures::ready;
6use std::pin::Pin;
7use std::task::{Context, Poll};
8
9/// Future for [`send`](super::MultipartWriteExt::send).
10#[derive(Debug)]
11#[must_use = "futures do nothing unless polled"]
12pub struct Send<'a, Wr: ?Sized + MultipartWrite<Part>, Part> {
13    feed: Feed<'a, Wr, Part>,
14    output: Option<Wr::Ret>,
15}
16
17impl<Wr: ?Sized + MultipartWrite<Part> + Unpin, Part> Unpin for Send<'_, Wr, Part> {}
18
19impl<'a, Wr: ?Sized + MultipartWrite<Part> + Unpin, Part> Send<'a, Wr, Part> {
20    pub(super) fn new(writer: &'a mut Wr, part: Part) -> Self {
21        Self {
22            feed: Feed::new(writer, part),
23            output: None,
24        }
25    }
26}
27
28impl<Wr, Part> FusedFuture for Send<'_, Wr, Part>
29where
30    Wr: FusedMultipartWrite<Part> + Unpin,
31{
32    fn is_terminated(&self) -> bool {
33        self.feed.is_terminated()
34    }
35}
36
37impl<Wr: ?Sized + MultipartWrite<Part> + Unpin, Part> Future for Send<'_, Wr, Part> {
38    type Output = Result<Wr::Ret, Wr::Error>;
39
40    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
41        let this = &mut *self;
42
43        if this.feed.is_part_pending() {
44            let ret = ready!(Pin::new(&mut this.feed).poll(cx))?;
45            this.output = Some(ret);
46            debug_assert!(!this.feed.is_part_pending());
47        }
48
49        ready!(this.feed.writer_pin_mut().poll_flush(cx))?;
50        let output = this.output.take().expect("polled Send after completion");
51
52        Poll::Ready(Ok(output))
53    }
54}