pub enum Body {
Bytes(Bytes),
MoreBytes(MoreBytes),
HyperBody(HyperBodyStream),
AsyncRead(Pin<Box<dyn AsyncRead + Send + Sync + 'static, Global>>),
BytesStream(Pin<Box<dyn BytesStream + Send + Sync + 'static, Global>>),
Empty,
}Expand description
The body for any request or response.
Variants
Bytes(Bytes)
MoreBytes(MoreBytes)
HyperBody(HyperBodyStream)
only available if the feature hyper_body is enabled.
AsyncRead(Pin<Box<dyn AsyncRead + Send + Sync + 'static, Global>>)
BytesStream(Pin<Box<dyn BytesStream + Send + Sync + 'static, Global>>)
Empty
Implementations
sourceimpl Body
impl Body
sourcepub fn from_bytes<B>(bytes: B) -> Body where
B: Into<Bytes>,
pub fn from_bytes<B>(bytes: B) -> Body where
B: Into<Bytes>,
Creates a new body with the give bytes, an empty body is returned if bytes is empty.
sourcepub fn copy_from_slice(slice: &[u8]) -> Body
pub fn copy_from_slice(slice: &[u8]) -> Body
Creates a new Body::Bytes from the given bytes if the slice
is not empty.
sourcepub fn from_async_read<R>(reader: R) -> Body where
R: 'static + AsyncRead + Send + Sync,
pub fn from_async_read<R>(reader: R) -> Body where
R: 'static + AsyncRead + Send + Sync,
Creates a new Body from an AsyncRead implementation.
This puts the AsyncRead in a box.
sourcepub fn from_bytes_stream<S>(stream: S) -> Body where
S: 'static + BytesStream + Send + Sync,
pub fn from_bytes_stream<S>(stream: S) -> Body where
S: 'static + BytesStream + Send + Sync,
Creates a new Body from an BytesStream implementation.
This puts the BytesStream in a box.
sourcepub fn from_hyper_body(body: Body) -> Body
pub fn from_hyper_body(body: Body) -> Body
Creates a new Body from a hyper Body. Aftwards you can set
a size limit with set_size_limit.
Note
Works only with the hyper_body feature.
sourcepub fn set_size_limit(&mut self, max_size: usize) -> bool
pub fn set_size_limit(&mut self, max_size: usize) -> bool
Sets a read size limit to the HyperBody. Returns true if the size limit was set.
Note
Works only with the hyper_body feature.
When the size limit is reached an io::Error::Other with SizeLimitReached
is returned.
Panics while reading
If the body was already read more than the max_size or the max_size is 0.
sourcepub fn into_http_body(
self
) -> StreamHttpBody<Pin<Box<dyn BytesStream + Send + Sync + 'static, Global>>>
pub fn into_http_body(
self
) -> StreamHttpBody<Pin<Box<dyn BytesStream + Send + Sync + 'static, Global>>>
Converts the Body to a StreamHttpBody which implements http_body::Body,
useful when using hyper.
sourcepub fn into_bytes_stream(
self
) -> Pin<Box<dyn BytesStream + Send + Sync + 'static, Global>>
pub fn into_bytes_stream(
self
) -> Pin<Box<dyn BytesStream + Send + Sync + 'static, Global>>
Converts the Body to a boxed BytesStream.
sourcepub fn into_async_read(
self
) -> Pin<Box<dyn AsyncRead + Send + Sync + 'static, Global>>
pub fn into_async_read(
self
) -> Pin<Box<dyn AsyncRead + Send + Sync + 'static, Global>>
Converts the Body to a boxed AsyncRead.
sourcepub async fn into_more_bytes(self) -> Result<MoreBytes, Error>
pub async fn into_more_bytes(self) -> Result<MoreBytes, Error>
Converts the body into MoreBytes returning an error if reading failed or the size limit was reached.
sourcepub async fn into_string(self) -> Result<String, Error>
pub async fn into_string(self) -> Result<String, Error>
Converts the body into a String.
Note
If possible, avoid this function as it is really inefficient.
For tests or quick debugging however it is quite suitable.
sourcepub async fn as_slice(&'_ mut self) -> Result<&'_ [u8], Error>
pub async fn as_slice(&'_ mut self) -> Result<&'_ [u8], Error>
Converts the body into to Body::Bytes, returning the slice.
Note
If possible, avoid this function as it is really inefficient.
sourcepub async fn copy_to_async_write<W>(
self,
writer: &'_ mut W
) -> Result<(), Error> where
W: AsyncWrite + Unpin,
pub async fn copy_to_async_write<W>(
self,
writer: &'_ mut W
) -> Result<(), Error> where
W: AsyncWrite + Unpin,
Writes the entire body to an AsyncWrite implementer.
sourcepub fn add_timeout(self, timeout: Duration) -> BodyWithTimeout
pub fn add_timeout(self, timeout: Duration) -> BodyWithTimeout
Trait Implementations
sourceimpl From<HyperBodyStream> for Body
impl From<HyperBodyStream> for Body
sourcefn from(b: HyperBodyStream) -> Body
fn from(b: HyperBodyStream) -> Body
Converts to this type from the input type.
sourceimpl IntoResponse for Body
impl IntoResponse for Body
fn into_response(self) -> Response
Auto Trait Implementations
impl !RefUnwindSafe for Body
impl Send for Body
impl Sync for Body
impl Unpin for Body
impl !UnwindSafe for Body
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more