Trait actix_http::body::MessageBody
source · pub trait MessageBody {
type Error: Into<Box<dyn StdError>>;
// Required methods
fn size(&self) -> BodySize;
fn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Option<Result<Bytes, Self::Error>>>;
// Provided methods
fn try_into_bytes(self) -> Result<Bytes, Self>
where Self: Sized { ... }
fn boxed(self) -> BoxBody
where Self: Sized + 'static { ... }
}
Expand description
An interface for types that can be used as a response body.
It is not usually necessary to create custom body types, this trait is already implemented for a large number of sensible body types including:
- Empty body:
()
- Text-based:
String
,&'static str
,ByteString
. - Byte-based:
Bytes
,BytesMut
,Vec<u8>
,&'static [u8]
; - Streams:
BodyStream
,SizedStream
§Examples
struct Repeat {
chunk: String,
n_times: usize,
}
impl MessageBody for Repeat {
type Error = Infallible;
fn size(&self) -> BodySize {
BodySize::Sized((self.chunk.len() * self.n_times) as u64)
}
fn poll_next(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Self::Error>>> {
let payload_string = self.chunk.repeat(self.n_times);
let payload_bytes = Bytes::from(payload_string);
Poll::Ready(Some(Ok(payload_bytes)))
}
}
Required Associated Types§
Required Methods§
sourcefn size(&self) -> BodySize
fn size(&self) -> BodySize
Body size hint.
If BodySize::None
is returned, optimizations that skip reading the body are allowed.
sourcefn poll_next(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Option<Result<Bytes, Self::Error>>>
fn poll_next( self: Pin<&mut Self>, cx: &mut Context<'_> ) -> Poll<Option<Result<Bytes, Self::Error>>>
Attempt to pull out the next chunk of body bytes.
§Return Value
Similar to the Stream
interface, there are several possible return values, each indicating
a distinct state:
Poll::Pending
means that this body’s next chunk is not ready yet. Implementations must ensure that the current task will be notified when the next chunk may be ready.Poll::Ready(Some(val))
means that the body has successfully produced a chunk,val
, and may produce further values on subsequentpoll_next
calls.Poll::Ready(None)
means that the body is complete, andpoll_next
should not be invoked again.
§Panics
Once a body is complete (i.e., poll_next
returned Ready(None)
), calling its poll_next
method again may panic, block forever, or cause other kinds of problems; this trait places
no requirements on the effects of such a call. However, as the poll_next
method is not
marked unsafe, Rust’s usual rules apply: calls must never cause UB, regardless of its state.
Provided Methods§
sourcefn try_into_bytes(self) -> Result<Bytes, Self>where
Self: Sized,
fn try_into_bytes(self) -> Result<Bytes, Self>where
Self: Sized,
Try to convert into the complete chunk of body bytes.
Override this method if the complete body can be trivially extracted. This is useful for
optimizations where poll_next
calls can be avoided.
Body types with BodySize::None
are allowed to return empty Bytes
. Although, if calling
this method, it is recommended to check size
first and return early.
§Errors
The default implementation will error and return the original type back to the caller for further use.
sourcefn boxed(self) -> BoxBodywhere
Self: Sized + 'static,
fn boxed(self) -> BoxBodywhere
Self: Sized + 'static,
Wraps this body into a BoxBody
.
No-op when called on a BoxBody
, meaning there is no risk of double boxing when calling
this on a generic MessageBody
. Prefer this over BoxBody::new
when a boxed body
is required.
Implementations on Foreign Types§
source§impl MessageBody for &'static str
impl MessageBody for &'static str
source§impl MessageBody for &'static [u8]
impl MessageBody for &'static [u8]
source§impl MessageBody for Cow<'static, str>
impl MessageBody for Cow<'static, str>
source§impl MessageBody for Cow<'static, [u8]>
impl MessageBody for Cow<'static, [u8]>
source§impl MessageBody for Infallible
impl MessageBody for Infallible
source§impl MessageBody for ()
impl MessageBody for ()
source§impl MessageBody for String
impl MessageBody for String
source§impl MessageBody for Vec<u8>
impl MessageBody for Vec<u8>
source§impl MessageBody for ByteString
impl MessageBody for ByteString
source§impl MessageBody for Bytes
impl MessageBody for Bytes
source§impl MessageBody for BytesMut
impl MessageBody for BytesMut
source§impl<B> MessageBody for &mut B
impl<B> MessageBody for &mut B
source§impl<B> MessageBody for Box<B>
impl<B> MessageBody for Box<B>
source§impl<T, B> MessageBody for Pin<T>
impl<T, B> MessageBody for Pin<T>
Implementors§
source§impl MessageBody for None
impl MessageBody for None
type Error = Infallible
source§impl<B> MessageBody for Encoder<B>where
B: MessageBody,
Available on crate feature __compress
only.
impl<B> MessageBody for Encoder<B>where
B: MessageBody,
__compress
only.