Skip to main content

Body

Struct Body 

Source
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

Source

pub fn empty() -> Self

Construct an empty body that completes without yielding any frames.

Source

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.

Source

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.

Source

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.

Source

pub fn is_streaming(&self) -> bool

true if the body was created from a streaming transport channel.

Source

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.

Source

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

Buffered length when known, None for streaming bodies.

Source

pub fn h3_capacity(&self) -> Option<H3BodyCapacity>

Snapshot H3 streaming response buffer pressure when this body is backed by the native HTTP/3 transport.

Source

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.

Source

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.

Source

pub fn frame(&mut self) -> FrameFuture<'_>

Poll the next frame asynchronously. Returns None after end-of-stream.

Source

pub fn chunk(&mut self) -> ChunkFuture<'_>

Poll the next data chunk asynchronously. Returns None after end-of-stream.

Source

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.

Trait Implementations§

Source§

impl Body for Body

Source§

type Data = Bytes

Values yielded by the Body.
Source§

type Error = Error

The error type this Body might generate.
Source§

fn poll_frame( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>>

Attempt to pull out the next data buffer of this stream.
Source§

fn is_end_stream(&self) -> bool

Returns true when the end of stream has been reached. Read more
Source§

fn size_hint(&self) -> SizeHint

Returns the bounds on the remaining length of the stream. Read more
Source§

impl Clone for Body

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Body

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Body

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Bytes> for Body

Source§

fn from(value: Bytes) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl !Freeze for Body

§

impl !RefUnwindSafe for Body

§

impl !Sync for Body

§

impl !UnwindSafe for Body

§

impl Send for Body

§

impl Unpin for Body

§

impl UnsafeUnpin for Body

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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