pub enum Body {
Bytes(Bytes),
Stream(Pin<Box<dyn Stream<Item = Result<Bytes>> + Send + 'static>>),
}Expand description
A response body.
Construct buffered bodies with From (Body::from(bytes),
"text".into()), an empty body with Body::empty, or a streaming body
with Body::from_stream. The engine sends buffered bodies in one shot and
streamed bodies frame-by-frame.
Variants§
Bytes(Bytes)
A fully-buffered, in-memory body.
Stream(Pin<Box<dyn Stream<Item = Result<Bytes>> + Send + 'static>>)
A lazily-produced stream of byte chunks.
Implementations§
Source§impl Body
impl Body
Sourcepub fn from_stream<S, E>(stream: S) -> Self
pub fn from_stream<S, E>(stream: S) -> Self
Build a streaming body from any Stream of byte chunks. Chunk errors are
converted into Error and surface when the body is read/collected.
Sourcepub fn as_bytes(&self) -> Option<&Bytes>
pub fn as_bytes(&self) -> Option<&Bytes>
Borrow the buffered bytes, or None if this body is a stream. Used by
middleware that only post-processes already-materialized bodies.
Sourcepub fn as_slice(&self) -> Option<&[u8]>
pub fn as_slice(&self) -> Option<&[u8]>
Borrow the buffered bytes as a slice, or None for a stream.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
True if this is a buffered, empty body. A stream is never reported empty (its length is unknown until read).
Sourcepub async fn into_bytes(self) -> Result<Bytes>
pub async fn into_bytes(self) -> Result<Bytes>
Collect the whole body into Bytes (returns the buffer directly when
already buffered, or drains the stream).