Struct axum::extract::MatchedPath
source · [−]pub struct MatchedPath(_);
Available on crate feature
matched-path
only.Expand description
Access the path in the router that matches the request.
use axum::{
Router,
extract::MatchedPath,
routing::get,
};
let app = Router::new().route(
"/users/:id",
get(|path: MatchedPath| async move {
let path = path.as_str();
// `path` will be "/users/:id"
})
);
MatchedPath
can also be accessed from middleware via request extensions.
This is useful for example with Trace
to
create a span that contains the matched path:
use axum::{
Router,
extract::MatchedPath,
http::Request,
routing::get,
};
use tower_http::trace::TraceLayer;
let app = Router::new()
.route("/users/:id", get(|| async { /* ... */ }))
.layer(
TraceLayer::new_for_http().make_span_with(|req: &Request<_>| {
let path = if let Some(path) = req.extensions().get::<MatchedPath>() {
path.as_str()
} else {
req.uri().path()
};
tracing::info_span!("http-request", %path)
}),
);
Implementations
sourceimpl MatchedPath
impl MatchedPath
Trait Implementations
sourceimpl Clone for MatchedPath
impl Clone for MatchedPath
sourcefn clone(&self) -> MatchedPath
fn clone(&self) -> MatchedPath
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for MatchedPath
impl Debug for MatchedPath
sourceimpl<B> FromRequest<B> for MatchedPath where
B: Send,
impl<B> FromRequest<B> for MatchedPath where
B: Send,
type Rejection = MatchedPathRejection
type Rejection = MatchedPathRejection
If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response. Read more
sourcefn from_request<'life0, 'async_trait>(
req: &'life0 mut RequestParts<B>
) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'async_trait>(
req: &'life0 mut RequestParts<B>
) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
Perform the extraction.
Auto Trait Implementations
impl RefUnwindSafe for MatchedPath
impl Send for MatchedPath
impl Sync for MatchedPath
impl Unpin for MatchedPath
impl UnwindSafe for MatchedPath
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more