SharedBodyExt

Trait SharedBodyExt 

Source
pub trait SharedBodyExt: Body {
    // Provided method
    fn into_shared(self) -> SharedBody<Self>
       where Self: Sized + Unpin,
             Self::Data: Clone,
             Self::Error: Clone { ... }
}
Expand description

Extension trait for http_body::Body that provides the into_shared method.

This trait allows any HTTP body that meets the requirements to be easily converted into a SharedBody for sharing across multiple consumers.

Provided Methods§

Source

fn into_shared(self) -> SharedBody<Self>
where Self: Sized + Unpin, Self::Data: Clone, Self::Error: Clone,

Converts this HTTP body into a SharedBody.

This method consumes the original body and returns a SharedBody that can be cloned to create multiple consumers sharing the same underlying body state.

§Requirements

The body must satisfy:

  • http_body::Body: The type must implement the Body trait
  • Unpin: Required for safe polling without additional pinning
  • Clone for Self::Data: Body data must be cloneable
  • Clone for Self::Error: Body error type must be cloneable
§Examples
use shared_http_body::{SharedBody, SharedBodyExt};
use http_body_util::{BodyExt, StreamBody};
use http_body::Frame;
use bytes::Bytes;
use futures_util::stream;

let chunks = vec!["hello", "world"];
let stream = stream::iter(chunks.into_iter().map(|s| Ok::<_, std::convert::Infallible>(Frame::data(Bytes::from(s)))));
let body = StreamBody::new(stream);

// Use the extension trait to convert to SharedBody
let shared_body = body.into_shared();

let result = shared_body.collect().await.unwrap().to_bytes();
assert_eq!(result, Bytes::from("helloworld"));

Implementors§

Source§

impl<B> SharedBodyExt for B
where B: Body,