Skip to main content

BodyExt

Trait BodyExt 

Source
pub trait BodyExt: Body {
    // Provided methods
    fn frame(&mut self) -> Frame<'_, Self> 
       where Self: Unpin { ... }
    fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
       where Self: Sized,
             F: FnMut(Frame<Self::Data>) -> Frame<B>,
             B: Buf { ... }
    fn inspect_frame<F>(self, f: F) -> InspectFrame<Self, F>
       where Self: Sized,
             F: FnMut(&Frame<Self::Data>) { ... }
    fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
       where Self: Sized,
             F: FnMut(Self::Error) -> E { ... }
    fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
       where Self: Sized,
             F: FnMut(&Self::Error) { ... }
    fn boxed(self) -> BoxBody<Self::Data, Self::Error>
       where Self: Sized + Send + Sync + 'static { ... }
    fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
       where Self: Sized + Send + 'static { ... }
    fn collect(self) -> Collect<Self> 
       where Self: Sized { ... }
    fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
       where Self: Sized,
             F: Future<Output = Option<Result<HeaderMap, Self::Error>>> { ... }
    fn into_stream(self) -> BodyStream<Self>
       where Self: Sized { ... }
    fn into_data_stream(self) -> BodyDataStream<Self>
       where Self: Sized { ... }
    fn fuse(self) -> Fuse<Self>
       where Self: Sized { ... }
}
Expand description

An extension trait for http_body::Body adding various combinators and adapters

Provided Methods§

Source

fn frame(&mut self) -> Frame<'_, Self>
where Self: Unpin,

Returns a future that resolves to the next Frame, if any.

Source

fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
where Self: Sized, F: FnMut(Frame<Self::Data>) -> Frame<B>, B: Buf,

Maps this body’s frame to a different kind.

Source

fn inspect_frame<F>(self, f: F) -> InspectFrame<Self, F>
where Self: Sized, F: FnMut(&Frame<Self::Data>),

A body that calls a function with a reference to each frame before yielding it.

Source

fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
where Self: Sized, F: FnMut(Self::Error) -> E,

Maps this body’s error value to a different value.

Source

fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
where Self: Sized, F: FnMut(&Self::Error),

A body that calls a function with a reference to an error before yielding it.

Source

fn boxed(self) -> BoxBody<Self::Data, Self::Error>
where Self: Sized + Send + Sync + 'static,

Turn this body into a boxed trait object.

Source

fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
where Self: Sized + Send + 'static,

Turn this body into a boxed trait object that is !Sync.

Source

fn collect(self) -> Collect<Self>
where Self: Sized,

Turn this body into Collected body which will collect all the DATA frames and trailers.

Source

fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
where Self: Sized, F: Future<Output = Option<Result<HeaderMap, Self::Error>>>,

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;

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 {
    // ...
}
Source

fn into_stream(self) -> BodyStream<Self>
where Self: Sized,

Turn this body into BodyStream.

Source

fn into_data_stream(self) -> BodyDataStream<Self>
where Self: Sized,

Turn this body into BodyDataStream.

Source

fn fuse(self) -> Fuse<Self>
where Self: Sized,

Creates a “fused” body.

This Body yields Poll::Ready(None) forever after the underlying body yields Poll::Ready(None), or an error Poll::Ready(Some(Err(_))), once.

See Fuse<B> for more information.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> BodyExt for T
where T: Body + ?Sized,