Compare commits

...

10 Commits

Author SHA1 Message Date
urane 4dcbc0e6b0
Merge f4118bb01f into 024addfc40 2026-01-06 06:08:01 +09:00
dependabot[bot] 024addfc40
build(deps): bump serde_json from 1.0.145 to 1.0.148 (#3865)
Bumps [serde_json](https://github.com/serde-rs/json) from 1.0.145 to 1.0.148.
- [Release notes](https://github.com/serde-rs/json/releases)
- [Commits](https://github.com/serde-rs/json/compare/v1.0.145...v1.0.148)

---
updated-dependencies:
- dependency-name: serde_json
  dependency-version: 1.0.148
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-12-30 00:51:22 +00:00
dependabot[bot] 06ad9309b8
build(deps): bump itoa from 1.0.15 to 1.0.17 (#3863)
Bumps [itoa](https://github.com/dtolnay/itoa) from 1.0.15 to 1.0.17.
- [Release notes](https://github.com/dtolnay/itoa/releases)
- [Commits](https://github.com/dtolnay/itoa/compare/1.0.15...1.0.17)

---
updated-dependencies:
- dependency-name: itoa
  dependency-version: 1.0.17
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-12-30 00:51:03 +00:00
dependabot[bot] 9f9855d1a2
build(deps): bump tracing from 0.1.43 to 0.1.44 (#3864)
Bumps [tracing](https://github.com/tokio-rs/tracing) from 0.1.43 to 0.1.44.
- [Release notes](https://github.com/tokio-rs/tracing/releases)
- [Commits](https://github.com/tokio-rs/tracing/compare/tracing-0.1.43...tracing-0.1.44)

---
updated-dependencies:
- dependency-name: tracing
  dependency-version: 0.1.44
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-12-30 00:50:49 +00:00
dependabot[bot] 4c62e88edb
build(deps): bump derive_more from 2.1.0 to 2.1.1 (#3862)
Bumps [derive_more](https://github.com/JelteF/derive_more) from 2.1.0 to 2.1.1.
- [Release notes](https://github.com/JelteF/derive_more/releases)
- [Changelog](https://github.com/JelteF/derive_more/blob/master/CHANGELOG.md)
- [Commits](https://github.com/JelteF/derive_more/compare/v2.1.0...v2.1.1)

---
updated-dependencies:
- dependency-name: derive_more
  dependency-version: 2.1.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-12-29 23:15:28 +00:00
dependabot[bot] 2d84d20ebd
build(deps): bump taiki-e/install-action from 2.63.1 to 2.65.6 (#3861)
Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.63.1 to 2.65.6.
- [Release notes](https://github.com/taiki-e/install-action/releases)
- [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md)
- [Commits](61e5998d10...28a9d316db)

---
updated-dependencies:
- dependency-name: taiki-e/install-action
  dependency-version: 2.65.6
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-12-29 23:15:16 +00:00
dependabot[bot] 2b8db90c4d
build(deps): bump rustls-pki-types from 1.13.1 to 1.13.2 (#3860)
Bumps [rustls-pki-types](https://github.com/rustls/pki-types) from 1.13.1 to 1.13.2.
- [Release notes](https://github.com/rustls/pki-types/releases)
- [Commits](https://github.com/rustls/pki-types/compare/v/1.13.1...v/1.13.2)

---
updated-dependencies:
- dependency-name: rustls-pki-types
  dependency-version: 1.13.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-12-29 01:06:46 +00:00
urane f4118bb01f
Merge branch 'master' into 2866 2025-09-26 08:38:43 +01:00
uraneko 89527c5ec5 resolve conflict (removes line added by mistake) 2025-09-26 08:36:30 +01:00
uraneko 390555bdb6 add function single generic type support 2025-09-26 07:26:14 +01:00
8 changed files with 157 additions and 48 deletions

View File

@ -49,7 +49,7 @@ jobs:
toolchain: ${{ matrix.version.version }} toolchain: ${{ matrix.version.version }}
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
uses: taiki-e/install-action@61e5998d108b2b55a81b9b386c18bd46e4237e4f # v2.63.1 uses: taiki-e/install-action@28a9d316db64b78a951f3f8587a5d08cc97ad8eb # v2.65.6
with: with:
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
@ -83,7 +83,7 @@ jobs:
uses: actions-rust-lang/setup-rust-toolchain@1780873c7b576612439a134613cc4cc74ce5538c # v1.15.2 uses: actions-rust-lang/setup-rust-toolchain@1780873c7b576612439a134613cc4cc74ce5538c # v1.15.2
- name: Install just, cargo-hack - name: Install just, cargo-hack
uses: taiki-e/install-action@61e5998d108b2b55a81b9b386c18bd46e4237e4f # v2.63.1 uses: taiki-e/install-action@28a9d316db64b78a951f3f8587a5d08cc97ad8eb # v2.65.6
with: with:
tool: just,cargo-hack tool: just,cargo-hack

View File

@ -64,7 +64,7 @@ jobs:
toolchain: ${{ matrix.version.version }} toolchain: ${{ matrix.version.version }}
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
uses: taiki-e/install-action@61e5998d108b2b55a81b9b386c18bd46e4237e4f # v2.63.1 uses: taiki-e/install-action@28a9d316db64b78a951f3f8587a5d08cc97ad8eb # v2.65.6
with: with:
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
@ -117,7 +117,7 @@ jobs:
toolchain: nightly toolchain: nightly
- name: Install just - name: Install just
uses: taiki-e/install-action@61e5998d108b2b55a81b9b386c18bd46e4237e4f # v2.63.1 uses: taiki-e/install-action@28a9d316db64b78a951f3f8587a5d08cc97ad8eb # v2.65.6
with: with:
tool: just tool: just

View File

@ -24,7 +24,7 @@ jobs:
components: llvm-tools components: llvm-tools
- name: Install just, cargo-llvm-cov, cargo-nextest - name: Install just, cargo-llvm-cov, cargo-nextest
uses: taiki-e/install-action@61e5998d108b2b55a81b9b386c18bd46e4237e4f # v2.63.1 uses: taiki-e/install-action@28a9d316db64b78a951f3f8587a5d08cc97ad8eb # v2.65.6
with: with:
tool: just,cargo-llvm-cov,cargo-nextest tool: just,cargo-llvm-cov,cargo-nextest

View File

@ -77,7 +77,7 @@ jobs:
toolchain: ${{ vars.RUST_VERSION_EXTERNAL_TYPES }} toolchain: ${{ vars.RUST_VERSION_EXTERNAL_TYPES }}
- name: Install just - name: Install just
uses: taiki-e/install-action@61e5998d108b2b55a81b9b386c18bd46e4237e4f # v2.63.1 uses: taiki-e/install-action@28a9d316db64b78a951f3f8587a5d08cc97ad8eb # v2.65.6
with: with:
tool: just tool: just

36
Cargo.lock generated
View File

@ -1117,18 +1117,18 @@ dependencies = [
[[package]] [[package]]
name = "derive_more" name = "derive_more"
version = "2.1.0" version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10b768e943bed7bf2cab53df09f4bc34bfd217cdb57d971e769874c9a6710618" checksum = "d751e9e49156b02b44f9c1815bcb94b984cdcc4396ecc32521c739452808b134"
dependencies = [ dependencies = [
"derive_more-impl", "derive_more-impl",
] ]
[[package]] [[package]]
name = "derive_more-impl" name = "derive_more-impl"
version = "2.1.0" version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d286bfdaf75e988b4a78e013ecd79c581e06399ab53fbacd2d916c2f904f30b" checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb"
dependencies = [ dependencies = [
"convert_case", "convert_case",
"proc-macro2", "proc-macro2",
@ -1833,9 +1833,9 @@ dependencies = [
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.15" version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2"
[[package]] [[package]]
name = "jiff" name = "jiff"
@ -2544,9 +2544,9 @@ dependencies = [
[[package]] [[package]]
name = "rustls-pki-types" name = "rustls-pki-types"
version = "1.13.1" version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "708c0f9d5f54ba0272468c1d306a52c495b31fa155e91bc25371e6df7996908c" checksum = "21e6f2ab2928ca4291b86736a8bd920a277a399bba1589409d72154ff87c1282"
dependencies = [ dependencies = [
"zeroize", "zeroize",
] ]
@ -2697,15 +2697,15 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.145" version = "1.0.148"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c" checksum = "3084b546a1dd6289475996f182a22aba973866ea8e8b02c51d9f46b1336a22da"
dependencies = [ dependencies = [
"itoa", "itoa",
"memchr", "memchr",
"ryu",
"serde", "serde",
"serde_core", "serde_core",
"zmij",
] ]
[[package]] [[package]]
@ -3192,9 +3192,9 @@ checksum = "df8b2b54733674ad286d16267dcfc7a71ed5c776e4ac7aa3c3e2561f7c637bf2"
[[package]] [[package]]
name = "tracing" name = "tracing"
version = "0.1.43" version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d15d90a0b5c19378952d479dc858407149d7bb45a14de0142f6c534b16fc647" checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
dependencies = [ dependencies = [
"log", "log",
"pin-project-lite", "pin-project-lite",
@ -3215,9 +3215,9 @@ dependencies = [
[[package]] [[package]]
name = "tracing-core" name = "tracing-core"
version = "0.1.35" version = "0.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a04e24fab5c89c6a36eb8558c9656f30d81de51dfa4d3b45f26b21d61fa0a6c" checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
dependencies = [ dependencies = [
"once_cell", "once_cell",
] ]
@ -3872,6 +3872,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "zmij"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f4a4e8e9dc5c62d159f04fcdbe07f4c3fb710415aab4754bf11505501e3251d"
[[package]] [[package]]
name = "zstd" name = "zstd"
version = "0.13.3" version = "0.13.3"

View File

@ -328,6 +328,9 @@ pub struct Route {
/// Name of the handler function being annotated. /// Name of the handler function being annotated.
name: syn::Ident, name: syn::Ident,
/// function generic
type_generic: Option<syn::TypeParam>,
/// Args passed to routing macro. /// Args passed to routing macro.
/// ///
/// When using `#[routes]`, this will contain args for each specific routing macro. /// When using `#[routes]`, this will contain args for each specific routing macro.
@ -344,6 +347,13 @@ impl Route {
pub fn new(args: RouteArgs, ast: syn::ItemFn, method: Option<MethodType>) -> syn::Result<Self> { pub fn new(args: RouteArgs, ast: syn::ItemFn, method: Option<MethodType>) -> syn::Result<Self> {
let name = ast.sig.ident.clone(); let name = ast.sig.ident.clone();
let generics = ast.sig.generics.params.clone();
let type_generic = if let Some(syn::GenericParam::Type(ty)) = generics.into_iter().next() {
Some(ty)
} else {
None
};
// Try and pull out the doc comments so that we can reapply them to the generated struct. // Try and pull out the doc comments so that we can reapply them to the generated struct.
// Note that multi line doc comments are converted to multiple doc attributes. // Note that multi line doc comments are converted to multiple doc attributes.
let doc_attributes = ast let doc_attributes = ast
@ -370,6 +380,7 @@ impl Route {
} }
Ok(Self { Ok(Self {
type_generic,
name, name,
args: vec![args], args: vec![args],
ast, ast,
@ -380,6 +391,13 @@ impl Route {
fn multiple(args: Vec<Args>, ast: syn::ItemFn) -> syn::Result<Self> { fn multiple(args: Vec<Args>, ast: syn::ItemFn) -> syn::Result<Self> {
let name = ast.sig.ident.clone(); let name = ast.sig.ident.clone();
let generics = ast.sig.generics.params.clone();
let type_generic = if let Some(syn::GenericParam::Type(ty)) = generics.into_iter().next() {
Some(ty)
} else {
None
};
// Try and pull out the doc comments so that we can reapply them to the generated struct. // Try and pull out the doc comments so that we can reapply them to the generated struct.
// Note that multi line doc comments are converted to multiple doc attributes. // Note that multi line doc comments are converted to multiple doc attributes.
let doc_attributes = ast let doc_attributes = ast
@ -398,6 +416,7 @@ impl Route {
Ok(Self { Ok(Self {
name, name,
type_generic,
args, args,
ast, ast,
doc_attributes, doc_attributes,
@ -409,6 +428,7 @@ impl ToTokens for Route {
fn to_tokens(&self, output: &mut TokenStream2) { fn to_tokens(&self, output: &mut TokenStream2) {
let Self { let Self {
name, name,
type_generic,
ast, ast,
args, args,
doc_attributes, doc_attributes,
@ -421,6 +441,17 @@ impl ToTokens for Route {
#[cfg(feature = "compat-routing-macros-force-pub")] #[cfg(feature = "compat-routing-macros-force-pub")]
let vis = syn::Visibility::Public(<Token![pub]>::default()); let vis = syn::Visibility::Public(<Token![pub]>::default());
let (struct_generic, trait_generic, impl_type_generic) =
if let Some(syn::TypeParam { ident, bounds, .. }) = type_generic {
(
Some(quote! { <#ident> (core::marker::PhantomData<T>) }),
Some(quote! { <#ident: #bounds + 'static> }),
Some(quote! { <#ident> }),
)
} else {
(None, None, None)
};
let registrations: TokenStream2 = args let registrations: TokenStream2 = args
.iter() .iter()
.map(|args| { .map(|args| {
@ -453,13 +484,19 @@ impl ToTokens for Route {
} }
}; };
let type_generic = if let Some(syn::TypeParam { ident, .. }) = type_generic {
Some(quote! { ::<#ident> })
} else {
None
};
quote! { quote! {
let __resource = ::actix_web::Resource::new(#path) let __resource = ::actix_web::Resource::new(#path)
.name(#resource_name) .name(#resource_name)
#method_guards #method_guards
#(.guard(::actix_web::guard::fn_guard(#guards)))* #(.guard(::actix_web::guard::fn_guard(#guards)))*
#(.wrap(#wrappers))* #(.wrap(#wrappers))*
.to(#name); .to(#name #type_generic);
::actix_web::dev::HttpServiceFactory::register(__resource, __config); ::actix_web::dev::HttpServiceFactory::register(__resource, __config);
} }
}) })
@ -468,9 +505,11 @@ impl ToTokens for Route {
let stream = quote! { let stream = quote! {
#(#doc_attributes)* #(#doc_attributes)*
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#vis struct #name; #[derive(Default)]
#vis struct #name #struct_generic;
impl ::actix_web::dev::HttpServiceFactory for #name { impl #trait_generic ::actix_web::dev::HttpServiceFactory for #name #impl_type_generic
{
fn register(self, __config: &mut actix_web::dev::AppService) { fn register(self, __config: &mut actix_web::dev::AppService) {
#ast #ast
#registrations #registrations

View File

@ -5,10 +5,10 @@ description = "Actix Web is a powerful, pragmatic, and extremely fast web framew
authors = ["Nikolay Kim <fafhrd91@gmail.com>", "Rob Ede <robjtede@icloud.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>", "Rob Ede <robjtede@icloud.com>"]
keywords = ["actix", "http", "web", "framework", "async"] keywords = ["actix", "http", "web", "framework", "async"]
categories = [ categories = [
"network-programming", "network-programming",
"asynchronous", "asynchronous",
"web-programming::http-server", "web-programming::http-server",
"web-programming::websocket", "web-programming::websocket",
] ]
homepage = "https://actix.rs" homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-web" repository = "https://github.com/actix/actix-web"
@ -18,17 +18,17 @@ rust-version.workspace = true
[package.metadata.docs.rs] [package.metadata.docs.rs]
features = [ features = [
"macros", "macros",
"openssl", "openssl",
"rustls-0_20", "rustls-0_20",
"rustls-0_21", "rustls-0_21",
"rustls-0_22", "rustls-0_22",
"rustls-0_23", "rustls-0_23",
"compress-brotli", "compress-brotli",
"compress-gzip", "compress-gzip",
"compress-zstd", "compress-zstd",
"cookies", "cookies",
"secure-cookies", "secure-cookies",
] ]
[package.metadata.cargo_check_external_types] [package.metadata.cargo_check_external_types]
@ -58,15 +58,15 @@ allowed_external_types = [
[features] [features]
default = [ default = [
"macros", "macros",
"compress-brotli", "compress-brotli",
"compress-gzip", "compress-gzip",
"compress-zstd", "compress-zstd",
"cookies", "cookies",
"http2", "http2",
"unicode", "unicode",
"compat", "compat",
"ws", "ws",
] ]
# Brotli algorithm content-encoding support # Brotli algorithm content-encoding support
@ -203,6 +203,10 @@ required-features = ["compress-brotli", "compress-gzip", "compress-zstd"]
name = "basic" name = "basic"
required-features = ["compress-gzip"] required-features = ["compress-gzip"]
[[example]]
name = "issue"
path = "examples/2866.rs"
[[example]] [[example]]
name = "uds" name = "uds"
required-features = ["compress-gzip"] required-features = ["compress-gzip"]

View File

@ -0,0 +1,60 @@
use actix_web::{
dev::Server,
get,
web::{self, Data},
App, HttpServer, Responder,
};
use serde::Serialize;
#[derive(Debug, Serialize, Clone, Copy)]
pub struct User {
id: u64,
}
pub trait UserRepository {
fn get_user(&self) -> User;
}
#[derive(Clone)]
struct UserClient;
impl UserRepository for UserClient {
fn get_user(&self) -> User {
User { id: 99 }
}
}
// when uncommenting following the line, the type checking is unaccepted
// because of cannot infer type parameter T
#[get("/")]
async fn index<T: UserRepository>(client: web::Data<T>) -> impl Responder {
let user = client.into_inner().get_user();
web::Json(user)
}
#[get("hello/{who}")]
async fn hello(who: web::Path<String>) -> impl Responder {
format!("<h1>hello {who}</h1>")
}
pub fn create_server<T: UserRepository + Send + Sync + 'static + Clone>(
search: T,
) -> Result<Server, std::io::Error> {
let server = HttpServer::new(move || {
App::new()
.app_data(Data::new(search.clone()))
// .route("/", web::get().to(index::<T>))
.service(index::<T>(core::marker::PhantomData::<T>))
.service(hello)
})
.bind("127.0.0.1:8080")?
.run();
Ok(server)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
println!("\x1b[1;2;36mserving on http://localhost:8080\x1b[0m");
let user_client = UserClient;
create_server(user_client).unwrap().await
}