//! Utilities for [`http_body::Body`]. //! //! [`BodyExt`] adds extensions to the common trait. //! //! [`Empty`] and [`Full`] provide simple implementations.
mod collected; pubmod combinators; mod either; mod empty; mod full; mod limited; mod stream;
/// An extension trait for [`http_body::Body`] adding various combinators and adapters pubtrait BodyExt: http_body::Body { /// Returns a future that resolves to the next [`Frame`], if any. /// /// [`Frame`]: combinators::Frame fn frame(&mutself) -> combinators::Frame<'_, Self> where Self: Unpin,
{
combinators::Frame(self)
}
/// Maps this body's frame to a different kind. fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F> where Self: Sized,
F: FnMut(http_body::Frame<Self::Data>) -> http_body::Frame<B>,
B: bytes::Buf,
{
MapFrame::new(self, f)
}
/// Maps this body's error value to a different value. fn map_err<F, E>(self, f: F) -> MapErr<Self, F> where Self: Sized,
F: FnMut(Self::Error) -> E,
{
MapErr::new(self, f)
}
/// Turn this body into a boxed trait object. fn boxed(self) -> BoxBody<Self::Data, Self::Error> where Self: Sized + Send + Sync + 'static,
{
BoxBody::new(self)
}
/// Turn this body into a boxed trait object that is !Sync. fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error> where Self: Sized + Send + 'static,
{
UnsyncBoxBody::new(self)
}
/// Turn this body into [`Collected`] body which will collect all the DATA frames /// and trailers. fn collect(self) -> combinators::Collect<Self> where Self: Sized,
{
combinators::Collect {
body: self,
collected: Some(crate::Collected::default()),
}
}
/// Add trailers to the body. /// /// The trailers will be sent when all previous frames have been sent and the `trailers` future /// resolves. /// /// # Example /// /// ``` /// use http::HeaderMap; /// use http_body_util::{Full, BodyExt}; /// use bytes::Bytes; /// /// # #[tokio::main] /// async fn main() { /// let (tx, rx) = tokio::sync::oneshot::channel::<HeaderMap>(); /// /// let body = Full::<Bytes>::from("Hello, World!") /// // add trailers via a future /// .with_trailers(async move { /// match rx.await { /// Ok(trailers) => Some(Ok(trailers)), /// Err(_err) => None, /// } /// }); /// /// // compute the trailers in the background /// tokio::spawn(async move { /// let _ = tx.send(compute_trailers().await); /// }); /// /// async fn compute_trailers() -> HeaderMap { /// // ... /// # unimplemented!() /// } /// # } /// ``` fn with_trailers<F>(self, trailers: F) -> combinators::WithTrailers<Self, F> where Self: Sized,
F: std::future::Future<Output = Option<Result<http::HeaderMap, Self::Error>>>,
{
combinators::WithTrailers::new(self, trailers)
}
/// Turn this body into [`BodyDataStream`]. fn into_data_stream(self) -> BodyDataStream<Self> where Self: Sized,
{
BodyDataStream::new(self)
}
}
impl<T: ?Sized> BodyExt for T where T: http_body::Body {}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.