use crate::Body; use bytes::Buf; use std::{ fmt, pin::Pin, task::{Context, Poll}, }; /// A boxed [`Body`] trait object. pub struct BoxBody { inner: Pin + Send + Sync + 'static>>, } /// A boxed [`Body`] trait object that is !Sync. pub struct UnsyncBoxBody { inner: Pin + Send + 'static>>, } impl BoxBody { /// Create a new `BoxBody`. pub fn new(body: B) -> Self where B: Body + Send + Sync + 'static, D: Buf, { Self { inner: Box::pin(body), } } } impl fmt::Debug for BoxBody { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("BoxBody").finish() } } impl 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) -> crate::SizeHint { self.inner.size_hint() } } impl Default for BoxBody where D: Buf + 'static, { fn default() -> Self { BoxBody::new(crate::Empty::new().map_err(|err| match err {})) } } // === UnsyncBoxBody === impl UnsyncBoxBody { /// Create a new `BoxBody`. pub fn new(body: B) -> Self where B: Body + Send + 'static, D: Buf, { Self { inner: Box::pin(body), } } } impl fmt::Debug for UnsyncBoxBody { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("UnsyncBoxBody").finish() } } impl Body for UnsyncBoxBody 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) -> crate::SizeHint { self.inner.size_hint() } } impl Default for UnsyncBoxBody where D: Buf + 'static, { fn default() -> Self { UnsyncBoxBody::new(crate::Empty::new().map_err(|err| match err {})) } }