tower_hyper/
util.rs

1//! Util for working with hyper and tower
2
3use futures::{try_ready, Async, Future, Poll};
4use hyper::client::connect::Connect;
5use tower_service::Service;
6
7pub use hyper::client::connect::{Destination, HttpConnector};
8
9/// A bridge between `hyper::client::connect::Connect` types
10/// and `tower_util::MakeConnection`.
11///
12/// # Example
13///
14/// ```
15/// # use tower_hyper::util::Connector;
16/// # use tower_hyper::client::{Connect, Builder};
17/// # use hyper::client::HttpConnector;
18/// # use tokio_executor::DefaultExecutor;
19/// let connector = Connector::new(HttpConnector::new(1));
20/// let mut hyper = Connect::new(connector);
21/// # let hyper: Connect<hyper::client::connect::Destination, Vec<u8>, Connector<HttpConnector>, DefaultExecutor> = hyper;
22/// ```
23#[derive(Debug)]
24pub struct Connector<C> {
25    inner: C,
26}
27
28/// The future that resolves to the eventual inner transport
29/// as built by `hyper::client::connect::Connect`.
30#[derive(Debug)]
31pub struct ConnectorFuture<C>
32where
33    C: Connect,
34{
35    inner: C::Future,
36}
37
38impl<C> Connector<C>
39where
40    C: Connect,
41{
42    /// Construct a new connector from a `hyper::client::connect::Connect`
43    pub fn new(inner: C) -> Self {
44        Connector { inner }
45    }
46}
47
48impl<C> Service<Destination> for Connector<C>
49where
50    C: Connect,
51{
52    type Response = C::Transport;
53    type Error = C::Error;
54    type Future = ConnectorFuture<C>;
55
56    fn poll_ready(&mut self) -> Poll<(), Self::Error> {
57        Ok(().into())
58    }
59
60    fn call(&mut self, target: Destination) -> Self::Future {
61        let fut = self.inner.connect(target);
62        ConnectorFuture { inner: fut }
63    }
64}
65
66impl<C> Future for ConnectorFuture<C>
67where
68    C: Connect,
69{
70    type Item = C::Transport;
71    type Error = C::Error;
72
73    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
74        let (transport, _) = try_ready!(self.inner.poll());
75
76        Ok(Async::Ready(transport))
77    }
78}