use bytes::Buf; use http_body::{Body as _, Empty}; use std::{ fmt, pin::Pin, task::{Context, Poll}, }; pub use hyper::body::Body; /// A boxed [`Body`] trait object. pub struct BoxBody { inner: Pin + Send + Sync + 'static>>, } impl BoxBody { /// Create a new `BoxBody`. pub fn new(body: B) -> Self where B: http_body::Body + Send + Sync + 'static, D: Buf, { Self { inner: Box::pin(body), } } } // TODO: upstream this to http-body? impl Default for BoxBody where D: bytes::Buf + 'static, { fn default() -> Self { BoxBody::new(Empty::::new().map_err(|err| match err {})) } } impl fmt::Debug for BoxBody { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("BoxBody").finish() } } impl http_body::Body for BoxBody where D: Buf, { type Data = D; type Error = E; fn poll_data( mut self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll>> { self.inner.as_mut().poll_data(cx) } fn poll_trailers( mut self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll, Self::Error>> { self.inner.as_mut().poll_trailers(cx) } fn is_end_stream(&self) -> bool { self.inner.is_end_stream() } fn size_hint(&self) -> http_body::SizeHint { self.inner.size_hint() } }