Skip to content

Commit bf10ffb

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 156f05b commit bf10ffb

File tree

90 files changed

+1009
-899
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

+1009
-899
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, <=0.17", 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, <=0.3.0-rc.17"
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;
@@ -129,7 +128,7 @@ mod key {
129128
/// 32`.
130129
pub enum Cookies<'a> {
131130
#[doc(hidden)]
132-
Jarred(RefMut<'a, CookieJar>, &'a Key),
131+
Jarred(CookieJar, &'a Key, Box<dyn FnOnce(CookieJar) + Send + 'a>),
133132
#[doc(hidden)]
134133
Empty(CookieJar)
135134
}
@@ -138,8 +137,8 @@ impl<'a> Cookies<'a> {
138137
/// WARNING: This is unstable! Do not use this method outside of Rocket!
139138
#[inline]
140139
#[doc(hidden)]
141-
pub fn new(jar: RefMut<'a, CookieJar>, key: &'a Key) -> Cookies<'a> {
142-
Cookies::Jarred(jar, key)
140+
pub fn new<F: FnOnce(CookieJar) + Send + 'a>(jar: CookieJar, key: &'a Key, on_drop: F) -> Cookies<'a> {
141+
Cookies::Jarred(jar, key, Box::new(on_drop))
143142
}
144143

145144
/// WARNING: This is unstable! Do not use this method outside of Rocket!
@@ -161,7 +160,7 @@ impl<'a> Cookies<'a> {
161160
#[inline]
162161
#[doc(hidden)]
163162
pub fn add_original(&mut self, cookie: Cookie<'static>) {
164-
if let Cookies::Jarred(ref mut jar, _) = *self {
163+
if let Cookies::Jarred(ref mut jar, _, _) = *self {
165164
jar.add_original(cookie)
166165
}
167166
}
@@ -181,7 +180,7 @@ impl<'a> Cookies<'a> {
181180
/// ```
182181
pub fn get(&self, name: &str) -> Option<&Cookie<'static>> {
183182
match *self {
184-
Cookies::Jarred(ref jar, _) => jar.get(name),
183+
Cookies::Jarred(ref jar, _, _) => jar.get(name),
185184
Cookies::Empty(_) => None
186185
}
187186
}
@@ -206,7 +205,7 @@ impl<'a> Cookies<'a> {
206205
/// }
207206
/// ```
208207
pub fn add(&mut self, cookie: Cookie<'static>) {
209-
if let Cookies::Jarred(ref mut jar, _) = *self {
208+
if let Cookies::Jarred(ref mut jar, _, _) = *self {
210209
jar.add(cookie)
211210
}
212211
}
@@ -232,7 +231,7 @@ impl<'a> Cookies<'a> {
232231
/// }
233232
/// ```
234233
pub fn remove(&mut self, cookie: Cookie<'static>) {
235-
if let Cookies::Jarred(ref mut jar, _) = *self {
234+
if let Cookies::Jarred(ref mut jar, _, _) = *self {
236235
jar.remove(cookie)
237236
}
238237
}
@@ -243,7 +242,7 @@ impl<'a> Cookies<'a> {
243242
#[doc(hidden)]
244243
pub fn reset_delta(&mut self) {
245244
match *self {
246-
Cookies::Jarred(ref mut jar, _) => jar.reset_delta(),
245+
Cookies::Jarred(ref mut jar, ..) => jar.reset_delta(),
247246
Cookies::Empty(ref mut jar) => jar.reset_delta()
248247
}
249248
}
@@ -264,7 +263,7 @@ impl<'a> Cookies<'a> {
264263
/// ```
265264
pub fn iter(&self) -> impl Iterator<Item=&Cookie<'static>> {
266265
match *self {
267-
Cookies::Jarred(ref jar, _) => jar.iter(),
266+
Cookies::Jarred(ref jar, _, _) => jar.iter(),
268267
Cookies::Empty(ref jar) => jar.iter()
269268
}
270269
}
@@ -274,12 +273,22 @@ impl<'a> Cookies<'a> {
274273
#[doc(hidden)]
275274
pub fn delta(&self) -> Delta<'_> {
276275
match *self {
277-
Cookies::Jarred(ref jar, _) => jar.delta(),
276+
Cookies::Jarred(ref jar, _, _) => jar.delta(),
278277
Cookies::Empty(ref jar) => jar.delta()
279278
}
280279
}
281280
}
282281

282+
impl<'a> Drop for Cookies<'a> {
283+
fn drop(&mut self) {
284+
if let Cookies::Jarred(ref mut jar, _, ref mut on_drop) = *self {
285+
let jar = std::mem::replace(jar, CookieJar::new());
286+
let on_drop = std::mem::replace(on_drop, Box::new(|_| {}));
287+
on_drop(jar);
288+
}
289+
}
290+
}
291+
283292
#[cfg(feature = "private-cookies")]
284293
impl Cookies<'_> {
285294
/// Returns a reference to the `Cookie` inside this collection with the name
@@ -302,7 +311,7 @@ impl Cookies<'_> {
302311
/// ```
303312
pub fn get_private(&mut self, name: &str) -> Option<Cookie<'static>> {
304313
match *self {
305-
Cookies::Jarred(ref mut jar, key) => jar.private(key).get(name),
314+
Cookies::Jarred(ref mut jar, key, _) => jar.private(key).get(name),
306315
Cookies::Empty(_) => None
307316
}
308317
}
@@ -338,7 +347,7 @@ impl Cookies<'_> {
338347
/// }
339348
/// ```
340349
pub fn add_private(&mut self, mut cookie: Cookie<'static>) {
341-
if let Cookies::Jarred(ref mut jar, key) = *self {
350+
if let Cookies::Jarred(ref mut jar, key, _) = *self {
342351
Cookies::set_private_defaults(&mut cookie);
343352
jar.private(key).add(cookie)
344353
}
@@ -348,7 +357,7 @@ impl Cookies<'_> {
348357
/// WARNING: This is unstable! Do not use this method outside of Rocket!
349358
#[doc(hidden)]
350359
pub fn add_original_private(&mut self, mut cookie: Cookie<'static>) {
351-
if let Cookies::Jarred(ref mut jar, key) = *self {
360+
if let Cookies::Jarred(ref mut jar, key, _) = *self {
352361
Cookies::set_private_defaults(&mut cookie);
353362
jar.private(key).add_original(cookie)
354363
}
@@ -402,7 +411,7 @@ impl Cookies<'_> {
402411
/// }
403412
/// ```
404413
pub fn remove_private(&mut self, mut cookie: Cookie<'static>) {
405-
if let Cookies::Jarred(ref mut jar, key) = *self {
414+
if let Cookies::Jarred(ref mut jar, key, _) = *self {
406415
if cookie.path().is_none() {
407416
cookie.set_path("/");
408417
}
@@ -415,7 +424,7 @@ impl Cookies<'_> {
415424
impl fmt::Debug for Cookies<'_> {
416425
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
417426
match *self {
418-
Cookies::Jarred(ref jar, _) => jar.fmt(f),
427+
Cookies::Jarred(ref jar, _, _) => jar.fmt(f),
419428
Cookies::Empty(ref jar) => jar.fmt(f)
420429
}
421430
}

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)