Use 'futures::future::BoxFuture' instead of 'Pin<Box<...>>' for readability.

This commit is contained in:
Jacob Pratt 2019-09-06 13:14:27 -04:00 committed by Sergio Benitez
parent bd929ef617
commit c7ecfc69c3
10 changed files with 24 additions and 27 deletions

View File

@ -154,7 +154,7 @@ macro_rules! default_catchers {
let mut map = HashMap::new(); let mut map = HashMap::new();
$( $(
fn $fn_name<'r>(req: &'r Request<'_>) -> std::pin::Pin<Box<dyn std::future::Future<Output = response::Result<'r>> + Send + 'r>> { fn $fn_name<'r>(req: &'r Request<'_>) -> futures::future::BoxFuture<'r, response::Result<'r>> {
status::Custom(Status::from_code($code).unwrap(), status::Custom(Status::from_code($code).unwrap(),
content::Html(error_page_template!($code, $name, $description)) content::Html(error_page_template!($code, $name, $description))
).respond_to(req) ).respond_to(req)

View File

@ -1,11 +1,11 @@
use futures::future::Future; use futures::future::BoxFuture;
use crate::{Request, Data}; use crate::{Request, Data};
use crate::handler::{Outcome, ErrorHandler}; use crate::handler::{Outcome, ErrorHandler};
use crate::http::{Method, MediaType}; use crate::http::{Method, MediaType};
/// Type of a static handler, which users annotate with Rocket's attribute. /// Type of a static handler, which users annotate with Rocket's attribute.
pub type StaticHandler = for<'r> fn(&'r Request<'_>, Data) -> std::pin::Pin<Box<dyn Future<Output = Outcome<'r>> + Send + 'r>>; pub type StaticHandler = for<'r> fn(&'r Request<'_>, Data) -> BoxFuture<'r, Outcome<'r>>;
/// Information generated by the `route` attribute during codegen. /// Information generated by the `route` attribute during codegen.
pub struct StaticRouteInfo { pub struct StaticRouteInfo {

View File

@ -1,7 +1,6 @@
use std::borrow::Borrow; use std::borrow::Borrow;
use std::pin::Pin;
use futures::future::{ready, Future, FutureExt}; use futures::future::{ready, FutureExt, BoxFuture};
use futures::io::AsyncReadExt; use futures::io::AsyncReadExt;
use crate::outcome::{self, IntoOutcome}; use crate::outcome::{self, IntoOutcome};
@ -112,8 +111,8 @@ pub type Transformed<'a, T> =
Outcome<&'a <T as FromData<'a>>::Borrowed, <T as FromData<'a>>::Error> Outcome<&'a <T as FromData<'a>>::Borrowed, <T as FromData<'a>>::Error>
>; >;
pub type TransformFuture<'a, T, E> = Pin<Box<dyn Future<Output = Transform<Outcome<T, E>>> + Send + 'a>>; pub type TransformFuture<'a, T, E> = BoxFuture<'a, Transform<Outcome<T, E>>>;
pub type FromDataFuture<'a, T, E> = Pin<Box<dyn Future<Output = Outcome<T, E>> + Send + 'a>>; pub type FromDataFuture<'a, T, E> = BoxFuture<'a, Outcome<T, E>>;
/// Trait implemented by data guards to derive a value from request body data. /// Trait implemented by data guards to derive a value from request body data.
/// ///

View File

@ -2,7 +2,7 @@ use std::io;
use std::pin::Pin; use std::pin::Pin;
use futures::io::{AsyncRead, AsyncReadExt as _}; use futures::io::{AsyncRead, AsyncReadExt as _};
use futures::future::{Future}; use futures::future::BoxFuture;
use futures::stream::Stream; use futures::stream::Stream;
use futures::task::{Poll, Context}; use futures::task::{Poll, Context};
@ -73,7 +73,7 @@ pub trait AsyncReadExt: AsyncRead {
} }
// TODO.async: Verify correctness of this implementation. // TODO.async: Verify correctness of this implementation.
fn read_max<'a>(&'a mut self, mut buf: &'a mut [u8]) -> Pin<Box<dyn Future<Output=io::Result<usize>> + Send + '_>> fn read_max<'a>(&'a mut self, mut buf: &'a mut [u8]) -> BoxFuture<'_, io::Result<usize>>
where Self: Send + Unpin where Self: Send + Unpin
{ {
Box::pin(async move { Box::pin(async move {

View File

@ -1,7 +1,7 @@
use std::future::Future;
use std::pin::Pin;
use std::sync::Mutex; use std::sync::Mutex;
use futures::future::BoxFuture;
use crate::{Rocket, Request, Response, Data}; use crate::{Rocket, Request, Response, Data};
use crate::fairing::{Fairing, Kind, Info}; use crate::fairing::{Fairing, Kind, Info};
@ -51,7 +51,7 @@ enum AdHocKind {
Request(Box<dyn Fn(&mut Request<'_>, &Data) + Send + Sync + 'static>), Request(Box<dyn Fn(&mut Request<'_>, &Data) + Send + Sync + 'static>),
/// An ad-hoc **response** fairing. Called when a response is ready to be /// An ad-hoc **response** fairing. Called when a response is ready to be
/// sent to a client. /// sent to a client.
Response(Box<dyn for<'a, 'r> Fn(&'a Request<'r>, &'a mut Response<'r>) -> Pin<Box<dyn Future<Output=()> + Send + 'a>> + Send + Sync + 'static>), Response(Box<dyn for<'a, 'r> Fn(&'a Request<'r>, &'a mut Response<'r>) -> BoxFuture<'a, ()> + Send + Sync + 'static>),
} }
impl AdHoc { impl AdHoc {
@ -128,7 +128,7 @@ impl AdHoc {
/// }); /// });
/// ``` /// ```
pub fn on_response<F>(name: &'static str, f: F) -> AdHoc pub fn on_response<F>(name: &'static str, f: F) -> AdHoc
where F: for<'a, 'r> Fn(&'a Request<'r>, &'a mut Response<'r>) -> Pin<Box<dyn Future<Output=()> + Send + 'a>> + Send + Sync + 'static where F: for<'a, 'r> Fn(&'a Request<'r>, &'a mut Response<'r>) -> BoxFuture<'a, ()> + Send + Sync + 'static
{ {
AdHoc { name, kind: AdHocKind::Response(Box::new(f)) } AdHoc { name, kind: AdHocKind::Response(Box::new(f)) }
} }
@ -170,7 +170,7 @@ impl Fairing for AdHoc {
} }
} }
fn on_response<'a, 'r>(&'a self, request: &'a Request<'r>, response: &'a mut Response<'r>) -> Pin<Box<dyn Future<Output=()> + Send + 'a>> { fn on_response<'a, 'r>(&'a self, request: &'a Request<'r>, response: &'a mut Response<'r>) -> BoxFuture<'a, ()> {
if let AdHocKind::Response(ref callback) = self.kind { if let AdHocKind::Response(ref callback) = self.kind {
callback(request, response) callback(request, response)
} else { } else {

View File

@ -47,8 +47,7 @@
//! of other `Fairings` are not jeopardized. For instance, unless it is made //! of other `Fairings` are not jeopardized. For instance, unless it is made
//! abundantly clear, a fairing should not rewrite every request. //! abundantly clear, a fairing should not rewrite every request.
use std::pin::Pin; use futures::future::BoxFuture;
use std::future::Future;
use crate::{Rocket, Request, Response, Data}; use crate::{Rocket, Request, Response, Data};
@ -419,7 +418,7 @@ pub trait Fairing: Send + Sync + 'static {
/// ///
/// The default implementation of this method does nothing. /// The default implementation of this method does nothing.
#[allow(unused_variables)] #[allow(unused_variables)]
fn on_response<'a, 'r>(&'a self, request: &'a Request<'r>, response: &'a mut Response<'r>) -> Pin<Box<dyn Future<Output=()> + Send + 'a>> { fn on_response<'a, 'r>(&'a self, request: &'a Request<'r>, response: &'a mut Response<'r>) -> BoxFuture<'a, ()> {
Box::pin(async { }) Box::pin(async { })
} }
} }
@ -446,7 +445,7 @@ impl<T: Fairing> Fairing for std::sync::Arc<T> {
} }
#[inline] #[inline]
fn on_response<'a, 'r>(&'a self, request: &'a Request<'r>, response: &'a mut Response<'r>) -> Pin<Box<dyn Future<Output=()> + Send + 'a>> { fn on_response<'a, 'r>(&'a self, request: &'a Request<'r>, response: &'a mut Response<'r>) -> BoxFuture<'a, ()> {
(self as &T).on_response(request, response) (self as &T).on_response(request, response)
} }
} }

View File

@ -1,6 +1,6 @@
//! Types and traits for request and error handlers and their return values. //! Types and traits for request and error handlers and their return values.
use futures::future::Future; use futures::future::BoxFuture;
use crate::data::Data; use crate::data::Data;
use crate::request::Request; use crate::request::Request;
@ -12,7 +12,7 @@ use crate::outcome;
pub type Outcome<'r> = outcome::Outcome<Response<'r>, Status, Data>; pub type Outcome<'r> = outcome::Outcome<Response<'r>, Status, Data>;
/// Type alias for the unwieldy `Handler` return type /// Type alias for the unwieldy `Handler` return type
pub type HandlerFuture<'r> = std::pin::Pin<Box<dyn Future<Output = Outcome<'r>> + Send + 'r>>; pub type HandlerFuture<'r> = BoxFuture<'r, Outcome<'r>>;
/// Trait implemented by types that can handle requests. /// Trait implemented by types that can handle requests.
/// ///
@ -186,7 +186,7 @@ impl<F: Clone + Sync + Send + 'static> Handler for F
/// The type of an error handler. /// The type of an error handler.
pub type ErrorHandler = for<'r> fn(&'r Request<'_>) -> ErrorHandlerFuture<'r>; pub type ErrorHandler = for<'r> fn(&'r Request<'_>) -> ErrorHandlerFuture<'r>;
pub type ErrorHandlerFuture<'r> = std::pin::Pin<Box<dyn Future<Output = response::Result<'r>> + Send + 'r>>; pub type ErrorHandlerFuture<'r> = BoxFuture<'r, response::Result<'r>>;
impl<'r> Outcome<'r> { impl<'r> Outcome<'r> {
/// Return the `Outcome` of response to `req` from `responder`. /// Return the `Outcome` of response to `req` from `responder`.

View File

@ -49,4 +49,4 @@ pub use self::debug::Debug;
/// Type alias for the `Result` of a `Responder::respond` call. /// Type alias for the `Result` of a `Responder::respond` call.
pub type Result<'r> = std::result::Result<self::Response<'r>, crate::http::Status>; pub type Result<'r> = std::result::Result<self::Response<'r>, crate::http::Status>;
/// Type alias for the `Result` of a `Responder::respond` call. /// Type alias for the `Result` of a `Responder::respond` call.
pub type ResultFuture<'r> = std::pin::Pin<Box<dyn std::future::Future<Output=Result<'r>> + Send + 'r>>; pub type ResultFuture<'r> = futures::future::BoxFuture<'r, Result<'r>>;

View File

@ -6,9 +6,8 @@ use std::mem;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use std::pin::Pin;
use futures::future::{Future, FutureExt, TryFutureExt}; use futures::future::{Future, FutureExt, TryFutureExt, BoxFuture};
use futures::stream::StreamExt; use futures::stream::StreamExt;
use futures::task::SpawnExt; use futures::task::SpawnExt;
use futures_tokio_compat::Compat as TokioCompat; use futures_tokio_compat::Compat as TokioCompat;
@ -256,7 +255,7 @@ impl Rocket {
request._set_method(Method::Get); request._set_method(Method::Get);
// Return early so we don't set cookies twice. // Return early so we don't set cookies twice.
let try_next: Pin<Box<dyn Future<Output = _> + Send>> = Box::pin(self.route_and_process(request, data)); let try_next: BoxFuture<'_, _> = Box::pin(self.route_and_process(request, data));
return try_next.await; return try_next.await;
} else { } else {
// No match was found and it can't be autohandled. 404. // No match was found and it can't be autohandled. 404.

View File

@ -3,7 +3,7 @@ mod route;
use std::collections::hash_map::HashMap; use std::collections::hash_map::HashMap;
use futures::future::Future; use futures::future::BoxFuture;
pub use self::route::Route; pub use self::route::Route;
@ -14,7 +14,7 @@ use crate::http::Method;
type Selector = Method; type Selector = Method;
// A handler to use when one is needed temporarily. // A handler to use when one is needed temporarily.
pub(crate) fn dummy_handler<'r>(r: &'r Request<'_>, _: crate::Data) -> std::pin::Pin<Box<dyn Future<Output = crate::handler::Outcome<'r>> + Send + 'r>> { pub(crate) fn dummy_handler<'r>(r: &'r Request<'_>, _: crate::Data) -> BoxFuture<'r, crate::handler::Outcome<'r>> {
crate::Outcome::from(r, ()) crate::Outcome::from(r, ())
} }