1#![cfg_attr(docsrs, feature(doc_cfg))]
30use std::collections::VecDeque;
31use std::convert::Infallible as Never;
32use std::ops::DerefMut;
33use std::pin::Pin;
34use std::task::{Context, Poll};
35
36pub mod io;
37
38pub mod stream;
39#[doc(inline)]
40pub use stream::MultipartStreamExt;
41
42pub mod write;
43#[doc(inline)]
44pub use write::MultipartWriteExt;
45
46pub trait MultipartWrite<Part> {
49 type Ret;
51
52 type Output;
54
55 type Error;
57
58 fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
70
71 fn start_send(self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error>;
87
88 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
96
97 fn poll_complete(
105 self: Pin<&mut Self>,
106 cx: &mut Context<'_>,
107 ) -> Poll<Result<Self::Output, Self::Error>>;
108}
109
110pub type BoxMultipartWrite<'a, Part, R, T, E> =
113 Pin<Box<dyn MultipartWrite<Part, Ret = R, Output = T, Error = E> + Send + 'a>>;
114
115pub type LocalBoxMultipartWrite<'a, Part, R, T, E> =
117 Pin<Box<dyn MultipartWrite<Part, Ret = R, Output = T, Error = E> + 'a>>;
118
119pub trait FusedMultipartWrite<Part>: MultipartWrite<Part> {
122 fn is_terminated(&self) -> bool;
124}
125
126impl<W: ?Sized + MultipartWrite<Part> + Unpin, Part> MultipartWrite<Part> for &mut W {
127 type Ret = W::Ret;
128 type Output = W::Output;
129 type Error = W::Error;
130
131 fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
132 Pin::new(&mut **self).poll_ready(cx)
133 }
134
135 fn start_send(mut self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
136 Pin::new(&mut **self).start_send(part)
137 }
138
139 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
140 Pin::new(&mut **self).poll_flush(cx)
141 }
142
143 fn poll_complete(
144 mut self: Pin<&mut Self>,
145 cx: &mut Context<'_>,
146 ) -> Poll<Result<Self::Output, Self::Error>> {
147 Pin::new(&mut **self).poll_complete(cx)
148 }
149}
150
151impl<W: ?Sized + FusedMultipartWrite<Part> + Unpin, Part> FusedMultipartWrite<Part> for &mut W {
152 fn is_terminated(&self) -> bool {
153 <W as FusedMultipartWrite<Part>>::is_terminated(&**self)
154 }
155}
156
157impl<P, Part> MultipartWrite<Part> for Pin<P>
158where
159 P: DerefMut + Unpin,
160 P::Target: MultipartWrite<Part>,
161{
162 type Ret = <P::Target as MultipartWrite<Part>>::Ret;
163 type Output = <P::Target as MultipartWrite<Part>>::Output;
164 type Error = <P::Target as MultipartWrite<Part>>::Error;
165
166 fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
167 self.get_mut().as_mut().poll_ready(cx)
168 }
169
170 fn start_send(self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
171 self.get_mut().as_mut().start_send(part)
172 }
173
174 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
175 self.get_mut().as_mut().poll_flush(cx)
176 }
177
178 fn poll_complete(
179 self: Pin<&mut Self>,
180 cx: &mut Context<'_>,
181 ) -> Poll<Result<Self::Output, Self::Error>> {
182 self.get_mut().as_mut().poll_complete(cx)
183 }
184}
185
186impl<P, Part> FusedMultipartWrite<Part> for Pin<P>
187where
188 P: DerefMut + Unpin,
189 P::Target: FusedMultipartWrite<Part>,
190{
191 fn is_terminated(&self) -> bool {
192 <P::Target as FusedMultipartWrite<Part>>::is_terminated(&**self)
193 }
194}
195
196impl<T> MultipartWrite<T> for Vec<T> {
197 type Ret = ();
198 type Output = Self;
199 type Error = Never;
200
201 fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
202 Poll::Ready(Ok(()))
203 }
204
205 fn start_send(self: Pin<&mut Self>, part: T) -> Result<Self::Ret, Self::Error> {
206 unsafe { self.get_unchecked_mut() }.push(part);
207 Ok(())
208 }
209
210 fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
211 Poll::Ready(Ok(()))
212 }
213
214 fn poll_complete(
215 self: Pin<&mut Self>,
216 _cx: &mut Context<'_>,
217 ) -> Poll<Result<Self::Output, Self::Error>> {
218 let this: &mut Vec<T> = unsafe { self.get_unchecked_mut() };
219 let out = std::mem::take(this);
220 Poll::Ready(Ok(out))
221 }
222}
223
224impl<T> MultipartWrite<T> for VecDeque<T> {
225 type Ret = ();
226 type Output = Self;
227 type Error = Never;
228
229 fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
230 Poll::Ready(Ok(()))
231 }
232
233 fn start_send(self: Pin<&mut Self>, part: T) -> Result<Self::Ret, Self::Error> {
234 unsafe { self.get_unchecked_mut() }.push_back(part);
235 Ok(())
236 }
237
238 fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
239 Poll::Ready(Ok(()))
240 }
241
242 fn poll_complete(
243 self: Pin<&mut Self>,
244 _cx: &mut Context<'_>,
245 ) -> Poll<Result<Self::Output, Self::Error>> {
246 let this: &mut VecDeque<T> = unsafe { self.get_unchecked_mut() };
247 let out = std::mem::take(this);
248 Poll::Ready(Ok(out))
249 }
250}