Skip to content

Commit 5c26956

Browse files
authored
update to std::future::Future (#265)
* update to rust 2018 edition * update to std::future::Future
1 parent 9eb4bb4 commit 5c26956

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2185
-1877
lines changed

Cargo.toml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ categories = ["web-programming::http-server"]
1111
keywords = ["warp", "server", "http", "hyper"]
1212
autotests = true
1313
autoexamples = true
14+
edition = "2018"
15+
16+
[package.metadata.docs.rs]
17+
features = ["tls"]
1418

1519
[dependencies]
1620
bytes = "0.4.8"
17-
futures = "0.1"
21+
futures-preview = "0.3.0-alpha.19"
22+
futures-util-preview = "0.3.0-alpha.19"
1823
headers = "0.2"
1924
http = "0.1"
20-
hyper = "0.12.18"
25+
hyper = { version = "=0.13.0-alpha.4", features = ["unstable-stream"] }
2126
log = "0.4"
2227
mime = "0.3"
2328
mime_guess = "2.0.0"
@@ -26,13 +31,13 @@ scoped-tls = "1.0"
2631
serde = "1.0"
2732
serde_json = "1.0"
2833
serde_urlencoded = "0.6"
29-
tokio = "0.1.11"
30-
tokio-io = "0.1"
31-
rustls = { version = "0.15", optional = true }
32-
tokio-threadpool = "0.1.7"
34+
tokio = "0.2.0-alpha.6"
35+
tokio-executor = "=0.2.0-alpha.6"
36+
rustls = { version = "0.16", optional = true }
3337
# tls is enabled by default, we don't want that yet
3438
tungstenite = { default-features = false, version = "0.9", optional = true }
3539
urlencoding = "1.0.0"
40+
pin-project = "0.4.5"
3641

3742
[dev-dependencies]
3843
pretty_env_logger = "0.3"
@@ -44,9 +49,6 @@ default = ["multipart", "websocket"]
4449
websocket = ["tungstenite"]
4550
tls = ["rustls"]
4651

47-
[package.metadata.docs.rs]
48-
features = ["tls"]
49-
5052
[profile.release]
5153
codegen-units = 1
5254
incremental = false

examples/body.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#![deny(warnings)]
2-
extern crate serde;
3-
#[macro_use]
4-
extern crate serde_derive;
5-
extern crate pretty_env_logger;
6-
extern crate warp;
2+
3+
use serde_derive::{Deserialize, Serialize};
74

85
use warp::Filter;
96

@@ -13,7 +10,8 @@ struct Employee {
1310
rate: u32,
1411
}
1512

16-
fn main() {
13+
#[tokio::main]
14+
async fn main() {
1715
pretty_env_logger::init();
1816

1917
// POST /employees/:rate {"name":"Sean","rate":2}
@@ -28,5 +26,5 @@ fn main() {
2826
warp::reply::json(&employee)
2927
});
3028

31-
warp::serve(promote).run(([127, 0, 0, 1], 3030));
29+
warp::serve(promote).run(([127, 0, 0, 1], 3030)).await
3230
}

examples/dir.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#![deny(warnings)]
2-
extern crate pretty_env_logger;
3-
extern crate warp;
42

5-
fn main() {
3+
#[tokio::main]
4+
async fn main() {
65
pretty_env_logger::init();
76

8-
warp::serve(warp::fs::dir("examples/dir")).run(([127, 0, 0, 1], 3030));
7+
warp::serve(warp::fs::dir("examples/dir")).run(([127, 0, 0, 1], 3030)).await;
98
}

examples/errors.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
#![deny(warnings)]
2-
extern crate serde;
3-
#[macro_use]
4-
extern crate serde_derive;
5-
extern crate pretty_env_logger;
6-
extern crate warp;
72

83
use std::error::Error as StdError;
94
use std::fmt::{self, Display};
105

6+
use serde_derive::Serialize;
117
use warp::http::StatusCode;
12-
use warp::{Filter, Rejection, Reply};
8+
use warp::{Future, Filter, Rejection, Reply};
139

1410
#[derive(Copy, Clone, Debug)]
1511
enum Error {
@@ -34,49 +30,53 @@ impl Display for Error {
3430

3531
impl StdError for Error {}
3632

37-
fn main() {
33+
#[tokio::main]
34+
async fn main() {
3835
let hello = warp::path::end().map(warp::reply);
3936

4037
let oops =
41-
warp::path("oops").and_then(|| Err::<StatusCode, _>(warp::reject::custom(Error::Oops)));
38+
warp::path("oops").and_then(|| futures::future::err::<StatusCode, _>(warp::reject::custom(Error::Oops)));
4239

4340
let nope =
44-
warp::path("nope").and_then(|| Err::<StatusCode, _>(warp::reject::custom(Error::Nope)));
41+
warp::path("nope").and_then(|| futures::future::err::<StatusCode, _>(warp::reject::custom(Error::Nope)));
4542

4643
let routes = warp::get2()
4744
.and(hello.or(oops).or(nope))
4845
.recover(customize_error);
4946

50-
warp::serve(routes).run(([127, 0, 0, 1], 3030));
47+
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
5148
}
5249

5350
// This function receives a `Rejection` and tries to return a custom
5451
// value, othewise simply passes the rejection along.
55-
fn customize_error(err: Rejection) -> Result<impl Reply, Rejection> {
56-
if let Some(&err) = err.find_cause::<Error>() {
57-
let code = match err {
58-
Error::Nope => StatusCode::BAD_REQUEST,
59-
Error::Oops => StatusCode::INTERNAL_SERVER_ERROR,
60-
};
61-
let msg = err.to_string();
52+
fn customize_error(err: Rejection) -> impl Future< Output = Result<impl Reply, Rejection>> {
53+
let err = {
54+
if let Some(&err) = err.find_cause::<Error>() {
55+
let code = match err {
56+
Error::Nope => StatusCode::BAD_REQUEST,
57+
Error::Oops => StatusCode::INTERNAL_SERVER_ERROR,
58+
};
59+
let msg = err.to_string();
6260

63-
let json = warp::reply::json(&ErrorMessage {
64-
code: code.as_u16(),
65-
message: msg,
66-
});
67-
Ok(warp::reply::with_status(json, code))
68-
} else if let Some(_) = err.find_cause::<warp::reject::MethodNotAllowed>() {
69-
// We can handle a specific error, here METHOD_NOT_ALLOWED,
70-
// and render it however we want
71-
let code = StatusCode::METHOD_NOT_ALLOWED;
72-
let json = warp::reply::json(&ErrorMessage {
73-
code: code.as_u16(),
74-
message: "oops, you aren't allowed to use this method.".into(),
75-
});
76-
Ok(warp::reply::with_status(json, code))
77-
} else {
78-
// Could be a NOT_FOUND, or any other internal error... here we just
79-
// let warp use its default rendering.
80-
Err(err)
81-
}
61+
let json = warp::reply::json(&ErrorMessage {
62+
code: code.as_u16(),
63+
message: msg,
64+
});
65+
Ok(warp::reply::with_status(json, code))
66+
} else if let Some(_) = err.find_cause::<warp::reject::MethodNotAllowed>() {
67+
// We can handle a specific error, here METHOD_NOT_ALLOWED,
68+
// and render it however we want
69+
let code = StatusCode::METHOD_NOT_ALLOWED;
70+
let json = warp::reply::json(&ErrorMessage {
71+
code: code.as_u16(),
72+
message: "oops, you aren't allowed to use this method.".into(),
73+
});
74+
Ok(warp::reply::with_status(json, code))
75+
} else {
76+
// Could be a NOT_FOUND, or any other internal error... here we just
77+
// let warp use its default rendering.
78+
Err(err)
79+
}
80+
};
81+
futures::future::ready(err)
8282
}

examples/file.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#![deny(warnings)]
2-
extern crate pretty_env_logger;
3-
extern crate warp;
42

53
use warp::Filter;
64

7-
fn main() {
5+
#[tokio::main]
6+
async fn main() {
87
pretty_env_logger::init();
98

109
let readme = warp::get2()
@@ -18,5 +17,5 @@ fn main() {
1817
// GET /ex/... => ./examples/..
1918
let routes = readme.or(examples);
2019

21-
warp::serve(routes).run(([127, 0, 0, 1], 3030));
20+
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
2221
}

examples/futures.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#![deny(warnings)]
2-
extern crate tokio;
3-
extern crate warp;
42

53
use std::str::FromStr;
64
use std::time::{Duration, Instant};
7-
use tokio::timer::Delay;
8-
use warp::{Filter, Future};
5+
use tokio::timer::delay;
6+
use warp::Filter;
97

108
/// A newtype to enforce our maximum allowed seconds.
119
struct Seconds(u64);
@@ -23,21 +21,15 @@ impl FromStr for Seconds {
2321
}
2422
}
2523

26-
fn main() {
24+
#[tokio::main]
25+
async fn main() {
2726
// Match `/:u32`...
2827
let routes = warp::path::param()
2928
// and_then create a `Future` that will simply wait N seconds...
30-
.and_then(|Seconds(seconds)| {
31-
Delay::new(Instant::now() + Duration::from_secs(seconds))
32-
// return the number of seconds again...
33-
.map(move |()| seconds)
34-
// An error from `Delay` means a big problem with the server...
35-
.map_err(|timer_err| {
36-
eprintln!("timer error: {}", timer_err);
37-
warp::reject::custom(timer_err)
38-
})
39-
})
40-
.map(|seconds| format!("I waited {} seconds!", seconds));
29+
.and_then(|Seconds(seconds): Seconds| async move {
30+
delay(Instant::now() + Duration::from_secs(seconds)).await;
31+
Ok::<String, warp::Rejection>(format!("I waited {} seconds!", seconds))
32+
});
4133

42-
warp::serve(routes).run(([127, 0, 0, 1], 3030));
34+
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
4335
}

examples/handlebars_template.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
#![deny(warnings)]
2-
extern crate handlebars;
3-
extern crate hyper;
4-
extern crate warp;
5-
#[macro_use]
6-
extern crate serde_json;
7-
extern crate serde;
8-
92
use std::error::Error;
103
use std::sync::Arc;
114

125
use handlebars::Handlebars;
6+
use serde_json::json;
137
use serde::Serialize;
148
use warp::Filter;
159

@@ -26,7 +20,8 @@ where
2620
.unwrap_or_else(|err| err.description().to_owned())
2721
}
2822

29-
fn main() {
23+
#[tokio::main]
24+
async fn main() {
3025
let template = "<!DOCTYPE html>
3126
<html>
3227
<head>
@@ -58,5 +53,5 @@ fn main() {
5853
})
5954
.map(handlebars);
6055

61-
warp::serve(route).run(([127, 0, 0, 1], 3030));
56+
warp::serve(route).run(([127, 0, 0, 1], 3030)).await;
6257
}

examples/headers.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#![deny(warnings)]
2-
extern crate pretty_env_logger;
3-
extern crate warp;
4-
52
use std::net::SocketAddr;
63
use warp::Filter;
74

@@ -11,7 +8,8 @@ use warp::Filter;
118
/// - `Accept` is exactly `*/*`
129
///
1310
/// If these conditions don't match, a 404 is returned.
14-
fn main() {
11+
#[tokio::main]
12+
async fn main() {
1513
pretty_env_logger::init();
1614

1715
// For this example, we assume no DNS was used,
@@ -25,5 +23,5 @@ fn main() {
2523
.and(accept_stars)
2624
.map(|addr| format!("accepting stars on {}", addr));
2725

28-
warp::serve(routes).run(([127, 0, 0, 1], 3030));
26+
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
2927
}

examples/hello.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#![deny(warnings)]
2-
extern crate warp;
3-
42
use warp::Filter;
53

6-
fn main() {
4+
#[tokio::main]
5+
async fn main() {
76
// Match any request and return hello world!
87
let routes = warp::any().map(|| "Hello, World!");
98

10-
warp::serve(routes).run(([127, 0, 0, 1], 3030));
9+
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
1110
}

examples/returning.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate warp;
2-
31
use warp::{filters::BoxedFilter, Filter, Rejection, Reply};
42

53
// Option 1: BoxedFilter
@@ -15,7 +13,8 @@ pub fn index_filter() -> impl Filter<Extract = (&'static str,), Error = Rejectio
1513
warp::path::end().map(|| "Index page")
1614
}
1715

18-
pub fn main() {
16+
#[tokio::main]
17+
async fn main() {
1918
let routes = index_filter().or(assets_filter());
20-
warp::serve(routes).run(([127, 0, 0, 1], 3030));
19+
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
2120
}

0 commit comments

Comments
 (0)