Skip to main content

ResponseBody

Enum ResponseBody 

Source
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(). Calling bytes() or text() 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.

Fields

§bytes: Bytes

The buffered body bytes.

§

Streaming

Streaming body.

Fields

§stream: BoxBytesStream

The 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: BoxBytesStream

The 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.

§content_encoding: String

The original Content-Encoding header value.

§limit: DecompressionLimit

Limits applied when decoded mode is selected.

§

Consumed

The streaming body has already been consumed.

Implementations§

Source§

impl ResponseBody

Source

pub fn buffered(bytes: Bytes) -> Self

Create a buffered response body.

Source

pub fn streaming(stream: BoxBytesStream) -> Self

Create a streaming response body from a type-erased stream.

Source

pub fn is_empty(&self) -> bool

Returns true if the body is empty.

Source

pub fn len(&self) -> Option<usize>

Returns the buffered body length, if available.

For streaming bodies, returns None until the body is fully collected.

Source

pub fn is_buffered(&self) -> bool

Returns true if this is a buffered body.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for ResponseBody

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more