pub enum ResponseBody {
Buffered {
bytes: Bytes,
},
Streaming {
stream: BoxBytesStream,
lease: Option<Arc<PoolGuard>>,
},
EncodedStreaming {
stream: BoxBytesStream,
lease: Option<Arc<PoolGuard>>,
content_encoding: String,
limit: DecompressionLimit,
},
Consumed,
}Expand description
Response body handle.
Supports two consumption modes:
- Buffered: the entire body is held in memory. Created by
ResponseBody::buffered(). Callingbytes()ortext()is O(1). - Streaming: body chunks are yielded incrementally. Created by
ResponseBody::streaming(). The stream must be fully consumed or dropped before the attached pool lease is released.
§Single-consumption semantics
A response body can be consumed once via bytes(), text(),
bytes_stream(), or raw_bytes_stream(). For compressed streaming bodies,
raw and decoded selection are mutually exclusive. After consumption,
further reads return an error.
Variants§
Buffered
Fully buffered body.
Streaming
Streaming body.
Fields
stream: BoxBytesStreamThe body stream.
lease: Option<Arc<PoolGuard>>Optional pool permit holder. Released on drop.
The guard privately carries the response read/total lifecycle
policy; see crate::pool::PoolGuard. Manually constructed
lease-free bodies carry no implicit client timeout.
EncodedStreaming
A compressed streaming body whose encoded source is retained until the caller selects raw or decoded consumption.
Fields
stream: BoxBytesStreamThe encoded body stream.
lease: Option<Arc<PoolGuard>>Optional pool permit holder. Released on drop.
As with ResponseBody::Streaming, the guard privately carries
the read/total policy applied to the final selected stream.
limit: DecompressionLimitLimits applied when decoded mode is selected.
Consumed
The streaming body has already been consumed.
Implementations§
Source§impl ResponseBody
impl ResponseBody
Sourcepub fn streaming(stream: BoxBytesStream) -> Self
pub fn streaming(stream: BoxBytesStream) -> Self
Create a streaming response body from a type-erased stream.
Sourcepub fn len(&self) -> Option<usize>
pub fn len(&self) -> Option<usize>
Returns the buffered body length, if available.
For streaming bodies, returns None until the body is fully
collected.
Sourcepub fn is_buffered(&self) -> bool
pub fn is_buffered(&self) -> bool
Returns true if this is a buffered body.
Sourcepub async fn bytes(&mut self) -> Result<Bytes>
pub async fn bytes(&mut self) -> Result<Bytes>
Consume the body and return all bytes.
For buffered bodies, this is O(1). For streaming bodies, this
collects all chunks into a single Bytes buffer.
§Limits
The configured decoded-body limit is enforced for both encoded and unencoded response bodies.
§Errors
Returns an error if the body has already been consumed or if a stream chunk fails.
Sourcepub async fn text(&mut self) -> Result<String>
pub async fn text(&mut self) -> Result<String>
Consume the body and return it as a UTF-8 string.
§Errors
Returns an error if the body is not valid UTF-8 or has already been consumed.
Sourcepub fn bytes_stream(&mut self) -> Result<BoxBytesStream>
pub fn bytes_stream(&mut self) -> Result<BoxBytesStream>
Consume the body and return a stream of byte chunks.
For buffered bodies, yields the entire body as a single chunk. For streaming bodies, yields chunks incrementally.
§Errors
Returns an error if the body has already been consumed.
Sourcepub fn raw_bytes_stream(&mut self) -> Result<BoxBytesStream>
pub fn raw_bytes_stream(&mut self) -> Result<BoxBytesStream>
Consume the body and return the encoded response byte stream.
For compressed streaming responses, this selects the original encoded transport body and bypasses the existing decoder chain. The selection is one-shot; a body cannot subsequently be consumed in decoded mode. Buffered bodies follow the ordinary streaming path because their encoded representation is no longer available.
§Errors
Returns an error if the body has already been consumed or if the decoder configuration is invalid while selecting a stream.