Skip to content

Commit 32a948e

Browse files
authored
Fix cargo hack warnings (#3114)
1 parent ed4d560 commit 32a948e

File tree

7 files changed

+100
-91
lines changed

7 files changed

+100
-91
lines changed

.github/workflows/CI.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ jobs:
4343

4444
cargo-hack:
4545
runs-on: ubuntu-24.04
46+
env:
47+
# Fail the build if there are any warnings
48+
RUSTFLAGS: "-D warnings"
4649
steps:
4750
- uses: actions/checkout@v4
4851
- uses: dtolnay/rust-toolchain@stable

axum/src/extract/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub mod rejection;
1111
pub mod ws;
1212

1313
pub(crate) mod nested_path;
14+
#[cfg(feature = "original-uri")]
15+
mod original_uri;
1416
mod raw_form;
1517
mod raw_query;
1618
mod request_parts;
@@ -72,7 +74,7 @@ pub use self::query::Query;
7274

7375
#[cfg(feature = "original-uri")]
7476
#[doc(inline)]
75-
pub use self::request_parts::OriginalUri;
77+
pub use self::original_uri::OriginalUri;
7678

7779
#[cfg(feature = "ws")]
7880
#[doc(inline)]

axum/src/extract/original_uri.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use super::{Extension, FromRequestParts};
2+
use http::{request::Parts, Uri};
3+
use std::convert::Infallible;
4+
5+
/// Extractor that gets the original request URI regardless of nesting.
6+
///
7+
/// This is necessary since [`Uri`](http::Uri), when used as an extractor, will
8+
/// have the prefix stripped if used in a nested service.
9+
///
10+
/// # Example
11+
///
12+
/// ```
13+
/// use axum::{
14+
/// routing::get,
15+
/// Router,
16+
/// extract::OriginalUri,
17+
/// http::Uri
18+
/// };
19+
///
20+
/// let api_routes = Router::new()
21+
/// .route(
22+
/// "/users",
23+
/// get(|uri: Uri, OriginalUri(original_uri): OriginalUri| async {
24+
/// // `uri` is `/users`
25+
/// // `original_uri` is `/api/users`
26+
/// }),
27+
/// );
28+
///
29+
/// let app = Router::new().nest("/api", api_routes);
30+
/// # let _: Router = app;
31+
/// ```
32+
///
33+
/// # Extracting via request extensions
34+
///
35+
/// `OriginalUri` can also be accessed from middleware via request extensions.
36+
/// This is useful for example with [`Trace`](tower_http::trace::Trace) to
37+
/// create a span that contains the full path, if your service might be nested:
38+
///
39+
/// ```
40+
/// use axum::{
41+
/// Router,
42+
/// extract::OriginalUri,
43+
/// http::Request,
44+
/// routing::get,
45+
/// };
46+
/// use tower_http::trace::TraceLayer;
47+
///
48+
/// let api_routes = Router::new()
49+
/// .route("/users/{id}", get(|| async { /* ... */ }))
50+
/// .layer(
51+
/// TraceLayer::new_for_http().make_span_with(|req: &Request<_>| {
52+
/// let path = if let Some(path) = req.extensions().get::<OriginalUri>() {
53+
/// // This will include `/api`
54+
/// path.0.path().to_owned()
55+
/// } else {
56+
/// // The `OriginalUri` extension will always be present if using
57+
/// // `Router` unless another extractor or middleware has removed it
58+
/// req.uri().path().to_owned()
59+
/// };
60+
/// tracing::info_span!("http-request", %path)
61+
/// }),
62+
/// );
63+
///
64+
/// let app = Router::new().nest("/api", api_routes);
65+
/// # let _: Router = app;
66+
/// ```
67+
#[derive(Debug, Clone)]
68+
pub struct OriginalUri(pub Uri);
69+
70+
impl<S> FromRequestParts<S> for OriginalUri
71+
where
72+
S: Send + Sync,
73+
{
74+
type Rejection = Infallible;
75+
76+
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
77+
let uri = Extension::<Self>::from_request_parts(parts, state)
78+
.await
79+
.unwrap_or_else(|_| Extension(OriginalUri(parts.uri.clone())))
80+
.0;
81+
Ok(uri)
82+
}
83+
}
84+
85+
axum_core::__impl_deref!(OriginalUri: Uri);

axum/src/extract/request_parts.rs

Lines changed: 3 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,6 @@
1-
use super::{Extension, FromRequestParts};
2-
use http::{request::Parts, Uri};
3-
use std::convert::Infallible;
4-
5-
/// Extractor that gets the original request URI regardless of nesting.
6-
///
7-
/// This is necessary since [`Uri`](http::Uri), when used as an extractor, will
8-
/// have the prefix stripped if used in a nested service.
9-
///
10-
/// # Example
11-
///
12-
/// ```
13-
/// use axum::{
14-
/// routing::get,
15-
/// Router,
16-
/// extract::OriginalUri,
17-
/// http::Uri
18-
/// };
19-
///
20-
/// let api_routes = Router::new()
21-
/// .route(
22-
/// "/users",
23-
/// get(|uri: Uri, OriginalUri(original_uri): OriginalUri| async {
24-
/// // `uri` is `/users`
25-
/// // `original_uri` is `/api/users`
26-
/// }),
27-
/// );
28-
///
29-
/// let app = Router::new().nest("/api", api_routes);
30-
/// # let _: Router = app;
31-
/// ```
32-
///
33-
/// # Extracting via request extensions
34-
///
35-
/// `OriginalUri` can also be accessed from middleware via request extensions.
36-
/// This is useful for example with [`Trace`](tower_http::trace::Trace) to
37-
/// create a span that contains the full path, if your service might be nested:
38-
///
39-
/// ```
40-
/// use axum::{
41-
/// Router,
42-
/// extract::OriginalUri,
43-
/// http::Request,
44-
/// routing::get,
45-
/// };
46-
/// use tower_http::trace::TraceLayer;
47-
///
48-
/// let api_routes = Router::new()
49-
/// .route("/users/{id}", get(|| async { /* ... */ }))
50-
/// .layer(
51-
/// TraceLayer::new_for_http().make_span_with(|req: &Request<_>| {
52-
/// let path = if let Some(path) = req.extensions().get::<OriginalUri>() {
53-
/// // This will include `/api`
54-
/// path.0.path().to_owned()
55-
/// } else {
56-
/// // The `OriginalUri` extension will always be present if using
57-
/// // `Router` unless another extractor or middleware has removed it
58-
/// req.uri().path().to_owned()
59-
/// };
60-
/// tracing::info_span!("http-request", %path)
61-
/// }),
62-
/// );
63-
///
64-
/// let app = Router::new().nest("/api", api_routes);
65-
/// # let _: Router = app;
66-
/// ```
67-
#[cfg(feature = "original-uri")]
68-
#[derive(Debug, Clone)]
69-
pub struct OriginalUri(pub Uri);
70-
71-
#[cfg(feature = "original-uri")]
72-
impl<S> FromRequestParts<S> for OriginalUri
73-
where
74-
S: Send + Sync,
75-
{
76-
type Rejection = Infallible;
77-
78-
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
79-
let uri = Extension::<Self>::from_request_parts(parts, state)
80-
.await
81-
.unwrap_or_else(|_| Extension(OriginalUri(parts.uri.clone())))
82-
.0;
83-
Ok(uri)
84-
}
85-
}
86-
87-
#[cfg(feature = "original-uri")]
88-
axum_core::__impl_deref!(OriginalUri: Uri);
89-
1+
/// This module contains the tests for the `impl<S> FromRequestParts<S> for Parts`
2+
/// implementation in the `axum-core` crate. The tests cannot be moved there
3+
/// because we don't have access to the `TestClient` and `Router` types there.
904
#[cfg(test)]
915
mod tests {
926
use crate::{extract::Extension, routing::get, test_helpers::*, Router};

axum/src/macros.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,29 @@ macro_rules! all_the_tuples {
6868
}
6969

7070
#[cfg(feature = "tracing")]
71+
#[allow(unused_macros)]
7172
macro_rules! trace {
7273
($($tt:tt)*) => {
7374
tracing::trace!($($tt)*)
7475
}
7576
}
7677

7778
#[cfg(feature = "tracing")]
79+
#[allow(unused_macros)]
7880
macro_rules! error {
7981
($($tt:tt)*) => {
8082
tracing::error!($($tt)*)
8183
};
8284
}
8385

8486
#[cfg(not(feature = "tracing"))]
87+
#[allow(unused_macros)]
8588
macro_rules! trace {
8689
($($tt:tt)*) => {};
8790
}
8891

8992
#[cfg(not(feature = "tracing"))]
93+
#[allow(unused_macros)]
9094
macro_rules! error {
9195
($($tt:tt)*) => {};
9296
}

axum/src/routing/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ impl<S> fmt::Debug for Router<S> {
9999
}
100100

101101
pub(crate) const NEST_TAIL_PARAM: &str = "__private__axum_nest_tail_param";
102+
#[cfg(feature = "matched-path")]
102103
pub(crate) const NEST_TAIL_PARAM_CAPTURE: &str = "/{*__private__axum_nest_tail_param}";
103104
pub(crate) const FALLBACK_PARAM: &str = "__private__axum_fallback";
104105
pub(crate) const FALLBACK_PARAM_PATH: &str = "/{*__private__axum_fallback}";

axum/src/routing/path_router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ where
369369

370370
pub(super) fn call_with_state(
371371
&self,
372-
mut req: Request,
372+
#[cfg_attr(not(feature = "original-uri"), allow(unused_mut))] mut req: Request,
373373
state: S,
374374
) -> Result<RouteFuture<Infallible>, (Request, S)> {
375375
#[cfg(feature = "original-uri")]

0 commit comments

Comments
 (0)