#[doc(hidden)]
pub use http_body::Body as HttpBody;
pub use hyper::body::Body;
use bytes::Bytes;
use crate::error::{BoxError, Error};
pub type BoxBody = http_body::combinators::UnsyncBoxBody<Bytes, Error>;
pub fn boxed<B>(body: B) -> BoxBody
where
B: http_body::Body<Data = Bytes> + Send + 'static,
B::Error: Into<BoxError>,
{
try_downcast(body).unwrap_or_else(|body| body.map_err(Error::new).boxed_unsync())
}
#[doc(hidden)]
pub(crate) fn try_downcast<T, K>(k: K) -> Result<T, K>
where
T: 'static,
K: Send + 'static,
{
let mut k = Some(k);
if let Some(k) = <dyn std::any::Any>::downcast_mut::<Option<T>>(&mut k) {
Ok(k.take().unwrap())
} else {
Err(k.unwrap())
}
}
pub(crate) fn empty() -> BoxBody {
boxed(http_body::Empty::new())
}
#[doc(hidden)]
pub fn to_boxed<B>(body: B) -> BoxBody
where
Body: From<B>,
{
boxed(Body::from(body))
}