pub struct OptionalPath<T>(pub Option<T>);
Expand description
Extractor that extracts path arguments the same way as Path
, except if there aren’t any.
This extractor can be used in place of Path
when you have two routes that you want to handle
in mostly the same way, where one has a path parameter and the other one doesn’t.
§Example
use std::num::NonZeroU32;
use axum::{
response::IntoResponse,
routing::get,
Router,
};
use axum_extra::extract::OptionalPath;
async fn render_blog(OptionalPath(page): OptionalPath<NonZeroU32>) -> impl IntoResponse {
// Convert to u32, default to page 1 if not specified
let page = page.map_or(1, |param| param.get());
// ...
}
let app = Router::new()
.route("/blog", get(render_blog))
.route("/blog/:page", get(render_blog));
Tuple Fields§
§0: Option<T>
Trait Implementations§
Source§impl<T: Debug> Debug for OptionalPath<T>
impl<T: Debug> Debug for OptionalPath<T>
Source§impl<T, S> FromRequestParts<S> for OptionalPath<T>
impl<T, S> FromRequestParts<S> for OptionalPath<T>
Source§type Rejection = PathRejection
type Rejection = PathRejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.
Auto Trait Implementations§
impl<T> Freeze for OptionalPath<T>where
T: Freeze,
impl<T> RefUnwindSafe for OptionalPath<T>where
T: RefUnwindSafe,
impl<T> Send for OptionalPath<T>where
T: Send,
impl<T> Sync for OptionalPath<T>where
T: Sync,
impl<T> Unpin for OptionalPath<T>where
T: Unpin,
impl<T> UnwindSafe for OptionalPath<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.