From b05e8fa8a6d6c919234df8fa2e1406435165304c Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Sat, 22 Jun 2024 13:42:52 +0800 Subject: [PATCH] remove generic type param from IntoCtx. (#1036) * remove generic type param from IntoCtx. * doc test fix. * codegen doc fix. * fix example * doc fmt fix. --- codegen/src/lib.rs | 25 ---------------- examples/multi-http-services/Cargo.toml | 2 +- http/src/h2/proto/headers.rs | 14 ++++----- http/src/util/middleware/context.rs | 4 +-- tls/src/rustls_uring.rs | 10 +++++-- web/CHANGES.md | 4 ++- web/Cargo.toml | 2 +- web/src/app/mod.rs | 40 ++++++++++++++++++------- web/src/error/mod.rs | 10 +++---- web/src/middleware/mod.rs | 22 +++++++------- 10 files changed, 66 insertions(+), 67 deletions(-) diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index 317abcf9e..d9b38b50a 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -26,31 +26,6 @@ pub fn state_impl(item: TokenStream) -> TokenStream { /// `method = get` for example. /// - `enclosed = `: typed middleware applied to route. /// - `enclosed_fn = `: async function as middleware applied to route -/// -/// # Example -/// ```rust(no_run) -/// # use xitca_web::{codegen::route, handler::handler_service, service::Service, App, WebContext}; -/// #[route("/", method = get, enclosed_fn = middleware_fn)] -/// async fn index() -> &'static str { -/// "" -/// } -/// -/// async fn middleware_fn(service: &S, ctx: WebContext<'_, C, B>) -> Result -/// where -/// S: for<'r> Service, Response = Res, Error = Err> -/// { -/// service.call(ctx).await -/// } -/// -/// App::new() -/// // add generated index typed route to application. -/// .at_typed(index) -/// # .at("/nah", handler_service(nah)); -/// -/// # async fn nah(_: &WebContext<'_>) -> &'static str { -/// # // needed to infer the body type of request -/// # "" -/// # } /// ``` #[proc_macro_attribute] pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream { diff --git a/examples/multi-http-services/Cargo.toml b/examples/multi-http-services/Cargo.toml index beee8a480..ff80b5339 100644 --- a/examples/multi-http-services/Cargo.toml +++ b/examples/multi-http-services/Cargo.toml @@ -11,7 +11,7 @@ xitca-service = "0.1" openssl = "0.10.44" quinn = { version = "0.11", features = ["ring"] } -rustls = "0.23" +rustls = { version = "0.23", default-features = false, features = ["ring"] } rustls-pemfile = "2" tracing = { version = "0.1.40", default-features = false } tracing-subscriber = { version = "0.3.16", default-features = false, features = ["env-filter", "fmt"] } diff --git a/http/src/h2/proto/headers.rs b/http/src/h2/proto/headers.rs index e2b72a569..a6ac5ff1f 100644 --- a/http/src/h2/proto/headers.rs +++ b/http/src/h2/proto/headers.rs @@ -226,10 +226,9 @@ where (self.header_block.pseudo, self.header_block.fields) } - #[cfg(feature = "unstable")] - pub fn pseudo_mut(&mut self) -> &mut Pseudo { - &mut self.header_block.pseudo - } + // pub fn pseudo_mut(&mut self) -> &mut Pseudo { + // &mut self.header_block.pseudo + // } pub fn fields(&self) -> &HeaderMap { &self.header_block.fields @@ -371,10 +370,9 @@ impl Pseudo { ResponsePseudo { status } } - #[cfg(feature = "unstable")] - pub fn set_status(&mut self, value: StatusCode) { - self.status = Some(value); - } + // pub fn set_status(&mut self, value: StatusCode) { + // self.status = Some(value); + // } pub fn set_scheme(&mut self, scheme: uri::Scheme) { let bytes_str = match scheme.as_str() { diff --git a/http/src/util/middleware/context.rs b/http/src/util/middleware/context.rs index 0476fe856..6507deb59 100644 --- a/http/src/util/middleware/context.rs +++ b/http/src/util/middleware/context.rs @@ -11,9 +11,7 @@ use crate::http::{BorrowReq, BorrowReqMut}; /// State is roughly doing the same thing as `move ||` style closure capture. The difference comes /// down to: /// -/// - The captured state is constructed lazily when [Service::call] method is -/// called. -/// +/// - The captured state is constructed lazily when [Service::call] method is called. /// - State can be referenced in nested types and beyond closures. /// /// # Example: diff --git a/tls/src/rustls_uring.rs b/tls/src/rustls_uring.rs index 19cb73037..31f290997 100644 --- a/tls/src/rustls_uring.rs +++ b/tls/src/rustls_uring.rs @@ -24,8 +24,14 @@ use self::buf::WriteBuf; /// For now due to design limitation TlsStream offers concurrency with [AsyncBufRead::read] and /// [AsyncBufWrite::write] but in either case the async function must run to completion and cancel /// it prematurely would cause panic. -/// ```rust(no_run) -/// # async fn complete(stream: TlsStream) { +/// ``` +/// use xitca_io::{ +/// io_uring::{AsyncBufRead, AsyncBufWrite}, +/// net::io_uring::TcpStream +/// }; +/// use xitca_tls::rustls_uring::{ServerConnection, TlsStream}; +/// +/// async fn complete(stream: TlsStream) { /// let _ = stream.read(vec![0; 128]).await; /// let _ = stream.read(vec![0; 128]).await; // serialize read to complete is ok. /// diff --git a/web/CHANGES.md b/web/CHANGES.md index 5ae96afcc..80c65dfd7 100644 --- a/web/CHANGES.md +++ b/web/CHANGES.md @@ -1,4 +1,6 @@ -# unreleased +# unreleased 0.5.1 +## Change +- remove generic type param from `IntoCtx` trait # 0.5.0 ## Add diff --git a/web/Cargo.toml b/web/Cargo.toml index ebd3157a4..7c9395002 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xitca-web" -version = "0.5.0" +version = "0.5.1" edition = "2021" license = "Apache-2.0" description = "an async web framework" diff --git a/web/src/app/mod.rs b/web/src/app/mod.rs index 36a7f7911..2444c47e6 100644 --- a/web/src/app/mod.rs +++ b/web/src/app/mod.rs @@ -41,18 +41,24 @@ type DefaultWebObject = WebObject = AppRouter, Infallible>>; // helper trait to poly between () and Box as application state. -pub trait IntoCtx { - fn into_ctx(self) -> impl Fn() -> BoxFuture + Send + Sync; +pub trait IntoCtx { + type Ctx; + + fn into_ctx(self) -> impl Fn() -> BoxFuture + Send + Sync; } -impl IntoCtx<()> for () { - fn into_ctx(self) -> impl Fn() -> BoxFuture<()> + Send + Sync { +impl IntoCtx for () { + type Ctx = (); + + fn into_ctx(self) -> impl Fn() -> BoxFuture + Send + Sync { || Box::pin(ready(Ok(()))) } } -impl IntoCtx for CtxBuilder { - fn into_ctx(self) -> impl Fn() -> BoxFuture + Send + Sync { +impl IntoCtx for CtxBuilder { + type Ctx = C; + + fn into_ctx(self) -> impl Fn() -> BoxFuture + Send + Sync { self } } @@ -449,7 +455,7 @@ where where R::Response: ReadyService + for<'r> Service, Response = WebResponse, Error = SE>, SE: for<'r> Service, Response = WebResponse, Error = Infallible>, - CF: IntoCtx, + CF: IntoCtx, C: 'static, { let App { ctx_builder, router } = self; @@ -469,7 +475,7 @@ where SE: for<'r> Service, Response = WebResponse, Error = Infallible> + 'static, ResB: Stream> + 'static, BE: error::Error + Send + Sync + 'static, - CF: IntoCtx + 'static, + CF: IntoCtx + 'static, C: 'static, { struct BoxApp(S); @@ -508,7 +514,7 @@ where R::Response: ReadyService + for<'r> Service, Response = WebResponse, Error = SE>, SE: for<'r> Service, Response = WebResponse, Error = Infallible> + 'static, ResB: 'static, - CF: IntoCtx + 'static, + CF: IntoCtx + 'static, C: 'static, { crate::server::HttpServer::serve(self.finish()) @@ -535,7 +541,19 @@ where } } -impl Service for App +impl Service for App +where + R: Service, +{ + type Response = R::Response; + type Error = R::Error; + + async fn call(&self, req: Arg) -> Result { + self.router.call(req).await + } +} + +impl Service for App> where R: Service, { @@ -543,6 +561,8 @@ where type Error = R::Error; async fn call(&self, req: Arg) -> Result { + // TODO: + // enable nesting application state? self.router.call(req).await } } diff --git a/web/src/error/mod.rs b/web/src/error/mod.rs index 19689e35b..58d69d6c9 100644 --- a/web/src/error/mod.rs +++ b/web/src/error/mod.rs @@ -3,13 +3,13 @@ //! In xitca-web error is treated as high level type and handled lazily. //! //! - high level: -//! An error type is represented firstly and mostly as a Rust type with useful trait bounds.It doesn't -//! necessarily mapped and/or converted into http response immediately. User is encouraged to pass the -//! error value around and convert it to http response on condition they prefer. +//! An error type is represented firstly and mostly as a Rust type with useful trait bounds.It doesn't +//! necessarily mapped and/or converted into http response immediately. User is encouraged to pass the +//! error value around and convert it to http response on condition they prefer. //! //! - lazy: -//! Since an error is passed as value mostly the error is handled lazily when the value is needed. -//! Including but not limiting to: formatting, logging, generating http response. +//! Since an error is passed as value mostly the error is handled lazily when the value is needed. +//! Including but not limiting to: formatting, logging, generating http response. //! //! # Example //! ```rust diff --git a/web/src/middleware/mod.rs b/web/src/middleware/mod.rs index db5c29e65..91151e222 100644 --- a/web/src/middleware/mod.rs +++ b/web/src/middleware/mod.rs @@ -263,24 +263,24 @@ //! ``` //! The main take away from the table should be: //! - a type enclosed by middleware(s) is always the last one to take ownership of input request -//! and the first one to take ownership of output response (it's tasked with producing it) +//! and the first one to take ownership of output response (it's tasked with producing it) //! - a middleware always take ownership of input before the type it enclosed on and always take -//! ownership of output response after +//! ownership of output response after //! - multiple middlewares always go in a reverse order between input request and output -//! response: the input request goes from bottom to top.(in above example it goes from -//! `middleware2 -> middleware1 -> handler`). And the output response goes from top to bottom -//! (in above example it goes from `handler -> middleware1 -> middleware2`) +//! response: the input request goes from bottom to top.(in above example it goes from +//! `middleware2 -> middleware1 -> handler`). And the output response goes from top to bottom +//! (in above example it goes from `handler -> middleware1 -> middleware2`) //! - `enclosed` and `enclosed_fn` share the same ordering rule however they are mixed in usage //! //! # Type mutation //! - In [`WebContext<'_, C, B>`] type the last generic type param `B` is for http body type. -//! middleware and service are free to transform it's type and constraint it's inner/next service -//! to accept the new type as it's request http body type. [`DeCompress`] and [`Limit`] middleware are -//! examples of this practice. [`TypeEraser`] middleware on the other hand can be used to reserve the -//! type mutation and restore `B` type to it's default as [`RequestBody`] type. In this case web context -//! can be written in short form as [`WebContext<'_, C>`]. +//! middleware and service are free to transform it's type and constraint it's inner/next service +//! to accept the new type as it's request http body type. [`DeCompress`] and [`Limit`] middleware are +//! examples of this practice. [`TypeEraser`] middleware on the other hand can be used to reserve the +//! type mutation and restore `B` type to it's default as [`RequestBody`] type. In this case web context +//! can be written in short form as [`WebContext<'_, C>`]. //! - [`WebResponse`] type share the characteristic as web context type. The `B` type can be transform -//! into new type by services and middleware while type eraser is able to reverse the process. +//! into new type by services and middleware while type eraser is able to reverse the process. //! //! [`App::with_state`]: crate::App::with_state //! [`Service`]: crate::service::Service