1use std::borrow::Cow;
2use std::convert::Infallible;
3use std::ops::DerefMut;
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7pub use bytes::Bytes;
8
9use crate::{Request, Response};
10
11mod boxed;
12pub use boxed::BoxBody;
13
14mod ext;
15pub use ext::BodyExt;
16
17mod map_err;
18pub use map_err::MapErr;
19
20mod next;
21pub use next::Next;
22
23mod stream;
24pub use stream::{BodyStream, StreamBody};
25
26mod size_hint;
27pub use size_hint::SizeHint;
28
29pub trait Body {
31 type Error;
33
34 fn poll_next(
36 self: Pin<&mut Self>,
37 cx: &mut Context<'_>,
38 ) -> Poll<Option<Result<Bytes, Self::Error>>>;
39
40 fn size_hint(&self) -> SizeHint {
42 SizeHint::default()
43 }
44}
45
46impl<B> Body for &mut B
47where
48 B: Body + Unpin + ?Sized,
49{
50 type Error = B::Error;
51
52 fn poll_next(
53 mut self: Pin<&mut Self>,
54 cx: &mut Context<'_>,
55 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
56 Pin::new(&mut **self).poll_next(cx)
57 }
58
59 fn size_hint(&self) -> SizeHint {
60 (**self).size_hint()
61 }
62}
63
64impl<P> Body for Pin<P>
65where
66 P: DerefMut + Unpin,
67 P::Target: Body,
68{
69 type Error = <P::Target as Body>::Error;
70
71 fn poll_next(
72 self: Pin<&mut Self>,
73 cx: &mut Context<'_>,
74 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
75 self.get_mut().as_mut().poll_next(cx)
76 }
77
78 fn size_hint(&self) -> SizeHint {
79 (**self).size_hint()
80 }
81}
82
83impl<B> Body for Box<B>
84where
85 B: Body + Unpin + ?Sized,
86{
87 type Error = B::Error;
88
89 fn poll_next(
90 mut self: Pin<&mut Self>,
91 cx: &mut Context<'_>,
92 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
93 Pin::new(&mut **self).poll_next(cx)
94 }
95
96 fn size_hint(&self) -> SizeHint {
97 (**self).size_hint()
98 }
99}
100
101impl<B> Body for Request<B>
102where
103 B: Body,
104{
105 type Error = B::Error;
106
107 fn poll_next(
108 self: Pin<&mut Self>,
109 cx: &mut Context<'_>,
110 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
111 unsafe { self.map_unchecked_mut(Request::body_mut).poll_next(cx) }
112 }
113
114 fn size_hint(&self) -> SizeHint {
115 self.body().size_hint()
116 }
117}
118
119impl<B> Body for Response<B>
120where
121 B: Body,
122{
123 type Error = B::Error;
124
125 fn poll_next(
126 self: Pin<&mut Self>,
127 cx: &mut Context<'_>,
128 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
129 unsafe { self.map_unchecked_mut(Response::body_mut).poll_next(cx) }
130 }
131
132 fn size_hint(&self) -> SizeHint {
133 self.body().size_hint()
134 }
135}
136
137impl Body for () {
138 type Error = Infallible;
139
140 fn poll_next(
141 self: Pin<&mut Self>,
142 _: &mut Context<'_>,
143 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
144 Poll::Ready(None)
145 }
146
147 fn size_hint(&self) -> SizeHint {
148 SizeHint::with_exact(0)
149 }
150}
151
152impl Body for &'static [u8] {
153 type Error = Infallible;
154
155 fn poll_next(
156 self: Pin<&mut Self>,
157 _: &mut Context<'_>,
158 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
159 if self.is_empty() {
160 Poll::Ready(None)
161 } else {
162 Poll::Ready(Some(Ok(Bytes::from_static(std::mem::take(self.get_mut())))))
163 }
164 }
165
166 fn size_hint(&self) -> SizeHint {
167 SizeHint::with_exact(self.len() as u64)
168 }
169}
170
171impl Body for Vec<u8> {
172 type Error = Infallible;
173
174 fn poll_next(
175 self: Pin<&mut Self>,
176 _: &mut Context<'_>,
177 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
178 if self.is_empty() {
179 Poll::Ready(None)
180 } else {
181 Poll::Ready(Some(Ok(Bytes::from(std::mem::take(self.get_mut())))))
182 }
183 }
184
185 fn size_hint(&self) -> SizeHint {
186 SizeHint::with_exact(self.len() as u64)
187 }
188}
189
190impl Body for Cow<'static, [u8]> {
191 type Error = Infallible;
192
193 fn poll_next(
194 self: Pin<&mut Self>,
195 cx: &mut Context<'_>,
196 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
197 match self.get_mut() {
198 Cow::Borrowed(v) => Pin::new(v).poll_next(cx),
199 Cow::Owned(v) => Pin::new(v).poll_next(cx),
200 }
201 }
202
203 fn size_hint(&self) -> SizeHint {
204 SizeHint::with_exact(self.len() as u64)
205 }
206}
207
208impl Body for &'static str {
209 type Error = Infallible;
210
211 fn poll_next(
212 self: Pin<&mut Self>,
213 _: &mut Context<'_>,
214 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
215 if self.is_empty() {
216 Poll::Ready(None)
217 } else {
218 Poll::Ready(Some(Ok(Bytes::from_static(
219 std::mem::take(self.get_mut()).as_bytes(),
220 ))))
221 }
222 }
223
224 fn size_hint(&self) -> SizeHint {
225 SizeHint::with_exact(self.len() as u64)
226 }
227}
228
229impl Body for String {
230 type Error = Infallible;
231
232 fn poll_next(
233 self: Pin<&mut Self>,
234 _: &mut Context<'_>,
235 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
236 if self.is_empty() {
237 Poll::Ready(None)
238 } else {
239 Poll::Ready(Some(Ok(Bytes::from(std::mem::take(self.get_mut())))))
240 }
241 }
242
243 fn size_hint(&self) -> SizeHint {
244 SizeHint::with_exact(self.len() as u64)
245 }
246}
247
248impl Body for Cow<'static, str> {
249 type Error = Infallible;
250
251 fn poll_next(
252 self: Pin<&mut Self>,
253 cx: &mut Context<'_>,
254 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
255 match self.get_mut() {
256 Cow::Borrowed(v) => Pin::new(v).poll_next(cx),
257 Cow::Owned(v) => Pin::new(v).poll_next(cx),
258 }
259 }
260
261 fn size_hint(&self) -> SizeHint {
262 SizeHint::with_exact(self.len() as u64)
263 }
264}
265
266impl Body for Bytes {
267 type Error = Infallible;
268
269 fn poll_next(
270 self: Pin<&mut Self>,
271 _: &mut Context<'_>,
272 ) -> Poll<Option<Result<Bytes, Self::Error>>> {
273 if self.is_empty() {
274 Poll::Ready(None)
275 } else {
276 Poll::Ready(Some(Ok(std::mem::take(self.get_mut()))))
277 }
278 }
279
280 fn size_hint(&self) -> SizeHint {
281 SizeHint::with_exact(self.len() as u64)
282 }
283}