feat(lib): update to std::future::Future

BREAKING CHANGE: All usage of async traits (`Future`, `Stream`,
`AsyncRead`, `AsyncWrite`, etc) are updated to newer versions.
This commit is contained in:
Sean McArthur
2019-07-09 15:37:43 -07:00
parent da9b0319ef
commit 8f4b05ae78
37 changed files with 1526 additions and 1548 deletions

View File

@@ -1,3 +1,4 @@
#![feature(async_await)]
#![deny(warnings)]
extern crate hyper;
extern crate pretty_env_logger;
@@ -6,7 +7,7 @@ use std::env;
use std::io::{self, Write};
use hyper::Client;
use hyper::rt::{self, Future, Stream};
use hyper::rt;
fn main() {
pretty_env_logger::init();
@@ -35,31 +36,34 @@ fn main() {
rt::run(fetch_url(url));
}
fn fetch_url(url: hyper::Uri) -> impl Future<Item=(), Error=()> {
async fn fetch_url(url: hyper::Uri) {
let client = Client::new();
client
// Fetch the url...
.get(url)
// And then, if we get a response back...
.and_then(|res| {
println!("Response: {}", res.status());
println!("Headers: {:#?}", res.headers());
let res = match client.get(url).await {
Ok(res) => res,
Err(err) => {
eprintln!("Response Error: {}", err);
return;
}
};
// The body is a stream, and for_each returns a new Future
// when the stream is finished, and calls the closure on
// each chunk of the body...
res.into_body().for_each(|chunk| {
println!("Response: {}", res.status());
println!("Headers: {:#?}\n", res.headers());
let mut body = res.into_body();
while let Some(next) = body.next().await {
match next {
Ok(chunk) => {
io::stdout().write_all(&chunk)
.map_err(|e| panic!("example expects stdout is open, error={}", e))
})
})
// If all good, just tell the user...
.map(|_| {
println!("\n\nDone.");
})
// If there was an error, let the user know...
.map_err(|err| {
eprintln!("Error {}", err);
})
.expect("example expects stdout is open");
},
Err(err) => {
eprintln!("Body Error: {}", err);
return;
}
}
}
println!("\n\nDone!");
}

View File

@@ -1,27 +1,38 @@
#![feature(async_await)]
#![deny(warnings)]
extern crate hyper;
extern crate pretty_env_logger;
use hyper::{Body, Request, Response, Server};
use hyper::service::service_fn_ok;
use hyper::rt::{self, Future};
use hyper::service::{make_service_fn, service_fn};
use hyper::rt;
fn main() {
pretty_env_logger::init();
async fn hello(_: Request<Body>) -> Result<Response<Body>, hyper::Error> {
Ok(Response::new(Body::from("Hello World!")))
}
async fn serve() {
let addr = ([127, 0, 0, 1], 3000).into();
let server = Server::bind(&addr)
.serve(|| {
.serve(make_service_fn(|_| {
// This is the `Service` that will handle the connection.
// `service_fn_ok` is a helper to convert a function that
// returns a Response into a `Service`.
service_fn_ok(move |_: Request<Body>| {
Response::new(Body::from("Hello World!"))
})
})
.map_err(|e| eprintln!("server error: {}", e));
async {
Ok::<_, hyper::Error>(service_fn(hello))
}
}));
println!("Listening on http://{}", addr);
rt::run(server);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
}
fn main() {
pretty_env_logger::init();
rt::run(serve());
}