-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
compat.rs
59 lines (48 loc) · 1.42 KB
/
compat.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! Wrappers to build compatibility with the `http` crate.
use std::io;
use futures::{Future, Poll, Stream};
use http;
use tokio_service::Service;
use client::{Connect2, Client, FutureResponse};
use error::Error;
use proto::Body;
/// A Client to make outgoing HTTP requests.
#[derive(Debug)]
pub struct CompatClient<C, B = Body> {
inner: Client<C, B>
}
pub(super) fn client<C, B>(client: Client<C, B>) -> CompatClient<C, B> {
CompatClient { inner: client }
}
impl<C, B> Service for CompatClient<C, B>
where C: Connect2<Error=io::Error>,
C::Transport: 'static,
C::Future: 'static,
B: Stream<Error=Error> + 'static,
B::Item: AsRef<[u8]>,
{
type Request = http::Request<B>;
type Response = http::Response<Body>;
type Error = Error;
type Future = CompatFutureResponse;
fn call(&self, req: Self::Request) -> Self::Future {
future(self.inner.call(req.into()))
}
}
/// A `Future` that will resolve to an `http::Response`.
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
pub struct CompatFutureResponse {
inner: FutureResponse
}
pub(super) fn future(fut: FutureResponse) -> CompatFutureResponse {
CompatFutureResponse { inner: fut }
}
impl Future for CompatFutureResponse {
type Item = http::Response<Body>;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Error> {
self.inner.poll()
.map(|a| a.map(|r| r.into()))
}
}