Update examples to use new Tokio (#316)
The old `tokio-core` crate is deprecated in favour of the `tokio` crate, which also provides the new multithreaded Tokio runtime. Although `h2` is generic over the runtime, the examples do depend on `tokio`. This branch update the example code to use the new `tokio` library rather than `tokio-core`. It also updates the examples in `RustDoc`. For the most part, this was pretty trivial --- simply replacing `handle.spawn(...)` with `tokio::spawn(...)` and `core.run(...)` with `tokio::run(...)`. There were a couple of cases where error and item types from spawned futures had to be mapped to `(), ()`. Alternatively, this could have been avoided by using the single-threaded runtime and calling `block_on` instead to await the value of those futures, but I thought it was better to reduce the amount of tokio-specific code in the examples. Signed-off-by: Eliza Weisman <eliza@buoyant.io>
This commit is contained in:
@@ -68,27 +68,21 @@
|
||||
//! extern crate futures;
|
||||
//! extern crate h2;
|
||||
//! extern crate http;
|
||||
//! extern crate tokio_core;
|
||||
//! extern crate tokio;
|
||||
//!
|
||||
//! use h2::client;
|
||||
//!
|
||||
//! use futures::*;
|
||||
//! # use futures::future::ok;
|
||||
//! use http::*;
|
||||
//!
|
||||
//! use tokio_core::net::TcpStream;
|
||||
//! use tokio_core::reactor;
|
||||
//! use tokio::net::TcpStream;
|
||||
//!
|
||||
//! pub fn main() {
|
||||
//! let mut core = reactor::Core::new().unwrap();
|
||||
//! let handle = core.handle();
|
||||
//!
|
||||
//! let addr = "127.0.0.1:5928".parse().unwrap();
|
||||
//!
|
||||
//! core.run({
|
||||
//! # let _ =
|
||||
//! tokio::run(
|
||||
//! // Establish TCP connection to the server.
|
||||
//! TcpStream::connect(&addr, &handle)
|
||||
//! TcpStream::connect(&addr)
|
||||
//! .map_err(|_| {
|
||||
//! panic!("failed to establish TCP connection")
|
||||
//! })
|
||||
@@ -98,7 +92,7 @@
|
||||
//! .map_err(|_| panic!("HTTP/2.0 connection failed"));
|
||||
//!
|
||||
//! // Spawn a new task to drive the connection state
|
||||
//! handle.spawn(connection);
|
||||
//! tokio::spawn(connection);
|
||||
//!
|
||||
//! // Wait until the `SendRequest` handle has available
|
||||
//! // capacity.
|
||||
@@ -139,9 +133,8 @@
|
||||
//! })
|
||||
//! })
|
||||
//! })
|
||||
//! # ;
|
||||
//! # ok::<_, ()>(())
|
||||
//! }).ok().expect("failed to perform HTTP/2.0 request");
|
||||
//! .map_err(|e| panic!("failed to perform HTTP/2.0 request: {:?}", e))
|
||||
//! )
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
|
||||
@@ -67,27 +67,23 @@
|
||||
//! extern crate futures;
|
||||
//! extern crate h2;
|
||||
//! extern crate http;
|
||||
//! extern crate tokio_core;
|
||||
//! extern crate tokio;
|
||||
//!
|
||||
//! use futures::{Future, Stream};
|
||||
//! # use futures::future::ok;
|
||||
//! use h2::server;
|
||||
//! use http::{Response, StatusCode};
|
||||
//! use tokio_core::reactor;
|
||||
//! use tokio_core::net::TcpListener;
|
||||
//! use tokio::net::TcpListener;
|
||||
//!
|
||||
//! pub fn main () {
|
||||
//! let mut core = reactor::Core::new().unwrap();
|
||||
//! let handle = core.handle();
|
||||
//!
|
||||
//! let addr = "127.0.0.1:5928".parse().unwrap();
|
||||
//! let listener = TcpListener::bind(&addr, &handle).unwrap();
|
||||
//! let listener = TcpListener::bind(&addr,).unwrap();
|
||||
//!
|
||||
//! core.run({
|
||||
//! tokio::run({
|
||||
//! // Accept all incoming TCP connections.
|
||||
//! listener.incoming().for_each(move |(socket, _)| {
|
||||
//! listener.incoming().for_each(move |socket| {
|
||||
//! // Spawn a new task to process each connection.
|
||||
//! handle.spawn({
|
||||
//! tokio::spawn({
|
||||
//! // Start the HTTP/2.0 connection handshake
|
||||
//! server::handshake(socket)
|
||||
//! .and_then(|h2| {
|
||||
@@ -114,8 +110,9 @@
|
||||
//!
|
||||
//! Ok(())
|
||||
//! })
|
||||
//! # .select(ok(()))
|
||||
//! }).ok().expect("failed to run HTTP/2.0 server");
|
||||
//! .map_err(|e| panic!("failed to run HTTP/2.0 server: {:?}", e))
|
||||
//! # .select(ok(())).map(|_|()).map_err(|_|())
|
||||
//! });
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
|
||||
Reference in New Issue
Block a user