pub struct Body { /* private fields */ }Expand description
Public response body implementing http_body::Body.
The cutover replaced the legacy mpsc::Receiver<Result<Bytes>> response
surface with this poll-based body. Buffered responses (returned by
RequestBuilder::send) carry their bytes inline and emit them as a single
data frame. H1 streaming responses poll the socket directly; other
transports use their current internal delivery until their poll-body
transport cutovers land.
Cloning a streaming body is rejected at runtime because the transport body
has a single consumer; only [Body::Empty]/buffered bodies clone cheaply.
Implementations§
Source§impl Body
impl Body
Sourcepub fn from_bytes(bytes: impl Into<Bytes>) -> Self
pub fn from_bytes(bytes: impl Into<Bytes>) -> Self
Construct a buffered body that yields the given bytes once and then signals end-of-stream. Cheap to clone and to query for length.
Sourcepub async fn trailers(&mut self) -> Result<Option<Headers>>
pub async fn trailers(&mut self) -> Result<Option<Headers>>
Await HTTP/2 response trailers for this body, if any.
Only H2 streaming bodies can carry trailers, and only when the caller
requested them (te: trailers). Every other body variant returns
Ok(None). See [crate::transport::h2::H2Body::trailers] for the
three-state contract (clean end and not-requested both map to
Ok(None); a stream reset maps to Err).
Public so language bindings (node/python) can surface gRPC
grpc-status/grpc-message trailers from a streaming Body.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
true for an empty buffered body. Streaming bodies report false
because the buffered length is unknown until the body is drained.
Sourcepub fn is_streaming(&self) -> bool
pub fn is_streaming(&self) -> bool
true if the body was created from a streaming transport channel.
Sourcepub fn as_bytes(&self) -> Option<&Bytes>
pub fn as_bytes(&self) -> Option<&Bytes>
Return a reference to the buffered bytes when the body is fully
materialized, or None if the body is streaming or already drained.
Sourcepub fn buffered_len(&self) -> Option<usize>
pub fn buffered_len(&self) -> Option<usize>
Buffered length when known, None for streaming bodies.
Sourcepub fn h3_capacity(&self) -> Option<H3BodyCapacity>
pub fn h3_capacity(&self) -> Option<H3BodyCapacity>
Snapshot H3 streaming response buffer pressure when this body is backed by the native HTTP/3 transport.
Sourcepub fn capacity(&self) -> BodyCapacity
pub fn capacity(&self) -> BodyCapacity
Snapshot protocol-neutral response-body buffer pressure.
H2 and native H3 streaming bodies report their actual bounded driver queues. H1 and direct-owned H2 bodies stream directly from the socket instead of a public queue, so they report zero queued capacity/bytes. Buffered and empty bodies report their materialized byte state.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Convenience accessor for buffered bodies. Returns 0 for streaming
bodies; callers wanting to detect streaming should use
Body::buffered_len or Body::is_streaming.
Sourcepub fn frame(&mut self) -> FrameFuture<'_> ⓘ
pub fn frame(&mut self) -> FrameFuture<'_> ⓘ
Poll the next frame asynchronously. Returns None after end-of-stream.
Sourcepub fn chunk(&mut self) -> ChunkFuture<'_> ⓘ
pub fn chunk(&mut self) -> ChunkFuture<'_> ⓘ
Poll the next data chunk asynchronously. Returns None after end-of-stream.
Sourcepub async fn collect_to_bytes(&mut self) -> Result<Bytes>
pub async fn collect_to_bytes(&mut self) -> Result<Bytes>
Drain the body into a contiguous Bytes buffer.
For buffered bodies this is essentially a clone of the underlying bytes. For streaming bodies it polls the body to completion, so callers must opt in explicitly.