From de9290a8b7fc225360b0214fb9b6bd6059dd8935 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 11 Sep 2024 21:17:25 +0200 Subject: [PATCH 1/7] Add more must_use attributes (#2846) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … so people get a warning when they accidentally add a semicolon after the response expression in a handler function. Also update changelogs of axum-core, axum-extra. --- axum-core/CHANGELOG.md | 13 +++++++++++-- axum-core/src/response/into_response.rs | 1 + axum-core/src/response/into_response_parts.rs | 4 ++++ axum-core/src/response/mod.rs | 1 + axum-extra/CHANGELOG.md | 4 +++- axum-extra/src/response/attachment.rs | 2 +- 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/axum-core/CHANGELOG.md b/axum-core/CHANGELOG.md index 1544fff8..be3e53ea 100644 --- a/axum-core/CHANGELOG.md +++ b/axum-core/CHANGELOG.md @@ -7,9 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- **added:** Implement `Copy` for `DefaultBodyLimit` +- **added:** Derive `Clone` and `Copy` for `AppendHeaders` ([#2776]) +- **added:** `must_use` attribute on `AppendHeaders` ([#2846]) +- **added:** `must_use` attribute on `ErrorResponse` ([#2846]) +- **added:** `must_use` attribute on `IntoResponse::into_response` ([#2846]) +- **added:** `must_use` attribute on `IntoResponseParts` trait methods ([#2846]) +- **added:** Implement `Copy` for `DefaultBodyLimit` ([#2875]) - **added**: `DefaultBodyLimit::max` and `DefaultBodyLimit::disable` are now - allowed in const context + allowed in const context ([#2875]) + +[#2776]: https://github.com/tokio-rs/axum/pull/2776 +[#2846]: https://github.com/tokio-rs/axum/pull/2846 +[#2875]: https://github.com/tokio-rs/axum/pull/2875 # 0.4.3 (13. January, 2024) diff --git a/axum-core/src/response/into_response.rs b/axum-core/src/response/into_response.rs index 679b0cbb..915b55ef 100644 --- a/axum-core/src/response/into_response.rs +++ b/axum-core/src/response/into_response.rs @@ -111,6 +111,7 @@ use std::{ /// ``` pub trait IntoResponse { /// Create a response. + #[must_use] fn into_response(self) -> Response; } diff --git a/axum-core/src/response/into_response_parts.rs b/axum-core/src/response/into_response_parts.rs index 72b61bc7..95564823 100644 --- a/axum-core/src/response/into_response_parts.rs +++ b/axum-core/src/response/into_response_parts.rs @@ -105,21 +105,25 @@ pub struct ResponseParts { impl ResponseParts { /// Gets a reference to the response headers. + #[must_use] pub fn headers(&self) -> &HeaderMap { self.res.headers() } /// Gets a mutable reference to the response headers. + #[must_use] pub fn headers_mut(&mut self) -> &mut HeaderMap { self.res.headers_mut() } /// Gets a reference to the response extensions. + #[must_use] pub fn extensions(&self) -> &Extensions { self.res.extensions() } /// Gets a mutable reference to the response extensions. + #[must_use] pub fn extensions_mut(&mut self) -> &mut Extensions { self.res.extensions_mut() } diff --git a/axum-core/src/response/mod.rs b/axum-core/src/response/mod.rs index 6b66c60e..dd6728b1 100644 --- a/axum-core/src/response/mod.rs +++ b/axum-core/src/response/mod.rs @@ -117,6 +117,7 @@ where /// /// See [`Result`] for more details. #[derive(Debug)] +#[must_use] pub struct ErrorResponse(Response); impl From for ErrorResponse diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index 4d49aab5..2936da58 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning]. # Unreleased -- None. +- **added:** The `response::Attachment` type ([#2789]) + +[#2789]: https://github.com/tokio-rs/axum/pull/2789 # 0.9.3 (24. March, 2024) diff --git a/axum-extra/src/response/attachment.rs b/axum-extra/src/response/attachment.rs index 923ad991..2063d30f 100644 --- a/axum-extra/src/response/attachment.rs +++ b/axum-extra/src/response/attachment.rs @@ -33,8 +33,8 @@ use tracing::error; /// # Note /// /// If you use axum with hyper, hyper will set the `Content-Length` if it is known. -/// #[derive(Debug)] +#[must_use] pub struct Attachment { inner: T, filename: Option, From f446b420ba1652489c226637ae267247def97498 Mon Sep 17 00:00:00 2001 From: tiaoxizhan Date: Fri, 13 Sep 2024 01:44:44 +0800 Subject: [PATCH 2/7] chore: Add missing symbol (#2909) Signed-off-by: tiaoxizhan --- axum-macros/src/from_request.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/axum-macros/src/from_request.rs b/axum-macros/src/from_request.rs index 191f1445..c5826307 100644 --- a/axum-macros/src/from_request.rs +++ b/axum-macros/src/from_request.rs @@ -180,7 +180,7 @@ pub(crate) fn expand(item: syn::Item, tr: Trait) -> syn::Result { variants, } = item; - let generics_error = format!("`#[derive({tr})] on enums don't support generics"); + let generics_error = format!("`#[derive({tr})]` on enums don't support generics"); if !generics.params.is_empty() { return Err(syn::Error::new_spanned(generics, generics_error)); From d2d74f6ad19c6ebb95cfbe562f394dd869666d22 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 20 Sep 2024 19:43:38 +0200 Subject: [PATCH 3/7] Upgrade tower and tower-http --- axum-core/Cargo.toml | 4 ++-- axum-extra/Cargo.toml | 6 +++--- axum/Cargo.toml | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/axum-core/Cargo.toml b/axum-core/Cargo.toml index f9e78692..76dde1bc 100644 --- a/axum-core/Cargo.toml +++ b/axum-core/Cargo.toml @@ -32,7 +32,7 @@ tower-layer = "0.3" tower-service = "0.3" # optional dependencies -tower-http = { version = "0.5.0", optional = true, features = ["limit"] } +tower-http = { version = "0.6.0", optional = true, features = ["limit"] } tracing = { version = "0.1.37", default-features = false, optional = true } [dev-dependencies] @@ -41,7 +41,7 @@ axum-extra = { path = "../axum-extra", features = ["typed-header"] } futures-util = { version = "0.3", default-features = false, features = ["alloc"] } hyper = "1.0.0" tokio = { version = "1.25.0", features = ["macros"] } -tower-http = { version = "0.5.0", features = ["limit"] } +tower-http = { version = "0.6.0", features = ["limit"] } [package.metadata.cargo-public-api-crates] allowed = [ diff --git a/axum-extra/Cargo.toml b/axum-extra/Cargo.toml index e091cd9c..3032f661 100644 --- a/axum-extra/Cargo.toml +++ b/axum-extra/Cargo.toml @@ -49,7 +49,7 @@ http-body-util = "0.1.0" mime = "0.3" pin-project-lite = "0.2" serde = "1.0" -tower = { version = "0.4", default-features = false, features = ["util"] } +tower = { version = "0.5.1", default-features = false, features = ["util"] } tower-layer = "0.3" tower-service = "0.3" @@ -76,8 +76,8 @@ reqwest = { version = "0.12", default-features = false, features = ["json", "str serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.71" tokio = { version = "1.14", features = ["full"] } -tower = { version = "0.4", features = ["util"] } -tower-http = { version = "0.5.0", features = ["map-response-body", "timeout"] } +tower = { version = "0.5.1", features = ["util"] } +tower-http = { version = "0.6.0", features = ["map-response-body", "timeout"] } [package.metadata.docs.rs] all-features = true diff --git a/axum/Cargo.toml b/axum/Cargo.toml index a88a1997..ff3de511 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -57,7 +57,7 @@ pin-project-lite = "0.2.7" rustversion = "1.0.9" serde = "1.0" sync_wrapper = "1.0.0" -tower = { version = "0.4.13", default-features = false, features = ["util"] } +tower = { version = "0.5.1", default-features = false, features = ["util"] } tower-layer = "0.3.2" tower-service = "0.3" @@ -76,7 +76,7 @@ tokio-tungstenite = { version = "0.23", optional = true } tracing = { version = "0.1", default-features = false, optional = true } [dependencies.tower-http] -version = "0.5.0" +version = "0.6.0" optional = true features = [ # all tower-http features except (de)?compression-zstd which doesn't @@ -132,7 +132,7 @@ rustdoc-args = ["--cfg", "docsrs"] [dev-dependencies.tower] package = "tower" -version = "0.4.10" +version = "0.5.1" features = [ "util", "timeout", @@ -143,7 +143,7 @@ features = [ ] [dev-dependencies.tower-http] -version = "0.5.0" +version = "0.6.0" features = [ # all tower-http features except (de)?compression-zstd which doesn't # build on `--target armv5te-unknown-linux-musleabi` From 88de9fc407dca5f36b7eb58d0c86195ce3543324 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 20 Sep 2024 19:38:29 +0200 Subject: [PATCH 4/7] ci: Add more allowances for multiple version of the same crate --- deny.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/deny.toml b/deny.toml index d95bdf29..c32c8715 100644 --- a/deny.toml +++ b/deny.toml @@ -33,6 +33,11 @@ skip-tree = [ { name = "regex-automata" }, # pulled in by hyper { name = "socket2" }, + # hyper-util hasn't upgraded to 0.5 yet, but it's the same service / layer + # crates beneath + { name = "tower" }, + # tower hasn't upgraded to 1.0 yet + { name = "sync_wrapper" }, ] [sources] From 0ba73b5c6e10f2725130f6bbfcd432778cc97975 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 20 Sep 2024 20:36:00 +0200 Subject: [PATCH 5/7] Implement FromRequest for BytesMut (#2583) Co-authored-by: Yann Simon --- axum-core/src/extract/request_parts.rs | 33 +++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/axum-core/src/extract/request_parts.rs b/axum-core/src/extract/request_parts.rs index 73f54db7..789b9c1c 100644 --- a/axum-core/src/extract/request_parts.rs +++ b/axum-core/src/extract/request_parts.rs @@ -1,7 +1,7 @@ use super::{rejection::*, FromRequest, FromRequestParts, Request}; use crate::{body::Body, RequestExt}; use async_trait::async_trait; -use bytes::Bytes; +use bytes::{BufMut, Bytes, BytesMut}; use http::{request::Parts, Extensions, HeaderMap, Method, Uri, Version}; use http_body_util::BodyExt; use std::convert::Infallible; @@ -71,6 +71,37 @@ where } } +#[async_trait] +impl FromRequest for BytesMut +where + S: Send + Sync, +{ + type Rejection = BytesRejection; + + async fn from_request(req: Request, _: &S) -> Result { + let mut body = req.into_limited_body(); + let mut bytes = BytesMut::new(); + body_to_bytes_mut(&mut body, &mut bytes).await?; + Ok(bytes) + } +} + +async fn body_to_bytes_mut(body: &mut Body, bytes: &mut BytesMut) -> Result<(), BytesRejection> { + while let Some(frame) = body + .frame() + .await + .transpose() + .map_err(FailedToBufferBody::from_err)? + { + let Ok(data) = frame.into_data() else { + return Ok(()); + }; + bytes.put(data); + } + + Ok(()) +} + #[async_trait] impl FromRequest for Bytes where From f1723890cba9d4602268de21533bddad70b91488 Mon Sep 17 00:00:00 2001 From: yhx-12243 Date: Sat, 21 Sep 2024 03:02:52 +0800 Subject: [PATCH 6/7] Avoid reallocation for String body extraction (#2857) --- axum-core/Cargo.toml | 2 +- axum-core/src/extract/request_parts.rs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/axum-core/Cargo.toml b/axum-core/Cargo.toml index 76dde1bc..c21b218e 100644 --- a/axum-core/Cargo.toml +++ b/axum-core/Cargo.toml @@ -19,7 +19,7 @@ __private_docs = ["dep:tower-http"] [dependencies] async-trait = "0.1.67" -bytes = "1.0" +bytes = "1.2" futures-util = { version = "0.3", default-features = false, features = ["alloc"] } http = "1.0.0" http-body = "1.0.0" diff --git a/axum-core/src/extract/request_parts.rs b/axum-core/src/extract/request_parts.rs index 789b9c1c..5663ddd4 100644 --- a/axum-core/src/extract/request_parts.rs +++ b/axum-core/src/extract/request_parts.rs @@ -137,9 +137,7 @@ where } })?; - let string = std::str::from_utf8(&bytes) - .map_err(InvalidUtf8::from_err)? - .to_owned(); + let string = String::from_utf8(bytes.into()).map_err(InvalidUtf8::from_err)?; Ok(string) } From a73dab417d592aa46a013342388340b6fe719bac Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 20 Sep 2024 21:17:41 +0200 Subject: [PATCH 7/7] Bump versions --- axum-core/CHANGELOG.md | 2 +- axum-core/Cargo.toml | 2 +- axum-extra/CHANGELOG.md | 2 +- axum-extra/Cargo.toml | 8 ++++---- axum-macros/CHANGELOG.md | 2 +- axum-macros/Cargo.toml | 2 +- axum/CHANGELOG.md | 2 +- axum/Cargo.toml | 6 +++--- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/axum-core/CHANGELOG.md b/axum-core/CHANGELOG.md index be3e53ea..464ee314 100644 --- a/axum-core/CHANGELOG.md +++ b/axum-core/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -# Unreleased +# 0.4.4 - **added:** Derive `Clone` and `Copy` for `AppendHeaders` ([#2776]) - **added:** `must_use` attribute on `AppendHeaders` ([#2846]) diff --git a/axum-core/Cargo.toml b/axum-core/Cargo.toml index c21b218e..2d4d0759 100644 --- a/axum-core/Cargo.toml +++ b/axum-core/Cargo.toml @@ -9,7 +9,7 @@ license = "MIT" name = "axum-core" readme = "README.md" repository = "https://github.com/tokio-rs/axum" -version = "0.4.3" # remember to also bump the version that axum and axum-extra depend on +version = "0.4.4" # remember to also bump the version that axum and axum-extra depend on [features] tracing = ["dep:tracing"] diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index 2936da58..d26c506d 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog], and this project adheres to [Semantic Versioning]. -# Unreleased +# 0.9.4 - **added:** The `response::Attachment` type ([#2789]) diff --git a/axum-extra/Cargo.toml b/axum-extra/Cargo.toml index 3032f661..06aa0766 100644 --- a/axum-extra/Cargo.toml +++ b/axum-extra/Cargo.toml @@ -9,7 +9,7 @@ license = "MIT" name = "axum-extra" readme = "README.md" repository = "https://github.com/tokio-rs/axum" -version = "0.9.3" +version = "0.9.4" [features] default = ["tracing"] @@ -39,8 +39,8 @@ typed-header = ["dep:headers"] typed-routing = ["dep:axum-macros", "dep:percent-encoding", "dep:serde_html_form", "dep:form_urlencoded"] [dependencies] -axum = { path = "../axum", version = "0.7.2", default-features = false } -axum-core = { path = "../axum-core", version = "0.4.3" } +axum = { path = "../axum", version = "0.7.6", default-features = false } +axum-core = { path = "../axum-core", version = "0.4.4" } bytes = "1.1.0" futures-util = { version = "0.3", default-features = false, features = ["alloc"] } http = "1.0.0" @@ -54,7 +54,7 @@ tower-layer = "0.3" tower-service = "0.3" # optional dependencies -axum-macros = { path = "../axum-macros", version = "0.4.1", optional = true } +axum-macros = { path = "../axum-macros", version = "0.4.2", optional = true } cookie = { package = "cookie", version = "0.18.0", features = ["percent-encode"], optional = true } form_urlencoded = { version = "1.1.0", optional = true } headers = { version = "0.4.0", optional = true } diff --git a/axum-macros/CHANGELOG.md b/axum-macros/CHANGELOG.md index 6d9fa42b..b707dda9 100644 --- a/axum-macros/CHANGELOG.md +++ b/axum-macros/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -# Unreleased +# 0.4.2 - **added:** Add `#[debug_middleware]` ([#1993], [#2725]) diff --git a/axum-macros/Cargo.toml b/axum-macros/Cargo.toml index b51e9f8a..d24af7e5 100644 --- a/axum-macros/Cargo.toml +++ b/axum-macros/Cargo.toml @@ -9,7 +9,7 @@ license = "MIT" name = "axum-macros" readme = "README.md" repository = "https://github.com/tokio-rs/axum" -version = "0.4.1" # remember to also bump the version that axum and axum-extra depends on +version = "0.4.2" # remember to also bump the version that axum and axum-extra depends on [features] default = [] diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 1a69f7f7..0379672c 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -# Unreleased +# 0.7.6 - **change:** Avoid cloning `Arc` during deserialization of `Path` - **added:** `axum::serve::Serve::tcp_nodelay` and `axum::serve::WithGracefulShutdown::tcp_nodelay` ([#2653]) diff --git a/axum/Cargo.toml b/axum/Cargo.toml index ff3de511..4ef57819 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "axum" -version = "0.7.5" +version = "0.7.6" categories = ["asynchronous", "network-programming", "web-programming::http-server"] description = "Web framework that focuses on ergonomics and modularity" edition = "2021" @@ -42,7 +42,7 @@ __private_docs = ["tower/full", "dep:tower-http"] [dependencies] async-trait = "0.1.67" -axum-core = { path = "../axum-core", version = "0.4.3" } +axum-core = { path = "../axum-core", version = "0.4.4" } bytes = "1.0" futures-util = { version = "0.3", default-features = false, features = ["alloc"] } http = "1.0.0" @@ -62,7 +62,7 @@ tower-layer = "0.3.2" tower-service = "0.3" # optional dependencies -axum-macros = { path = "../axum-macros", version = "0.4.1", optional = true } +axum-macros = { path = "../axum-macros", version = "0.4.2", optional = true } base64 = { version = "0.21.0", optional = true } hyper = { version = "1.1.0", optional = true } hyper-util = { version = "0.1.3", features = ["tokio", "server", "service"], optional = true }