Skip to content

Commit

Permalink
enabling async closure as middleware function (#1161)
Browse files Browse the repository at this point in the history
* add nightly feature enabling async closure as middleware

* fix test

* add test for async closure

* remove broken test

* bump msrv

* change log fix

* switch to beta channel for ci testing.

* clippy fix

* ci update
  • Loading branch information
fakeshadow authored Feb 21, 2025
1 parent aeb734a commit 7b96fdb
Show file tree
Hide file tree
Showing 109 changed files with 320 additions and 334 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- { name: Linux, os: ubuntu-latest }
- { name: macOS, os: macos-latest }
version:
- 1.79
- 1.85
name: http check @ ${{ matrix.target.name }} - ${{ matrix.version }}
runs-on: ${{ matrix.target.os }}

Expand Down Expand Up @@ -98,7 +98,7 @@ jobs:
- { name: Linux, os: ubuntu-latest }
- { name: macOS, os: macos-latest }
version:
- 1.79
- 1.85
name: client check @ ${{ matrix.target.name }} - ${{ matrix.version }}
runs-on: ${{ matrix.target.os }}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

# Minimum Supported Rust Version

The latest release of the crate supports 1.79 and above rust versions.
The latest release of the crate supports 1.85 and above rust versions.

**[⬆️ Back to Top](#xitca-web)**

Expand Down
7 changes: 4 additions & 3 deletions codegen/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# unreleased 0.4.0
## Fix
- fix `xitca_web::WebContext` parsing when generic body type is presented.

## Change
- macro is refactored to target xitca-web `0.7.0`
- bump MSRV to `1.85` and Rust edition 2024

## Fix
- fix `xitca_web::WebContext` parsing when generic body type is presented.

# 0.3.1
## Fix
Expand Down
2 changes: 1 addition & 1 deletion codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "xitca-codegen"
version = "0.4.0"
edition = "2021"
edition = "2024"
license = "Apache-2.0"
description = "proc macro for xitca"
repository = "https://github.com/HFQR/xitca-web"
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{spanned::Spanned, Error, ItemImpl, Type};
use syn::{Error, ItemImpl, Type, spanned::Spanned};

pub(crate) fn error(_: TokenStream, item: ItemImpl) -> Result<TokenStream, Error> {
let Type::Path(ref err_ty) = *item.self_ty else {
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod service;
mod state;

use proc_macro::TokenStream;
use syn::{spanned::Spanned, Error, ImplItem, ImplItemFn};
use syn::{Error, ImplItem, ImplItemFn, spanned::Spanned};

#[proc_macro_derive(State, attributes(borrow))]
pub fn state_impl(item: TokenStream) -> TokenStream {
Expand Down
7 changes: 4 additions & 3 deletions codegen/src/route.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{
Error, Expr, FnArg, GenericArgument, ItemFn, PathArguments, Type,
parse::{Parse, ParseStream},
punctuated::Punctuated,
spanned::Spanned,
Error, Expr, FnArg, GenericArgument, ItemFn, PathArguments, Type,
};

pub(crate) fn route(attr: Args, input: ItemFn) -> Result<TokenStream, Error> {
Expand Down Expand Up @@ -105,7 +105,7 @@ pub(crate) fn route(attr: Args, input: ItemFn) -> Result<TokenStream, Error> {
return Err(Error::new(path.span(), format!("expect {ident}<_>")));
};
match arg.args.last() {
Some(GenericArgument::Type(ref ty)) => {
Some(GenericArgument::Type(ty)) => {
state.push(State::Partial(ty));
}
_ => return Err(Error::new(ty.span(), "expect state type.")),
Expand All @@ -115,6 +115,7 @@ pub(crate) fn route(attr: Args, input: ItemFn) -> Result<TokenStream, Error> {
let PathArguments::AngleBracketed(ref arg) = path.arguments else {
return Err(Error::new(path.span(), format!("expect &{ident}<'_, _>")));
};

let mut args = arg.args.iter();

match args.next() {
Expand All @@ -123,7 +124,7 @@ pub(crate) fn route(attr: Args, input: ItemFn) -> Result<TokenStream, Error> {
}

match args.next() {
Some(GenericArgument::Type(ref ty)) => {
Some(GenericArgument::Type(ty)) => {
state.push(State::Full(ty));
}
None => {
Expand Down
16 changes: 8 additions & 8 deletions codegen/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use proc_macro::TokenStream;
use quote::quote;
use syn::{
__private::{Span, TokenStream2},
Error, FnArg, GenericArgument, GenericParam, Ident, ImplItemFn, ItemImpl, Pat, PatIdent, PathArguments, ReturnType,
Stmt, Type, TypePath, WhereClause,
punctuated::Punctuated,
spanned::Spanned,
token::Comma,
Error, FnArg, GenericArgument, GenericParam, Ident, ImplItemFn, ItemImpl, Pat, PatIdent, PathArguments, ReturnType,
Stmt, Type, TypePath, WhereClause,
};

use crate::find_async_method;
Expand Down Expand Up @@ -184,7 +184,7 @@ impl<'a> BuilderImpl<'a> {
return Err(Error::new(
recv.span(),
"new_service method does not accept Self as receiver",
))
));
}
FnArg::Typed(ty) => match (ty.pat.as_ref(), ty.ty.as_ref()) {
(Pat::Wild(_), Type::Reference(ty_ref)) if ty_ref.mutability.is_none() => {
Expand All @@ -197,7 +197,7 @@ impl<'a> BuilderImpl<'a> {
return Err(Error::new(
ty.span(),
"new_service must receive ServiceFactory type as immutable reference",
))
));
}
},
};
Expand All @@ -212,7 +212,7 @@ impl<'a> BuilderImpl<'a> {
return Err(Error::new(
recv.span(),
"new_service method does not accept Self as receiver",
))
));
}
FnArg::Typed(ty) => match ty.pat.as_ref() {
Pat::Wild(_) => (default_pat_ident("_service"), &*ty.ty),
Expand All @@ -221,7 +221,7 @@ impl<'a> BuilderImpl<'a> {
return Err(Error::new(
ty.span(),
"new_service method must use 'arg: Arg' as second function argument",
))
));
}
},
};
Expand Down Expand Up @@ -271,7 +271,7 @@ impl<'a> CallImpl<'a> {
)
})? {
FnArg::Receiver(recv) => {
return Err(Error::new(recv.span(), "call method does not accept Self as receiver"))
return Err(Error::new(recv.span(), "call method does not accept Self as receiver"));
}
FnArg::Typed(ty) => match ty.pat.as_ref() {
Pat::Wild(_) => (default_pat_ident("_req"), &*ty.ty),
Expand All @@ -280,7 +280,7 @@ impl<'a> CallImpl<'a> {
return Err(Error::new(
ty.span(),
"call method must use 'req: Req' as second function argument",
))
));
}
},
};
Expand Down
2 changes: 1 addition & 1 deletion http-ws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ base64 = { version = "0.22.0", default-features = false }
bytes = "1.4"
futures-core = { version = "0.3.25", default-features = false }
http = "1"
rand = { version = "0.8.5" }
rand = { version = "0.9.0" }
sha1 = "0.10"
tracing = { version = "0.1.40", default-features = false }

Expand Down
1 change: 1 addition & 0 deletions http/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# unreleased 0.7.0
## Change
- bump MSRV to `1.85` and Rust edition 2024
- update `xitca-service` to `0.3.0`

# 0.6.0
Expand Down
4 changes: 2 additions & 2 deletions http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "xitca-http"
version = "0.7.0"
edition = "2021"
edition = "2024"
license = "Apache-2.0"
description = "http library for xitca"
repository = "https://github.com/HFQR/xitca-web"
Expand Down Expand Up @@ -38,7 +38,7 @@ router = ["xitca-router"]

[dependencies]
xitca-io = "0.4.0"
xitca-service = { version = "0.3.0", features = ["alloc", "std"] }
xitca-service = { version = "0.3.0", features = ["alloc"] }
xitca-unsafe-collection = { version = "0.2.0", features = ["bytes"] }

futures-core = "0.3.17"
Expand Down
4 changes: 2 additions & 2 deletions http/benches/h1_decode.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::time::SystemTime;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{Criterion, black_box, criterion_group, criterion_main};
use httpdate::HttpDate;
use tokio::time::Instant;
use xitca_http::{
bytes::BytesMut,
date::{DateTime, DATE_VALUE_LENGTH},
date::{DATE_VALUE_LENGTH, DateTime},
h1::proto::context::Context,
};

Expand Down
2 changes: 1 addition & 1 deletion http/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use xitca_service::Service;

use super::{
body::RequestBody,
config::{HttpServiceConfig, DEFAULT_HEADER_LIMIT, DEFAULT_READ_BUF_LIMIT, DEFAULT_WRITE_BUF_LIMIT},
config::{DEFAULT_HEADER_LIMIT, DEFAULT_READ_BUF_LIMIT, DEFAULT_WRITE_BUF_LIMIT, HttpServiceConfig},
service::HttpService,
tls,
};
Expand Down
2 changes: 1 addition & 1 deletion http/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
use httpdate::HttpDate;
use tokio::{
task::JoinHandle,
time::{interval, Instant},
time::{Instant, interval},
};

/// Trait for getting current date/time.
Expand Down
2 changes: 1 addition & 1 deletion http/src/h1/body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::{
cell::{RefCell, RefMut},
future::{poll_fn, Future},
future::{Future, poll_fn},
ops::DerefMut,
pin::Pin,
task::{Context, Poll, Waker},
Expand Down
2 changes: 1 addition & 1 deletion http/src/h1/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::fmt;

use xitca_service::Service;

use crate::builder::{marker, HttpServiceBuilder};
use crate::builder::{HttpServiceBuilder, marker};

use super::service::H1Service;

Expand Down
6 changes: 3 additions & 3 deletions http/src/h1/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::{
convert::Infallible,
future::{pending, poll_fn, Future},
future::{Future, pending, poll_fn},
marker::PhantomData,
net::SocketAddr,
pin::{pin, Pin},
pin::{Pin, pin},
time::Duration,
};

Expand All @@ -25,8 +25,8 @@ use crate::{
error::Error,
},
http::{
response::{Parts, Response},
StatusCode,
response::{Parts, Response},
},
util::{
buffered::{BufferedIo, ListWriteBuf, ReadBuf, WriteBuf},
Expand Down
6 changes: 3 additions & 3 deletions http/src/h1/dispatcher_unreal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use xitca_io::{
io::{AsyncIo, Interest},
net::TcpStream,
};
use xitca_service::{AsyncFn, Service};
use xitca_service::Service;
use xitca_unsafe_collection::bytes::read_buf;

use crate::date::{DateTime, DateTimeHandle, DateTimeService};
Expand Down Expand Up @@ -107,7 +107,7 @@ impl<F, C> Dispatcher<F, C> {

impl<F, C> Service<TcpStream> for Dispatcher<F, C>
where
F: for<'h, 'b> AsyncFn<(Request<'h, C>, Response<'h>), Output = Response<'h, 3>>,
F: for<'h, 'b> AsyncFn(Request<'h, C>, Response<'h>) -> Response<'h, 3>,
{
type Response = ();
type Error = Error;
Expand Down Expand Up @@ -154,7 +154,7 @@ where
date: self.date.get(),
};

self.handler.call((req, res)).await;
(self.handler)(req, res).await;

r_buf.advance(len);
}
Expand Down
12 changes: 6 additions & 6 deletions http/src/h1/dispatcher_uring.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use core::{
cell::RefCell,
fmt,
future::{poll_fn, Future},
future::{Future, poll_fn},
marker::PhantomData,
mem,
net::SocketAddr,
ops::{Deref, DerefMut},
pin::{pin, Pin},
task::{self, ready, Poll, Waker},
pin::{Pin, pin},
task::{self, Poll, Waker, ready},
};

use std::{io, net::Shutdown, rc::Rc};
Expand All @@ -17,7 +17,7 @@ use pin_project_lite::pin_project;
use tracing::trace;
use xitca_io::{
bytes::BytesMut,
io_uring::{write_all, AsyncBufRead, AsyncBufWrite, BoundedBuf},
io_uring::{AsyncBufRead, AsyncBufWrite, BoundedBuf, write_all},
};
use xitca_service::Service;
use xitca_unsafe_collection::futures::SelectOutput;
Expand All @@ -28,12 +28,12 @@ use crate::{
config::HttpServiceConfig,
date::DateTime,
h1::{body::RequestBody, error::Error},
http::{response::Response, StatusCode},
http::{StatusCode, response::Response},
util::timer::{KeepAlive, Timeout},
};

use super::{
dispatcher::{status_only, Timer},
dispatcher::{Timer, status_only},
proto::{
codec::{ChunkResult, TransferCoding},
context::Context,
Expand Down
2 changes: 1 addition & 1 deletion http/src/h1/proto/buf_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::convert::Infallible;
use std::io::Write;

use crate::{
bytes::{buf::Chain, Buf, BufMut, BufMutWriter, Bytes, BytesMut, EitherBuf},
bytes::{Buf, BufMut, BufMutWriter, Bytes, BytesMut, EitherBuf, buf::Chain},
util::buffered::{BufWrite, ListWriteBuf, WriteBuf},
};

Expand Down
2 changes: 1 addition & 1 deletion http/src/h1/proto/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::{mem, net::SocketAddr};

use crate::http::{header::HeaderMap, Extensions};
use crate::http::{Extensions, header::HeaderMap};

/// Context is connection specific struct contain states for processing.
pub struct Context<'a, D, const HEADER_LIMIT: usize> {
Expand Down
2 changes: 1 addition & 1 deletion http/src/h1/proto/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use httparse::Status;
use crate::{
bytes::{Buf, Bytes, BytesMut},
http::{
header::{HeaderMap, HeaderName, HeaderValue, CONNECTION, CONTENT_LENGTH, EXPECT, TRANSFER_ENCODING, UPGRADE},
Extension, Method, Request, RequestExt, Uri, Version,
header::{CONNECTION, CONTENT_LENGTH, EXPECT, HeaderMap, HeaderName, HeaderValue, TRANSFER_ENCODING, UPGRADE},
},
};

Expand Down
4 changes: 2 additions & 2 deletions http/src/h1/proto/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::{
bytes::{Bytes, BytesMut},
date::DateTime,
http::{
header::{HeaderMap, CONNECTION, CONTENT_LENGTH, DATE, SET_COOKIE, TE, TRANSFER_ENCODING, UPGRADE},
response::Parts,
StatusCode, Version,
header::{CONNECTION, CONTENT_LENGTH, DATE, HeaderMap, SET_COOKIE, TE, TRANSFER_ENCODING, UPGRADE},
response::Parts,
},
};

Expand Down
2 changes: 1 addition & 1 deletion http/src/h2/body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::{
pin::Pin,
task::{ready, Context, Poll},
task::{Context, Poll, ready},
};

use futures_core::stream::Stream;
Expand Down
Loading

0 comments on commit 7b96fdb

Please sign in to comment.