|
1 | 1 | use std::fmt::Debug;
|
2 | 2 | use std::net::SocketAddr;
|
3 | 3 |
|
| 4 | +use futures_core::future::BoxFuture; |
| 5 | + |
4 | 6 | use crate::router::Route;
|
5 | 7 | use crate::request::Request;
|
6 | 8 | use crate::outcome::{self, IntoOutcome};
|
@@ -32,6 +34,22 @@ impl<S, E> IntoOutcome<S, (Status, E), ()> for Result<S, E> {
|
32 | 34 | }
|
33 | 35 | }
|
34 | 36 |
|
| 37 | +/// Type alias for the future returned by [`FromRequestAsync::from_request`]. |
| 38 | +pub type FromRequestFuture<'fut, T, E> = BoxFuture<'fut, Outcome<T, E>>; |
| 39 | + |
| 40 | +pub trait FromRequestAsync<'a, 'r>: Sized { |
| 41 | + /// The associated error to be returned if derivation fails. |
| 42 | + type Error: Debug; |
| 43 | + |
| 44 | + /// Derives an instance of `Self` from the incoming request metadata. |
| 45 | + /// |
| 46 | + /// If the derivation is successful, an outcome of `Success` is returned. If |
| 47 | + /// the derivation fails in an unrecoverable fashion, `Failure` is returned. |
| 48 | + /// `Forward` is returned to indicate that the request should be forwarded |
| 49 | + /// to other matching routes, if any. |
| 50 | + fn from_request<'fut>(request: &'a Request<'r>) -> FromRequestFuture<'fut, Self, Self::Error> where 'a: 'fut; |
| 51 | +} |
| 52 | + |
35 | 53 | /// Trait implemented by request guards to derive a value from incoming
|
36 | 54 | /// requests.
|
37 | 55 | ///
|
@@ -356,6 +374,14 @@ pub trait FromRequest<'a, 'r>: Sized {
|
356 | 374 | fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error>;
|
357 | 375 | }
|
358 | 376 |
|
| 377 | +impl<'a, 'r, T: FromRequest<'a, 'r>> FromRequestAsync<'a, 'r> for T { |
| 378 | + type Error = T::Error; |
| 379 | + |
| 380 | + fn from_request<'fut>(request: &'a Request<'r>) -> BoxFuture<'fut, Outcome<Self, Self::Error>> where 'a: 'fut { |
| 381 | + Box::pin(async move { T::from_request(request) }) |
| 382 | + } |
| 383 | +} |
| 384 | + |
359 | 385 | impl FromRequest<'_, '_> for Method {
|
360 | 386 | type Error = std::convert::Infallible;
|
361 | 387 |
|
|
0 commit comments