Skip to main content

multipart_write/write/
for_each_recv.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, ready};
7
8use crate::{FusedMultipartWrite, MultipartWrite};
9
10pin_project_lite::pin_project! {
11    /// `MultipartWrite` for [`for_each_recv`].
12    ///
13    /// [`for_each_recv`]: super::MultipartWriteExt::for_each_recv
14    #[must_use = "futures do nothing unless polled"]
15    pub struct ForEachRecv<Wr, Part, Fut, F> {
16        #[pin]
17        writer: Wr,
18        #[pin]
19        fut: Option<Fut>,
20        f: F,
21        _p: PhantomData<Part>,
22    }
23}
24
25impl<Wr, Part, Fut, F> ForEachRecv<Wr, Part, Fut, F> {
26    pub(super) fn new(writer: Wr, f: F) -> Self {
27        Self { writer, fut: None, f, _p: PhantomData }
28    }
29
30    /// Consumes `ForEachRecv`, returning the underlying writer.
31    pub fn into_inner(self) -> Wr {
32        self.writer
33    }
34
35    /// Acquires a reference to the underlying writer.
36    pub fn get_ref(&self) -> &Wr {
37        &self.writer
38    }
39
40    /// Acquires a mutable reference to the underlying writer.
41    ///
42    /// It is inadvisable to directly write to the underlying writer.
43    pub fn get_mut(&mut self) -> &mut Wr {
44        &mut self.writer
45    }
46
47    /// Acquires a pinned mutable reference to the underlying writer.
48    ///
49    /// It is inadvisable to directly write to the underlying writer.
50    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
51        self.project().writer
52    }
53}
54
55impl<Wr, Part, Fut, F> FusedMultipartWrite<Part>
56    for ForEachRecv<Wr, Part, Fut, F>
57where
58    Wr: FusedMultipartWrite<Part>,
59    Wr::Recv: Clone,
60    F: FnMut(Wr::Recv) -> Fut,
61    Fut: Future<Output = ()>,
62{
63    fn is_terminated(&self) -> bool {
64        self.writer.is_terminated() && self.fut.is_none()
65    }
66}
67
68impl<Wr, Part, Fut, F> MultipartWrite<Part> for ForEachRecv<Wr, Part, Fut, F>
69where
70    Wr: MultipartWrite<Part>,
71    Wr::Recv: Clone,
72    F: FnMut(Wr::Recv) -> Fut,
73    Fut: Future<Output = ()>,
74{
75    type Error = Wr::Error;
76    type Output = Wr::Output;
77    type Recv = Wr::Recv;
78
79    fn poll_ready(
80        self: Pin<&mut Self>,
81        cx: &mut Context<'_>,
82    ) -> Poll<Result<(), Self::Error>> {
83        let mut this = self.project();
84        if let Some(fut) = this.fut.as_mut().as_pin_mut() {
85            ready!(fut.poll(cx));
86            this.fut.set(None);
87        }
88        this.writer.poll_ready(cx)
89    }
90
91    fn start_send(
92        self: Pin<&mut Self>,
93        part: Part,
94    ) -> Result<Self::Recv, Self::Error> {
95        let mut this = self.project();
96        let recv = this.writer.as_mut().start_send(part)?;
97        let fut = (this.f)(recv.clone());
98        this.fut.set(Some(fut));
99        Ok(recv)
100    }
101
102    fn poll_flush(
103        self: Pin<&mut Self>,
104        cx: &mut Context<'_>,
105    ) -> Poll<Result<(), Self::Error>> {
106        self.project().writer.poll_flush(cx)
107    }
108
109    fn poll_complete(
110        self: Pin<&mut Self>,
111        cx: &mut Context<'_>,
112    ) -> Poll<Result<Self::Output, Self::Error>> {
113        self.project().writer.poll_complete(cx)
114    }
115}
116
117impl<Wr, Part, Fut, F> Debug for ForEachRecv<Wr, Part, Fut, F>
118where
119    Wr: Debug,
120    Fut: Debug,
121{
122    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
123        f.debug_struct("ForEachRecv")
124            .field("writer", &self.writer)
125            .field("fut", &self.fut)
126            .finish()
127    }
128}