From 7961711fc73f2f5378f803715c8e5d2f546c8f27 Mon Sep 17 00:00:00 2001 From: Joey de Waal <99046430+joeydewaal@users.noreply.github.com> Date: Fri, 30 Jan 2026 12:34:46 +0100 Subject: [PATCH] Accept owned locations in Redirect constructors (#3635) --- axum-extra/src/routing/mod.rs | 2 +- axum/CHANGELOG.md | 1 + axum/src/response/redirect.rs | 16 ++++++++-------- examples/tls-graceful-shutdown/src/main.rs | 2 +- examples/tls-rustls/src/main.rs | 2 +- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/axum-extra/src/routing/mod.rs b/axum-extra/src/routing/mod.rs index 028fcdd0..d32f4bbc 100644 --- a/axum-extra/src/routing/mod.rs +++ b/axum-extra/src/routing/mod.rs @@ -379,7 +379,7 @@ where }); if let Some(new_uri) = new_uri { - Redirect::permanent(&new_uri.to_string()).into_response() + Redirect::permanent(new_uri.to_string()).into_response() } else { StatusCode::BAD_REQUEST.into_response() } diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 8e9b255e..95e5c848 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **changed:** `serve` has an additional generic argument and can now work with any response body type, not just `axum::body::Body` ([#3205]) - **changed:** Update minimum rust version to 1.80 ([#3620]) +- **changed:** `Redirect` constructors now accept any `impl Into` ([#3635]) [#3158]: https://github.com/tokio-rs/axum/pull/3158 [#3261]: https://github.com/tokio-rs/axum/pull/3261 diff --git a/axum/src/response/redirect.rs b/axum/src/response/redirect.rs index e33928cd..696430f8 100644 --- a/axum/src/response/redirect.rs +++ b/axum/src/response/redirect.rs @@ -34,8 +34,8 @@ impl Redirect { /// [`Redirect::temporary`] should be used instead. /// /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303 - pub fn to(uri: &str) -> Self { - Self::with_status_code(StatusCode::SEE_OTHER, uri) + pub fn to(uri: impl Into) -> Self { + Self::with_status_code(StatusCode::SEE_OTHER, uri.into()) } /// Create a new [`Redirect`] that uses a [`307 Temporary Redirect`][mdn] status code. @@ -44,15 +44,15 @@ impl Redirect { /// method and body. /// /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307 - pub fn temporary(uri: &str) -> Self { - Self::with_status_code(StatusCode::TEMPORARY_REDIRECT, uri) + pub fn temporary(uri: impl Into) -> Self { + Self::with_status_code(StatusCode::TEMPORARY_REDIRECT, uri.into()) } /// Create a new [`Redirect`] that uses a [`308 Permanent Redirect`][mdn] status code. /// /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308 - pub fn permanent(uri: &str) -> Self { - Self::with_status_code(StatusCode::PERMANENT_REDIRECT, uri) + pub fn permanent(uri: impl Into) -> Self { + Self::with_status_code(StatusCode::PERMANENT_REDIRECT, uri.into()) } /// Returns the HTTP status code of the `Redirect`. @@ -71,7 +71,7 @@ impl Redirect { // use the `Location` header, namely `304 Not Modified`. // // We're open to adding more constructors upon request, if they make sense :) - fn with_status_code(status_code: StatusCode, uri: &str) -> Self { + fn with_status_code(status_code: StatusCode, uri: String) -> Self { assert!( status_code.is_redirection(), "not a redirection status code" @@ -79,7 +79,7 @@ impl Redirect { Self { status_code, - location: uri.to_owned(), + location: uri, } } } diff --git a/examples/tls-graceful-shutdown/src/main.rs b/examples/tls-graceful-shutdown/src/main.rs index 164a87d3..11dd4dea 100644 --- a/examples/tls-graceful-shutdown/src/main.rs +++ b/examples/tls-graceful-shutdown/src/main.rs @@ -120,7 +120,7 @@ where let redirect = move |uri: Uri| async move { match make_https(uri, ports.https) { - Ok(uri) => Ok(Redirect::permanent(&uri.to_string())), + Ok(uri) => Ok(Redirect::permanent(uri.to_string())), Err(error) => { tracing::warn!(%error, "failed to convert URI to HTTPS"); Err(StatusCode::BAD_REQUEST) diff --git a/examples/tls-rustls/src/main.rs b/examples/tls-rustls/src/main.rs index 7218047e..e85aad17 100644 --- a/examples/tls-rustls/src/main.rs +++ b/examples/tls-rustls/src/main.rs @@ -86,7 +86,7 @@ async fn redirect_http_to_https(ports: Ports) { let redirect = move |uri: Uri| async move { match make_https(uri, ports.https) { - Ok(uri) => Ok(Redirect::permanent(&uri.to_string())), + Ok(uri) => Ok(Redirect::permanent(uri.to_string())), Err(error) => { tracing::warn!(%error, "failed to convert URI to HTTPS"); Err(StatusCode::BAD_REQUEST)