Trait BodyExt
pub trait BodyExt: Body {
Show 15 methods
// 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 capture<S>(self, sink: S) -> CaptureBody<Self, S>
where Self: Sized + Body<Data = Bytes>,
S: BodyCaptureSink { ... }
fn capture_buffered(
self,
limit: CaptureLimit,
) -> (CaptureBody<Self, BufferedBodyCapture>, CaptureHandle)
where Self: Sized + Body<Data = Bytes> { ... }
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 collect_with(self, opts: CollectOptions) -> CollectWith<Self> ⓘ
where Self: Sized + Body<Data = Bytes> + Send + Sync + Unpin + 'static,
Self::Error: Into<Box<dyn Error + Sync + Send>> { ... }
fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
where Self: Sized,
F: Future<Output = Option<Result<HeaderMap, Self::Error>>> { ... }
fn into_data_stream(self) -> BodyDataStream<Self>
where Self: Sized { ... }
fn into_stream(self) -> BodyStream<Self>
where Self: Sized { ... }
fn fuse(self) -> Fuse<Self>
where Self: Sized { ... }
}http and std only.Expand description
An extension trait for crate::body::http_body::Body adding various combinators and adapters
Provided Methods§
fn frame(&mut self) -> Frame<'_, Self> ⓘwhere
Self: Unpin,
fn frame(&mut self) -> Frame<'_, Self> ⓘwhere
Self: Unpin,
Returns a future that resolves to the next Frame, if any.
fn inspect_frame<F>(self, f: F) -> InspectFrame<Self, F>
fn inspect_frame<F>(self, f: F) -> InspectFrame<Self, F>
A body that calls a function with a reference to each frame before yielding it.
fn capture<S>(self, sink: S) -> CaptureBody<Self, S>
fn capture<S>(self, sink: S) -> CaptureBody<Self, S>
Forward this byte body while asynchronously sending owned frame copies to a sink.
fn capture_buffered(
self,
limit: CaptureLimit,
) -> (CaptureBody<Self, BufferedBodyCapture>, CaptureHandle)
fn capture_buffered( self, limit: CaptureLimit, ) -> (CaptureBody<Self, BufferedBodyCapture>, CaptureHandle)
Forward this byte body while retaining a bounded or unlimited copy in memory.
fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
A body that calls a function with a reference to an error before yielding it.
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
Turn this body into a boxed trait object that is !Sync.
fn collect(self) -> Collect<Self> ⓘwhere
Self: Sized,
fn collect(self) -> Collect<Self> ⓘwhere
Self: Sized,
Turn this body into Collected body which will collect all the DATA frames
and trailers.
On a body stream error the returned future yields a CollectError that
still carries the bytes read before the failure. Use collect_with to
additionally bound the size and/or time and keep the unread remainder
forwardable.
fn collect_with(self, opts: CollectOptions) -> CollectWith<Self> ⓘ
fn collect_with(self, opts: CollectOptions) -> CollectWith<Self> ⓘ
Collect this body, but bounded by the size cap and/or timeout in
CollectOptions.
On success returns the Collected body, exactly like collect. When
a bound is hit it stops early with a CollectError that retains the
bytes read so far and the unread remainder — call
CollectError::into_full_body to reassemble and forward the body on
untouched (handy for proxies).
This is the soft, recoverable counterpart to Limited: where Limited
hard-fails with a LengthLimitError and discards the body the moment
its cap is crossed, collect_with loses nothing — the bytes read and the
remainder are both preserved.
fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
Add trailers to the body.
The trailers will be sent when all previous frames have been sent and the trailers future
resolves.
§Example
use rama_http_types::HeaderMap;
use rama_http_types::body::util::{Full, BodyExt};
use rama_core::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 {
// ...
}fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
Turn this body into BodyDataStream.
fn into_stream(self) -> BodyStream<Self>where
Self: Sized,
fn into_stream(self) -> BodyStream<Self>where
Self: Sized,
Turn this body into BodyStream.
This can be combined with stream combinators to observe each frame or error asynchronously without changing it. The observer is awaited before the item is yielded, so it naturally applies backpressure without blocking an executor thread or cloning the frame:
use rama_core::futures::StreamExt as _;
use rama_http_types::body::{Body, Frame, util::{BodyExt, StreamBody}};
async fn observe<D, E>(_item: &Result<Frame<D>, E>) {}
let body = Body::from("hello");
let body = StreamBody::new(body.into_stream().then(|item| async move {
observe(&item).await;
item
}));
let collected = BodyExt::collect(body).await.unwrap();
assert_eq!(collected.to_bytes(), "hello");Stream combinators observe yielded items, not the normal end marker
(None). Use an appropriate body or layer lifecycle hook when normal
end-of-stream must also be observed. Converting the resulting stream
back with StreamBody does not preserve the original body’s
SizeHint or early
Body::is_end_stream
indication.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".