Trait BodyExt

trait BodyExt: http_body::Body

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

Provided Methods

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

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

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

Maps this body's frame to a different kind.

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

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

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

Turn this body into a boxed trait object.

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

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

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

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

fn with_trailers<F>(self: Self, trailers: F) -> WithTrailers<Self, F>
where
    Self: Sized,
    F: Future<Output = Option<Result<HeaderMap, <Self as >::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;

# #[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 into_data_stream(self: Self) -> BodyDataStream<Self>
where
    Self: Sized

Turn this body into BodyDataStream.

Implementors