Compare commits

...

6 Commits

Author SHA1 Message Date
urane 24f6913ab7
Merge f4118bb01f into d119500f93 2025-11-16 15:54:53 +09:00
Yuki Okushi d119500f93
release: actix-web v4.12.0 (#3830) 2025-11-16 15:52:36 +09:00
Yuki Okushi a3f95ee1ef
feat: improve `HttpResponseBuilder::streaming` with SizedStream (#3829) 2025-11-16 15:22:29 +09: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
7 changed files with 149 additions and 43 deletions

2
Cargo.lock generated
View File

@ -345,7 +345,7 @@ dependencies = [
[[package]] [[package]]
name = "actix-web" name = "actix-web"
version = "4.11.0" version = "4.12.0"
dependencies = [ dependencies = [
"actix-codec", "actix-codec",
"actix-files", "actix-files",

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

@ -2,8 +2,10 @@
## Unreleased ## Unreleased
## 4.12.0
- `actix_web::response::builder::HttpResponseBuilder::streaming()` now sets `Content-Type` to `application/octet-stream` if `Content-Type` does not exist. - `actix_web::response::builder::HttpResponseBuilder::streaming()` now sets `Content-Type` to `application/octet-stream` if `Content-Type` does not exist.
- `actix_web::response::builder::HttpResponseBuilder::streaming()` now calls `actix_web::response::builder::HttpResponseBuilder::no_chunking()` if `Content-Length` is set by user. - `actix_web::response::builder::HttpResponseBuilder::streaming()` now calls `actix_web::response::builder::HttpResponseBuilder::no_chunking()` and returns `SizedStream` if `Content-Length` is set by user.
- Add `ws` crate feature (on-by-default) which forwards to `actix-http` and guards some of its `ResponseError` impls. - Add `ws` crate feature (on-by-default) which forwards to `actix-http` and guards some of its `ResponseError` impls.
- Add public export for `EitherExtractError` in `error` module. - Add public export for `EitherExtractError` in `error` module.

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-web" name = "actix-web"
version = "4.11.0" version = "4.12.0"
description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust" description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust"
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"]
@ -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

@ -8,10 +8,10 @@
<!-- prettier-ignore-start --> <!-- prettier-ignore-start -->
[![crates.io](https://img.shields.io/crates/v/actix-web?label=latest)](https://crates.io/crates/actix-web) [![crates.io](https://img.shields.io/crates/v/actix-web?label=latest)](https://crates.io/crates/actix-web)
[![Documentation](https://docs.rs/actix-web/badge.svg?version=4.11.0)](https://docs.rs/actix-web/4.11.0) [![Documentation](https://docs.rs/actix-web/badge.svg?version=4.12.0)](https://docs.rs/actix-web/4.12.0)
![MSRV](https://img.shields.io/badge/rustc-1.72+-ab6000.svg) ![MSRV](https://img.shields.io/badge/rustc-1.72+-ab6000.svg)
![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/actix-web.svg) ![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/actix-web.svg)
[![Dependency Status](https://deps.rs/crate/actix-web/4.11.0/status.svg)](https://deps.rs/crate/actix-web/4.11.0) [![Dependency Status](https://deps.rs/crate/actix-web/4.12.0/status.svg)](https://deps.rs/crate/actix-web/4.12.0)
<br /> <br />
[![CI](https://github.com/actix/actix-web/actions/workflows/ci.yml/badge.svg)](https://github.com/actix/actix-web/actions/workflows/ci.yml) [![CI](https://github.com/actix/actix-web/actions/workflows/ci.yml/badge.svg)](https://github.com/actix/actix-web/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/actix/actix-web/graph/badge.svg?token=dSwOnp9QCv)](https://codecov.io/gh/actix/actix-web) [![codecov](https://codecov.io/gh/actix/actix-web/graph/badge.svg?token=dSwOnp9QCv)](https://codecov.io/gh/actix/actix-web)

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
}

View File

@ -11,7 +11,7 @@ use futures_core::Stream;
use serde::Serialize; use serde::Serialize;
use crate::{ use crate::{
body::{BodyStream, BoxBody, MessageBody}, body::{BodyStream, BoxBody, MessageBody, SizedStream},
dev::Extensions, dev::Extensions,
error::{Error, JsonPayloadError}, error::{Error, JsonPayloadError},
http::{ http::{
@ -335,18 +335,19 @@ impl HttpResponseBuilder {
} }
} }
if let Some(parts) = self.inner() { let content_length = self
if let Some(length) = parts.headers.get(header::CONTENT_LENGTH) { .inner()
if let Ok(length) = length.to_str() { .and_then(|parts| parts.headers.get(header::CONTENT_LENGTH))
if let Ok(length) = length.parse::<u64>() { .and_then(|value| value.to_str().ok())
self.no_chunking(length); .and_then(|value| value.parse::<u64>().ok());
}
}
}
}
if let Some(len) = content_length {
self.no_chunking(len);
self.body(SizedStream::new(len, stream))
} else {
self.body(BodyStream::new(stream)) self.body(BodyStream::new(stream))
} }
}
/// Set a JSON body and build the `HttpResponse`. /// Set a JSON body and build the `HttpResponse`.
/// ///