Skip to content

Commit

Permalink
rework ReadyService trait. (#193)
Browse files Browse the repository at this point in the history
* rework ReadyService trait.

* fix readyservice impls.

* fix codegen for ReadyService.
  • Loading branch information
fakeshadow authored Apr 28, 2022
1 parent 1515b51 commit fda87e8
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 57 deletions.
51 changes: 31 additions & 20 deletions http-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,27 @@ pub fn middleware_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
};

match ready_impl {
Some(ReadyImpl { ready_stmts, ready_res_ty}) => {
quote! {
#base

impl<#generic_ty> ::xitca_service::ready::ReadyService<#req_ty> for #service_ty
#where_clause
{
type Ready = #ready_res_ty;
type ReadyFuture<'f> where Self: 'f = impl ::core::future::Future<Output = Result<Self::Ready, Self::Error>>;
#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
async move {
#(#ready_stmts)*
}
Some(ReadyImpl {
ready_stmts,
ready_ret_ty: ready_res_ty,
}) => quote! {
#base

impl<#generic_ty> ::xitca_service::ready::ReadyService<#req_ty> for #service_ty
#where_clause
{
type Ready = #ready_res_ty;
type ReadyFuture<'f> = impl ::core::future::Future<Output = Self::Ready> where Self: 'f ;
#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
async move {
#(#ready_stmts)*
}
}
}.into()
}
}
None => base.into()
.into(),
None => base.into(),
}
}

Expand Down Expand Up @@ -169,18 +171,18 @@ impl<'a> CallImpl<'a> {

struct ReadyImpl<'a> {
ready_stmts: &'a [Stmt],
ready_res_ty: &'a Type,
ready_ret_ty: &'a Type,
}

impl<'a> ReadyImpl<'a> {
fn try_from_items(items: &'a [ImplItem]) -> Option<Self> {
find_async_method(items, "ready").map(|ready_impl| {
let (ready_res_ty, _) = extract_res_ty(&ready_impl.sig.output);
let ready_ret_ty = ready_ret_ty(&ready_impl.sig.output);
let ready_stmts = &ready_impl.block.stmts;

Self {
ready_stmts,
ready_res_ty,
ready_ret_ty,
}
})
}
Expand All @@ -203,7 +205,16 @@ fn extract_res_ty(ret: &ReturnType) -> (&Type, &Type) {
}
}

panic!("new_service method must output Result<Self, <Error>>")
panic!("build method must output Result<Self, <Error>>")
}

// Extract types from a return type of function.
fn ready_ret_ty(ret: &ReturnType) -> &Type {
if let ReturnType::Type(_, ty) = ret {
return &*ty;
}

panic!("ready method must output ReadyService::Ready type")
}

// generate a default PatIdent
Expand Down
4 changes: 2 additions & 2 deletions http/src/h1/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ where
B: Stream<Item = Result<Bytes, BE>>,
{
type Ready = S::Ready;
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where Self: 'f;
type ReadyFuture<'f> = S::ReadyFuture<'f> where Self: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
async move { self.service.ready().await.map_err(HttpServiceError::Service) }
self.service.ready()
}
}
4 changes: 2 additions & 2 deletions http/src/h2/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ where
BE: fmt::Debug,
{
type Ready = S::Ready;
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where Self: 'f;
type ReadyFuture<'f> = S::ReadyFuture<'f> where Self: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
async move { self.service.ready().await.map_err(HttpServiceError::Service) }
self.service.ready()
}
}
4 changes: 2 additions & 2 deletions http/src/h3/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ where
BE: fmt::Debug,
{
type Ready = S::Ready;
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where Self: 'f;
type ReadyFuture<'f> = S::ReadyFuture<'f>;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
async move { self.service.ready().await.map_err(HttpServiceError::Service) }
self.service.ready()
}
}
4 changes: 2 additions & 2 deletions http/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ where
BE: fmt::Debug,
{
type Ready = S::Ready;
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where Self: 'f;
type ReadyFuture<'f> = S::ReadyFuture<'f> where Self: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
async move { self.service.ready().await.map_err(HttpServiceError::Service) }
self.service.ready()
}
}
2 changes: 1 addition & 1 deletion http/src/util/service/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ where
S: for<'c, 's> ReadyService<&'c mut Context<'s, Req, C>, Response = Res, Error = Err, Ready = R>,
{
type Ready = R;
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where Self: 'f;
type ReadyFuture<'f> = impl Future<Output = Self::Ready> where Self: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
Expand Down
4 changes: 2 additions & 2 deletions http/src/util/service/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ where
Req: BorrowReq<http::Method>,
{
type Ready = ();
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where Self: 'f;
type ReadyFuture<'f> = impl Future<Output = Self::Ready> where Self: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
async { Ok(()) }
async {}
}
}

Expand Down
4 changes: 2 additions & 2 deletions http/src/util/service/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ where
Req: BorrowReq<http::Uri>,
{
type Ready = ();
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where Self: 'f;
type ReadyFuture<'f> = impl Future<Output = Self::Ready> where S: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
async { Ok(()) }
async {}
}
}

Expand Down
3 changes: 1 addition & 2 deletions server/src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ where

tokio::task::spawn_local(async move {
loop {
// TODO: What if service return Error when ready.
let ready = service.ready().await.ok();
let ready = service.ready().await;

match listener.accept().await {
Ok(stream) => {
Expand Down
8 changes: 4 additions & 4 deletions service/src/ready/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ where
S1: ReadyService<S::Response, Error = S::Error>,
{
type Ready = PipelineT<S::Ready, S1::Ready>;
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where Self: 'f;
type ReadyFuture<'f> = impl Future<Output = Self::Ready> where Self: 'f;

fn ready(&self) -> Self::ReadyFuture<'_> {
async move {
let first = self.first.ready().await?;
let second = self.second.ready().await?;
Ok(PipelineT::new(first, second))
let first = self.first.ready().await;
let second = self.second.ready().await;
PipelineT::new(first, second)
}
}
}
7 changes: 2 additions & 5 deletions service/src/ready/enclosed_fn.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::future::Future;

use crate::{
async_closure::AsyncClosure,
pipeline::{marker::EnclosedFn, PipelineT},
Expand All @@ -11,13 +9,12 @@ impl<S, Req, T, Res, Err> ReadyService<Req> for PipelineT<S, T, EnclosedFn>
where
S: ReadyService<Req>,
T: for<'s> AsyncClosure<(&'s S, Req), Output = Result<Res, Err>>,
Err: From<S::Error>,
{
type Ready = S::Ready;
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where Self: 'f;
type ReadyFuture<'f> = S::ReadyFuture<'f> where Self: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
async move { self.first.ready().await.map_err(Into::into) }
self.first.ready()
}
}
4 changes: 2 additions & 2 deletions service/src/ready/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ where
Fut: Future<Output = Result<Res, Err>>,
{
type Ready = ();
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where Self: 'f;
type ReadyFuture<'f> = impl Future<Output = Self::Ready> where F: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
async { Ok(()) }
async {}
}
}
7 changes: 3 additions & 4 deletions service/src/ready/map_err.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::future::Future;

use crate::pipeline::{marker::MapErr, PipelineT};

use super::ReadyService;
Expand All @@ -10,9 +8,10 @@ where
F: Fn(S::Error) -> E,
{
type Ready = S::Ready;
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where Self: 'f;
type ReadyFuture<'f> = S::ReadyFuture<'f> where Self: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
async move { self.first.ready().await.map_err(&self.second) }
self.first.ready()
}
}
10 changes: 5 additions & 5 deletions service/src/ready/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ use super::service::Service;
/// }
///
/// impl ReadyService<()> for Foo {
/// type Ready = Permit;
/// type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>>;
/// type Ready = Result<Permit, Self::Error>;
/// type ReadyFuture<'f> = impl Future<Output = Self::Ready>;
///
/// fn ready(&self) -> Self::ReadyFuture<'_> {
/// async move {
Expand Down Expand Up @@ -78,7 +78,7 @@ use super::service::Service;
pub trait ReadyService<Req>: Service<Req> {
type Ready;

type ReadyFuture<'f>: Future<Output = Result<Self::Ready, Self::Error>>
type ReadyFuture<'f>: Future<Output = Self::Ready>
where
Self: 'f;

Expand All @@ -92,7 +92,7 @@ macro_rules! impl_alloc {
S: ReadyService<Req> + ?Sized,
{
type Ready = S::Ready;
type ReadyFuture<'f> = S::ReadyFuture<'f> where Self: 'f;
type ReadyFuture<'f> = S::ReadyFuture<'f> where S: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
Expand All @@ -114,7 +114,7 @@ where
type Ready = <S::Target as ReadyService<Req>>::Ready;
type ReadyFuture<'f> = <S::Target as ReadyService<Req>>::ReadyFuture<'f>
where
Self: 'f;
S: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
Expand Down
2 changes: 1 addition & 1 deletion test/tests/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where
Ok(TestMiddlewareService(service))
}

async fn ready(&self) -> Result<S::Ready, Box<dyn std::error::Error>> {
async fn ready(&self) -> S::Ready {
self.0.ready().await
}

Expand Down
2 changes: 1 addition & 1 deletion web/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ where
S: for<'c1, 's1> ReadyService<&'c1 mut WebRequest<'s1, C>, Response = Res, Error = Err, Ready = R>,
{
type Ready = R;
type ReadyFuture<'f> = impl Future<Output = Result<Self::Ready, Self::Error>> where S: 'f;
type ReadyFuture<'f> = impl Future<Output = Self::Ready> where S: 'f;

#[inline]
fn ready(&self) -> Self::ReadyFuture<'_> {
Expand Down

0 comments on commit fda87e8

Please sign in to comment.