axum_core/extract/
option.rs1use std::future::Future;
2
3use http::request::Parts;
4
5use crate::response::IntoResponse;
6
7use super::{private, FromRequest, FromRequestParts, Request};
8
9pub trait OptionalFromRequestParts<S>: Sized {
12 type Rejection: IntoResponse;
16
17 fn from_request_parts(
19 parts: &mut Parts,
20 state: &S,
21 ) -> impl Future<Output = Result<Option<Self>, Self::Rejection>> + Send;
22}
23
24pub trait OptionalFromRequest<S, M = private::ViaRequest>: Sized {
26 type Rejection: IntoResponse;
30
31 fn from_request(
33 req: Request,
34 state: &S,
35 ) -> impl Future<Output = Result<Option<Self>, Self::Rejection>> + Send;
36}
37
38#[diagnostic::do_not_recommend]
41impl<S, T> FromRequestParts<S> for Option<T>
42where
43 T: OptionalFromRequestParts<S>,
44 S: Send + Sync,
45{
46 type Rejection = T::Rejection;
47
48 #[allow(clippy::use_self)]
49 fn from_request_parts(
50 parts: &mut Parts,
51 state: &S,
52 ) -> impl Future<Output = Result<Option<T>, Self::Rejection>> {
53 T::from_request_parts(parts, state)
54 }
55}
56
57#[diagnostic::do_not_recommend]
58impl<S, T> FromRequest<S> for Option<T>
59where
60 T: OptionalFromRequest<S>,
61 S: Send + Sync,
62{
63 type Rejection = T::Rejection;
64
65 #[allow(clippy::use_self)]
66 async fn from_request(req: Request, state: &S) -> Result<Option<T>, Self::Rejection> {
67 T::from_request(req, state).await
68 }
69}