A pit full of rusty nails
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125use std::pin::Pin;
use color_eyre::eyre::Error as BodyError;use http_body_util::{BodyExt, Empty, Full};use hyper::body::{Body as HttpBody, Buf};
pub use hyper::body::Bytes;
/// A boxed [`Body`] trait object that is !Send.pub struct UnsendBoxBody<D, E> { inner: Pin<Box<dyn HttpBody<Data = D, Error = E> + 'static>>,}
impl<D, E> UnsendBoxBody<D, E> { /// Create a new `BoxBody`. #[inline] pub fn new<B>(body: B) -> Self where B: HttpBody<Data = D, Error = E> + 'static, D: Buf, { Self { inner: Box::pin(body), } }}
impl<D, E> std::fmt::Debug for UnsendBoxBody<D, E> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("UnsendBoxBody").finish_non_exhaustive() }}
impl<D, E> HttpBody for UnsendBoxBody<D, E>where D: Buf,{ type Data = D; type Error = E;
#[inline] fn poll_frame( mut self: Pin<&mut Self>, cx: &mut std::task::Context<'_>, ) -> std::task::Poll<Option<Result<hyper::body::Frame<Self::Data>, Self::Error>>> { self.inner.as_mut().poll_frame(cx) }
#[inline] fn is_end_stream(&self) -> bool { self.inner.is_end_stream() }
#[inline] fn size_hint(&self) -> hyper::body::SizeHint { self.inner.size_hint() }}
impl From<&'static str> for Body { fn from(value: &'static str) -> Self { Body::new(Full::from(value)) }}
impl From<Bytes> for Body { fn from(value: Bytes) -> Self { Body::new(Full::new(value)) }}
pub struct Body(UnsendBoxBody<Bytes, BodyError>);
impl Body { pub fn new<B>(body: B) -> Self where B: HttpBody<Data = Bytes> + 'static, B::Error: Into<BodyError>, { let downcasted = try_downcast_err(body).unwrap_or_else(|body| body.map_err(Into::into)); Self(UnsendBoxBody::new(downcasted)) }
pub fn empty() -> Self { Self(UnsendBoxBody::new(Empty::new().map_err(Into::into))) }}
impl hyper::body::Body for Body { type Data = Bytes;
type Error = BodyError;
#[inline] fn poll_frame( mut self: Pin<&mut Self>, cx: &mut std::task::Context<'_>, ) -> std::task::Poll<Option<Result<hyper::body::Frame<Self::Data>, Self::Error>>> { self.as_mut().0.inner.as_mut().poll_frame(cx) }
#[inline] fn is_end_stream(&self) -> bool { self.0.is_end_stream() }
#[inline] fn size_hint(&self) -> hyper::body::SizeHint { self.0.size_hint() }}
fn try_downcast_err<T, K>(k: K) -> Result<T, K>where T: 'static, K: '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()) }}