Struct trillium_http::Body

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

The trillium representation of a http body. This can contain either &'static [u8] content, Vec<u8> content, or a boxed AsyncRead type.

Implementations§

source§

impl Body

source

pub fn new_streaming( async_read: impl AsyncRead + Send + Sync + 'static, len: Option<u64>, ) -> Self

Construct a new body from a streaming [AsyncRead] source. If you have the body content in memory already, prefer Body::new_static or one of the From conversions.

source

pub fn new_static(content: impl Into<Cow<'static, [u8]>>) -> Self

Construct a fixed-length Body from a Vec<u8> or &'static [u8].

source

pub fn static_bytes(&self) -> Option<&[u8]>

Retrieve a borrow of the static content in this body. If this body is a streaming body or an empty body, this will return None.

source

pub fn into_reader(self) -> Pin<Box<dyn AsyncRead + Send + Sync>>

Transform this Body into a dyn AsyncRead. This will wrap static content in a [Cursor]. Note that this is different from reading directly from the Body, which includes chunked encoding.

source

pub async fn into_bytes(self) -> Result<Cow<'static, [u8]>>

Consume this body and return the full content. If the body was constructed with [Body::new_streaming], this will read the entire streaming body into memory, awaiting the streaming source’s completion. This function will return an error if a streaming body has already been read to completion.

§Errors

This returns an error variant if either of the following conditions are met:

  • there is an io error when reading from the underlying transport such as a disconnect
  • the body has already been read to completion
source

pub fn bytes_read(&self) -> u64

Retrieve the number of bytes that have been read from this body

source

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

returns the content length of this body, if known and available.

source

pub fn is_empty(&self) -> bool

determine if the this body represents no data

source

pub fn is_static(&self) -> bool

determine if the this body represents static content

source

pub fn is_streaming(&self) -> bool

determine if the this body represents streaming content

Trait Implementations§

source§

impl AsyncRead for Body

source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize>>

Attempt to read from the AsyncRead into buf. Read more
§

fn poll_read_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>], ) -> Poll<Result<usize, Error>>

Attempt to read from the AsyncRead into bufs using vectored IO operations. 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() -> Body

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

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

source§

fn from(content: &'static [u8]) -> Self

Converts to this type from the input type.
source§

impl From<&'static str> for Body

source§

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

Converts to this type from the input type.
source§

impl From<Cow<'static, [u8]>> for Body

source§

fn from(value: Cow<'static, [u8]>) -> Self

Converts to this type from the input type.
source§

impl From<Cow<'static, str>> for Body

source§

fn from(value: Cow<'static, str>) -> Self

Converts to this type from the input type.
source§

impl<Transport> From<ReceivedBody<'static, Transport>> for Body
where Transport: AsyncRead + AsyncWrite + Send + Sync + Unpin + 'static,

source§

fn from(rb: ReceivedBody<'static, Transport>) -> Self

Converts to this type from the input type.
source§

impl From<String> for Body

source§

fn from(s: String) -> Self

Converts to this type from the input type.
source§

impl From<Vec<u8>> for Body

source§

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

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 Unpin for Body

§

impl !UnwindSafe 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
§

impl<R> AsyncReadExt for R
where R: AsyncRead + ?Sized,

§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>
where Self: Unpin,

Reads some bytes from the byte stream. Read more
§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>], ) -> ReadVectoredFuture<'a, Self>
where Self: Unpin,

Like [read()][AsyncReadExt::read()], except it reads into a slice of buffers. Read more
§

fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8>, ) -> ReadToEndFuture<'a, Self>
where Self: Unpin,

Reads the entire contents and appends them to a Vec. Read more
§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String, ) -> ReadToStringFuture<'a, Self>
where Self: Unpin,

Reads the entire contents and appends them to a String. Read more
§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>
where Self: Unpin,

Reads the exact number of bytes required to fill buf. Read more
§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Converts this [AsyncRead] into a [Stream] of bytes. Read more
§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: AsyncRead, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
§

fn boxed_reader<'a>(self) -> Pin<Box<dyn AsyncRead + Send + 'a>>
where Self: Sized + Send + 'a,

Boxes the reader and changes its type to dyn AsyncRead + Send + 'a. 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>,

§

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

§

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.