Trait aitch::Body [−][src]
pub trait Body where
Self: Send + Sized + 'static, { type Future: Future<Item = Self, Error = Error> + Send; fn from_stream(stream: BodyStream) -> Self::Future; fn into_stream(self) -> BodyStream; fn into_body<OtherBody: Body>(self) -> OtherBody::Future { ... } }
A trait defining the types that can be used in HTTP requests and responses.
The http::Request<B>
and http::Response<B>
types from the http
crate do not restrict
the type of the body used in requests and bodies.
This type is used by aitch
to place some contraints on the body types used, so that handlers,
middlewares and servers can make assumptions about how to deserialize it from the raw HTTP
request, and how they should be serialized to the raw HTTP responses.
The trait aims to describe both non-streaming and streaming bodies, allowing it to be used both handlers that process the request asnchronously and those that do so synchronously.
Types implementing this trait must provide both a way to construct themselves from
BodyStream
, and a way to construct a BodyStream
from themselves. This allows the same
types to be used in both request and response bodies, and to allow multiple layers of
handlers/middlewares to interpret a request's body in different ways.
Empty Request/Response Bodies
For requests/responses where the body is not important (such as GET
HTTP requests, or error
responses), the unit type, ()
may be used to denote an empty body.
Example
An implementation of Body
for the bytes::Bytes
type (which is provided by aitch):
extern crate aitch; extern crate bytes; extern crate futures; use aitch::{Body, BodyStream}; use bytes::Bytes; use futures::{stream, Future, Stream}; // Define a wrapper, to allow implementing for foreign type. struct BytesWrapper(Bytes); impl Body for BytesWrapper { type Future = Box<Future<Item = Self, Error = aitch::Error> + Send>; fn from_stream(stream: BodyStream) -> Self::Future { Box::new(stream.concat2().map(BytesWrapper)) } fn into_stream(self) -> BodyStream { Box::new(stream::once(Ok(self.0))) } }
Associated Types
Required Methods
fn from_stream(stream: BodyStream) -> Self::Future
Consume a BodyStream
and return a future which resolves to Self
.
fn into_stream(self) -> BodyStream
Consume Self
and returns a BodyStream
.
Provided Methods
fn into_body<OtherBody: Body>(self) -> OtherBody::Future
Convenience function to convert from one type implementing Body
to another.
Implementations on Foreign Types
impl Body for ()
[src]
impl Body for ()
type Future = FutureResult<(), Error>
fn from_stream(_: BodyStream) -> Self::Future
[src]
fn from_stream(_: BodyStream) -> Self::Future
fn into_stream(self) -> BodyStream
[src]
fn into_stream(self) -> BodyStream
fn into_body<OtherBody: Body>(self) -> OtherBody::Future
[src]
fn into_body<OtherBody: Body>(self) -> OtherBody::Future
impl Body for Bytes
[src]
impl Body for Bytes
type Future = Concat2<BodyStream>
fn from_stream(stream: BodyStream) -> Self::Future
[src]
fn from_stream(stream: BodyStream) -> Self::Future
fn into_stream(self) -> BodyStream
[src]
fn into_stream(self) -> BodyStream
fn into_body<OtherBody: Body>(self) -> OtherBody::Future
[src]
fn into_body<OtherBody: Body>(self) -> OtherBody::Future
impl Body for Vec<u8>
[src]
impl Body for Vec<u8>
type Future = Map<Concat2<BodyStream>, fn(_: Bytes) -> Vec<u8>>
fn from_stream(stream: BodyStream) -> Self::Future
[src]
fn from_stream(stream: BodyStream) -> Self::Future
fn into_stream(self) -> BodyStream
[src]
fn into_stream(self) -> BodyStream
fn into_body<OtherBody: Body>(self) -> OtherBody::Future
[src]
fn into_body<OtherBody: Body>(self) -> OtherBody::Future
impl Body for String
[src]
impl Body for String
type Future = AndThen<Concat2<BodyStream>, Result<String>, fn(_: Bytes) -> Result<String>>
fn from_stream(stream: BodyStream) -> Self::Future
[src]
fn from_stream(stream: BodyStream) -> Self::Future
fn into_stream(self) -> BodyStream
[src]
fn into_stream(self) -> BodyStream
fn into_body<OtherBody: Body>(self) -> OtherBody::Future
[src]
fn into_body<OtherBody: Body>(self) -> OtherBody::Future
Implementors
impl Body for BodyStream type Future = FutureResult<BodyStream, Error>;
impl<T> Body for Json<T> where
T: DeserializeOwned + Serialize + Send + 'static, type Future = AndThen<Concat2<BodyStream>, Result<Json<T>>, fn(_: Bytes) -> Result<Json<T>>>;