feat(rt): make tokio runtime optional

A Cargo feature `runtime` is added, which is enabled by default, that
includes the following:

- The `client::HttpConnector`, which uses `tokio::net::TcpStream`.
- The `server::AddrStream`, which uses `tokio::net::TcpListener`.
- The `hyper::rt` module, which includes useful utilities to work with
  the runtime without needing to import `futures` or `tokio` explicity.

Disabling the feature removes many of these niceties, but allows people
to use hyper in environments that have an alternative runtime, without
needing to download an unused one.
This commit is contained in:
Sean McArthur
2018-04-23 16:56:26 -07:00
committed by GitHub
parent 62a5c1188a
commit d127201ef2
21 changed files with 541 additions and 410 deletions

View File

@@ -9,23 +9,22 @@
//! higher-level [Server](super) API.
use std::fmt;
use std::net::SocketAddr;
#[cfg(feature = "runtime")] use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
#[cfg(feature = "runtime")] use std::time::Duration;
use bytes::Bytes;
use futures::{Async, Future, Poll, Stream};
use futures::future::{Either, Executor};
use tokio_io::{AsyncRead, AsyncWrite};
//TODO: change these tokio:: to sub-crates
use tokio::reactor::Handle;
#[cfg(feature = "runtime")] use tokio_reactor::Handle;
use common::Exec;
use proto;
use body::{Body, Payload};
use service::{NewService, Service};
pub use super::tcp::AddrIncoming;
#[cfg(feature = "runtime")] pub use super::tcp::AddrIncoming;
/// A lower-level configuration of the HTTP protocol.
///
@@ -190,22 +189,23 @@ impl Http {
/// # Example
///
/// ```
/// # extern crate futures;
/// # extern crate hyper;
/// # extern crate tokio;
/// # extern crate tokio_io;
/// # use futures::Future;
/// # #[cfg(feature = "runtime")]
/// # extern crate tokio;
/// # use hyper::{Body, Request, Response};
/// # use hyper::service::Service;
/// # use hyper::server::conn::Http;
/// # use tokio_io::{AsyncRead, AsyncWrite};
/// # use tokio::reactor::Handle;
/// # #[cfg(feature = "runtime")]
/// # fn run<I, S>(some_io: I, some_service: S)
/// # where
/// # I: AsyncRead + AsyncWrite + Send + 'static,
/// # S: Service<ReqBody=Body, ResBody=Body> + Send + 'static,
/// # S::Future: Send
/// # {
/// # use hyper::rt::Future;
/// # use tokio::reactor::Handle;
/// let http = Http::new();
/// let conn = http.serve_connection(some_io, some_service);
///
@@ -213,7 +213,7 @@ impl Http {
/// eprintln!("server connection error: {}", e);
/// });
///
/// tokio::spawn(fut);
/// hyper::rt::spawn(fut);
/// # }
/// # fn main() {}
/// ```
@@ -252,6 +252,7 @@ impl Http {
/// to accept connections. Each connection will be processed with the
/// `new_service` object provided, creating a new service per
/// connection.
#[cfg(feature = "runtime")]
pub fn serve_addr<S, Bd>(&self, addr: &SocketAddr, new_service: S) -> ::Result<Serve<AddrIncoming, S>>
where
S: NewService<ReqBody=Body, ResBody=Bd>,
@@ -271,6 +272,7 @@ impl Http {
/// to accept connections. Each connection will be processed with the
/// `new_service` object provided, creating a new service per
/// connection.
#[cfg(feature = "runtime")]
pub fn serve_addr_handle<S, Bd>(&self, addr: &SocketAddr, handle: &Handle, new_service: S) -> ::Result<Serve<AddrIncoming, S>>
where
S: NewService<ReqBody=Body, ResBody=Bd>,
@@ -465,6 +467,7 @@ where
// ===== impl SpawnAll =====
#[cfg(feature = "runtime")]
impl<S> SpawnAll<AddrIncoming, S> {
pub(super) fn local_addr(&self) -> SocketAddr {
self.serve.incoming.local_addr()