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

Consume a BodyStream and return a future which resolves to Self.

Consume Self and returns a BodyStream.

Provided Methods

Convenience function to convert from one type implementing Body to another.

Implementations on Foreign Types

impl Body for ()
[src]

impl Body for Bytes
[src]

impl Body for Vec<u8>
[src]

impl Body for String
[src]

Implementors