puzz_http/body/
boxed.rs

1use std::fmt;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use bytes::Bytes;
6
7use super::{Body, BodyExt, SizeHint};
8
9pub struct BoxBody {
10    inner: Pin<Box<dyn Body<Error = Box<dyn std::error::Error>>>>,
11}
12
13impl BoxBody {
14    pub fn new<B>(body: B) -> Self
15    where
16        B: Body + 'static,
17        B::Error: Into<Box<dyn std::error::Error>>,
18    {
19        Self {
20            inner: Box::pin(body.map_err(Into::into)),
21        }
22    }
23}
24
25impl Body for BoxBody {
26    type Error = Box<dyn std::error::Error>;
27
28    fn poll_next(
29        mut self: Pin<&mut Self>,
30        cx: &mut Context<'_>,
31    ) -> Poll<Option<Result<Bytes, Self::Error>>> {
32        self.inner.as_mut().poll_next(cx)
33    }
34
35    fn size_hint(&self) -> SizeHint {
36        self.inner.size_hint()
37    }
38}
39
40impl fmt::Debug for BoxBody {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        f.debug_struct("BoxBody").finish()
43    }
44}
45
46impl Default for BoxBody {
47    fn default() -> Self {
48        BoxBody::new(())
49    }
50}