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:
Eliza Weisman
2018-09-25 14:33:49 -07:00
committed by GitHub
parent 12e0d26945
commit 00ca534c4a
6 changed files with 38 additions and 57 deletions

View File

@@ -3,7 +3,7 @@ extern crate futures;
extern crate h2;
extern crate http;
extern crate rustls;
extern crate tokio_core;
extern crate tokio;
extern crate tokio_rustls;
extern crate webpki;
extern crate webpki_roots;
@@ -13,8 +13,7 @@ use h2::client;
use futures::*;
use http::{Method, Request};
use tokio_core::net::TcpStream;
use tokio_core::reactor;
use tokio::net::TcpStream;
use rustls::Session;
use tokio_rustls::ClientConfigExt;
@@ -44,13 +43,10 @@ pub fn main() {
println!("ADDR: {:?}", addr);
let mut core = reactor::Core::new().unwrap();
let handle = core.handle();
let tcp = TcpStream::connect(&addr, &handle);
let tcp = TcpStream::connect(&addr);
let dns_name = DNSNameRef::try_from_ascii_str("http2.akamai.com").unwrap();
let tcp = tcp.then(|res| {
let tcp = tcp.then(move |res| {
let tcp = res.unwrap();
tls_client_config
.connect_async(dns_name, tcp)
@@ -87,7 +83,9 @@ pub fn main() {
h2.join(stream)
})
});
})
.map_err(|e| eprintln!("ERROR: {:?}", e))
.map(|((), ())| ());
core.run(tcp).unwrap();
tokio::run(tcp);
}