use std::future::Future; use http::request::Parts; use crate::response::IntoResponse; use super::{private, FromRequest, FromRequestParts, Request}; /// Customize the behavior of `Option` as a [`FromRequestParts`] /// extractor. pub trait OptionalFromRequestParts: Sized { /// If the extractor fails, it will use this "rejection" type. /// /// A rejection is a kind of error that can be converted into a response. type Rejection: IntoResponse; /// Perform the extraction. fn from_request_parts( parts: &mut Parts, state: &S, ) -> impl Future, Self::Rejection>> + Send; } /// Customize the behavior of `Option` as a [`FromRequest`] extractor. pub trait OptionalFromRequest: Sized { /// If the extractor fails, it will use this "rejection" type. /// /// A rejection is a kind of error that can be converted into a response. type Rejection: IntoResponse; /// Perform the extraction. fn from_request( req: Request, state: &S, ) -> impl Future, Self::Rejection>> + Send; } impl FromRequestParts for Option where T: OptionalFromRequestParts, S: Send + Sync, { type Rejection = T::Rejection; fn from_request_parts( parts: &mut Parts, state: &S, ) -> impl Future, Self::Rejection>> { T::from_request_parts(parts, state) } } impl FromRequest for Option where T: OptionalFromRequest, S: Send + Sync, { type Rejection = T::Rejection; async fn from_request(req: Request, state: &S) -> Result, Self::Rejection> { T::from_request(req, state).await } }