Skip to main content

Body

Enum Body 

Source
pub enum Body {
    Empty,
    Bytes(Bytes),
    Stream(BodyStream),
}
Expand description

An HTTP response body: nothing, some bytes, or a stream of chunks.

Variants§

§

Empty

No body at all — distinct from a zero-length one, so 204 No Content can omit Content-Length entirely.

§

Bytes(Bytes)

A body already in memory. Its length is known, so Content-Length can be set.

§

Stream(BodyStream)

A body produced incrementally. Its length is unknown, so it goes out chunked.

Implementations§

Source§

impl Body

Source

pub fn empty() -> Self

An empty body.

Source

pub fn from_bytes(bytes: impl Into<Bytes>) -> Self

A body from anything byte-like.

Source

pub fn from_stream<S>(stream: S) -> Self
where S: Stream<Item = Result<Bytes, Error>> + Send + 'static,

A streaming body.

Source

pub fn size_hint(&self) -> Option<u64>

The body’s length in bytes, when it is known up front. None for a stream.

Source

pub fn is_empty(&self) -> bool

Whether this body is definitely empty. false for a stream, which may yet yield nothing — we cannot know without polling it.

Source

pub async fn collect(self) -> Result<Bytes, Error>

Collect the whole body into memory.

Drains a stream to completion, so only call it on a body you know is bounded — in tests, or on a response you are about to assert against.

Trait Implementations§

Source§

impl Body for Body

Lets a Body be handed straight to hyper, which writes responses through the http-body trait.

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<Bytes>, Error>>>

Attempt to pull out the next frame of this stream. Read more
Source§

fn is_end_stream(&self) -> bool

A hint that may return true when the end of stream has been reached. Read more
Source§

fn size_hint(&self) -> SizeHint

A hint that returns the bounds on the remaining length of the stream. 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<&'static str> for Body

Source§

fn from(text: &'static str) -> Self

Converts to this type from the input type.
Source§

impl From<()> for Body

Source§

fn from(_: ()) -> Self

Converts to this type from the input type.
Source§

impl From<Bytes> for Body

Source§

fn from(bytes: Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Body

Source§

fn from(text: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Body

Source§

fn from(bytes: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl IntoResponse for Body

Source§

fn into_response(self) -> Response

Build the response.

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.