Skip to content

Commit

Permalink
Document usage of concrete state in FromRequest macro (#2550) (#2581)
Browse files Browse the repository at this point in the history
  • Loading branch information
mladedav authored Feb 11, 2024
1 parent 62324aa commit 66b3b3d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions axum-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,55 @@ use from_request::Trait::{FromRequest, FromRequestParts};
/// }
/// ```
///
/// ## Concrete state
///
/// If the extraction can be done only for a concrete state, that type can be specified with
/// `#[from_request(state(YourState))]`:
///
/// ```
/// use axum::extract::{FromRequest, FromRequestParts};
///
/// #[derive(Clone)]
/// struct CustomState;
///
/// struct MyInnerType;
///
/// #[axum::async_trait]
/// impl FromRequestParts<CustomState> for MyInnerType {
/// // ...
/// # type Rejection = ();
///
/// # async fn from_request_parts(
/// # _parts: &mut axum::http::request::Parts,
/// # _state: &CustomState
/// # ) -> Result<Self, Self::Rejection> {
/// # todo!()
/// # }
/// }
///
/// #[derive(FromRequest)]
/// #[from_request(state(CustomState))]
/// struct MyExtractor {
/// custom: MyInnerType,
/// body: String,
/// }
/// ```
///
/// This is not needed for a `State<T>` as the type is inferred in that case.
///
/// ```
/// use axum::extract::{FromRequest, FromRequestParts, State};
///
/// #[derive(Clone)]
/// struct CustomState;
///
/// #[derive(FromRequest)]
/// struct MyExtractor {
/// custom: State<CustomState>,
/// body: String,
/// }
/// ```
///
/// # The whole type at once
///
/// By using `#[from_request(via(...))]` on the container you can extract the whole type at once,
Expand Down

0 comments on commit 66b3b3d

Please sign in to comment.