Skip to content

Commit b1518a2

Browse files
jebrosenSergioBenitez
authored andcommitted
Convert core to async and add support for async routes.
Minimum rustc bump required for rust-lang/rust#61775
1 parent d8fac7a commit b1518a2

File tree

90 files changed

+1000
-901
lines changed

Some content is hidden

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

90 files changed

+1000
-901
lines changed

core/codegen/src/attribute/catch.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn _catch(args: TokenStream, input: TokenStream) -> Result<TokenStream> {
5151
let status_code = status.0.code;
5252

5353
// Variables names we'll use and reuse.
54-
define_vars_and_mods!(req, catcher, response, Request, Response);
54+
define_vars_and_mods!(req, catcher, Request, Response, ErrorHandlerFuture);
5555

5656
// Determine the number of parameters that will be passed in.
5757
let (fn_sig, inputs) = match catch.function.sig.inputs.len() {
@@ -84,12 +84,14 @@ pub fn _catch(args: TokenStream, input: TokenStream) -> Result<TokenStream> {
8484

8585
/// Rocket code generated wrapping catch function.
8686
#[doc(hidden)]
87-
#vis fn #generated_fn_name<'_b>(#req: &'_b #Request) -> #response::Result<'_b> {
88-
let __response = #catcher_response;
89-
#Response::build()
90-
.status(#status)
91-
.merge(__response)
92-
.ok()
87+
#vis fn #generated_fn_name<'_b>(#req: &'_b #Request) -> #ErrorHandlerFuture<'_b> {
88+
Box::pin(async move {
89+
let __response = #catcher_response;
90+
#Response::build()
91+
.status(#status)
92+
.merge(__response)
93+
.ok()
94+
})
9395
}
9496

9597
/// Rocket code generated static catcher info.

core/codegen/src/attribute/route.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn data_expr(ident: &syn::Ident, ty: &syn::Type) -> TokenStream2 {
178178
define_vars_and_mods!(req, data, FromData, Outcome, Transform);
179179
let span = ident.span().unstable().join(ty.span()).unwrap().into();
180180
quote_spanned! { span =>
181-
let __transform = <#ty as #FromData>::transform(#req, #data);
181+
let __transform = <#ty as #FromData>::transform(#req, #data).await;
182182

183183
#[allow(unreachable_patterns, unreachable_code)]
184184
let __outcome = match __transform {
@@ -195,7 +195,7 @@ fn data_expr(ident: &syn::Ident, ty: &syn::Type) -> TokenStream2 {
195195
};
196196

197197
#[allow(non_snake_case, unreachable_patterns, unreachable_code)]
198-
let #ident: #ty = match <#ty as #FromData>::from_data(#req, __outcome) {
198+
let #ident: #ty = match <#ty as #FromData>::from_data(#req, __outcome).await {
199199
#Outcome::Success(__d) => __d,
200200
#Outcome::Forward(__d) => return #Outcome::Forward(__d),
201201
#Outcome::Failure((__c, _)) => return #Outcome::Failure(__c),
@@ -369,8 +369,18 @@ fn generate_respond_expr(route: &Route) -> TokenStream2 {
369369
let parameter_names = route.inputs.iter()
370370
.map(|(_, rocket_ident, _)| rocket_ident);
371371

372+
let responder_stmt = if route.function.sig.asyncness.is_some() {
373+
quote_spanned! { ret_span =>
374+
let ___responder = #user_handler_fn_name(#(#parameter_names),*).await;
375+
}
376+
} else {
377+
quote_spanned! { ret_span =>
378+
let ___responder = #user_handler_fn_name(#(#parameter_names),*);
379+
}
380+
};
381+
372382
quote_spanned! { ret_span =>
373-
let ___responder = #user_handler_fn_name(#(#parameter_names),*);
383+
#responder_stmt
374384
#handler::Outcome::from(#req, ___responder)
375385
}
376386
}
@@ -403,7 +413,7 @@ fn codegen_route(route: Route) -> Result<TokenStream> {
403413
}
404414

405415
// Gather everything we need.
406-
define_vars_and_mods!(req, data, handler, Request, Data, StaticRouteInfo);
416+
define_vars_and_mods!(req, data, Request, Data, StaticRouteInfo, HandlerFuture);
407417
let (vis, user_handler_fn) = (&route.function.vis, &route.function);
408418
let user_handler_fn_name = &user_handler_fn.sig.ident;
409419
let generated_fn_name = user_handler_fn_name.prepend(ROUTE_FN_PREFIX);
@@ -424,12 +434,14 @@ fn codegen_route(route: Route) -> Result<TokenStream> {
424434
#vis fn #generated_fn_name<'_b>(
425435
#req: &'_b #Request,
426436
#data: #Data
427-
) -> #handler::Outcome<'_b> {
428-
#(#req_guard_definitions)*
429-
#(#parameter_definitions)*
430-
#data_stmt
431-
432-
#generated_respond_expr
437+
) -> #HandlerFuture<'_b> {
438+
Box::pin(async move {
439+
#(#req_guard_definitions)*
440+
#(#parameter_definitions)*
441+
#data_stmt
442+
443+
#generated_respond_expr
444+
})
433445
}
434446

435447
/// Rocket code generated wrapping URI macro.

core/codegen/src/lib.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(proc_macro_diagnostic, proc_macro_span)]
2+
#![feature(async_await)]
23
#![recursion_limit="128"]
34

45
#![doc(html_root_url = "https://api.rocket.rs/v0.5")]
@@ -96,12 +97,8 @@ vars_and_mods! {
9697
Data => rocket::Data,
9798
StaticRouteInfo => rocket::StaticRouteInfo,
9899
SmallVec => rocket::http::private::SmallVec,
99-
_Option => ::std::option::Option,
100-
_Result => ::std::result::Result,
101-
_Some => ::std::option::Option::Some,
102-
_None => ::std::option::Option::None,
103-
_Ok => ::std::result::Result::Ok,
104-
_Err => ::std::result::Result::Err,
100+
HandlerFuture => rocket::handler::HandlerFuture,
101+
ErrorHandlerFuture => rocket::handler::ErrorHandlerFuture,
105102
}
106103

107104
macro_rules! define_vars_and_mods {

core/codegen/tests/route.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(proc_macro_hygiene)]
1+
#![feature(proc_macro_hygiene, async_await)]
22

33
// Rocket sometimes generates mangled identifiers that activate the
44
// non_snake_case lint. We deny the lint in this test to ensure that

core/http/Cargo.toml

+3-8
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,22 @@ edition = "2018"
1616

1717
[features]
1818
default = []
19-
tls = ["rustls", "hyper-sync-rustls"]
19+
tls = ["tokio-rustls"]
2020
private-cookies = ["cookie/private", "cookie/key-expansion"]
2121

2222
[dependencies]
2323
smallvec = "1.0"
2424
percent-encoding = "1"
25-
hyper = { version = "0.12.31", default-features = false, features = ["tokio"] }
25+
hyper = { version = "0.12.31", default-features = false, features = ["runtime"] }
2626
http = "0.1.17"
2727
mime = "0.3.13"
2828
time = "0.2.11"
2929
indexmap = "1.0"
30-
rustls = { version = "0.16", optional = true }
3130
state = "0.4"
31+
tokio-rustls = { version = "0.10.3", optional = true }
3232
cookie = { version = "0.14.0", features = ["percent-encode"] }
3333
pear = "0.1"
3434
unicode-xid = "0.2"
3535

36-
[dependencies.hyper-sync-rustls]
37-
version = "=0.3.0-rc.6"
38-
features = ["server"]
39-
optional = true
40-
4136
[dev-dependencies]
4237
rocket = { version = "0.5.0-dev", path = "../lib" }

core/http/src/cookies.rs

+25-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::fmt;
2-
use std::cell::RefMut;
32

43
use crate::Header;
54
use cookie::Delta;
@@ -128,7 +127,7 @@ mod key {
128127
/// 32`.
129128
pub enum Cookies<'a> {
130129
#[doc(hidden)]
131-
Jarred(RefMut<'a, CookieJar>, &'a Key),
130+
Jarred(CookieJar, &'a Key, Box<dyn FnOnce(CookieJar) + Send + 'a>),
132131
#[doc(hidden)]
133132
Empty(CookieJar)
134133
}
@@ -137,8 +136,8 @@ impl<'a> Cookies<'a> {
137136
/// WARNING: This is unstable! Do not use this method outside of Rocket!
138137
#[inline]
139138
#[doc(hidden)]
140-
pub fn new(jar: RefMut<'a, CookieJar>, key: &'a Key) -> Cookies<'a> {
141-
Cookies::Jarred(jar, key)
139+
pub fn new<F: FnOnce(CookieJar) + Send + 'a>(jar: CookieJar, key: &'a Key, on_drop: F) -> Cookies<'a> {
140+
Cookies::Jarred(jar, key, Box::new(on_drop))
142141
}
143142

144143
/// WARNING: This is unstable! Do not use this method outside of Rocket!
@@ -160,7 +159,7 @@ impl<'a> Cookies<'a> {
160159
#[inline]
161160
#[doc(hidden)]
162161
pub fn add_original(&mut self, cookie: Cookie<'static>) {
163-
if let Cookies::Jarred(ref mut jar, _) = *self {
162+
if let Cookies::Jarred(ref mut jar, _, _) = *self {
164163
jar.add_original(cookie)
165164
}
166165
}
@@ -180,7 +179,7 @@ impl<'a> Cookies<'a> {
180179
/// ```
181180
pub fn get(&self, name: &str) -> Option<&Cookie<'static>> {
182181
match *self {
183-
Cookies::Jarred(ref jar, _) => jar.get(name),
182+
Cookies::Jarred(ref jar, _, _) => jar.get(name),
184183
Cookies::Empty(_) => None
185184
}
186185
}
@@ -205,7 +204,7 @@ impl<'a> Cookies<'a> {
205204
/// }
206205
/// ```
207206
pub fn add(&mut self, cookie: Cookie<'static>) {
208-
if let Cookies::Jarred(ref mut jar, _) = *self {
207+
if let Cookies::Jarred(ref mut jar, _, _) = *self {
209208
jar.add(cookie)
210209
}
211210
}
@@ -231,7 +230,7 @@ impl<'a> Cookies<'a> {
231230
/// }
232231
/// ```
233232
pub fn remove(&mut self, cookie: Cookie<'static>) {
234-
if let Cookies::Jarred(ref mut jar, _) = *self {
233+
if let Cookies::Jarred(ref mut jar, _, _) = *self {
235234
jar.remove(cookie)
236235
}
237236
}
@@ -242,7 +241,7 @@ impl<'a> Cookies<'a> {
242241
#[doc(hidden)]
243242
pub fn reset_delta(&mut self) {
244243
match *self {
245-
Cookies::Jarred(ref mut jar, _) => jar.reset_delta(),
244+
Cookies::Jarred(ref mut jar, ..) => jar.reset_delta(),
246245
Cookies::Empty(ref mut jar) => jar.reset_delta()
247246
}
248247
}
@@ -263,7 +262,7 @@ impl<'a> Cookies<'a> {
263262
/// ```
264263
pub fn iter(&self) -> impl Iterator<Item=&Cookie<'static>> {
265264
match *self {
266-
Cookies::Jarred(ref jar, _) => jar.iter(),
265+
Cookies::Jarred(ref jar, _, _) => jar.iter(),
267266
Cookies::Empty(ref jar) => jar.iter()
268267
}
269268
}
@@ -273,12 +272,22 @@ impl<'a> Cookies<'a> {
273272
#[doc(hidden)]
274273
pub fn delta(&self) -> Delta<'_> {
275274
match *self {
276-
Cookies::Jarred(ref jar, _) => jar.delta(),
275+
Cookies::Jarred(ref jar, _, _) => jar.delta(),
277276
Cookies::Empty(ref jar) => jar.delta()
278277
}
279278
}
280279
}
281280

281+
impl<'a> Drop for Cookies<'a> {
282+
fn drop(&mut self) {
283+
if let Cookies::Jarred(ref mut jar, _, ref mut on_drop) = *self {
284+
let jar = std::mem::replace(jar, CookieJar::new());
285+
let on_drop = std::mem::replace(on_drop, Box::new(|_| {}));
286+
on_drop(jar);
287+
}
288+
}
289+
}
290+
282291
#[cfg(feature = "private-cookies")]
283292
impl Cookies<'_> {
284293
/// Returns a reference to the `Cookie` inside this collection with the name
@@ -301,7 +310,7 @@ impl Cookies<'_> {
301310
/// ```
302311
pub fn get_private(&mut self, name: &str) -> Option<Cookie<'static>> {
303312
match *self {
304-
Cookies::Jarred(ref mut jar, key) => jar.private(key).get(name),
313+
Cookies::Jarred(ref mut jar, key, _) => jar.private(key).get(name),
305314
Cookies::Empty(_) => None
306315
}
307316
}
@@ -337,7 +346,7 @@ impl Cookies<'_> {
337346
/// }
338347
/// ```
339348
pub fn add_private(&mut self, mut cookie: Cookie<'static>) {
340-
if let Cookies::Jarred(ref mut jar, key) = *self {
349+
if let Cookies::Jarred(ref mut jar, key, _) = *self {
341350
Cookies::set_private_defaults(&mut cookie);
342351
jar.private(key).add(cookie)
343352
}
@@ -347,7 +356,7 @@ impl Cookies<'_> {
347356
/// WARNING: This is unstable! Do not use this method outside of Rocket!
348357
#[doc(hidden)]
349358
pub fn add_original_private(&mut self, mut cookie: Cookie<'static>) {
350-
if let Cookies::Jarred(ref mut jar, key) = *self {
359+
if let Cookies::Jarred(ref mut jar, key, _) = *self {
351360
Cookies::set_private_defaults(&mut cookie);
352361
jar.private(key).add_original(cookie)
353362
}
@@ -401,7 +410,7 @@ impl Cookies<'_> {
401410
/// }
402411
/// ```
403412
pub fn remove_private(&mut self, mut cookie: Cookie<'static>) {
404-
if let Cookies::Jarred(ref mut jar, key) = *self {
413+
if let Cookies::Jarred(ref mut jar, key, _) = *self {
405414
if cookie.path().is_none() {
406415
cookie.set_path("/");
407416
}
@@ -414,7 +423,7 @@ impl Cookies<'_> {
414423
impl fmt::Debug for Cookies<'_> {
415424
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
416425
match *self {
417-
Cookies::Jarred(ref jar, _) => jar.fmt(f),
426+
Cookies::Jarred(ref jar, _, _) => jar.fmt(f),
418427
Cookies::Empty(ref jar) => jar.fmt(f)
419428
}
420429
}

core/http/src/hyper.rs

+21-43
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,44 @@
44
//! These types will, with certainty, be removed with time, but they reside here
55
//! while necessary.
66
7-
#[doc(hidden)] pub use hyper::{Body, Request, Response};
7+
#[doc(hidden)] pub use hyper::{Body, Request, Response, Server};
88
#[doc(hidden)] pub use hyper::body::Payload as Payload;
99
#[doc(hidden)] pub use hyper::error::Error;
10-
#[doc(hidden)] pub use hyper::server::Server;
11-
#[doc(hidden)] pub use hyper::service::{MakeService, Service};
10+
#[doc(hidden)] pub use hyper::service::{make_service_fn, MakeService, Service};
11+
#[doc(hidden)] pub use hyper::server::conn::{AddrIncoming, AddrStream};
1212

1313
#[doc(hidden)] pub use hyper::Chunk;
14+
#[doc(hidden)] pub use http::header::HeaderMap;
1415
#[doc(hidden)] pub use http::header::HeaderName as HeaderName;
1516
#[doc(hidden)] pub use http::header::HeaderValue as HeaderValue;
1617
#[doc(hidden)] pub use http::method::Method;
17-
#[doc(hidden)] pub use http::request::Parts;
18+
#[doc(hidden)] pub use http::request::Parts as RequestParts;
19+
#[doc(hidden)] pub use http::response::Builder as ResponseBuilder;
1820
#[doc(hidden)] pub use http::status::StatusCode;
1921
#[doc(hidden)] pub use http::uri::Uri;
2022

21-
/// Type alias to `hyper::Response<'a, hyper::net::Fresh>`.
22-
// TODO #[doc(hidden)] pub type FreshResponse<'a> = self::Response<'a, self::net::Fresh>;
23-
24-
/// Reexported Hyper header types.
23+
/// Reexported http header types.
2524
pub mod header {
26-
use crate::Header;
27-
28-
macro_rules! import_hyper_items {
29-
($($item:ident),*) => ($(pub use hyper::header::$item;)*)
30-
}
31-
32-
macro_rules! import_hyper_headers {
25+
macro_rules! import_http_headers {
3326
($($name:ident),*) => ($(
3427
pub use http::header::$name as $name;
3528
)*)
3629
}
3730

38-
// import_hyper_items! {
39-
// Accept, AcceptCharset, AcceptEncoding, AcceptLanguage, AcceptRanges,
40-
// AccessControlAllowCredentials, AccessControlAllowHeaders,
41-
// AccessControlAllowMethods, AccessControlExposeHeaders,
42-
// AccessControlMaxAge, AccessControlRequestHeaders,
43-
// AccessControlRequestMethod, Allow, Authorization, Basic, Bearer,
44-
// CacheControl, Connection, ContentDisposition, ContentEncoding,
45-
// ContentLanguage, ContentLength, ContentRange, ContentType, Date, ETag,
46-
// EntityTag, Expires, From, Headers, Host, HttpDate, IfModifiedSince,
47-
// IfUnmodifiedSince, LastModified, Location, Origin, Prefer,
48-
// PreferenceApplied, Protocol, Quality, QualityItem, Referer,
49-
// StrictTransportSecurity, TransferEncoding, Upgrade, UserAgent,
50-
// AccessControlAllowOrigin, ByteRangeSpec, CacheDirective, Charset,
51-
// ConnectionOption, ContentRangeSpec, DispositionParam, DispositionType,
52-
// Encoding, Expect, IfMatch, IfNoneMatch, IfRange, Pragma, Preference,
53-
// ProtocolName, Range, RangeUnit, ReferrerPolicy, Vary, Scheme, q, qitem
54-
// }
55-
//
56-
import_hyper_headers! {
57-
ACCEPT, ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS,
31+
import_http_headers! {
32+
ACCEPT, ACCEPT_CHARSET, ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES,
33+
ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS,
5834
ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
5935
ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE,
60-
ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, ACCEPT_CHARSET,
61-
ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES, ALLOW, CACHE_CONTROL,
62-
CONNECTION, CONTENT_DISPOSITION, CONTENT_ENCODING, CONTENT_LANGUAGE,
63-
CONTENT_LENGTH, CONTENT_RANGE, DATE, ETAG, EXPECT, EXPIRES, HOST, IF_MATCH,
64-
IF_MODIFIED_SINCE, IF_NONE_MATCH, IF_RANGE, IF_UNMODIFIED_SINCE, LAST_MODIFIED,
65-
LOCATION, ORIGIN, PRAGMA, RANGE, REFERER,
66-
REFERRER_POLICY, STRICT_TRANSPORT_SECURITY, TRANSFER_ENCODING, UPGRADE,
67-
USER_AGENT, VARY
36+
ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, ALLOW,
37+
AUTHORIZATION, CACHE_CONTROL, CONNECTION, CONTENT_DISPOSITION,
38+
CONTENT_ENCODING, CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_LOCATION,
39+
CONTENT_RANGE, CONTENT_SECURITY_POLICY,
40+
CONTENT_SECURITY_POLICY_REPORT_ONLY, CONTENT_TYPE, DATE, ETAG, EXPECT,
41+
EXPIRES, FORWARDED, FROM, HOST, IF_MATCH, IF_MODIFIED_SINCE,
42+
IF_NONE_MATCH, IF_RANGE, IF_UNMODIFIED_SINCE, LAST_MODIFIED, LINK,
43+
LOCATION, ORIGIN, PRAGMA, RANGE, REFERER, REFERRER_POLICY, REFRESH,
44+
STRICT_TRANSPORT_SECURITY, TE, TRANSFER_ENCODING, UPGRADE, USER_AGENT,
45+
VARY
6846
}
6947
}

0 commit comments

Comments
 (0)