Skip to main content

Body

Struct Body 

Source
pub struct Body(/* private fields */);
Expand description

The body of a request or a response. It is empty, bytes already in memory, or a stream read as fast as the platform delivers it.

The fields that come after a body — its trailers — are read with trailers once the body has ended: it reads what is left of the body first, discarding those bytes to reach its end, as HTTP sends its trailers only after the last byte, so asking for them never waits for a reader that never comes. A request carries its own with with_trailers, which the body answers once its bytes have been read, so the fields may be made from the bytes streamed (a checksum). Asking again answers the same fields. A body made from bytes can be sent again with try_clone, which is what a redirect that resends its body needs.

Implementations§

Source§

impl Body

Source

pub fn empty() -> Body

No bytes at all: a GET’s body, or a response with nothing in it.

Source

pub fn from_stream(stream: impl BodyStream + 'static) -> Body

The bytes stream reads, as they come.

Source

pub fn with_trailers(self, trailers: HeaderMap) -> Body

trailers, sent after this body ends: the body answers them once its bytes have been read, as HTTP sends its trailers after the last byte. A body whose stream has fields of its own has them replaced. A client whose protocol layer cannot send trailers fails the request rather than dropping them. Over HTTP/1.1 the request names its trailers in a Trailer header, as RFC 9110 §6.6.2 asks of a sender: a client may drop a field it was not told of (hyper does), and a body’s trailers may not be known until it ends, so the request says them, not the client.

Source

pub fn try_clone(&self) -> Option<Body>

A second body with the same bytes, for a request a client sends again on a redirect that resends its body. Some for a body made from bytes and for an empty one, with the fields it sends; None for one that streams, whose bytes are gone once they are read and whose fields may depend on them.

Source

pub fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> BodyReadFuture<'a>

Reads the next bytes of the body into buf, answering how many it read, 0 at the end: a piece at a time, as the platform delivers it.

Source

pub fn read_all(&mut self) -> BodyReadAllFuture<'_>

The whole body, read through: the bytes a request carries, or a response’s.

Source

pub fn trailers(&mut self) -> BodyTrailersFuture<'_>

The fields that came after the body, once it has ended: reads what is left of the body first, discarding those bytes to reach its end, as HTTP sends its trailers only after its last byte, so asking for them never waits for a reader that never comes. The bytes are discarded into a buffer on the stack, so a body that has already ended costs no allocation. Asking again answers the same fields, which a client that sends them keeps; None where the response has none, and always None on a client whose protocol layer does not expose them.

Source

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

How many bytes the whole body carries, when its sender said so: Some(0) for an empty body, and None for one whose length is unknown.

Trait Implementations§

Source§

impl Debug for Body

Source§

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

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

impl Default for Body

Source§

fn default() -> Body

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

impl From<&'static [u8]> for Body

Source§

fn from(bytes: &'static [u8]) -> Body

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Body

Source§

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

Converts to this type from the input type.

Auto Trait Implementations§

§

impl !Freeze for Body

§

impl !RefUnwindSafe for Body

§

impl !Send for Body

§

impl !Sync for Body

§

impl !UnwindSafe for Body

§

impl Forget for Body

§

impl Move for Body

§

impl Unpin for Body
where *const u8: Unpin, Align8<*mut ()>: Unpin, &'static Vtable: Unpin, Box<BodyKind>: Unpin, Box<[Pos]>: Unpin, *const u8 is !null: Unpin, Box<dyn BodyStream>: Unpin,

§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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

Source§

fn upcast(&self) -> Option<&T>