feat(lib): redesign API to use Futures and Tokio

There are many changes involved with this, but let's just talk about
user-facing changes.

- Creating a `Client` and `Server` now needs a Tokio `Core` event loop
to attach to.
- `Request` and `Response` both no longer implement the
`std::io::{Read,Write}` traits, but instead represent their bodies as a
`futures::Stream` of items, where each item is a `Chunk`.
- The `Client.request` method now takes a `Request`, instead of being
used as a builder, and returns a `Future` that resolves to `Response`.
- The `Handler` trait for servers is no more, and instead the Tokio
`Service` trait is used. This allows interoperability with generic
middleware.

BREAKING CHANGE: A big sweeping set of breaking changes.
This commit is contained in:
Sean McArthur
2016-11-17 17:31:42 -08:00
parent e23689122a
commit 2d2d5574a6
43 changed files with 2775 additions and 5033 deletions

97
src/http/body.rs Normal file
View File

@@ -0,0 +1,97 @@
use std::convert::From;
use std::sync::Arc;
use tokio_proto;
use http::Chunk;
use futures::{Poll, Stream};
use futures::sync::mpsc;
pub type TokioBody = tokio_proto::streaming::Body<Chunk, ::Error>;
/// A `Stream` for `Chunk`s used in requests and responses.
#[derive(Debug)]
pub struct Body(TokioBody);
impl Body {
/// Return an empty body stream
pub fn empty() -> Body {
Body(TokioBody::empty())
}
/// Return a body stream with an associated sender half
pub fn pair() -> (mpsc::Sender<Result<Chunk, ::Error>>, Body) {
let (tx, rx) = TokioBody::pair();
let rx = Body(rx);
(tx, rx)
}
}
impl Stream for Body {
type Item = Chunk;
type Error = ::Error;
fn poll(&mut self) -> Poll<Option<Chunk>, ::Error> {
self.0.poll()
}
}
impl From<Body> for tokio_proto::streaming::Body<Chunk, ::Error> {
fn from(b: Body) -> tokio_proto::streaming::Body<Chunk, ::Error> {
b.0
}
}
impl From<tokio_proto::streaming::Body<Chunk, ::Error>> for Body {
fn from(tokio_body: tokio_proto::streaming::Body<Chunk, ::Error>) -> Body {
Body(tokio_body)
}
}
impl From<mpsc::Receiver<Result<Chunk, ::Error>>> for Body {
fn from(src: mpsc::Receiver<Result<Chunk, ::Error>>) -> Body {
Body(src.into())
}
}
impl From<Chunk> for Body {
fn from (chunk: Chunk) -> Body {
Body(TokioBody::from(chunk))
}
}
impl From<Vec<u8>> for Body {
fn from (vec: Vec<u8>) -> Body {
Body(TokioBody::from(Chunk::from(vec)))
}
}
impl From<Arc<Vec<u8>>> for Body {
fn from (vec: Arc<Vec<u8>>) -> Body {
Body(TokioBody::from(Chunk::from(vec)))
}
}
impl From<&'static [u8]> for Body {
fn from (slice: &'static [u8]) -> Body {
Body(TokioBody::from(Chunk::from(slice)))
}
}
impl From<String> for Body {
fn from (s: String) -> Body {
Body(TokioBody::from(Chunk::from(s.into_bytes())))
}
}
impl From<&'static str> for Body {
fn from (slice: &'static str) -> Body {
Body(TokioBody::from(Chunk::from(slice.as_bytes())))
}
}
fn _assert_send() {
fn _assert<T: Send>() {}
_assert::<Body>();
_assert::<Chunk>();
}