From dc3ce09cf2410ff752047c67229e70ec4240529d Mon Sep 17 00:00:00 2001 From: Sinder Date: Thu, 2 Oct 2025 05:47:44 +0800 Subject: [PATCH 1/2] Refactor `From<[Item,N>` implementations to use generic `IntoIterator` where possible Remove deprecated `array::IntoIter::new` where `IntoIterator` doesn't fit --- tower-http/src/cors/allow_headers.rs | 18 +++++++----------- tower-http/src/cors/allow_methods.rs | 5 ++--- tower-http/src/cors/allow_origin.rs | 5 ++--- tower-http/src/cors/expose_headers.rs | 18 +++++++----------- tower-http/src/cors/mod.rs | 5 ++--- tower-http/src/cors/vary.rs | 18 ++++++------------ 6 files changed, 26 insertions(+), 43 deletions(-) diff --git a/tower-http/src/cors/allow_headers.rs b/tower-http/src/cors/allow_headers.rs index 06c19928..1c4929b2 100644 --- a/tower-http/src/cors/allow_headers.rs +++ b/tower-http/src/cors/allow_headers.rs @@ -1,4 +1,4 @@ -use std::{array, fmt}; +use std::{fmt}; use http::{ header::{self, HeaderName, HeaderValue}, @@ -86,16 +86,12 @@ impl From for AllowHeaders { } } -impl From<[HeaderName; N]> for AllowHeaders { - fn from(arr: [HeaderName; N]) -> Self { - #[allow(deprecated)] // Can be changed when MSRV >= 1.53 - Self::list(array::IntoIter::new(arr)) - } -} - -impl From> for AllowHeaders { - fn from(vec: Vec) -> Self { - Self::list(vec) +impl From for AllowHeaders +where + I: IntoIterator, +{ + fn from(iter: I) -> Self { + Self::list(iter) } } diff --git a/tower-http/src/cors/allow_methods.rs b/tower-http/src/cors/allow_methods.rs index df1a3cbd..41ac8305 100644 --- a/tower-http/src/cors/allow_methods.rs +++ b/tower-http/src/cors/allow_methods.rs @@ -1,4 +1,4 @@ -use std::{array, fmt}; +use std::{fmt}; use http::{ header::{self, HeaderName, HeaderValue}, @@ -108,8 +108,7 @@ impl From for AllowMethods { impl From<[Method; N]> for AllowMethods { fn from(arr: [Method; N]) -> Self { - #[allow(deprecated)] // Can be changed when MSRV >= 1.53 - Self::list(array::IntoIter::new(arr)) + Self::list(arr) } } diff --git a/tower-http/src/cors/allow_origin.rs b/tower-http/src/cors/allow_origin.rs index d5fdd7b6..646220fa 100644 --- a/tower-http/src/cors/allow_origin.rs +++ b/tower-http/src/cors/allow_origin.rs @@ -4,7 +4,7 @@ use http::{ }; use pin_project_lite::pin_project; use std::{ - array, fmt, + fmt, future::Future, pin::Pin, sync::Arc, @@ -203,8 +203,7 @@ impl From for AllowOrigin { impl From<[HeaderValue; N]> for AllowOrigin { fn from(arr: [HeaderValue; N]) -> Self { - #[allow(deprecated)] // Can be changed when MSRV >= 1.53 - Self::list(array::IntoIter::new(arr)) + Self::list(arr) } } diff --git a/tower-http/src/cors/expose_headers.rs b/tower-http/src/cors/expose_headers.rs index 2b1a2267..7fa90133 100644 --- a/tower-http/src/cors/expose_headers.rs +++ b/tower-http/src/cors/expose_headers.rs @@ -1,4 +1,4 @@ -use std::{array, fmt}; +use std::{fmt}; use http::{ header::{self, HeaderName, HeaderValue}, @@ -69,16 +69,12 @@ impl From for ExposeHeaders { } } -impl From<[HeaderName; N]> for ExposeHeaders { - fn from(arr: [HeaderName; N]) -> Self { - #[allow(deprecated)] // Can be changed when MSRV >= 1.53 - Self::list(array::IntoIter::new(arr)) - } -} - -impl From> for ExposeHeaders { - fn from(vec: Vec) -> Self { - Self::list(vec) +impl From for ExposeHeaders +where + I: IntoIterator, +{ + fn from(iter: I) -> Self { + Self::list(iter) } } diff --git a/tower-http/src/cors/mod.rs b/tower-http/src/cors/mod.rs index 9da666c2..d5660ec2 100644 --- a/tower-http/src/cors/mod.rs +++ b/tower-http/src/cors/mod.rs @@ -57,7 +57,6 @@ use http::{ }; use pin_project_lite::pin_project; use std::{ - array, future::Future, mem, pin::Pin, @@ -813,10 +812,10 @@ fn ensure_usable_cors_rules(layer: &CorsLayer) { /// /// This is the default set of header names returned in the `vary` header pub fn preflight_request_headers() -> impl Iterator { - #[allow(deprecated)] // Can be changed when MSRV >= 1.53 - array::IntoIter::new([ + IntoIterator::into_iter([ header::ORIGIN, header::ACCESS_CONTROL_REQUEST_METHOD, header::ACCESS_CONTROL_REQUEST_HEADERS, ]) } + diff --git a/tower-http/src/cors/vary.rs b/tower-http/src/cors/vary.rs index 1ed7e672..9565fd72 100644 --- a/tower-http/src/cors/vary.rs +++ b/tower-http/src/cors/vary.rs @@ -1,5 +1,3 @@ -use std::array; - use http::header::{self, HeaderName, HeaderValue}; use super::preflight_request_headers; @@ -46,15 +44,11 @@ impl Default for Vary { } } -impl From<[HeaderName; N]> for Vary { - fn from(arr: [HeaderName; N]) -> Self { - #[allow(deprecated)] // Can be changed when MSRV >= 1.53 - Self::list(array::IntoIter::new(arr)) - } -} - -impl From> for Vary { - fn from(vec: Vec) -> Self { - Self::list(vec) +impl From for Vary +where + I: IntoIterator, +{ + fn from(iter: I) -> Self { + Self::list(iter) } } From 47e06eed7441231beffa4a62459cc3765e8768ce Mon Sep 17 00:00:00 2001 From: Sinder Date: Thu, 2 Oct 2025 06:21:07 +0800 Subject: [PATCH 2/2] Run cargo-fmt --- tower-http/src/cors/allow_headers.rs | 2 +- tower-http/src/cors/allow_methods.rs | 2 +- tower-http/src/cors/expose_headers.rs | 2 +- tower-http/src/cors/mod.rs | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/tower-http/src/cors/allow_headers.rs b/tower-http/src/cors/allow_headers.rs index 1c4929b2..ab0979b0 100644 --- a/tower-http/src/cors/allow_headers.rs +++ b/tower-http/src/cors/allow_headers.rs @@ -1,4 +1,4 @@ -use std::{fmt}; +use std::fmt; use http::{ header::{self, HeaderName, HeaderValue}, diff --git a/tower-http/src/cors/allow_methods.rs b/tower-http/src/cors/allow_methods.rs index 41ac8305..a2aeb642 100644 --- a/tower-http/src/cors/allow_methods.rs +++ b/tower-http/src/cors/allow_methods.rs @@ -1,4 +1,4 @@ -use std::{fmt}; +use std::fmt; use http::{ header::{self, HeaderName, HeaderValue}, diff --git a/tower-http/src/cors/expose_headers.rs b/tower-http/src/cors/expose_headers.rs index 7fa90133..c16e8224 100644 --- a/tower-http/src/cors/expose_headers.rs +++ b/tower-http/src/cors/expose_headers.rs @@ -1,4 +1,4 @@ -use std::{fmt}; +use std::fmt; use http::{ header::{self, HeaderName, HeaderValue}, diff --git a/tower-http/src/cors/mod.rs b/tower-http/src/cors/mod.rs index d5660ec2..d40994d0 100644 --- a/tower-http/src/cors/mod.rs +++ b/tower-http/src/cors/mod.rs @@ -818,4 +818,3 @@ pub fn preflight_request_headers() -> impl Iterator { header::ACCESS_CONTROL_REQUEST_HEADERS, ]) } -