Simplify FromRequestAsync trait definition.

This commit is contained in:
Jeb Rosen 2019-12-10 16:34:26 -08:00 committed by Sergio Benitez
parent 003bf77c29
commit 468f4d9314
3 changed files with 7 additions and 9 deletions

View File

@ -144,7 +144,7 @@ pub fn database_attr(attr: TokenStream, input: TokenStream) -> Result<TokenStrea
impl<'a, 'r> #request::FromRequestAsync<'a, 'r> for #guard_type { impl<'a, 'r> #request::FromRequestAsync<'a, 'r> for #guard_type {
type Error = (); type Error = ();
fn from_request<'fut>(request: &'a #request::Request<'r>) -> #request::FromRequestFuture<'fut, Self, Self::Error> where 'a: 'fut { fn from_request(request: &'a #request::Request<'r>) -> #request::FromRequestFuture<'a, Self, Self::Error> {
use ::rocket::{Outcome, http::Status}; use ::rocket::{Outcome, http::Status};
Box::pin(async move { Box::pin(async move {
let pool = ::rocket::try_outcome!(request.guard::<::rocket::State<'_, #pool_type>>()).0.clone(); let pool = ::rocket::try_outcome!(request.guard::<::rocket::State<'_, #pool_type>>()).0.clone();

View File

@ -47,7 +47,7 @@ pub trait FromRequestAsync<'a, 'r>: Sized {
/// the derivation fails in an unrecoverable fashion, `Failure` is returned. /// the derivation fails in an unrecoverable fashion, `Failure` is returned.
/// `Forward` is returned to indicate that the request should be forwarded /// `Forward` is returned to indicate that the request should be forwarded
/// to other matching routes, if any. /// to other matching routes, if any.
fn from_request<'fut>(request: &'a Request<'r>) -> FromRequestFuture<'fut, Self, Self::Error> where 'a: 'fut; fn from_request(request: &'a Request<'r>) -> FromRequestFuture<'a, Self, Self::Error>;
} }
/// Trait implemented by request guards to derive a value from incoming /// Trait implemented by request guards to derive a value from incoming
@ -377,7 +377,7 @@ pub trait FromRequest<'a, 'r>: Sized {
impl<'a, 'r, T: FromRequest<'a, 'r>> FromRequestAsync<'a, 'r> for T { impl<'a, 'r, T: FromRequest<'a, 'r>> FromRequestAsync<'a, 'r> for T {
type Error = T::Error; type Error = T::Error;
fn from_request<'fut>(request: &'a Request<'r>) -> BoxFuture<'fut, Outcome<Self, Self::Error>> where 'a: 'fut { fn from_request(request: &'a Request<'r>) -> BoxFuture<'a, Outcome<Self, Self::Error>> {
Box::pin(async move { T::from_request(request) }) Box::pin(async move { T::from_request(request) })
} }
} }
@ -453,7 +453,7 @@ impl FromRequest<'_, '_> for SocketAddr {
impl<'a, 'r, T: FromRequestAsync<'a, 'r> + 'a> FromRequestAsync<'a, 'r> for Result<T, T::Error> { impl<'a, 'r, T: FromRequestAsync<'a, 'r> + 'a> FromRequestAsync<'a, 'r> for Result<T, T::Error> {
type Error = std::convert::Infallible; type Error = std::convert::Infallible;
fn from_request<'fut>(request: &'a Request<'r>) -> BoxFuture<'fut, Outcome<Self, Self::Error>> where 'a: 'fut { fn from_request(request: &'a Request<'r>) -> BoxFuture<'a, Outcome<Self, Self::Error>> {
// TODO: FutureExt::map is a workaround (see rust-lang/rust#60658) // TODO: FutureExt::map is a workaround (see rust-lang/rust#60658)
use futures_util::future::FutureExt; use futures_util::future::FutureExt;
T::from_request(request).map(|x| match x { T::from_request(request).map(|x| match x {
@ -467,7 +467,7 @@ impl<'a, 'r, T: FromRequestAsync<'a, 'r> + 'a> FromRequestAsync<'a, 'r> for Resu
impl<'a, 'r, T: FromRequestAsync<'a, 'r> + 'a> FromRequestAsync<'a, 'r> for Option<T> { impl<'a, 'r, T: FromRequestAsync<'a, 'r> + 'a> FromRequestAsync<'a, 'r> for Option<T> {
type Error = std::convert::Infallible; type Error = std::convert::Infallible;
fn from_request<'fut>(request: &'a Request<'r>) -> BoxFuture<'fut, Outcome<Self, Self::Error>> where 'a: 'fut { fn from_request(request: &'a Request<'r>) -> BoxFuture<'a, Outcome<Self, Self::Error>> {
// TODO: FutureExt::map is a workaround (see rust-lang/rust#60658) // TODO: FutureExt::map is a workaround (see rust-lang/rust#60658)
use futures_util::future::FutureExt; use futures_util::future::FutureExt;
T::from_request(request).map(|x| match x { T::from_request(request).map(|x| match x {

View File

@ -44,8 +44,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Guard2 {
impl<'a, 'r> FromRequestAsync<'a, 'r> for Guard3 { impl<'a, 'r> FromRequestAsync<'a, 'r> for Guard3 {
type Error = (); type Error = ();
fn from_request<'fut>(req: &'a Request<'r>) -> FromRequestFuture<'fut, Self, ()> fn from_request(req: &'a Request<'r>) -> FromRequestFuture<'a, Self, ()>
where 'a: 'fut
{ {
Box::pin(async move { Box::pin(async move {
let atomics = try_outcome!(req.guard::<State<'_, Atomics>>()); let atomics = try_outcome!(req.guard::<State<'_, Atomics>>());
@ -62,8 +61,7 @@ impl<'a, 'r> FromRequestAsync<'a, 'r> for Guard3 {
impl<'a, 'r> FromRequestAsync<'a, 'r> for Guard4 { impl<'a, 'r> FromRequestAsync<'a, 'r> for Guard4 {
type Error = (); type Error = ();
fn from_request<'fut>(req: &'a Request<'r>) -> FromRequestFuture<'fut, Self, ()> fn from_request(req: &'a Request<'r>) -> FromRequestFuture<'a, Self, ()>
where 'a: 'fut
{ {
Box::pin(async move { Box::pin(async move {
try_outcome!(Guard3::from_request(req).await); try_outcome!(Guard3::from_request(req).await);