diff --git a/service-async/Cargo.toml b/service-async/Cargo.toml index bd594e4..a2ccaa1 100644 --- a/service-async/Cargo.toml +++ b/service-async/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "service-async" -version = "0.1.13" +version = "0.2.0" edition = "2021" authors = ["ChiHai "] @@ -13,10 +13,9 @@ repository = "https://github.com/ihciah/service-async" [dependencies] param = { version = "0.1.2", path = "../param" } -futures-util = "0.3" [target.'cfg(unix)'.dev-dependencies] -monoio = { version = "0.1.5" } +monoio = { version = "0.2.0" } [target.'cfg(not(unix))'.dev-dependencies] tokio = { version = "1", features = ["macros", "rt-multi-thread"] } diff --git a/service-async/examples/demo.rs b/service-async/examples/demo.rs index 336f4d5..770c899 100644 --- a/service-async/examples/demo.rs +++ b/service-async/examples/demo.rs @@ -1,8 +1,5 @@ -#![feature(impl_trait_in_assoc_type)] - use std::{ convert::Infallible, - future::Future, sync::atomic::{AtomicUsize, Ordering}, }; @@ -27,18 +24,13 @@ struct SvcA { impl Service<()> for SvcA { type Response = (); type Error = Infallible; - type Future<'cx> = impl Future> + 'cx - where - Self: 'cx; - - fn call(&self, _req: ()) -> Self::Future<'_> { - async move { - println!( - "SvcA called! pass_flag = {}, not_pass_flag = {}", - self.pass_flag, self.not_pass_flag - ); - Ok(()) - } + + async fn call(&self, _req: ()) -> Result { + println!( + "SvcA called! pass_flag = {}, not_pass_flag = {}", + self.pass_flag, self.not_pass_flag + ); + Ok(()) } } @@ -77,18 +69,13 @@ where { type Response = (); type Error = Infallible; - type Future<'cx> = impl Future> + 'cx - where - Self: 'cx; - - fn call(&self, req: usize) -> Self::Future<'_> { - async move { - let old = self.counter.fetch_add(req, Ordering::AcqRel); - let new = old + req; - println!("SvcB called! {old}->{new}"); - self.inner.call(()).await?; - Ok(()) - } + + async fn call(&self, req: usize) -> Result { + let old = self.counter.fetch_add(req, Ordering::AcqRel); + let new = old + req; + println!("SvcB called! {old}->{new}"); + self.inner.call(()).await?; + Ok(()) } } @@ -127,16 +114,11 @@ where { type Response = (); type Error = Infallible; - type Future<'cx> = impl Future> + 'cx - where - Self: 'cx, I: 'cx; - fn call(&self, req: I) -> Self::Future<'_> { - async move { - println!("SvcC called!"); - self.inner.call(req).await?; - Ok(()) - } + async fn call(&self, req: I) -> Result { + println!("SvcC called!"); + self.inner.call(req).await?; + Ok(()) } } diff --git a/service-async/src/boxed.rs b/service-async/src/boxed.rs index 91f07bd..18a9acf 100644 --- a/service-async/src/boxed.rs +++ b/service-async/src/boxed.rs @@ -1,10 +1,10 @@ use std::{ any::{Any, TypeId}, + future::Future, marker::PhantomData, + pin::Pin, }; -pub use futures_util::future::LocalBoxFuture; - use crate::{MakeService, Service}; pub struct BoxedService { @@ -57,12 +57,9 @@ impl Drop for BoxedService { impl Service for BoxedService { type Response = Response; type Error = E; - type Future<'cx> = LocalBoxFuture<'cx, Result> - where - Self: 'cx, Request: 'cx; #[inline] - fn call(&self, req: Request) -> Self::Future<'_> { + fn call(&self, req: Request) -> impl Future> { unsafe { (self.vtable.call)(self.svc, req) } } } @@ -81,15 +78,14 @@ where } } +type LocalStaticBoxedFuture = Pin> + 'static>>; + struct ServiceVtable { - call: unsafe fn(raw: *const (), req: T) -> LocalBoxFuture<'static, Result>, + call: unsafe fn(raw: *const (), req: T) -> LocalStaticBoxedFuture, drop: unsafe fn(raw: *const ()), } -unsafe fn call( - svc: *const (), - req: R, -) -> LocalBoxFuture<'static, Result> +unsafe fn call(svc: *const (), req: R) -> LocalStaticBoxedFuture where R: 'static, S: Service + 'static, diff --git a/service-async/src/either.rs b/service-async/src/either.rs index f535171..f768fb5 100644 --- a/service-async/src/either.rs +++ b/service-async/src/either.rs @@ -72,13 +72,9 @@ where { type Response = A::Response; type Error = A::Error; - type Future<'cx> = Either, B::Future<'cx>> - where - Self: 'cx, - R: 'cx; #[inline] - fn call(&self, req: R) -> Self::Future<'_> { + fn call(&self, req: R) -> impl Future> { match self { Either::Left(s) => Either::Left(s.call(req)), Either::Right(s) => Either::Right(s.call(req)), diff --git a/service-async/src/lib.rs b/service-async/src/lib.rs index f8f6e6b..7fdad00 100644 --- a/service-async/src/lib.rs +++ b/service-async/src/lib.rs @@ -1,5 +1,3 @@ -#![feature(impl_trait_in_assoc_type)] - use std::{future::Future, sync::Arc}; pub mod either; @@ -21,14 +19,8 @@ pub trait Service { /// Errors produced by the service. type Error; - /// The future response value. - type Future<'cx>: Future> - where - Self: 'cx, - Request: 'cx; - /// Process the request and return the response asynchronously. - fn call(&self, req: Request) -> Self::Future<'_>; + fn call(&self, req: Request) -> impl Future>; } pub trait MakeService { diff --git a/service-async/src/map.rs b/service-async/src/map.rs index 1a5a6c4..7e10379 100644 --- a/service-async/src/map.rs +++ b/service-async/src/map.rs @@ -34,13 +34,8 @@ where type Error = T::Error; - type Future<'cx> = impl Future> + 'cx - where - Self: 'cx, - R: 'cx; - #[inline] - fn call(&self, req: R) -> Self::Future<'_> { + fn call(&self, req: R) -> impl Future> { let req = self.f.map_target(req); self.inner.call(req) }