multipart_write/write/
then.rs1use crate::{FusedMultipartWrite, MultipartWrite};
2
3use futures_core::ready;
4use std::fmt::{self, Debug, Formatter};
5use std::pin::Pin;
6use std::task::{Context, Poll};
7
8pin_project_lite::pin_project! {
9 #[must_use = "futures do nothing unless polled"]
11 pub struct Then<Wr, F, Fut> {
12 #[pin]
13 writer: Wr,
14 #[pin]
15 future: Option<Fut>,
16 f: F,
17 }
18}
19
20impl<Wr, F, Fut> Then<Wr, F, Fut> {
21 pub(super) fn new(writer: Wr, f: F) -> Self {
22 Self {
23 writer,
24 future: None,
25 f,
26 }
27 }
28
29 pub fn get_ref(&self) -> &Wr {
31 &self.writer
32 }
33
34 pub fn get_mut(&mut self) -> &mut Wr {
38 &mut self.writer
39 }
40
41 pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
45 self.project().writer
46 }
47}
48
49impl<Wr, F, Fut, Part> FusedMultipartWrite<Part> for Then<Wr, F, Fut>
50where
51 Wr: FusedMultipartWrite<Part>,
52 F: FnMut(Wr::Output) -> Fut,
53 Fut: Future,
54{
55 fn is_terminated(&self) -> bool {
56 self.writer.is_terminated() && self.future.is_none()
57 }
58}
59
60impl<Wr, F, Fut, Part> MultipartWrite<Part> for Then<Wr, F, Fut>
61where
62 Wr: MultipartWrite<Part>,
63 F: FnMut(Wr::Output) -> Fut,
64 Fut: Future,
65{
66 type Ret = Wr::Ret;
67 type Output = Fut::Output;
68 type Error = Wr::Error;
69
70 fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
71 self.project().writer.poll_ready(cx)
72 }
73
74 fn start_send(self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
75 self.project().writer.start_send(part)
76 }
77
78 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
79 self.project().writer.poll_flush(cx)
80 }
81
82 fn poll_complete(
83 self: Pin<&mut Self>,
84 cx: &mut Context<'_>,
85 ) -> Poll<Result<Self::Output, Self::Error>> {
86 let mut this = self.project();
87
88 if this.future.is_none() {
89 let ret = ready!(this.writer.poll_complete(cx))?;
90 let fut = (this.f)(ret);
91 this.future.set(Some(fut));
92 }
93
94 let fut = this
95 .future
96 .as_mut()
97 .as_pin_mut()
98 .expect("polled Then after completion");
99 let ret = ready!(fut.poll(cx));
100 this.future.set(None);
101
102 Poll::Ready(Ok(ret))
103 }
104}
105
106impl<Wr, F, Fut> Debug for Then<Wr, F, Fut>
107where
108 Wr: Debug,
109 Fut: Debug,
110{
111 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
112 f.debug_struct("Then")
113 .field("writer", &self.writer)
114 .field("future", &self.future)
115 .field("f", &"F")
116 .finish()
117 }
118}