Remove boxed futures in server, use async fn.

This commit is contained in:
Sergio Benitez 2021-03-10 16:31:32 -08:00
parent 88cb6ec4d1
commit 191b93498e

View File

@ -3,7 +3,7 @@ use std::sync::Arc;
use std::panic::AssertUnwindSafe; use std::panic::AssertUnwindSafe;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use futures::future::{Future, FutureExt, BoxFuture}; use futures::future::FutureExt;
use tokio::sync::oneshot; use tokio::sync::oneshot;
use yansi::Paint; use yansi::Paint;
@ -221,37 +221,30 @@ impl Rocket {
response response
} }
/// Route the request and process the outcome to eventually get a response. async fn route_and_process<'s, 'r: 's>(
fn route_and_process<'s, 'r: 's>(
&'s self, &'s self,
request: &'r Request<'s>, request: &'r Request<'s>,
data: Data data: Data
) -> impl Future<Output = Response<'r>> + Send + 's { ) -> Response<'r> {
async move {
let mut response = match self.route(request, data).await { let mut response = match self.route(request, data).await {
Outcome::Success(response) => response, Outcome::Success(response) => response,
Outcome::Forward(data) => { Outcome::Forward(data) if request.method() == Method::Head => {
// There was no matching route. Autohandle `HEAD` requests.
if request.method() == Method::Head {
info_!("Autohandling {} request.", Paint::default("HEAD").bold()); info_!("Autohandling {} request.", Paint::default("HEAD").bold());
// Dispatch the request again with Method `GET`. // Dispatch the request again with Method `GET`.
request._set_method(Method::Get); request._set_method(Method::Get);
match self.route(request, data).await {
// Return early so we don't set cookies twice. Outcome::Success(response) => response,
let try_next: BoxFuture<'_, _> = Outcome::Failure(status) => self.handle_error(status, request).await,
Box::pin(self.route_and_process(request, data)); Outcome::Forward(_) => self.handle_error(Status::NotFound, request).await,
return try_next.await;
} else {
// No match was found and it can't be autohandled. 404.
self.handle_error(Status::NotFound, request).await
} }
} }
Outcome::Forward(_) => self.handle_error(Status::NotFound, request).await,
Outcome::Failure(status) => self.handle_error(status, request).await, Outcome::Failure(status) => self.handle_error(status, request).await,
}; };
// Set the cookies. Note that error responses will only include // Set the cookies. Note that error responses will only include cookies
// cookies set by the error handler. See `handle_error` for more. // set by the error handler. See `handle_error` for more.
let delta_jar = request.cookies().take_delta_jar(); let delta_jar = request.cookies().take_delta_jar();
for cookie in delta_jar.delta() { for cookie in delta_jar.delta() {
response.adjoin_header(cookie); response.adjoin_header(cookie);
@ -259,7 +252,6 @@ impl Rocket {
response response
} }
}
/// Tries to find a `Responder` for a given `request`. It does this by /// Tries to find a `Responder` for a given `request`. It does this by
/// routing the request and calling the handler for each matching route /// routing the request and calling the handler for each matching route
@ -267,12 +259,11 @@ impl Rocket {
/// additional routes to try (forward). The corresponding outcome for each /// additional routes to try (forward). The corresponding outcome for each
/// condition is returned. /// condition is returned.
#[inline] #[inline]
fn route<'s, 'r: 's>( async fn route<'s, 'r: 's>(
&'s self, &'s self,
request: &'r Request<'s>, request: &'r Request<'s>,
mut data: Data, mut data: Data,
) -> impl Future<Output = handler::Outcome<'r>> + 's { ) -> handler::Outcome<'r> {
async move {
// Go through the list of matching routes until we fail or succeed. // Go through the list of matching routes until we fail or succeed.
let matches = self.router.route(request); let matches = self.router.route(request);
for route in matches { for route in matches {
@ -298,7 +289,6 @@ impl Rocket {
error_!("No matching routes for {}.", request); error_!("No matching routes for {}.", request);
Outcome::Forward(data) Outcome::Forward(data)
} }
}
/// Invokes the handler with `req` for catcher with status `status`. /// Invokes the handler with `req` for catcher with status `status`.
/// ///