1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 16:52:58 +01:00

service trait takes request type parameter (#232)

This commit is contained in:
Rob Ede 2020-12-27 04:28:00 +00:00 committed by GitHub
parent 518bf3f6a6
commit 3ab8c3eb69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 1142 additions and 1136 deletions

View File

@ -40,8 +40,7 @@ impl<T> Clone for TcpConnectorFactory<T> {
}
}
impl<T: Address> ServiceFactory for TcpConnectorFactory<T> {
type Request = Connect<T>;
impl<T: Address> ServiceFactory<Connect<T>> for TcpConnectorFactory<T> {
type Response = Connection<T, TcpStream>;
type Error = ConnectError;
type Config = ();
@ -70,8 +69,7 @@ impl<T> Clone for TcpConnector<T> {
}
}
impl<T: Address> Service for TcpConnector<T> {
type Request = Connect<T>;
impl<T: Address> Service<Connect<T>> for TcpConnector<T> {
type Response = Connection<T, TcpStream>;
type Error = ConnectError;
#[allow(clippy::type_complexity)]

View File

@ -76,8 +76,8 @@ pub async fn start_default_resolver() -> Result<AsyncResolver, ConnectError> {
/// Create TCP connector service.
pub fn new_connector<T: Address + 'static>(
resolver: AsyncResolver,
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
+ Clone {
) -> impl Service<Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError> + Clone
{
pipeline(Resolver::new(resolver)).and_then(TcpConnector::new())
}
@ -85,8 +85,8 @@ pub fn new_connector<T: Address + 'static>(
pub fn new_connector_factory<T: Address + 'static>(
resolver: AsyncResolver,
) -> impl ServiceFactory<
Connect<T>,
Config = (),
Request = Connect<T>,
Response = Connection<T, TcpStream>,
Error = ConnectError,
InitError = (),
@ -96,15 +96,15 @@ pub fn new_connector_factory<T: Address + 'static>(
/// Create connector service with default parameters.
pub fn default_connector<T: Address + 'static>(
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
+ Clone {
) -> impl Service<Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError> + Clone
{
pipeline(Resolver::default()).and_then(TcpConnector::new())
}
/// Create connector service factory with default parameters.
pub fn default_connector_factory<T: Address + 'static>() -> impl ServiceFactory<
Connect<T>,
Config = (),
Request = Connect<T>,
Response = Connection<T, TcpStream>,
Error = ConnectError,
InitError = (),

View File

@ -54,8 +54,7 @@ impl<T> Clone for ResolverFactory<T> {
}
}
impl<T: Address> ServiceFactory for ResolverFactory<T> {
type Request = Connect<T>;
impl<T: Address> ServiceFactory<Connect<T>> for ResolverFactory<T> {
type Response = Connect<T>;
type Error = ConnectError;
type Config = ();
@ -102,8 +101,7 @@ impl<T> Clone for Resolver<T> {
}
}
impl<T: Address> Service for Resolver<T> {
type Request = Connect<T>;
impl<T: Address> Service<Connect<T>> for Resolver<T> {
type Response = Connect<T>;
type Error = ConnectError;
#[allow(clippy::type_complexity)]

View File

@ -70,8 +70,7 @@ impl<T> Clone for ConnectServiceFactory<T> {
}
}
impl<T: Address> ServiceFactory for ConnectServiceFactory<T> {
type Request = Connect<T>;
impl<T: Address> ServiceFactory<Connect<T>> for ConnectServiceFactory<T> {
type Response = Connection<T, TcpStream>;
type Error = ConnectError;
type Config = ();
@ -90,8 +89,7 @@ pub struct ConnectService<T> {
resolver: Resolver<T>,
}
impl<T: Address> Service for ConnectService<T> {
type Request = Connect<T>;
impl<T: Address> Service<Connect<T>> for ConnectService<T> {
type Response = Connection<T, TcpStream>;
type Error = ConnectError;
type Future = ConnectServiceResponse<T>;
@ -109,8 +107,8 @@ impl<T: Address> Service for ConnectService<T> {
}
enum ConnectState<T: Address> {
Resolve(<Resolver<T> as Service>::Future),
Connect(<TcpConnector<T> as Service>::Future),
Resolve(<Resolver<T> as Service<Connect<T>>>::Future),
Connect(<TcpConnector<T> as Service<Connect<T>>>::Future),
}
impl<T: Address> ConnectState<T> {
@ -160,8 +158,7 @@ pub struct TcpConnectService<T> {
resolver: Resolver<T>,
}
impl<T: Address + 'static> Service for TcpConnectService<T> {
type Request = Connect<T>;
impl<T: Address + 'static> Service<Connect<T>> for TcpConnectService<T> {
type Response = TcpStream;
type Error = ConnectError;
type Future = TcpConnectServiceResponse<T>;
@ -179,8 +176,8 @@ impl<T: Address + 'static> Service for TcpConnectService<T> {
}
enum TcpConnectState<T: Address> {
Resolve(<Resolver<T> as Service>::Future),
Connect(<TcpConnector<T> as Service>::Future),
Resolve(<Resolver<T> as Service<Connect<T>>>::Future),
Connect(<TcpConnector<T> as Service<Connect<T>>>::Future),
}
impl<T: Address> TcpConnectState<T> {

View File

@ -2,7 +2,10 @@ use std::collections::HashMap;
use std::{fmt, io, net};
use actix_rt::net::TcpStream;
use actix_service as actix;
use actix_service::{
fn_service, IntoServiceFactory as IntoBaseServiceFactory,
ServiceFactory as BaseServiceFactory,
};
use actix_utils::counter::CounterGuard;
use futures_util::future::{ok, Future, FutureExt, LocalBoxFuture};
use log::error;
@ -141,12 +144,10 @@ impl InternalServiceFactory for ConfiguredService {
let name = names.remove(&token).unwrap().0;
res.push((
token,
Box::new(StreamService::new(actix::fn_service(
move |_: TcpStream| {
error!("Service {:?} is not configured", name);
ok::<_, ()>(())
},
))),
Box::new(StreamService::new(fn_service(move |_: TcpStream| {
error!("Service {:?} is not configured", name);
ok::<_, ()>(())
}))),
));
};
}
@ -208,8 +209,8 @@ impl ServiceRuntime {
/// *ServiceConfig::bind()* or *ServiceConfig::listen()* methods.
pub fn service<T, F>(&mut self, name: &str, service: F)
where
F: actix::IntoServiceFactory<T>,
T: actix::ServiceFactory<Config = (), Request = TcpStream> + 'static,
F: IntoBaseServiceFactory<T, TcpStream>,
T: BaseServiceFactory<TcpStream, Config = ()> + 'static,
T::Future: 'static,
T::Service: 'static,
T::InitError: fmt::Debug,
@ -237,8 +238,8 @@ impl ServiceRuntime {
}
type BoxedNewService = Box<
dyn actix::ServiceFactory<
Request = (Option<CounterGuard>, StdStream),
dyn BaseServiceFactory<
(Option<CounterGuard>, StdStream),
Response = (),
Error = (),
InitError = (),
@ -252,15 +253,14 @@ struct ServiceFactory<T> {
inner: T,
}
impl<T> actix::ServiceFactory for ServiceFactory<T>
impl<T> BaseServiceFactory<(Option<CounterGuard>, StdStream)> for ServiceFactory<T>
where
T: actix::ServiceFactory<Config = (), Request = TcpStream>,
T: BaseServiceFactory<TcpStream, Config = ()>,
T::Future: 'static,
T::Service: 'static,
T::Error: 'static,
T::InitError: fmt::Debug + 'static,
{
type Request = (Option<CounterGuard>, StdStream);
type Response = ();
type Error = ();
type Config = ();

View File

@ -3,7 +3,7 @@ use std::net::SocketAddr;
use std::task::{Context, Poll};
use actix_rt::spawn;
use actix_service::{self as actix, Service, ServiceFactory as ActixServiceFactory};
use actix_service::{Service, ServiceFactory as BaseServiceFactory};
use actix_utils::counter::CounterGuard;
use futures_util::future::{err, ok, LocalBoxFuture, Ready};
use futures_util::{FutureExt, TryFutureExt};
@ -13,7 +13,7 @@ use super::Token;
use crate::socket::{FromStream, StdStream};
pub trait ServiceFactory<Stream: FromStream>: Send + Clone + 'static {
type Factory: actix::ServiceFactory<Config = (), Request = Stream>;
type Factory: BaseServiceFactory<Stream, Config = ()>;
fn create(&self) -> Self::Factory;
}
@ -28,31 +28,34 @@ pub(crate) trait InternalServiceFactory: Send {
pub(crate) type BoxedServerService = Box<
dyn Service<
Request = (Option<CounterGuard>, StdStream),
(Option<CounterGuard>, StdStream),
Response = (),
Error = (),
Future = Ready<Result<(), ()>>,
>,
>;
pub(crate) struct StreamService<T> {
service: T,
pub(crate) struct StreamService<S, I> {
service: S,
_phantom: PhantomData<I>,
}
impl<T> StreamService<T> {
pub(crate) fn new(service: T) -> Self {
StreamService { service }
impl<S, I> StreamService<S, I> {
pub(crate) fn new(service: S) -> Self {
StreamService {
service,
_phantom: PhantomData,
}
}
}
impl<T, I> Service for StreamService<T>
impl<S, I> Service<(Option<CounterGuard>, StdStream)> for StreamService<S, I>
where
T: Service<Request = I>,
T::Future: 'static,
T::Error: 'static,
S: Service<I>,
S::Future: 'static,
S::Error: 'static,
I: FromStream,
{
type Request = (Option<CounterGuard>, StdStream);
type Response = ();
type Error = ();
type Future = Ready<Result<(), ()>>;
@ -144,7 +147,7 @@ where
impl<F, T, I> ServiceFactory<I> for F
where
F: Fn() -> T + Send + Clone + 'static,
T: actix::ServiceFactory<Config = (), Request = I>,
T: BaseServiceFactory<I, Config = ()>,
I: FromStream,
{
type Factory = T;

View File

@ -1,8 +1,14 @@
# Changes
## Unreleased - 2020-xx-xx
* `Service`, other traits, and many type signatures now take the the request type as a type
parameter instead of an associated type. [#232]
* Upgrade `pin-project` to `1.0`.
[#232]: https://github.com/actix/actix-net/pull/232
## 1.0.6 - 2020-08-09
### Fixed

View File

@ -5,11 +5,14 @@ use actix_service::Service;
use criterion::{criterion_main, Criterion};
use futures_util::future::join_all;
use futures_util::future::TryFutureExt;
use std::cell::{RefCell, UnsafeCell};
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll};
use std::{
cell::{RefCell, UnsafeCell},
marker::PhantomData,
};
/*
* Test services A,B for AndThen service implementations
@ -28,71 +31,72 @@ async fn svc2(req: usize) -> Result<usize, ()> {
* Cut down version of actix_service::AndThenService based on actix-service::Cell
*/
struct AndThenUC<A, B>(Rc<UnsafeCell<(A, B)>>);
struct AndThenUC<A, Req, B>(Rc<UnsafeCell<(A, B)>>, PhantomData<Req>);
impl<A, B> AndThenUC<A, B> {
impl<A, Req, B> AndThenUC<A, Req, B> {
fn new(a: A, b: B) -> Self
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
Self(Rc::new(UnsafeCell::new((a, b))))
Self(Rc::new(UnsafeCell::new((a, b))), PhantomData)
}
}
impl<A, B> Clone for AndThenUC<A, B> {
impl<A, Req, B> Clone for AndThenUC<A, Req, B> {
fn clone(&self) -> Self {
Self(self.0.clone())
Self(self.0.clone(), PhantomData)
}
}
impl<A, B> Service for AndThenUC<A, B>
impl<A, Req, B> Service<Req> for AndThenUC<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Future = AndThenServiceResponse<A, B>;
type Future = AndThenServiceResponse<A, Req, B>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: A::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
let fut = unsafe { &mut *(*self.0).get() }.0.call(req);
AndThenServiceResponse {
state: State::A(fut, Some(self.0.clone())),
_phantom: PhantomData,
}
}
}
#[pin_project::pin_project]
pub(crate) struct AndThenServiceResponse<A, B>
pub(crate) struct AndThenServiceResponse<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
#[pin]
state: State<A, B>,
state: State<A, Req, B>,
_phantom: PhantomData<Req>,
}
#[pin_project::pin_project(project = StateProj)]
enum State<A, B>
enum State<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
A(#[pin] A::Future, Option<Rc<UnsafeCell<(A, B)>>>),
B(#[pin] B::Future),
Empty,
Empty(PhantomData<Req>),
}
impl<A, B> Future for AndThenServiceResponse<A, B>
impl<A, Req, B> Future for AndThenServiceResponse<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
type Output = Result<B::Response, A::Error>;
@ -103,7 +107,7 @@ where
StateProj::A(fut, b) => match fut.poll(cx)? {
Poll::Ready(res) => {
let b = b.take().unwrap();
this.state.set(State::Empty); // drop fut A
this.state.set(State::Empty(PhantomData)); // drop fut A
let fut = unsafe { &mut (*b.get()).1 }.call(res);
this.state.set(State::B(fut));
self.poll(cx)
@ -111,10 +115,10 @@ where
Poll::Pending => Poll::Pending,
},
StateProj::B(fut) => fut.poll(cx).map(|r| {
this.state.set(State::Empty);
this.state.set(State::Empty(PhantomData));
r
}),
StateProj::Empty => {
StateProj::Empty(_) => {
panic!("future must not be polled after it returned `Poll::Ready`")
}
}
@ -125,39 +129,38 @@ where
* AndThenRC - AndThen service based on RefCell
*/
struct AndThenRC<A, B>(Rc<RefCell<(A, B)>>);
struct AndThenRC<A, Req, B>(Rc<RefCell<(A, B)>>, PhantomData<Req>);
impl<A, B> AndThenRC<A, B> {
impl<A, Req, B> AndThenRC<A, Req, B> {
fn new(a: A, b: B) -> Self
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
Self(Rc::new(RefCell::new((a, b))))
Self(Rc::new(RefCell::new((a, b))), PhantomData)
}
}
impl<A, B> Clone for AndThenRC<A, B> {
impl<A, Req, B> Clone for AndThenRC<A, Req, B> {
fn clone(&self) -> Self {
Self(self.0.clone())
Self(self.0.clone(), PhantomData)
}
}
impl<A, B> Service for AndThenRC<A, B>
impl<A, Req, B> Service<Req> for AndThenRC<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Future = AndThenServiceResponseRC<A, B>;
type Future = AndThenServiceResponseRC<A, Req, B>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: A::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
let fut = self.0.borrow_mut().0.call(req);
AndThenServiceResponseRC {
state: StateRC::A(fut, Some(self.0.clone())),
@ -166,30 +169,30 @@ where
}
#[pin_project::pin_project]
pub(crate) struct AndThenServiceResponseRC<A, B>
pub(crate) struct AndThenServiceResponseRC<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
#[pin]
state: StateRC<A, B>,
state: StateRC<A, Req, B>,
}
#[pin_project::pin_project(project = StateRCProj)]
enum StateRC<A, B>
enum StateRC<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
A(#[pin] A::Future, Option<Rc<RefCell<(A, B)>>>),
B(#[pin] B::Future),
Empty,
Empty(PhantomData<Req>),
}
impl<A, B> Future for AndThenServiceResponseRC<A, B>
impl<A, Req, B> Future for AndThenServiceResponseRC<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
type Output = Result<B::Response, A::Error>;
@ -200,7 +203,7 @@ where
StateRCProj::A(fut, b) => match fut.poll(cx)? {
Poll::Ready(res) => {
let b = b.take().unwrap();
this.state.set(StateRC::Empty); // drop fut A
this.state.set(StateRC::Empty(PhantomData)); // drop fut A
let fut = b.borrow_mut().1.call(res);
this.state.set(StateRC::B(fut));
self.poll(cx)
@ -208,10 +211,10 @@ where
Poll::Pending => Poll::Pending,
},
StateRCProj::B(fut) => fut.poll(cx).map(|r| {
this.state.set(StateRC::Empty);
this.state.set(StateRC::Empty(PhantomData));
r
}),
StateRCProj::Empty => {
StateRCProj::Empty(_) => {
panic!("future must not be polled after it returned `Poll::Ready`")
}
}
@ -223,32 +226,31 @@ where
* and standard futures::future::and_then combinator in a Box
*/
struct AndThenRCFuture<A, B>(Rc<RefCell<(A, B)>>);
struct AndThenRCFuture<A, Req, B>(Rc<RefCell<(A, B)>>, PhantomData<Req>);
impl<A, B> AndThenRCFuture<A, B> {
impl<A, Req, B> AndThenRCFuture<A, Req, B> {
fn new(a: A, b: B) -> Self
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
Self(Rc::new(RefCell::new((a, b))))
Self(Rc::new(RefCell::new((a, b))), PhantomData)
}
}
impl<A, B> Clone for AndThenRCFuture<A, B> {
impl<A, Req, B> Clone for AndThenRCFuture<A, Req, B> {
fn clone(&self) -> Self {
Self(self.0.clone())
Self(self.0.clone(), PhantomData)
}
}
impl<A, B> Service for AndThenRCFuture<A, B>
impl<A, Req, B> Service<Req> for AndThenRCFuture<A, Req, B>
where
A: Service + 'static,
A: Service<Req> + 'static,
A::Future: 'static,
B: Service<Request = A::Response, Error = A::Error> + 'static,
B: Service<A::Response, Error = A::Error> + 'static,
B::Future: 'static,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Future = BoxFuture<Self::Response, Self::Error>;
@ -257,7 +259,7 @@ where
Poll::Ready(Ok(()))
}
fn call(&mut self, req: A::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
let fut = self.0.borrow_mut().0.call(req);
let core = self.0.clone();
let fut2 = move |res| (*core).borrow_mut().1.call(res);
@ -281,7 +283,7 @@ where
/// async_service_direct time: [1.0908 us 1.1656 us 1.2613 us]
pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str)
where
S: Service<Request = (), Response = usize, Error = ()> + Clone + 'static,
S: Service<(), Response = usize, Error = ()> + Clone + 'static,
{
let mut rt = actix_rt::System::new("test");

View File

@ -20,8 +20,7 @@ impl Clone for SrvUC {
}
}
impl Service for SrvUC {
type Request = ();
impl Service<()> for SrvUC {
type Response = usize;
type Error = ();
type Future = Ready<Result<Self::Response, ()>>;
@ -50,8 +49,7 @@ impl Clone for SrvRC {
}
}
impl Service for SrvRC {
type Request = ();
impl Service<()> for SrvRC {
type Response = usize;
type Error = ();
type Future = Ready<Result<Self::Response, ()>>;
@ -83,7 +81,7 @@ impl Service for SrvRC {
/// async_service_direct time: [1.0908 us 1.1656 us 1.2613 us]
pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str)
where
S: Service<Request = (), Response = usize, Error = ()> + Clone + 'static,
S: Service<(), Response = usize, Error = ()> + Clone + 'static,
{
let mut rt = actix_rt::System::new("test");

View File

@ -1,8 +1,8 @@
use std::cell::RefCell;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll};
use std::{cell::RefCell, marker::PhantomData};
use super::{Service, ServiceFactory};
@ -10,34 +10,33 @@ use super::{Service, ServiceFactory};
/// of another service which completes successfully.
///
/// This is created by the `Pipeline::and_then` method.
pub(crate) struct AndThenService<A, B>(Rc<RefCell<(A, B)>>);
pub(crate) struct AndThenService<A, B, Req>(Rc<RefCell<(A, B)>>, PhantomData<Req>);
impl<A, B> AndThenService<A, B> {
impl<A, B, Req> AndThenService<A, B, Req> {
/// Create new `AndThen` combinator
pub(crate) fn new(a: A, b: B) -> Self
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
Self(Rc::new(RefCell::new((a, b))))
Self(Rc::new(RefCell::new((a, b))), PhantomData)
}
}
impl<A, B> Clone for AndThenService<A, B> {
impl<A, B, Req> Clone for AndThenService<A, B, Req> {
fn clone(&self) -> Self {
AndThenService(self.0.clone())
AndThenService(self.0.clone(), PhantomData)
}
}
impl<A, B> Service for AndThenService<A, B>
impl<A, B, Req> Service<Req> for AndThenService<A, B, Req>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Future = AndThenServiceResponse<A, B>;
type Future = AndThenServiceResponse<A, B, Req>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let mut srv = self.0.borrow_mut();
@ -49,7 +48,7 @@ where
}
}
fn call(&mut self, req: A::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
AndThenServiceResponse {
state: State::A(self.0.borrow_mut().0.call(req), Some(self.0.clone())),
}
@ -57,30 +56,30 @@ where
}
#[pin_project::pin_project]
pub(crate) struct AndThenServiceResponse<A, B>
pub(crate) struct AndThenServiceResponse<A, B, Req>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
#[pin]
state: State<A, B>,
state: State<A, B, Req>,
}
#[pin_project::pin_project(project = StateProj)]
enum State<A, B>
enum State<A, B, Req>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
A(#[pin] A::Future, Option<Rc<RefCell<(A, B)>>>),
B(#[pin] B::Future),
Empty,
}
impl<A, B> Future for AndThenServiceResponse<A, B>
impl<A, B, Req> Future for AndThenServiceResponse<A, B, Req>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
type Output = Result<B::Response, A::Error>;
@ -110,27 +109,28 @@ where
}
/// `.and_then()` service factory combinator
pub(crate) struct AndThenServiceFactory<A, B>
pub(crate) struct AndThenServiceFactory<A, B, Req>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
A::Config: Clone,
B: ServiceFactory<
A::Response,
Config = A::Config,
Request = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{
inner: Rc<(A, B)>,
_phantom: PhantomData<Req>,
}
impl<A, B> AndThenServiceFactory<A, B>
impl<A, B, Req> AndThenServiceFactory<A, B, Req>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
A::Config: Clone,
B: ServiceFactory<
A::Response,
Config = A::Config,
Request = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
@ -139,29 +139,29 @@ where
pub(crate) fn new(a: A, b: B) -> Self {
Self {
inner: Rc::new((a, b)),
_phantom: PhantomData,
}
}
}
impl<A, B> ServiceFactory for AndThenServiceFactory<A, B>
impl<A, B, Req> ServiceFactory<Req> for AndThenServiceFactory<A, B, Req>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
A::Config: Clone,
B: ServiceFactory<
A::Response,
Config = A::Config,
Request = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Config = A::Config;
type Service = AndThenService<A::Service, B::Service>;
type Service = AndThenService<A::Service, B::Service, Req>;
type InitError = A::InitError;
type Future = AndThenServiceFactoryResponse<A, B>;
type Future = AndThenServiceFactoryResponse<A, B, Req>;
fn new_service(&self, cfg: A::Config) -> Self::Future {
let inner = &*self.inner;
@ -172,13 +172,13 @@ where
}
}
impl<A, B> Clone for AndThenServiceFactory<A, B>
impl<A, B, Req> Clone for AndThenServiceFactory<A, B, Req>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
A::Config: Clone,
B: ServiceFactory<
A::Response,
Config = A::Config,
Request = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
@ -186,15 +186,16 @@ where
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
_phantom: PhantomData,
}
}
}
#[pin_project::pin_project]
pub(crate) struct AndThenServiceFactoryResponse<A, B>
pub(crate) struct AndThenServiceFactoryResponse<A, B, Req>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response>,
A: ServiceFactory<Req>,
B: ServiceFactory<A::Response>,
{
#[pin]
fut_a: A::Future,
@ -205,10 +206,10 @@ where
b: Option<B::Service>,
}
impl<A, B> AndThenServiceFactoryResponse<A, B>
impl<A, B, Req> AndThenServiceFactoryResponse<A, B, Req>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response>,
A: ServiceFactory<Req>,
B: ServiceFactory<A::Response>,
{
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
AndThenServiceFactoryResponse {
@ -220,12 +221,12 @@ where
}
}
impl<A, B> Future for AndThenServiceFactoryResponse<A, B>
impl<A, B, Req> Future for AndThenServiceFactoryResponse<A, B, Req>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response, Error = A::Error, InitError = A::InitError>,
A: ServiceFactory<Req>,
B: ServiceFactory<A::Response, Error = A::Error, InitError = A::InitError>,
{
type Output = Result<AndThenService<A::Service, B::Service>, A::InitError>;
type Output = Result<AndThenService<A::Service, B::Service, Req>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
@ -263,8 +264,7 @@ mod tests {
struct Srv1(Rc<Cell<usize>>);
impl Service for Srv1 {
type Request = &'static str;
impl Service<&'static str> for Srv1 {
type Response = &'static str;
type Error = ();
type Future = Ready<Result<Self::Response, ()>>;
@ -282,8 +282,7 @@ mod tests {
#[derive(Clone)]
struct Srv2(Rc<Cell<usize>>);
impl Service for Srv2 {
type Request = &'static str;
impl Service<&'static str> for Srv2 {
type Response = (&'static str, &'static str);
type Error = ();
type Future = Ready<Result<Self::Response, ()>>;

View File

@ -8,66 +8,67 @@ use std::task::{Context, Poll};
use crate::{Service, ServiceFactory};
/// `Apply` service combinator
pub(crate) struct AndThenApplyFn<A, B, F, Fut, Res, Err>
pub(crate) struct AndThenApplyFn<S1, S2, F, Fut, Req, In, Res, Err>
where
A: Service,
B: Service,
F: FnMut(A::Response, &mut B) -> Fut,
S1: Service<Req>,
S2: Service<In>,
F: FnMut(S1::Response, &mut S2) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error> + From<B::Error>,
Err: From<S1::Error> + From<S2::Error>,
{
srv: Rc<RefCell<(A, B, F)>>,
r: PhantomData<(Fut, Res, Err)>,
svc: Rc<RefCell<(S1, S2, F)>>,
_phantom: PhantomData<(Fut, Req, In, Res, Err)>,
}
impl<A, B, F, Fut, Res, Err> AndThenApplyFn<A, B, F, Fut, Res, Err>
impl<S1, S2, F, Fut, Req, In, Res, Err> AndThenApplyFn<S1, S2, F, Fut, Req, In, Res, Err>
where
A: Service,
B: Service,
F: FnMut(A::Response, &mut B) -> Fut,
S1: Service<Req>,
S2: Service<In>,
F: FnMut(S1::Response, &mut S2) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error> + From<B::Error>,
Err: From<S1::Error> + From<S2::Error>,
{
/// Create new `Apply` combinator
pub(crate) fn new(a: A, b: B, f: F) -> Self {
pub(crate) fn new(a: S1, b: S2, wrap_fn: F) -> Self {
Self {
srv: Rc::new(RefCell::new((a, b, f))),
r: PhantomData,
svc: Rc::new(RefCell::new((a, b, wrap_fn))),
_phantom: PhantomData,
}
}
}
impl<A, B, F, Fut, Res, Err> Clone for AndThenApplyFn<A, B, F, Fut, Res, Err>
impl<S1, S2, F, Fut, Req, In, Res, Err> Clone
for AndThenApplyFn<S1, S2, F, Fut, Req, In, Res, Err>
where
A: Service,
B: Service,
F: FnMut(A::Response, &mut B) -> Fut,
S1: Service<Req>,
S2: Service<In>,
F: FnMut(S1::Response, &mut S2) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error> + From<B::Error>,
Err: From<S1::Error> + From<S2::Error>,
{
fn clone(&self) -> Self {
AndThenApplyFn {
srv: self.srv.clone(),
r: PhantomData,
svc: self.svc.clone(),
_phantom: PhantomData,
}
}
}
impl<A, B, F, Fut, Res, Err> Service for AndThenApplyFn<A, B, F, Fut, Res, Err>
impl<S1, S2, F, Fut, Req, In, Res, Err> Service<Req>
for AndThenApplyFn<S1, S2, F, Fut, Req, In, Res, Err>
where
A: Service,
B: Service,
F: FnMut(A::Response, &mut B) -> Fut,
S1: Service<Req>,
S2: Service<In>,
F: FnMut(S1::Response, &mut S2) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error> + From<B::Error>,
Err: From<S1::Error> + From<S2::Error>,
{
type Request = A::Request;
type Response = Res;
type Error = Err;
type Future = AndThenApplyFnFuture<A, B, F, Fut, Res, Err>;
type Future = AndThenApplyFnFuture<S1, S2, F, Fut, Req, In, Res, Err>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let mut inner = self.srv.borrow_mut();
let mut inner = self.svc.borrow_mut();
let not_ready = inner.0.poll_ready(cx)?.is_pending();
if inner.1.poll_ready(cx)?.is_pending() || not_ready {
Poll::Pending
@ -76,50 +77,49 @@ where
}
}
fn call(&mut self, req: A::Request) -> Self::Future {
let fut = self.srv.borrow_mut().0.call(req);
fn call(&mut self, req: Req) -> Self::Future {
let fut = self.svc.borrow_mut().0.call(req);
AndThenApplyFnFuture {
state: State::A(fut, Some(self.srv.clone())),
state: State::A(fut, Some(self.svc.clone())),
}
}
}
#[pin_project::pin_project]
pub(crate) struct AndThenApplyFnFuture<A, B, F, Fut, Res, Err>
pub(crate) struct AndThenApplyFnFuture<S1, S2, F, Fut, Req, In, Res, Err>
where
A: Service,
B: Service,
F: FnMut(A::Response, &mut B) -> Fut,
S1: Service<Req>,
S2: Service<In>,
F: FnMut(S1::Response, &mut S2) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error>,
Err: From<B::Error>,
Err: From<S1::Error> + From<S2::Error>,
{
#[pin]
state: State<A, B, F, Fut, Res, Err>,
state: State<S1, S2, F, Fut, Req, In, Res, Err>,
}
#[pin_project::pin_project(project = StateProj)]
enum State<A, B, F, Fut, Res, Err>
enum State<S1, S2, F, Fut, Req, In, Res, Err>
where
A: Service,
B: Service,
F: FnMut(A::Response, &mut B) -> Fut,
S1: Service<Req>,
S2: Service<In>,
F: FnMut(S1::Response, &mut S2) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error>,
Err: From<B::Error>,
Err: From<S1::Error> + From<S2::Error>,
{
A(#[pin] A::Future, Option<Rc<RefCell<(A, B, F)>>>),
A(#[pin] S1::Future, Option<Rc<RefCell<(S1, S2, F)>>>),
B(#[pin] Fut),
Empty,
Empty(PhantomData<In>),
}
impl<A, B, F, Fut, Res, Err> Future for AndThenApplyFnFuture<A, B, F, Fut, Res, Err>
impl<S1, S2, F, Fut, Req, In, Res, Err> Future
for AndThenApplyFnFuture<S1, S2, F, Fut, Req, In, Res, Err>
where
A: Service,
B: Service,
F: FnMut(A::Response, &mut B) -> Fut,
S1: Service<Req>,
S2: Service<In>,
F: FnMut(S1::Response, &mut S2) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error> + From<B::Error>,
Err: From<S1::Error> + From<S2::Error>,
{
type Output = Result<Res, Err>;
@ -129,8 +129,8 @@ where
match this.state.as_mut().project() {
StateProj::A(fut, b) => match fut.poll(cx)? {
Poll::Ready(res) => {
let b = b.take().unwrap();
this.state.set(State::Empty);
let b = Option::take(b).unwrap();
this.state.set(State::Empty(PhantomData));
let (_, b, f) = &mut *b.borrow_mut();
let fut = f(res, b);
this.state.set(State::B(fut));
@ -139,10 +139,10 @@ where
Poll::Pending => Poll::Pending,
},
StateProj::B(fut) => fut.poll(cx).map(|r| {
this.state.set(State::Empty);
this.state.set(State::Empty(PhantomData));
r
}),
StateProj::Empty => {
StateProj::Empty(_) => {
panic!("future must not be polled after it returned `Poll::Ready`")
}
}
@ -150,119 +150,127 @@ where
}
/// `AndThenApplyFn` service factory
pub(crate) struct AndThenApplyFnFactory<A, B, F, Fut, Res, Err> {
srv: Rc<(A, B, F)>,
r: PhantomData<(Fut, Res, Err)>,
pub(crate) struct AndThenApplyFnFactory<SF1, SF2, F, Fut, Req, In, Res, Err> {
srv: Rc<(SF1, SF2, F)>,
_phantom: PhantomData<(Fut, Req, In, Res, Err)>,
}
impl<A, B, F, Fut, Res, Err> AndThenApplyFnFactory<A, B, F, Fut, Res, Err>
impl<SF1, SF2, F, Fut, Req, In, Res, Err>
AndThenApplyFnFactory<SF1, SF2, F, Fut, Req, In, Res, Err>
where
A: ServiceFactory,
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
SF1: ServiceFactory<Req>,
SF2: ServiceFactory<In, Config = SF1::Config, InitError = SF1::InitError>,
F: FnMut(SF1::Response, &mut SF2::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error> + From<B::Error>,
Err: From<SF1::Error> + From<SF2::Error>,
{
/// Create new `ApplyNewService` new service instance
pub(crate) fn new(a: A, b: B, f: F) -> Self {
pub(crate) fn new(a: SF1, b: SF2, wrap_fn: F) -> Self {
Self {
srv: Rc::new((a, b, f)),
r: PhantomData,
srv: Rc::new((a, b, wrap_fn)),
_phantom: PhantomData,
}
}
}
impl<A, B, F, Fut, Res, Err> Clone for AndThenApplyFnFactory<A, B, F, Fut, Res, Err> {
impl<SF1, SF2, F, Fut, Req, In, Res, Err> Clone
for AndThenApplyFnFactory<SF1, SF2, F, Fut, Req, In, Res, Err>
{
fn clone(&self) -> Self {
Self {
srv: self.srv.clone(),
r: PhantomData,
_phantom: PhantomData,
}
}
}
impl<A, B, F, Fut, Res, Err> ServiceFactory for AndThenApplyFnFactory<A, B, F, Fut, Res, Err>
impl<SF1, SF2, F, Fut, Req, In, Res, Err> ServiceFactory<Req>
for AndThenApplyFnFactory<SF1, SF2, F, Fut, Req, In, Res, Err>
where
A: ServiceFactory,
A::Config: Clone,
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
SF1: ServiceFactory<Req>,
SF1::Config: Clone,
SF2: ServiceFactory<In, Config = SF1::Config, InitError = SF1::InitError>,
F: FnMut(SF1::Response, &mut SF2::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error> + From<B::Error>,
Err: From<SF1::Error> + From<SF2::Error>,
{
type Request = A::Request;
type Response = Res;
type Error = Err;
type Service = AndThenApplyFn<A::Service, B::Service, F, Fut, Res, Err>;
type Config = A::Config;
type InitError = A::InitError;
type Future = AndThenApplyFnFactoryResponse<A, B, F, Fut, Res, Err>;
type Service = AndThenApplyFn<SF1::Service, SF2::Service, F, Fut, Req, In, Res, Err>;
type Config = SF1::Config;
type InitError = SF1::InitError;
type Future = AndThenApplyFnFactoryResponse<SF1, SF2, F, Fut, Req, In, Res, Err>;
fn new_service(&self, cfg: A::Config) -> Self::Future {
fn new_service(&self, cfg: SF1::Config) -> Self::Future {
let srv = &*self.srv;
AndThenApplyFnFactoryResponse {
a: None,
b: None,
f: srv.2.clone(),
fut_a: srv.0.new_service(cfg.clone()),
fut_b: srv.1.new_service(cfg),
s1: None,
s2: None,
wrap_fn: srv.2.clone(),
fut_s1: srv.0.new_service(cfg.clone()),
fut_s2: srv.1.new_service(cfg),
_phantom: PhantomData,
}
}
}
#[pin_project::pin_project]
pub(crate) struct AndThenApplyFnFactoryResponse<A, B, F, Fut, Res, Err>
pub(crate) struct AndThenApplyFnFactoryResponse<SF1, SF2, F, Fut, Req, In, Res, Err>
where
A: ServiceFactory,
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
SF1: ServiceFactory<Req>,
SF2: ServiceFactory<In, Config = SF1::Config, InitError = SF1::InitError>,
F: FnMut(SF1::Response, &mut SF2::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error>,
Err: From<B::Error>,
Err: From<SF1::Error>,
Err: From<SF2::Error>,
{
#[pin]
fut_b: B::Future,
fut_s1: SF1::Future,
#[pin]
fut_a: A::Future,
f: F,
a: Option<A::Service>,
b: Option<B::Service>,
fut_s2: SF2::Future,
wrap_fn: F,
s1: Option<SF1::Service>,
s2: Option<SF2::Service>,
_phantom: PhantomData<In>,
}
impl<A, B, F, Fut, Res, Err> Future for AndThenApplyFnFactoryResponse<A, B, F, Fut, Res, Err>
impl<SF1, SF2, F, Fut, Req, In, Res, Err> Future
for AndThenApplyFnFactoryResponse<SF1, SF2, F, Fut, Req, In, Res, Err>
where
A: ServiceFactory,
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
SF1: ServiceFactory<Req>,
SF2: ServiceFactory<In, Config = SF1::Config, InitError = SF1::InitError>,
F: FnMut(SF1::Response, &mut SF2::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error> + From<B::Error>,
Err: From<SF1::Error> + From<SF2::Error>,
{
type Output =
Result<AndThenApplyFn<A::Service, B::Service, F, Fut, Res, Err>, A::InitError>;
type Output = Result<
AndThenApplyFn<SF1::Service, SF2::Service, F, Fut, Req, In, Res, Err>,
SF1::InitError,
>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
if this.a.is_none() {
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
*this.a = Some(service);
if this.s1.is_none() {
if let Poll::Ready(service) = this.fut_s1.poll(cx)? {
*this.s1 = Some(service);
}
}
if this.b.is_none() {
if let Poll::Ready(service) = this.fut_b.poll(cx)? {
*this.b = Some(service);
if this.s2.is_none() {
if let Poll::Ready(service) = this.fut_s2.poll(cx)? {
*this.s2 = Some(service);
}
}
if this.a.is_some() && this.b.is_some() {
if this.s1.is_some() && this.s2.is_some() {
Poll::Ready(Ok(AndThenApplyFn {
srv: Rc::new(RefCell::new((
this.a.take().unwrap(),
this.b.take().unwrap(),
this.f.clone(),
svc: Rc::new(RefCell::new((
Option::take(this.s1).unwrap(),
Option::take(this.s2).unwrap(),
this.wrap_fn.clone(),
))),
r: PhantomData,
_phantom: PhantomData,
}))
} else {
Poll::Pending
@ -280,29 +288,29 @@ mod tests {
#[derive(Clone)]
struct Srv;
impl Service for Srv {
type Request = ();
impl Service<u8> for Srv {
type Response = ();
type Error = ();
type Future = Ready<Result<(), ()>>;
type Future = Ready<Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
#[allow(clippy::unit_arg)]
fn call(&mut self, req: Self::Request) -> Self::Future {
ok(req)
fn call(&mut self, req: u8) -> Self::Future {
let _ = req;
ok(())
}
}
#[actix_rt::test]
async fn test_service() {
let mut srv = pipeline(ok).and_then_apply_fn(Srv, |req: &'static str, s| {
s.call(()).map_ok(move |res| (req, res))
s.call(1).map_ok(move |res| (req, res))
});
let res = lazy(|cx| srv.poll_ready(cx)).await;
assert_eq!(res, Poll::Ready(Ok(())));
assert!(res.is_ready());
let res = srv.call("srv").await;
assert!(res.is_ok());
@ -313,11 +321,11 @@ mod tests {
async fn test_service_factory() {
let new_srv = pipeline_factory(|| ok::<_, ()>(fn_service(ok))).and_then_apply_fn(
|| ok(Srv),
|req: &'static str, s| s.call(()).map_ok(move |res| (req, res)),
|req: &'static str, s| s.call(1).map_ok(move |res| (req, res)),
);
let mut srv = new_srv.new_service(()).await.unwrap();
let res = lazy(|cx| srv.poll_ready(cx)).await;
assert_eq!(res, Poll::Ready(Ok(())));
assert!(res.is_ready());
let res = srv.call("srv").await;
assert!(res.is_ok());

View File

@ -1,203 +1,209 @@
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{
future::Future,
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
};
use futures_util::ready;
use super::{IntoService, IntoServiceFactory, Service, ServiceFactory};
/// Apply transform function to a service.
pub fn apply_fn<T, F, R, In, Out, Err, U>(service: U, f: F) -> Apply<T, F, R, In, Out, Err>
///
/// The In and Out type params refer to the request and response types for the wrapped service.
pub fn apply_fn<I, S, F, Fut, Req, In, Res, Err>(
service: I,
wrap_fn: F,
) -> Apply<S, F, Req, In, Res, Err>
where
T: Service<Error = Err>,
F: FnMut(In, &mut T) -> R,
R: Future<Output = Result<Out, Err>>,
U: IntoService<T>,
I: IntoService<S, In>,
S: Service<In, Error = Err>,
F: FnMut(Req, &mut S) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
Apply::new(service.into_service(), f)
Apply::new(service.into_service(), wrap_fn)
}
/// Service factory that produces `apply_fn` service.
pub fn apply_fn_factory<T, F, R, In, Out, Err, U>(
service: U,
///
/// The In and Out type params refer to the request and response types for the wrapped service.
pub fn apply_fn_factory<I, SF, F, Fut, Req, In, Res, Err>(
service: I,
f: F,
) -> ApplyServiceFactory<T, F, R, In, Out, Err>
) -> ApplyFactory<SF, F, Req, In, Res, Err>
where
T: ServiceFactory<Error = Err>,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
U: IntoServiceFactory<T>,
I: IntoServiceFactory<SF, In>,
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
ApplyServiceFactory::new(service.into_factory(), f)
ApplyFactory::new(service.into_factory(), f)
}
/// `Apply` service combinator
pub struct Apply<T, F, R, In, Out, Err>
/// `Apply` service combinator.
///
/// The In and Out type params refer to the request and response types for the wrapped service.
pub struct Apply<S, F, Req, In, Res, Err>
where
T: Service<Error = Err>,
S: Service<In, Error = Err>,
{
service: T,
f: F,
r: PhantomData<(In, Out, R)>,
service: S,
wrap_fn: F,
_phantom: PhantomData<(Req, In, Res, Err)>,
}
impl<T, F, R, In, Out, Err> Apply<T, F, R, In, Out, Err>
impl<S, F, Fut, Req, In, Res, Err> Apply<S, F, Req, In, Res, Err>
where
T: Service<Error = Err>,
F: FnMut(In, &mut T) -> R,
R: Future<Output = Result<Out, Err>>,
S: Service<In, Error = Err>,
F: FnMut(Req, &mut S) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
/// Create new `Apply` combinator
fn new(service: T, f: F) -> Self {
fn new(service: S, wrap_fn: F) -> Self {
Self {
service,
f,
r: PhantomData,
wrap_fn,
_phantom: PhantomData,
}
}
}
impl<T, F, R, In, Out, Err> Clone for Apply<T, F, R, In, Out, Err>
impl<S, F, Fut, Req, In, Res, Err> Clone for Apply<S, F, Req, In, Res, Err>
where
T: Service<Error = Err> + Clone,
F: FnMut(In, &mut T) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
S: Service<In, Error = Err> + Clone,
F: FnMut(Req, &mut S) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
fn clone(&self) -> Self {
Apply {
service: self.service.clone(),
f: self.f.clone(),
r: PhantomData,
wrap_fn: self.wrap_fn.clone(),
_phantom: PhantomData,
}
}
}
impl<T, F, R, In, Out, Err> Service for Apply<T, F, R, In, Out, Err>
impl<S, F, Fut, Req, In, Res, Err> Service<Req> for Apply<S, F, Req, In, Res, Err>
where
T: Service<Error = Err>,
F: FnMut(In, &mut T) -> R,
R: Future<Output = Result<Out, Err>>,
S: Service<In, Error = Err>,
F: FnMut(Req, &mut S) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
type Request = In;
type Response = Out;
type Response = Res;
type Error = Err;
type Future = R;
type Future = Fut;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(futures_util::ready!(self.service.poll_ready(cx)))
Poll::Ready(ready!(self.service.poll_ready(cx)))
}
fn call(&mut self, req: In) -> Self::Future {
(self.f)(req, &mut self.service)
fn call(&mut self, req: Req) -> Self::Future {
(self.wrap_fn)(req, &mut self.service)
}
}
/// `apply()` service factory
pub struct ApplyServiceFactory<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
{
service: T,
f: F,
r: PhantomData<(R, In, Out)>,
/// `ApplyFactory` service factory combinator.
pub struct ApplyFactory<SF, F, Req, In, Res, Err> {
factory: SF,
wrap_fn: F,
_phantom: PhantomData<(Req, In, Res, Err)>,
}
impl<T, F, R, In, Out, Err> ApplyServiceFactory<T, F, R, In, Out, Err>
impl<SF, F, Fut, Req, In, Res, Err> ApplyFactory<SF, F, Req, In, Res, Err>
where
T: ServiceFactory<Error = Err>,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
/// Create new `ApplyNewService` new service instance
fn new(service: T, f: F) -> Self {
/// Create new `ApplyFactory` new service instance
fn new(factory: SF, wrap_fn: F) -> Self {
Self {
f,
service,
r: PhantomData,
factory,
wrap_fn,
_phantom: PhantomData,
}
}
}
impl<T, F, R, In, Out, Err> Clone for ApplyServiceFactory<T, F, R, In, Out, Err>
impl<SF, F, Fut, Req, In, Res, Err> Clone for ApplyFactory<SF, F, Req, In, Res, Err>
where
T: ServiceFactory<Error = Err> + Clone,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
SF: ServiceFactory<In, Error = Err> + Clone,
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
fn clone(&self) -> Self {
Self {
service: self.service.clone(),
f: self.f.clone(),
r: PhantomData,
factory: self.factory.clone(),
wrap_fn: self.wrap_fn.clone(),
_phantom: PhantomData,
}
}
}
impl<T, F, R, In, Out, Err> ServiceFactory for ApplyServiceFactory<T, F, R, In, Out, Err>
impl<SF, F, Fut, Req, In, Res, Err> ServiceFactory<Req>
for ApplyFactory<SF, F, Req, In, Res, Err>
where
T: ServiceFactory<Error = Err>,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
type Request = In;
type Response = Out;
type Response = Res;
type Error = Err;
type Config = T::Config;
type Service = Apply<T::Service, F, R, In, Out, Err>;
type InitError = T::InitError;
type Future = ApplyServiceFactoryResponse<T, F, R, In, Out, Err>;
type Config = SF::Config;
type Service = Apply<SF::Service, F, Req, In, Res, Err>;
type InitError = SF::InitError;
type Future = ApplyServiceFactoryResponse<SF, F, Fut, Req, In, Res, Err>;
fn new_service(&self, cfg: T::Config) -> Self::Future {
ApplyServiceFactoryResponse::new(self.service.new_service(cfg), self.f.clone())
fn new_service(&self, cfg: SF::Config) -> Self::Future {
let svc = self.factory.new_service(cfg);
ApplyServiceFactoryResponse::new(svc, self.wrap_fn.clone())
}
}
#[pin_project::pin_project]
pub struct ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
pub struct ApplyServiceFactoryResponse<SF, F, Fut, Req, In, Res, Err>
where
T: ServiceFactory<Error = Err>,
F: FnMut(In, &mut T::Service) -> R,
R: Future<Output = Result<Out, Err>>,
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
#[pin]
fut: T::Future,
f: Option<F>,
r: PhantomData<(In, Out)>,
fut: SF::Future,
wrap_fn: Option<F>,
_phantom: PhantomData<(Req, Res)>,
}
impl<T, F, R, In, Out, Err> ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
impl<SF, F, Fut, Req, In, Res, Err> ApplyServiceFactoryResponse<SF, F, Fut, Req, In, Res, Err>
where
T: ServiceFactory<Error = Err>,
F: FnMut(In, &mut T::Service) -> R,
R: Future<Output = Result<Out, Err>>,
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
fn new(fut: T::Future, f: F) -> Self {
fn new(fut: SF::Future, wrap_fn: F) -> Self {
Self {
f: Some(f),
fut,
r: PhantomData,
wrap_fn: Some(wrap_fn),
_phantom: PhantomData,
}
}
}
impl<T, F, R, In, Out, Err> Future for ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
impl<SF, F, Fut, Req, In, Res, Err> Future
for ApplyServiceFactoryResponse<SF, F, Fut, Req, In, Res, Err>
where
T: ServiceFactory<Error = Err>,
F: FnMut(In, &mut T::Service) -> R,
R: Future<Output = Result<Out, Err>>,
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
type Output = Result<Apply<T::Service, F, R, In, Out, Err>, T::InitError>;
type Output = Result<Apply<SF::Service, F, Req, In, Res, Err>, SF::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
if let Poll::Ready(svc) = this.fut.poll(cx)? {
Poll::Ready(Ok(Apply::new(svc, this.f.take().unwrap())))
} else {
Poll::Pending
}
let svc = ready!(this.fut.poll(cx))?;
Poll::Ready(Ok(Apply::new(svc, Option::take(this.wrap_fn).unwrap())))
}
}
@ -213,8 +219,7 @@ mod tests {
#[derive(Clone)]
struct Srv;
impl Service for Srv {
type Request = ();
impl Service<()> for Srv {
type Response = ();
type Error = ();
type Future = Ready<Result<(), ()>>;

View File

@ -7,152 +7,152 @@ use std::task::{Context, Poll};
use crate::{Service, ServiceFactory};
/// Convert `Fn(Config, &mut Service1) -> Future<Service2>` fn to a service factory
pub fn apply_cfg<F, C, T, R, S, E>(
srv: T,
/// Convert `Fn(Config, &mut Service1) -> Future<Service2>` fn to a service factory.
pub fn apply_cfg<S1, Req, F, Cfg, Fut, S2, Err>(
srv: S1,
f: F,
) -> impl ServiceFactory<
Config = C,
Request = S::Request,
Response = S::Response,
Error = S::Error,
Service = S,
InitError = E,
Future = R,
Req,
Config = Cfg,
Response = S2::Response,
Error = S2::Error,
Service = S2,
InitError = Err,
Future = Fut,
> + Clone
where
F: FnMut(C, &mut T) -> R,
T: Service,
R: Future<Output = Result<S, E>>,
S: Service,
S1: Service<Req>,
F: FnMut(Cfg, &mut S1) -> Fut,
Fut: Future<Output = Result<S2, Err>>,
S2: Service<Req>,
{
ApplyConfigService {
srv: Rc::new(RefCell::new((srv, f))),
_t: PhantomData,
_phantom: PhantomData,
}
}
/// Convert `Fn(Config, &mut Service1) -> Future<Service2>` fn to a service factory
/// Convert `Fn(Config, &mut ServiceFactory1) -> Future<ServiceFactory2>` fn to a service factory.
///
/// Service1 get constructed from `T` factory.
pub fn apply_cfg_factory<F, C, T, R, S>(
factory: T,
pub fn apply_cfg_factory<SF, Req, F, Cfg, Fut, S>(
factory: SF,
f: F,
) -> impl ServiceFactory<
Config = C,
Request = S::Request,
Req,
Config = Cfg,
Response = S::Response,
Error = S::Error,
Service = S,
InitError = T::InitError,
InitError = SF::InitError,
> + Clone
where
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
SF: ServiceFactory<Req, Config = ()>,
F: FnMut(Cfg, &mut SF::Service) -> Fut,
SF::InitError: From<SF::Error>,
Fut: Future<Output = Result<S, SF::InitError>>,
S: Service<Req>,
{
ApplyConfigServiceFactory {
srv: Rc::new(RefCell::new((factory, f))),
_t: PhantomData,
_phantom: PhantomData,
}
}
/// Convert `Fn(Config, &mut Server) -> Future<Service>` fn to NewService\
struct ApplyConfigService<F, C, T, R, S, E>
struct ApplyConfigService<S1, Req, F, Cfg, Fut, S2, Err>
where
F: FnMut(C, &mut T) -> R,
T: Service,
R: Future<Output = Result<S, E>>,
S: Service,
S1: Service<Req>,
F: FnMut(Cfg, &mut S1) -> Fut,
Fut: Future<Output = Result<S2, Err>>,
S2: Service<Req>,
{
srv: Rc<RefCell<(T, F)>>,
_t: PhantomData<(C, R, S)>,
srv: Rc<RefCell<(S1, F)>>,
_phantom: PhantomData<(Cfg, Req, Fut, S2)>,
}
impl<F, C, T, R, S, E> Clone for ApplyConfigService<F, C, T, R, S, E>
impl<S1, Req, F, Cfg, Fut, S2, Err> Clone for ApplyConfigService<S1, Req, F, Cfg, Fut, S2, Err>
where
F: FnMut(C, &mut T) -> R,
T: Service,
R: Future<Output = Result<S, E>>,
S: Service,
S1: Service<Req>,
F: FnMut(Cfg, &mut S1) -> Fut,
Fut: Future<Output = Result<S2, Err>>,
S2: Service<Req>,
{
fn clone(&self) -> Self {
ApplyConfigService {
srv: self.srv.clone(),
_t: PhantomData,
_phantom: PhantomData,
}
}
}
impl<F, C, T, R, S, E> ServiceFactory for ApplyConfigService<F, C, T, R, S, E>
impl<S1, Req, F, Cfg, Fut, S2, Err> ServiceFactory<Req>
for ApplyConfigService<S1, Req, F, Cfg, Fut, S2, Err>
where
F: FnMut(C, &mut T) -> R,
T: Service,
R: Future<Output = Result<S, E>>,
S: Service,
S1: Service<Req>,
F: FnMut(Cfg, &mut S1) -> Fut,
Fut: Future<Output = Result<S2, Err>>,
S2: Service<Req>,
{
type Config = C;
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S;
type Config = Cfg;
type Response = S2::Response;
type Error = S2::Error;
type Service = S2;
type InitError = E;
type Future = R;
type InitError = Err;
type Future = Fut;
fn new_service(&self, cfg: C) -> Self::Future {
fn new_service(&self, cfg: Cfg) -> Self::Future {
let (t, f) = &mut *self.srv.borrow_mut();
f(cfg, t)
}
}
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
struct ApplyConfigServiceFactory<F, C, T, R, S>
struct ApplyConfigServiceFactory<SF, Req, F, Cfg, Fut, S>
where
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
SF: ServiceFactory<Req, Config = ()>,
F: FnMut(Cfg, &mut SF::Service) -> Fut,
Fut: Future<Output = Result<S, SF::InitError>>,
S: Service<Req>,
{
srv: Rc<RefCell<(T, F)>>,
_t: PhantomData<(C, R, S)>,
srv: Rc<RefCell<(SF, F)>>,
_phantom: PhantomData<(Cfg, Req, Fut, S)>,
}
impl<F, C, T, R, S> Clone for ApplyConfigServiceFactory<F, C, T, R, S>
impl<SF, Req, F, Cfg, Fut, S> Clone for ApplyConfigServiceFactory<SF, Req, F, Cfg, Fut, S>
where
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
SF: ServiceFactory<Req, Config = ()>,
F: FnMut(Cfg, &mut SF::Service) -> Fut,
Fut: Future<Output = Result<S, SF::InitError>>,
S: Service<Req>,
{
fn clone(&self) -> Self {
Self {
srv: self.srv.clone(),
_t: PhantomData,
_phantom: PhantomData,
}
}
}
impl<F, C, T, R, S> ServiceFactory for ApplyConfigServiceFactory<F, C, T, R, S>
impl<SF, Req, F, Cfg, Fut, S> ServiceFactory<Req>
for ApplyConfigServiceFactory<SF, Req, F, Cfg, Fut, S>
where
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
SF: ServiceFactory<Req, Config = ()>,
SF::InitError: From<SF::Error>,
F: FnMut(Cfg, &mut SF::Service) -> Fut,
Fut: Future<Output = Result<S, SF::InitError>>,
S: Service<Req>,
{
type Config = C;
type Request = S::Request;
type Config = Cfg;
type Response = S::Response;
type Error = S::Error;
type Service = S;
type InitError = T::InitError;
type Future = ApplyConfigServiceFactoryResponse<F, C, T, R, S>;
type InitError = SF::InitError;
type Future = ApplyConfigServiceFactoryResponse<SF, Req, F, Cfg, Fut, S>;
fn new_service(&self, cfg: C) -> Self::Future {
fn new_service(&self, cfg: Cfg) -> Self::Future {
ApplyConfigServiceFactoryResponse {
cfg: Some(cfg),
store: self.srv.clone(),
@ -162,42 +162,43 @@ where
}
#[pin_project::pin_project]
struct ApplyConfigServiceFactoryResponse<F, C, T, R, S>
struct ApplyConfigServiceFactoryResponse<SF, Req, F, Cfg, Fut, S>
where
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
SF: ServiceFactory<Req, Config = ()>,
SF::InitError: From<SF::Error>,
F: FnMut(Cfg, &mut SF::Service) -> Fut,
Fut: Future<Output = Result<S, SF::InitError>>,
S: Service<Req>,
{
cfg: Option<C>,
store: Rc<RefCell<(T, F)>>,
cfg: Option<Cfg>,
store: Rc<RefCell<(SF, F)>>,
#[pin]
state: State<T, R, S>,
state: State<SF, Fut, S, Req>,
}
#[pin_project::pin_project(project = StateProj)]
enum State<T, R, S>
enum State<SF, Fut, S, Req>
where
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
SF: ServiceFactory<Req, Config = ()>,
SF::InitError: From<SF::Error>,
Fut: Future<Output = Result<S, SF::InitError>>,
S: Service<Req>,
{
A(#[pin] T::Future),
B(T::Service),
C(#[pin] R),
A(#[pin] SF::Future),
B(SF::Service),
C(#[pin] Fut),
}
impl<F, C, T, R, S> Future for ApplyConfigServiceFactoryResponse<F, C, T, R, S>
impl<SF, Req, F, Cfg, Fut, S> Future
for ApplyConfigServiceFactoryResponse<SF, Req, F, Cfg, Fut, S>
where
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
SF: ServiceFactory<Req, Config = ()>,
SF::InitError: From<SF::Error>,
F: FnMut(Cfg, &mut SF::Service) -> Fut,
Fut: Future<Output = Result<S, SF::InitError>>,
S: Service<Req>,
{
type Output = Result<S, T::InitError>;
type Output = Result<S, SF::InitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();

View File

@ -1,30 +1,30 @@
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{future::Future, marker::PhantomData};
use futures_util::future::FutureExt;
use crate::{Service, ServiceFactory};
pub type BoxFuture<I, E> = Pin<Box<dyn Future<Output = Result<I, E>>>>;
pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T>>>;
pub type BoxService<Req, Res, Err> =
Box<dyn Service<Request = Req, Response = Res, Error = Err, Future = BoxFuture<Res, Err>>>;
Box<dyn Service<Req, Response = Res, Error = Err, Future = BoxFuture<Result<Res, Err>>>>;
pub struct BoxServiceFactory<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
pub struct BoxServiceFactory<Cfg, Req, Res, Err, InitErr>(Inner<Cfg, Req, Res, Err, InitErr>);
/// Create boxed service factory
pub fn factory<T>(
factory: T,
) -> BoxServiceFactory<T::Config, T::Request, T::Response, T::Error, T::InitError>
pub fn factory<SF, Req>(
factory: SF,
) -> BoxServiceFactory<SF::Config, Req, SF::Response, SF::Error, SF::InitError>
where
T: ServiceFactory + 'static,
T::Request: 'static,
T::Response: 'static,
T::Service: 'static,
T::Future: 'static,
T::Error: 'static,
T::InitError: 'static,
SF: ServiceFactory<Req> + 'static,
Req: 'static,
SF::Response: 'static,
SF::Service: 'static,
SF::Future: 'static,
SF::Error: 'static,
SF::InitError: 'static,
{
BoxServiceFactory(Box::new(FactoryWrapper {
factory,
@ -33,78 +33,75 @@ where
}
/// Create boxed service
pub fn service<T>(service: T) -> BoxService<T::Request, T::Response, T::Error>
pub fn service<S, Req>(service: S) -> BoxService<Req, S::Response, S::Error>
where
T: Service + 'static,
T::Future: 'static,
S: Service<Req> + 'static,
Req: 'static,
S::Future: 'static,
{
Box::new(ServiceWrapper(service))
Box::new(ServiceWrapper(service, PhantomData))
}
type Inner<C, Req, Res, Err, InitErr> = Box<
dyn ServiceFactory<
Req,
Config = C,
Request = Req,
Response = Res,
Error = Err,
InitError = InitErr,
Service = BoxService<Req, Res, Err>,
Future = BoxFuture<BoxService<Req, Res, Err>, InitErr>,
Future = BoxFuture<Result<BoxService<Req, Res, Err>, InitErr>>,
>,
>;
impl<C, Req, Res, Err, InitErr> ServiceFactory for BoxServiceFactory<C, Req, Res, Err, InitErr>
impl<C, Req, Res, Err, InitErr> ServiceFactory<Req>
for BoxServiceFactory<C, Req, Res, Err, InitErr>
where
Req: 'static,
Res: 'static,
Err: 'static,
InitErr: 'static,
{
type Request = Req;
type Response = Res;
type Error = Err;
type InitError = InitErr;
type Config = C;
type Service = BoxService<Req, Res, Err>;
type Future = BoxFuture<Self::Service, InitErr>;
type Future = BoxFuture<Result<Self::Service, InitErr>>;
fn new_service(&self, cfg: C) -> Self::Future {
self.0.new_service(cfg)
}
}
struct FactoryWrapper<C, T: ServiceFactory> {
factory: T,
_t: std::marker::PhantomData<C>,
struct FactoryWrapper<SF, Req, C>
where
SF: ServiceFactory<Req>,
{
factory: SF,
_t: PhantomData<(C, Req)>,
}
impl<C, T, Req, Res, Err, InitErr> ServiceFactory for FactoryWrapper<C, T>
impl<SF, Req, Cfg, Res, Err, InitErr> ServiceFactory<Req> for FactoryWrapper<SF, Req, Cfg>
where
Req: 'static,
Res: 'static,
Err: 'static,
InitErr: 'static,
T: ServiceFactory<
Config = C,
Request = Req,
Response = Res,
Error = Err,
InitError = InitErr,
>,
T::Future: 'static,
T::Service: 'static,
<T::Service as Service>::Future: 'static,
SF: ServiceFactory<Req, Config = Cfg, Response = Res, Error = Err, InitError = InitErr>,
SF::Future: 'static,
SF::Service: 'static,
<SF::Service as Service<Req>>::Future: 'static,
{
type Request = Req;
type Response = Res;
type Error = Err;
type InitError = InitErr;
type Config = C;
type Config = Cfg;
type Service = BoxService<Req, Res, Err>;
type Future = BoxFuture<Self::Service, Self::InitError>;
type Future = BoxFuture<Result<Self::Service, Self::InitError>>;
fn new_service(&self, cfg: C) -> Self::Future {
fn new_service(&self, cfg: Cfg) -> Self::Future {
Box::pin(
self.factory
.new_service(cfg)
@ -113,33 +110,33 @@ where
}
}
struct ServiceWrapper<T: Service>(T);
struct ServiceWrapper<S: Service<Req>, Req>(S, PhantomData<Req>);
impl<T> ServiceWrapper<T>
impl<S, Req> ServiceWrapper<S, Req>
where
T: Service + 'static,
T::Future: 'static,
S: Service<Req> + 'static,
Req: 'static,
S::Future: 'static,
{
fn boxed(service: T) -> BoxService<T::Request, T::Response, T::Error> {
Box::new(ServiceWrapper(service))
fn boxed(service: S) -> BoxService<Req, S::Response, S::Error> {
Box::new(ServiceWrapper(service, PhantomData))
}
}
impl<T, Req, Res, Err> Service for ServiceWrapper<T>
impl<S, Req, Res, Err> Service<Req> for ServiceWrapper<S, Req>
where
T: Service<Request = Req, Response = Res, Error = Err>,
T::Future: 'static,
S: Service<Req, Response = Res, Error = Err>,
S::Future: 'static,
{
type Request = Req;
type Response = Res;
type Error = Err;
type Future = BoxFuture<Res, Err>;
type Future = BoxFuture<Result<Res, Err>>;
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.0.poll_ready(ctx)
}
fn call(&mut self, req: Self::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
Box::pin(self.0.call(req))
}
}

View File

@ -53,9 +53,11 @@ where
/// Ok(())
/// }
/// ```
pub fn fn_factory<F, Cfg, Srv, Fut, Err>(f: F) -> FnServiceNoConfig<F, Cfg, Srv, Fut, Err>
pub fn fn_factory<F, Cfg, Srv, Req, Fut, Err>(
f: F,
) -> FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>
where
Srv: Service,
Srv: Service<Req>,
F: Fn() -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
{
@ -92,13 +94,13 @@ where
/// Ok(())
/// }
/// ```
pub fn fn_factory_with_config<F, Fut, Cfg, Srv, Err>(
pub fn fn_factory_with_config<F, Fut, Cfg, Srv, Req, Err>(
f: F,
) -> FnServiceConfig<F, Fut, Cfg, Srv, Err>
) -> FnServiceConfig<F, Fut, Cfg, Srv, Req, Err>
where
F: Fn(Cfg) -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service,
Srv: Service<Req>,
{
FnServiceConfig::new(f)
}
@ -132,12 +134,11 @@ where
}
}
impl<F, Fut, Req, Res, Err> Service for FnService<F, Fut, Req, Res, Err>
impl<F, Fut, Req, Res, Err> Service<Req> for FnService<F, Fut, Req, Res, Err>
where
F: FnMut(Req) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
type Request = Req;
type Response = Res;
type Error = Err;
type Future = Fut;
@ -151,7 +152,7 @@ where
}
}
impl<F, Fut, Req, Res, Err> IntoService<FnService<F, Fut, Req, Res, Err>> for F
impl<F, Fut, Req, Res, Err> IntoService<FnService<F, Fut, Req, Res, Err>, Req> for F
where
F: FnMut(Req) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
@ -190,12 +191,11 @@ where
}
}
impl<F, Fut, Req, Res, Err> Service for FnServiceFactory<F, Fut, Req, Res, Err, ()>
impl<F, Fut, Req, Res, Err> Service<Req> for FnServiceFactory<F, Fut, Req, Res, Err, ()>
where
F: FnMut(Req) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
type Request = Req;
type Response = Res;
type Error = Err;
type Future = Fut;
@ -204,17 +204,17 @@ where
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Self::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
(self.f)(req)
}
}
impl<F, Fut, Req, Res, Err, Cfg> ServiceFactory for FnServiceFactory<F, Fut, Req, Res, Err, Cfg>
impl<F, Fut, Req, Res, Err, Cfg> ServiceFactory<Req>
for FnServiceFactory<F, Fut, Req, Res, Err, Cfg>
where
F: FnMut(Req) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
type Request = Req;
type Response = Res;
type Error = Err;
@ -229,7 +229,7 @@ where
}
impl<F, Fut, Req, Res, Err, Cfg>
IntoServiceFactory<FnServiceFactory<F, Fut, Req, Res, Err, Cfg>> for F
IntoServiceFactory<FnServiceFactory<F, Fut, Req, Res, Err, Cfg>, Req> for F
where
F: Fn(Req) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
@ -240,32 +240,32 @@ where
}
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
pub struct FnServiceConfig<F, Fut, Cfg, Srv, Err>
pub struct FnServiceConfig<F, Fut, Cfg, Srv, Req, Err>
where
F: Fn(Cfg) -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service,
Srv: Service<Req>,
{
f: F,
_t: PhantomData<(Fut, Cfg, Srv, Err)>,
_t: PhantomData<(Fut, Cfg, Req, Srv, Err)>,
}
impl<F, Fut, Cfg, Srv, Err> FnServiceConfig<F, Fut, Cfg, Srv, Err>
impl<F, Fut, Cfg, Srv, Req, Err> FnServiceConfig<F, Fut, Cfg, Srv, Req, Err>
where
F: Fn(Cfg) -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service,
Srv: Service<Req>,
{
fn new(f: F) -> Self {
FnServiceConfig { f, _t: PhantomData }
}
}
impl<F, Fut, Cfg, Srv, Err> Clone for FnServiceConfig<F, Fut, Cfg, Srv, Err>
impl<F, Fut, Cfg, Srv, Req, Err> Clone for FnServiceConfig<F, Fut, Cfg, Srv, Req, Err>
where
F: Fn(Cfg) -> Fut + Clone,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service,
Srv: Service<Req>,
{
fn clone(&self) -> Self {
FnServiceConfig {
@ -275,13 +275,13 @@ where
}
}
impl<F, Fut, Cfg, Srv, Err> ServiceFactory for FnServiceConfig<F, Fut, Cfg, Srv, Err>
impl<F, Fut, Cfg, Srv, Req, Err> ServiceFactory<Req>
for FnServiceConfig<F, Fut, Cfg, Srv, Req, Err>
where
F: Fn(Cfg) -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service,
Srv: Service<Req>,
{
type Request = Srv::Request;
type Response = Srv::Response;
type Error = Srv::Error;
@ -296,64 +296,65 @@ where
}
/// Converter for `Fn() -> Future<Service>` fn
pub struct FnServiceNoConfig<F, C, S, R, E>
pub struct FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>
where
F: Fn() -> R,
S: Service,
R: Future<Output = Result<S, E>>,
F: Fn() -> Fut,
Srv: Service<Req>,
Fut: Future<Output = Result<Srv, Err>>,
{
f: F,
_t: PhantomData<C>,
_t: PhantomData<(Cfg, Req)>,
}
impl<F, C, S, R, E> FnServiceNoConfig<F, C, S, R, E>
impl<F, Cfg, Srv, Req, Fut, Err> FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>
where
F: Fn() -> R,
R: Future<Output = Result<S, E>>,
S: Service,
F: Fn() -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service<Req>,
{
fn new(f: F) -> Self {
Self { f, _t: PhantomData }
}
}
impl<F, C, S, R, E> ServiceFactory for FnServiceNoConfig<F, C, S, R, E>
impl<F, Cfg, Srv, Req, Fut, Err> ServiceFactory<Req>
for FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>
where
F: Fn() -> R,
R: Future<Output = Result<S, E>>,
S: Service,
F: Fn() -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service<Req>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S;
type Config = C;
type InitError = E;
type Future = R;
type Response = Srv::Response;
type Error = Srv::Error;
type Service = Srv;
type Config = Cfg;
type InitError = Err;
type Future = Fut;
fn new_service(&self, _: C) -> Self::Future {
fn new_service(&self, _: Cfg) -> Self::Future {
(self.f)()
}
}
impl<F, C, S, R, E> Clone for FnServiceNoConfig<F, C, S, R, E>
impl<F, Cfg, Srv, Req, Fut, Err> Clone for FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>
where
F: Fn() -> R + Clone,
R: Future<Output = Result<S, E>>,
S: Service,
F: Fn() -> Fut + Clone,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service<Req>,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())
}
}
impl<F, C, S, R, E> IntoServiceFactory<FnServiceNoConfig<F, C, S, R, E>> for F
impl<F, Cfg, Srv, Req, Fut, Err>
IntoServiceFactory<FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>, Req> for F
where
F: Fn() -> R,
R: Future<Output = Result<S, E>>,
S: Service,
F: Fn() -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service<Req>,
{
fn into_factory(self) -> FnServiceNoConfig<F, C, S, R, E> {
fn into_factory(self) -> FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err> {
FnServiceNoConfig::new(self)
}
}

View File

@ -72,14 +72,11 @@ pub use self::transform::{apply, Transform};
/// ```rust,ignore
/// async fn my_service(req: u8) -> Result<u64, MyError>;
/// ```
pub trait Service {
/// Requests handled by the service.
type Request;
pub trait Service<Req> {
/// Responses given by the service.
type Response;
/// Errors produced by the service.
/// Errors produced by the service when polling readiness or executing call.
type Error;
/// The future response value.
@ -109,7 +106,7 @@ pub trait Service {
///
/// Calling `call` without calling `poll_ready` is permitted. The
/// implementation must be resilient to this fact.
fn call(&mut self, req: Self::Request) -> Self::Future;
fn call(&mut self, req: Req) -> Self::Future;
/// Map this service's output to a different type, returning a new service
/// of the resulting type.
@ -120,7 +117,7 @@ pub trait Service {
/// Note that this function consumes the receiving service and returns a
/// wrapped version of it, similar to the existing `map` methods in the
/// standard library.
fn map<F, R>(self, f: F) -> crate::dev::Map<Self, F, R>
fn map<F, R>(self, f: F) -> crate::dev::Map<Self, F, Req, R>
where
Self: Sized,
F: FnMut(Self::Response) -> R,
@ -136,7 +133,7 @@ pub trait Service {
///
/// Note that this function consumes the receiving service and returns a
/// wrapped version of it.
fn map_err<F, E>(self, f: F) -> crate::dev::MapErr<Self, F, E>
fn map_err<F, E>(self, f: F) -> crate::dev::MapErr<Self, Req, F, E>
where
Self: Sized,
F: Fn(Self::Error) -> E,
@ -154,10 +151,7 @@ pub trait Service {
/// requests on that new TCP stream.
///
/// `Config` is a service factory configuration type.
pub trait ServiceFactory {
/// Requests handled by the created services.
type Request;
pub trait ServiceFactory<Req> {
/// Responses given by the created services.
type Response;
@ -168,11 +162,7 @@ pub trait ServiceFactory {
type Config;
/// The kind of `Service` created by this factory.
type Service: Service<
Request = Self::Request,
Response = Self::Response,
Error = Self::Error,
>;
type Service: Service<Req, Response = Self::Response, Error = Self::Error>;
/// Errors potentially raised while building a service.
type InitError;
@ -185,7 +175,7 @@ pub trait ServiceFactory {
/// Map this service's output to a different type, returning a new service
/// of the resulting type.
fn map<F, R>(self, f: F) -> crate::map::MapServiceFactory<Self, F, R>
fn map<F, R>(self, f: F) -> crate::map::MapServiceFactory<Self, F, Req, R>
where
Self: Sized,
F: FnMut(Self::Response) -> R + Clone,
@ -194,7 +184,7 @@ pub trait ServiceFactory {
}
/// Map this service's error to a different error, returning a new service.
fn map_err<F, E>(self, f: F) -> crate::map_err::MapErrServiceFactory<Self, F, E>
fn map_err<F, E>(self, f: F) -> crate::map_err::MapErrServiceFactory<Self, Req, F, E>
where
Self: Sized,
F: Fn(Self::Error) -> E + Clone,
@ -203,7 +193,7 @@ pub trait ServiceFactory {
}
/// Map this factory's init error to a different error, returning a new service.
fn map_init_err<F, E>(self, f: F) -> crate::map_init_err::MapInitErr<Self, F, E>
fn map_init_err<F, E>(self, f: F) -> crate::map_init_err::MapInitErr<Self, F, Req, E>
where
Self: Sized,
F: Fn(Self::InitError) -> E + Clone,
@ -212,11 +202,10 @@ pub trait ServiceFactory {
}
}
impl<'a, S> Service for &'a mut S
impl<'a, S, Req> Service<Req> for &'a mut S
where
S: Service + 'a,
S: Service<Req> + 'a,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@ -225,16 +214,15 @@ where
(**self).poll_ready(ctx)
}
fn call(&mut self, request: Self::Request) -> S::Future {
fn call(&mut self, request: Req) -> S::Future {
(**self).call(request)
}
}
impl<S> Service for Box<S>
impl<S, Req> Service<Req> for Box<S>
where
S: Service + ?Sized,
S: Service<Req> + ?Sized,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@ -243,16 +231,15 @@ where
(**self).poll_ready(ctx)
}
fn call(&mut self, request: Self::Request) -> S::Future {
fn call(&mut self, request: Req) -> S::Future {
(**self).call(request)
}
}
impl<S> Service for RefCell<S>
impl<S, Req> Service<Req> for RefCell<S>
where
S: Service,
S: Service<Req>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@ -261,16 +248,15 @@ where
self.borrow_mut().poll_ready(ctx)
}
fn call(&mut self, request: Self::Request) -> S::Future {
fn call(&mut self, request: Req) -> S::Future {
self.borrow_mut().call(request)
}
}
impl<S> Service for Rc<RefCell<S>>
impl<S, Req> Service<Req> for Rc<RefCell<S>>
where
S: Service,
S: Service<Req>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@ -279,16 +265,15 @@ where
self.borrow_mut().poll_ready(ctx)
}
fn call(&mut self, request: Self::Request) -> S::Future {
fn call(&mut self, request: Req) -> S::Future {
(&mut (**self).borrow_mut()).call(request)
}
}
impl<S> ServiceFactory for Rc<S>
impl<S, Req> ServiceFactory<Req> for Rc<S>
where
S: ServiceFactory,
S: ServiceFactory<Req>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Config = S::Config;
@ -301,11 +286,10 @@ where
}
}
impl<S> ServiceFactory for Arc<S>
impl<S, Req> ServiceFactory<Req> for Arc<S>
where
S: ServiceFactory,
S: ServiceFactory<Req>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Config = S::Config;
@ -319,52 +303,52 @@ where
}
/// Trait for types that can be converted to a `Service`
pub trait IntoService<T>
pub trait IntoService<S, Req>
where
T: Service,
S: Service<Req>,
{
/// Convert to a `Service`
fn into_service(self) -> T;
fn into_service(self) -> S;
}
/// Trait for types that can be converted to a `ServiceFactory`
pub trait IntoServiceFactory<T>
pub trait IntoServiceFactory<SF, Req>
where
T: ServiceFactory,
SF: ServiceFactory<Req>,
{
/// Convert `Self` to a `ServiceFactory`
fn into_factory(self) -> T;
fn into_factory(self) -> SF;
}
impl<T> IntoService<T> for T
impl<S, Req> IntoService<S, Req> for S
where
T: Service,
S: Service<Req>,
{
fn into_service(self) -> T {
fn into_service(self) -> S {
self
}
}
impl<T> IntoServiceFactory<T> for T
impl<SF, Req> IntoServiceFactory<SF, Req> for SF
where
T: ServiceFactory,
SF: ServiceFactory<Req>,
{
fn into_factory(self) -> T {
fn into_factory(self) -> SF {
self
}
}
/// Convert object of type `T` to a service `S`
pub fn into_service<T, S>(tp: T) -> S
/// Convert object of type `U` to a service `S`
pub fn into_service<I, S, Req>(tp: I) -> S
where
S: Service,
T: IntoService<S>,
I: IntoService<S, Req>,
S: Service<Req>,
{
tp.into_service()
}
pub mod dev {
pub use crate::apply::{Apply, ApplyServiceFactory};
pub use crate::apply::{Apply, ApplyFactory};
pub use crate::fn_service::{
FnService, FnServiceConfig, FnServiceFactory, FnServiceNoConfig,
};

View File

@ -8,18 +8,18 @@ use super::{Service, ServiceFactory};
/// Service for the `map` combinator, changing the type of a service's response.
///
/// This is created by the `ServiceExt::map` method.
pub struct Map<A, F, Response> {
pub struct Map<A, F, Req, Res> {
service: A,
f: F,
_t: PhantomData<Response>,
_t: PhantomData<(Req, Res)>,
}
impl<A, F, Response> Map<A, F, Response> {
impl<A, F, Req, Res> Map<A, F, Req, Res> {
/// Create new `Map` combinator
pub(crate) fn new(service: A, f: F) -> Self
where
A: Service,
F: FnMut(A::Response) -> Response,
A: Service<Req>,
F: FnMut(A::Response) -> Res,
{
Self {
service,
@ -29,7 +29,7 @@ impl<A, F, Response> Map<A, F, Response> {
}
}
impl<A, F, Response> Clone for Map<A, F, Response>
impl<A, F, Req, Res> Clone for Map<A, F, Req, Res>
where
A: Clone,
F: Clone,
@ -43,52 +43,51 @@ where
}
}
impl<A, F, Response> Service for Map<A, F, Response>
impl<A, F, Req, Res> Service<Req> for Map<A, F, Req, Res>
where
A: Service,
F: FnMut(A::Response) -> Response + Clone,
A: Service<Req>,
F: FnMut(A::Response) -> Res + Clone,
{
type Request = A::Request;
type Response = Response;
type Response = Res;
type Error = A::Error;
type Future = MapFuture<A, F, Response>;
type Future = MapFuture<A, F, Req, Res>;
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(ctx)
}
fn call(&mut self, req: A::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
MapFuture::new(self.service.call(req), self.f.clone())
}
}
#[pin_project::pin_project]
pub struct MapFuture<A, F, Response>
pub struct MapFuture<A, F, Req, Res>
where
A: Service,
F: FnMut(A::Response) -> Response,
A: Service<Req>,
F: FnMut(A::Response) -> Res,
{
f: F,
#[pin]
fut: A::Future,
}
impl<A, F, Response> MapFuture<A, F, Response>
impl<A, F, Req, Res> MapFuture<A, F, Req, Res>
where
A: Service,
F: FnMut(A::Response) -> Response,
A: Service<Req>,
F: FnMut(A::Response) -> Res,
{
fn new(fut: A::Future, f: F) -> Self {
MapFuture { f, fut }
}
}
impl<A, F, Response> Future for MapFuture<A, F, Response>
impl<A, F, Req, Res> Future for MapFuture<A, F, Req, Res>
where
A: Service,
F: FnMut(A::Response) -> Response,
A: Service<Req>,
F: FnMut(A::Response) -> Res,
{
type Output = Result<Response, A::Error>;
type Output = Result<Res, A::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
@ -102,17 +101,17 @@ where
}
/// `MapNewService` new service combinator
pub struct MapServiceFactory<A, F, Res> {
pub struct MapServiceFactory<A, F, Req, Res> {
a: A,
f: F,
r: PhantomData<Res>,
r: PhantomData<(Res, Req)>,
}
impl<A, F, Res> MapServiceFactory<A, F, Res> {
impl<A, F, Req, Res> MapServiceFactory<A, F, Req, Res> {
/// Create new `Map` new service instance
pub(crate) fn new(a: A, f: F) -> Self
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: FnMut(A::Response) -> Res,
{
Self {
@ -123,7 +122,7 @@ impl<A, F, Res> MapServiceFactory<A, F, Res> {
}
}
impl<A, F, Res> Clone for MapServiceFactory<A, F, Res>
impl<A, F, Req, Res> Clone for MapServiceFactory<A, F, Req, Res>
where
A: Clone,
F: Clone,
@ -137,19 +136,18 @@ where
}
}
impl<A, F, Res> ServiceFactory for MapServiceFactory<A, F, Res>
impl<A, F, Req, Res> ServiceFactory<Req> for MapServiceFactory<A, F, Req, Res>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: FnMut(A::Response) -> Res + Clone,
{
type Request = A::Request;
type Response = Res;
type Error = A::Error;
type Config = A::Config;
type Service = Map<A::Service, F, Res>;
type Service = Map<A::Service, F, Req, Res>;
type InitError = A::InitError;
type Future = MapServiceFuture<A, F, Res>;
type Future = MapServiceFuture<A, F, Req, Res>;
fn new_service(&self, cfg: A::Config) -> Self::Future {
MapServiceFuture::new(self.a.new_service(cfg), self.f.clone())
@ -157,9 +155,9 @@ where
}
#[pin_project::pin_project]
pub struct MapServiceFuture<A, F, Res>
pub struct MapServiceFuture<A, F, Req, Res>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: FnMut(A::Response) -> Res,
{
#[pin]
@ -167,9 +165,9 @@ where
f: Option<F>,
}
impl<A, F, Res> MapServiceFuture<A, F, Res>
impl<A, F, Req, Res> MapServiceFuture<A, F, Req, Res>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: FnMut(A::Response) -> Res,
{
fn new(fut: A::Future, f: F) -> Self {
@ -177,12 +175,12 @@ where
}
}
impl<A, F, Res> Future for MapServiceFuture<A, F, Res>
impl<A, F, Req, Res> Future for MapServiceFuture<A, F, Req, Res>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: FnMut(A::Response) -> Res,
{
type Output = Result<Map<A::Service, F, Res>, A::InitError>;
type Output = Result<Map<A::Service, F, Req, Res>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
@ -204,8 +202,7 @@ mod tests {
struct Srv;
impl Service for Srv {
type Request = ();
impl Service<()> for Srv {
type Response = ();
type Error = ();
type Future = Ready<Result<(), ()>>;

View File

@ -6,121 +6,123 @@ use super::{IntoServiceFactory, ServiceFactory};
///
/// Note that this function consumes the receiving service factory and returns
/// a wrapped version of it.
pub fn map_config<T, U, F, C>(factory: U, f: F) -> MapConfig<T, F, C>
pub fn map_config<I, SF, S, Req, F, Cfg>(factory: I, f: F) -> MapConfig<SF, Req, F, Cfg>
where
T: ServiceFactory,
U: IntoServiceFactory<T>,
F: Fn(C) -> T::Config,
I: IntoServiceFactory<SF, Req>,
SF: ServiceFactory<Req>,
F: Fn(Cfg) -> SF::Config,
{
MapConfig::new(factory.into_factory(), f)
}
/// Replace config with unit
pub fn unit_config<T, U, C>(factory: U) -> UnitConfig<T, C>
/// Replace config with unit.
pub fn unit_config<I, SF, Cfg, Req>(factory: I) -> UnitConfig<SF, Cfg, Req>
where
T: ServiceFactory<Config = ()>,
U: IntoServiceFactory<T>,
I: IntoServiceFactory<SF, Req>,
SF: ServiceFactory<Req, Config = ()>,
{
UnitConfig::new(factory.into_factory())
}
/// `map_config()` adapter service factory
pub struct MapConfig<A, F, C> {
a: A,
f: F,
e: PhantomData<C>,
pub struct MapConfig<SF, Req, F, Cfg> {
factory: SF,
cfg_mapper: F,
e: PhantomData<(Cfg, Req)>,
}
impl<A, F, C> MapConfig<A, F, C> {
impl<SF, Req, F, Cfg> MapConfig<SF, Req, F, Cfg> {
/// Create new `MapConfig` combinator
pub(crate) fn new(a: A, f: F) -> Self
pub(crate) fn new(factory: SF, cfg_mapper: F) -> Self
where
A: ServiceFactory,
F: Fn(C) -> A::Config,
SF: ServiceFactory<Req>,
F: Fn(Cfg) -> SF::Config,
{
Self {
a,
f,
factory,
cfg_mapper,
e: PhantomData,
}
}
}
impl<A, F, C> Clone for MapConfig<A, F, C>
impl<SF, Req, F, Cfg> Clone for MapConfig<SF, Req, F, Cfg>
where
A: Clone,
SF: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
a: self.a.clone(),
f: self.f.clone(),
factory: self.factory.clone(),
cfg_mapper: self.cfg_mapper.clone(),
e: PhantomData,
}
}
}
impl<A, F, C> ServiceFactory for MapConfig<A, F, C>
impl<SF, Req, F, Cfg> ServiceFactory<Req> for MapConfig<SF, Req, F, Cfg>
where
A: ServiceFactory,
F: Fn(C) -> A::Config,
SF: ServiceFactory<Req>,
F: Fn(Cfg) -> SF::Config,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type Response = SF::Response;
type Error = SF::Error;
type Config = C;
type Service = A::Service;
type InitError = A::InitError;
type Future = A::Future;
type Config = Cfg;
type Service = SF::Service;
type InitError = SF::InitError;
type Future = SF::Future;
fn new_service(&self, cfg: C) -> Self::Future {
self.a.new_service((self.f)(cfg))
fn new_service(&self, cfg: Self::Config) -> Self::Future {
let mapped_cfg = (self.cfg_mapper)(cfg);
self.factory.new_service(mapped_cfg)
}
}
/// `unit_config()` config combinator
pub struct UnitConfig<A, C> {
a: A,
e: PhantomData<C>,
pub struct UnitConfig<SF, Cfg, Req> {
factory: SF,
_phantom: PhantomData<(Cfg, Req)>,
}
impl<A, C> UnitConfig<A, C>
impl<SF, Cfg, Req> UnitConfig<SF, Cfg, Req>
where
A: ServiceFactory<Config = ()>,
SF: ServiceFactory<Req, Config = ()>,
{
/// Create new `UnitConfig` combinator
pub(crate) fn new(a: A) -> Self {
Self { a, e: PhantomData }
}
}
impl<A, C> Clone for UnitConfig<A, C>
where
A: Clone,
{
fn clone(&self) -> Self {
pub(crate) fn new(factory: SF) -> Self {
Self {
a: self.a.clone(),
e: PhantomData,
factory,
_phantom: PhantomData,
}
}
}
impl<A, C> ServiceFactory for UnitConfig<A, C>
impl<SF, Cfg, Req> Clone for UnitConfig<SF, Cfg, Req>
where
A: ServiceFactory<Config = ()>,
SF: Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type Config = C;
type Service = A::Service;
type InitError = A::InitError;
type Future = A::Future;
fn new_service(&self, _: C) -> Self::Future {
self.a.new_service(())
fn clone(&self) -> Self {
Self {
factory: self.factory.clone(),
_phantom: PhantomData,
}
}
}
impl<SF, Cfg, Req> ServiceFactory<Req> for UnitConfig<SF, Cfg, Req>
where
SF: ServiceFactory<Req, Config = ()>,
{
type Response = SF::Response;
type Error = SF::Error;
type Config = Cfg;
type Service = SF::Service;
type InitError = SF::InitError;
type Future = SF::Future;
fn new_service(&self, _: Cfg) -> Self::Future {
self.factory.new_service(())
}
}

View File

@ -9,18 +9,18 @@ use super::{Service, ServiceFactory};
/// error.
///
/// This is created by the `ServiceExt::map_err` method.
pub struct MapErr<A, F, E> {
service: A,
pub struct MapErr<S, Req, F, E> {
service: S,
f: F,
_t: PhantomData<E>,
_t: PhantomData<(E, Req)>,
}
impl<A, F, E> MapErr<A, F, E> {
impl<S, Req, F, E> MapErr<S, Req, F, E> {
/// Create new `MapErr` combinator
pub(crate) fn new(service: A, f: F) -> Self
pub(crate) fn new(service: S, f: F) -> Self
where
A: Service,
F: Fn(A::Error) -> E,
S: Service<Req>,
F: Fn(S::Error) -> E,
{
Self {
service,
@ -30,9 +30,9 @@ impl<A, F, E> MapErr<A, F, E> {
}
}
impl<A, F, E> Clone for MapErr<A, F, E>
impl<S, Req, F, E> Clone for MapErr<S, Req, F, E>
where
A: Clone,
S: Clone,
F: Clone,
{
fn clone(&self) -> Self {
@ -44,29 +44,28 @@ where
}
}
impl<A, F, E> Service for MapErr<A, F, E>
impl<A, Req, F, E> Service<Req> for MapErr<A, Req, F, E>
where
A: Service,
A: Service<Req>,
F: Fn(A::Error) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = E;
type Future = MapErrFuture<A, F, E>;
type Future = MapErrFuture<A, Req, F, E>;
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(ctx).map_err(&self.f)
}
fn call(&mut self, req: A::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
MapErrFuture::new(self.service.call(req), self.f.clone())
}
}
#[pin_project::pin_project]
pub struct MapErrFuture<A, F, E>
pub struct MapErrFuture<A, Req, F, E>
where
A: Service,
A: Service<Req>,
F: Fn(A::Error) -> E,
{
f: F,
@ -74,9 +73,9 @@ where
fut: A::Future,
}
impl<A, F, E> MapErrFuture<A, F, E>
impl<A, Req, F, E> MapErrFuture<A, Req, F, E>
where
A: Service,
A: Service<Req>,
F: Fn(A::Error) -> E,
{
fn new(fut: A::Future, f: F) -> Self {
@ -84,9 +83,9 @@ where
}
}
impl<A, F, E> Future for MapErrFuture<A, F, E>
impl<A, Req, F, E> Future for MapErrFuture<A, Req, F, E>
where
A: Service,
A: Service<Req>,
F: Fn(A::Error) -> E,
{
type Output = Result<A::Response, E>;
@ -101,19 +100,19 @@ where
/// service's error.
///
/// This is created by the `NewServiceExt::map_err` method.
pub struct MapErrServiceFactory<A, F, E>
pub struct MapErrServiceFactory<A, Req, F, E>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: Fn(A::Error) -> E + Clone,
{
a: A,
f: F,
e: PhantomData<E>,
e: PhantomData<(E, Req)>,
}
impl<A, F, E> MapErrServiceFactory<A, F, E>
impl<A, Req, F, E> MapErrServiceFactory<A, Req, F, E>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: Fn(A::Error) -> E + Clone,
{
/// Create new `MapErr` new service instance
@ -126,9 +125,9 @@ where
}
}
impl<A, F, E> Clone for MapErrServiceFactory<A, F, E>
impl<A, Req, F, E> Clone for MapErrServiceFactory<A, Req, F, E>
where
A: ServiceFactory + Clone,
A: ServiceFactory<Req> + Clone,
F: Fn(A::Error) -> E + Clone,
{
fn clone(&self) -> Self {
@ -140,19 +139,18 @@ where
}
}
impl<A, F, E> ServiceFactory for MapErrServiceFactory<A, F, E>
impl<A, Req, F, E> ServiceFactory<Req> for MapErrServiceFactory<A, Req, F, E>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: Fn(A::Error) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = E;
type Config = A::Config;
type Service = MapErr<A::Service, F, E>;
type Service = MapErr<A::Service, Req, F, E>;
type InitError = A::InitError;
type Future = MapErrServiceFuture<A, F, E>;
type Future = MapErrServiceFuture<A, Req, F, E>;
fn new_service(&self, cfg: A::Config) -> Self::Future {
MapErrServiceFuture::new(self.a.new_service(cfg), self.f.clone())
@ -160,9 +158,9 @@ where
}
#[pin_project::pin_project]
pub struct MapErrServiceFuture<A, F, E>
pub struct MapErrServiceFuture<A, Req, F, E>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: Fn(A::Error) -> E,
{
#[pin]
@ -170,9 +168,9 @@ where
f: F,
}
impl<A, F, E> MapErrServiceFuture<A, F, E>
impl<A, Req, F, E> MapErrServiceFuture<A, Req, F, E>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: Fn(A::Error) -> E,
{
fn new(fut: A::Future, f: F) -> Self {
@ -180,12 +178,12 @@ where
}
}
impl<A, F, E> Future for MapErrServiceFuture<A, F, E>
impl<A, Req, F, E> Future for MapErrServiceFuture<A, Req, F, E>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: Fn(A::Error) -> E + Clone,
{
type Output = Result<MapErr<A::Service, F, E>, A::InitError>;
type Output = Result<MapErr<A::Service, Req, F, E>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
@ -206,8 +204,7 @@ mod tests {
struct Srv;
impl Service for Srv {
type Request = ();
impl Service<()> for Srv {
type Response = ();
type Error = ();
type Future = Ready<Result<(), ()>>;

View File

@ -6,16 +6,16 @@ use std::task::{Context, Poll};
use super::ServiceFactory;
/// `MapInitErr` service combinator
pub struct MapInitErr<A, F, E> {
pub struct MapInitErr<A, F, Req, Err> {
a: A,
f: F,
e: PhantomData<E>,
e: PhantomData<(Req, Err)>,
}
impl<A, F, E> MapInitErr<A, F, E>
impl<A, F, Req, Err> MapInitErr<A, F, Req, Err>
where
A: ServiceFactory,
F: Fn(A::InitError) -> E,
A: ServiceFactory<Req>,
F: Fn(A::InitError) -> Err,
{
/// Create new `MapInitErr` combinator
pub(crate) fn new(a: A, f: F) -> Self {
@ -27,7 +27,7 @@ where
}
}
impl<A, F, E> Clone for MapInitErr<A, F, E>
impl<A, F, Req, E> Clone for MapInitErr<A, F, Req, E>
where
A: Clone,
F: Clone,
@ -41,19 +41,18 @@ where
}
}
impl<A, F, E> ServiceFactory for MapInitErr<A, F, E>
impl<A, F, Req, E> ServiceFactory<Req> for MapInitErr<A, F, Req, E>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: Fn(A::InitError) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type Config = A::Config;
type Service = A::Service;
type InitError = E;
type Future = MapInitErrFuture<A, F, E>;
type Future = MapInitErrFuture<A, F, Req, E>;
fn new_service(&self, cfg: A::Config) -> Self::Future {
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
@ -61,9 +60,9 @@ where
}
#[pin_project::pin_project]
pub struct MapInitErrFuture<A, F, E>
pub struct MapInitErrFuture<A, F, Req, E>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: Fn(A::InitError) -> E,
{
f: F,
@ -71,9 +70,9 @@ where
fut: A::Future,
}
impl<A, F, E> MapInitErrFuture<A, F, E>
impl<A, F, Req, E> MapInitErrFuture<A, F, Req, E>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: Fn(A::InitError) -> E,
{
fn new(fut: A::Future, f: F) -> Self {
@ -81,9 +80,9 @@ where
}
}
impl<A, F, E> Future for MapInitErrFuture<A, F, E>
impl<A, F, Req, E> Future for MapInitErrFuture<A, F, Req, E>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
F: Fn(A::InitError) -> E,
{
type Output = Result<A::Service, E>;

View File

@ -1,5 +1,5 @@
use std::future::Future;
use std::task::{Context, Poll};
use std::{future::Future, marker::PhantomData};
use crate::and_then::{AndThenService, AndThenServiceFactory};
use crate::and_then_apply_fn::{AndThenApplyFn, AndThenApplyFnFactory};
@ -10,33 +10,39 @@ use crate::then::{ThenService, ThenServiceFactory};
use crate::{IntoService, IntoServiceFactory, Service, ServiceFactory};
/// Construct new pipeline with one service in pipeline chain.
pub fn pipeline<F, T>(service: F) -> Pipeline<T>
pub fn pipeline<I, S, Req>(service: I) -> Pipeline<S, Req>
where
F: IntoService<T>,
T: Service,
I: IntoService<S, Req>,
S: Service<Req>,
{
Pipeline {
service: service.into_service(),
_phantom: PhantomData,
}
}
/// Construct new pipeline factory with one service factory.
pub fn pipeline_factory<T, F>(factory: F) -> PipelineFactory<T>
pub fn pipeline_factory<I, SF, Req>(factory: I) -> PipelineFactory<SF, Req>
where
T: ServiceFactory,
F: IntoServiceFactory<T>,
I: IntoServiceFactory<SF, Req>,
SF: ServiceFactory<Req>,
{
PipelineFactory {
factory: factory.into_factory(),
_phantom: PhantomData,
}
}
/// Pipeline service - pipeline allows to compose multiple service into one service.
pub struct Pipeline<T> {
service: T,
pub struct Pipeline<S, Req> {
service: S,
_phantom: PhantomData<Req>,
}
impl<T: Service> Pipeline<T> {
impl<S, Req> Pipeline<S, Req>
where
S: Service<Req>,
{
/// Call another service after call to this one has resolved successfully.
///
/// This function can be used to chain two services together and ensure that
@ -46,41 +52,40 @@ impl<T: Service> Pipeline<T> {
///
/// Note that this function consumes the receiving service and returns a
/// wrapped version of it.
pub fn and_then<F, U>(
pub fn and_then<I, S1>(
self,
service: F,
) -> Pipeline<
impl Service<Request = T::Request, Response = U::Response, Error = T::Error> + Clone,
>
service: I,
) -> Pipeline<impl Service<Req, Response = S1::Response, Error = S::Error> + Clone, Req>
where
Self: Sized,
F: IntoService<U>,
U: Service<Request = T::Response, Error = T::Error>,
I: IntoService<S1, S::Response>,
S1: Service<S::Response, Error = S::Error>,
{
Pipeline {
service: AndThenService::new(self.service, service.into_service()),
_phantom: PhantomData,
}
}
/// Apply function to specified service and use it as a next service in
/// chain.
/// Apply function to specified service and use it as a next service in chain.
///
/// Short version of `pipeline_factory(...).and_then(apply_fn_factory(...))`
pub fn and_then_apply_fn<U, I, F, Fut, Res, Err>(
/// Short version of `pipeline_factory(...).and_then(apply_fn(...))`
pub fn and_then_apply_fn<I, S1, F, Fut, In, Res, Err>(
self,
service: I,
f: F,
) -> Pipeline<impl Service<Request = T::Request, Response = Res, Error = Err> + Clone>
wrap_fn: F,
) -> Pipeline<impl Service<Req, Response = Res, Error = Err> + Clone, Req>
where
Self: Sized,
I: IntoService<U>,
U: Service,
F: FnMut(T::Response, &mut U) -> Fut,
I: IntoService<S1, In>,
S1: Service<In>,
F: FnMut(S::Response, &mut S1) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<T::Error> + From<U::Error>,
Err: From<S::Error> + From<S1::Error>,
{
Pipeline {
service: AndThenApplyFn::new(self.service, service.into_service(), f),
service: AndThenApplyFn::new(self.service, service.into_service(), wrap_fn),
_phantom: PhantomData,
}
}
@ -89,19 +94,18 @@ impl<T: Service> Pipeline<T> {
///
/// Note that this function consumes the receiving pipeline and returns a
/// wrapped version of it.
pub fn then<F, U>(
pub fn then<F, S1>(
self,
service: F,
) -> Pipeline<
impl Service<Request = T::Request, Response = U::Response, Error = T::Error> + Clone,
>
) -> Pipeline<impl Service<Req, Response = S1::Response, Error = S::Error> + Clone, Req>
where
Self: Sized,
F: IntoService<U>,
U: Service<Request = Result<T::Response, T::Error>, Error = T::Error>,
F: IntoService<S1, Result<S::Response, S::Error>>,
S1: Service<Result<S::Response, S::Error>, Error = S::Error>,
{
Pipeline {
service: ThenService::new(self.service, service.into_service()),
_phantom: PhantomData,
}
}
@ -114,13 +118,14 @@ impl<T: Service> Pipeline<T> {
/// Note that this function consumes the receiving service and returns a
/// wrapped version of it, similar to the existing `map` methods in the
/// standard library.
pub fn map<F, R>(self, f: F) -> Pipeline<Map<T, F, R>>
pub fn map<F, R>(self, f: F) -> Pipeline<Map<S, F, Req, R>, Req>
where
Self: Sized,
F: FnMut(T::Response) -> R,
F: FnMut(S::Response) -> R,
{
Pipeline {
service: Map::new(self.service, f),
_phantom: PhantomData,
}
}
@ -132,114 +137,118 @@ impl<T: Service> Pipeline<T> {
///
/// Note that this function consumes the receiving service and returns a
/// wrapped version of it.
pub fn map_err<F, E>(self, f: F) -> Pipeline<MapErr<T, F, E>>
pub fn map_err<F, E>(self, f: F) -> Pipeline<MapErr<S, Req, F, E>, Req>
where
Self: Sized,
F: Fn(T::Error) -> E,
F: Fn(S::Error) -> E,
{
Pipeline {
service: MapErr::new(self.service, f),
_phantom: PhantomData,
}
}
}
impl<T> Clone for Pipeline<T>
impl<T, Req> Clone for Pipeline<T, Req>
where
T: Clone,
{
fn clone(&self) -> Self {
Pipeline {
service: self.service.clone(),
_phantom: PhantomData,
}
}
}
impl<T: Service> Service for Pipeline<T> {
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Future = T::Future;
impl<S: Service<Req>, Req> Service<Req> for Pipeline<S, Req> {
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
#[inline]
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), T::Error>> {
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
self.service.poll_ready(ctx)
}
#[inline]
fn call(&mut self, req: T::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
self.service.call(req)
}
}
/// Pipeline factory
pub struct PipelineFactory<T> {
factory: T,
pub struct PipelineFactory<SF, Req> {
factory: SF,
_phantom: PhantomData<Req>,
}
impl<T: ServiceFactory> PipelineFactory<T> {
impl<SF, Req> PipelineFactory<SF, Req>
where
SF: ServiceFactory<Req>,
{
/// Call another service after call to this one has resolved successfully.
pub fn and_then<F, U>(
pub fn and_then<I, SF1>(
self,
factory: F,
factory: I,
) -> PipelineFactory<
impl ServiceFactory<
Request = T::Request,
Response = U::Response,
Error = T::Error,
Config = T::Config,
InitError = T::InitError,
Service = impl Service<
Request = T::Request,
Response = U::Response,
Error = T::Error,
> + Clone,
Req,
Response = SF1::Response,
Error = SF::Error,
Config = SF::Config,
InitError = SF::InitError,
Service = impl Service<Req, Response = SF1::Response, Error = SF::Error> + Clone,
> + Clone,
Req,
>
where
Self: Sized,
T::Config: Clone,
F: IntoServiceFactory<U>,
U: ServiceFactory<
Config = T::Config,
Request = T::Response,
Error = T::Error,
InitError = T::InitError,
SF::Config: Clone,
I: IntoServiceFactory<SF1, SF::Response>,
SF1: ServiceFactory<
SF::Response,
Config = SF::Config,
Error = SF::Error,
InitError = SF::InitError,
>,
{
PipelineFactory {
factory: AndThenServiceFactory::new(self.factory, factory.into_factory()),
_phantom: PhantomData,
}
}
/// Apply function to specified service and use it as a next service in
/// chain.
/// Apply function to specified service and use it as a next service in chain.
///
/// Short version of `pipeline_factory(...).and_then(apply_fn_factory(...))`
pub fn and_then_apply_fn<U, I, F, Fut, Res, Err>(
pub fn and_then_apply_fn<I, SF1, Fut, F, In, Res, Err>(
self,
factory: I,
f: F,
wrap_fn: F,
) -> PipelineFactory<
impl ServiceFactory<
Request = T::Request,
Req,
Response = Res,
Error = Err,
Config = T::Config,
InitError = T::InitError,
Service = impl Service<Request = T::Request, Response = Res, Error = Err> + Clone,
Config = SF::Config,
InitError = SF::InitError,
Service = impl Service<Req, Response = Res, Error = Err> + Clone,
> + Clone,
Req,
>
where
Self: Sized,
T::Config: Clone,
I: IntoServiceFactory<U>,
U: ServiceFactory<Config = T::Config, InitError = T::InitError>,
F: FnMut(T::Response, &mut U::Service) -> Fut + Clone,
SF::Config: Clone,
I: IntoServiceFactory<SF1, In>,
SF1: ServiceFactory<In, Config = SF::Config, InitError = SF::InitError>,
F: FnMut(SF::Response, &mut SF1::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
Err: From<T::Error> + From<U::Error>,
Err: From<SF::Error> + From<SF1::Error>,
{
PipelineFactory {
factory: AndThenApplyFnFactory::new(self.factory, factory.into_factory(), f),
factory: AndThenApplyFnFactory::new(self.factory, factory.into_factory(), wrap_fn),
_phantom: PhantomData,
}
}
@ -249,96 +258,103 @@ impl<T: ServiceFactory> PipelineFactory<T> {
///
/// Note that this function consumes the receiving pipeline and returns a
/// wrapped version of it.
pub fn then<F, U>(
pub fn then<I, SF1>(
self,
factory: F,
factory: I,
) -> PipelineFactory<
impl ServiceFactory<
Request = T::Request,
Response = U::Response,
Error = T::Error,
Config = T::Config,
InitError = T::InitError,
Service = impl Service<
Request = T::Request,
Response = U::Response,
Error = T::Error,
> + Clone,
Req,
Response = SF1::Response,
Error = SF::Error,
Config = SF::Config,
InitError = SF::InitError,
Service = impl Service<Req, Response = SF1::Response, Error = SF::Error> + Clone,
> + Clone,
Req,
>
where
Self: Sized,
T::Config: Clone,
F: IntoServiceFactory<U>,
U: ServiceFactory<
Config = T::Config,
Request = Result<T::Response, T::Error>,
Error = T::Error,
InitError = T::InitError,
SF::Config: Clone,
I: IntoServiceFactory<SF1, Result<SF::Response, SF::Error>>,
SF1: ServiceFactory<
Result<SF::Response, SF::Error>,
Config = SF::Config,
Error = SF::Error,
InitError = SF::InitError,
>,
{
PipelineFactory {
factory: ThenServiceFactory::new(self.factory, factory.into_factory()),
_phantom: PhantomData,
}
}
/// Map this service's output to a different type, returning a new service
/// of the resulting type.
pub fn map<F, R>(self, f: F) -> PipelineFactory<MapServiceFactory<T, F, R>>
pub fn map<F, R>(self, f: F) -> PipelineFactory<MapServiceFactory<SF, F, Req, R>, Req>
where
Self: Sized,
F: FnMut(T::Response) -> R + Clone,
F: FnMut(SF::Response) -> R + Clone,
{
PipelineFactory {
factory: MapServiceFactory::new(self.factory, f),
_phantom: PhantomData,
}
}
/// Map this service's error to a different error, returning a new service.
pub fn map_err<F, E>(self, f: F) -> PipelineFactory<MapErrServiceFactory<T, F, E>>
pub fn map_err<F, E>(
self,
f: F,
) -> PipelineFactory<MapErrServiceFactory<SF, Req, F, E>, Req>
where
Self: Sized,
F: Fn(T::Error) -> E + Clone,
F: Fn(SF::Error) -> E + Clone,
{
PipelineFactory {
factory: MapErrServiceFactory::new(self.factory, f),
_phantom: PhantomData,
}
}
/// Map this factory's init error to a different error, returning a new service.
pub fn map_init_err<F, E>(self, f: F) -> PipelineFactory<MapInitErr<T, F, E>>
pub fn map_init_err<F, E>(self, f: F) -> PipelineFactory<MapInitErr<SF, F, Req, E>, Req>
where
Self: Sized,
F: Fn(T::InitError) -> E + Clone,
F: Fn(SF::InitError) -> E + Clone,
{
PipelineFactory {
factory: MapInitErr::new(self.factory, f),
_phantom: PhantomData,
}
}
}
impl<T> Clone for PipelineFactory<T>
impl<T, Req> Clone for PipelineFactory<T, Req>
where
T: Clone,
{
fn clone(&self) -> Self {
PipelineFactory {
factory: self.factory.clone(),
_phantom: PhantomData,
}
}
}
impl<T: ServiceFactory> ServiceFactory for PipelineFactory<T> {
type Config = T::Config;
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Service = T::Service;
type InitError = T::InitError;
type Future = T::Future;
impl<SF, Req> ServiceFactory<Req> for PipelineFactory<SF, Req>
where
SF: ServiceFactory<Req>,
{
type Config = SF::Config;
type Response = SF::Response;
type Error = SF::Error;
type Service = SF::Service;
type InitError = SF::InitError;
type Future = SF::Future;
#[inline]
fn new_service(&self, cfg: T::Config) -> Self::Future {
fn new_service(&self, cfg: SF::Config) -> Self::Future {
self.factory.new_service(cfg)
}
}

View File

@ -1,8 +1,8 @@
use std::cell::RefCell;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll};
use std::{cell::RefCell, marker::PhantomData};
use super::{Service, ServiceFactory};
@ -10,34 +10,33 @@ use super::{Service, ServiceFactory};
/// another service.
///
/// This is created by the `Pipeline::then` method.
pub(crate) struct ThenService<A, B>(Rc<RefCell<(A, B)>>);
pub(crate) struct ThenService<A, B, Req>(Rc<RefCell<(A, B)>>, PhantomData<Req>);
impl<A, B> ThenService<A, B> {
impl<A, B, Req> ThenService<A, B, Req> {
/// Create new `.then()` combinator
pub(crate) fn new(a: A, b: B) -> ThenService<A, B>
pub(crate) fn new(a: A, b: B) -> ThenService<A, B, Req>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
A: Service<Req>,
B: Service<Result<A::Response, A::Error>, Error = A::Error>,
{
Self(Rc::new(RefCell::new((a, b))))
Self(Rc::new(RefCell::new((a, b))), PhantomData)
}
}
impl<A, B> Clone for ThenService<A, B> {
impl<A, B, Req> Clone for ThenService<A, B, Req> {
fn clone(&self) -> Self {
ThenService(self.0.clone())
ThenService(self.0.clone(), PhantomData)
}
}
impl<A, B> Service for ThenService<A, B>
impl<A, B, Req> Service<Req> for ThenService<A, B, Req>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
A: Service<Req>,
B: Service<Result<A::Response, A::Error>, Error = A::Error>,
{
type Request = A::Request;
type Response = B::Response;
type Error = B::Error;
type Future = ThenServiceResponse<A, B>;
type Future = ThenServiceResponse<A, B, Req>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let mut srv = self.0.borrow_mut();
@ -49,7 +48,7 @@ where
}
}
fn call(&mut self, req: A::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
ThenServiceResponse {
state: State::A(self.0.borrow_mut().0.call(req), Some(self.0.clone())),
}
@ -57,30 +56,30 @@ where
}
#[pin_project::pin_project]
pub(crate) struct ThenServiceResponse<A, B>
pub(crate) struct ThenServiceResponse<A, B, Req>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
A: Service<Req>,
B: Service<Result<A::Response, A::Error>>,
{
#[pin]
state: State<A, B>,
state: State<A, B, Req>,
}
#[pin_project::pin_project(project = StateProj)]
enum State<A, B>
enum State<A, B, Req>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
A: Service<Req>,
B: Service<Result<A::Response, A::Error>>,
{
A(#[pin] A::Future, Option<Rc<RefCell<(A, B)>>>),
B(#[pin] B::Future),
Empty,
}
impl<A, B> Future for ThenServiceResponse<A, B>
impl<A, B, Req> Future for ThenServiceResponse<A, B, Req>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
A: Service<Req>,
B: Service<Result<A::Response, A::Error>>,
{
type Output = Result<B::Response, B::Error>;
@ -110,44 +109,43 @@ where
}
/// `.then()` service factory combinator
pub(crate) struct ThenServiceFactory<A, B>(Rc<(A, B)>);
pub(crate) struct ThenServiceFactory<A, B, Req>(Rc<(A, B)>, PhantomData<Req>);
impl<A, B> ThenServiceFactory<A, B>
impl<A, B, Req> ThenServiceFactory<A, B, Req>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
A::Config: Clone,
B: ServiceFactory<
Result<A::Response, A::Error>,
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{
/// Create new `AndThen` combinator
pub(crate) fn new(a: A, b: B) -> Self {
Self(Rc::new((a, b)))
Self(Rc::new((a, b)), PhantomData)
}
}
impl<A, B> ServiceFactory for ThenServiceFactory<A, B>
impl<A, B, Req> ServiceFactory<Req> for ThenServiceFactory<A, B, Req>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
A::Config: Clone,
B: ServiceFactory<
Result<A::Response, A::Error>,
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Config = A::Config;
type Service = ThenService<A::Service, B::Service>;
type Service = ThenService<A::Service, B::Service, Req>;
type InitError = A::InitError;
type Future = ThenServiceFactoryResponse<A, B>;
type Future = ThenServiceFactoryResponse<A, B, Req>;
fn new_service(&self, cfg: A::Config) -> Self::Future {
let srv = &*self.0;
@ -155,19 +153,19 @@ where
}
}
impl<A, B> Clone for ThenServiceFactory<A, B> {
impl<A, B, Req> Clone for ThenServiceFactory<A, B, Req> {
fn clone(&self) -> Self {
Self(self.0.clone())
Self(self.0.clone(), PhantomData)
}
}
#[pin_project::pin_project]
pub(crate) struct ThenServiceFactoryResponse<A, B>
pub(crate) struct ThenServiceFactoryResponse<A, B, Req>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
B: ServiceFactory<
Result<A::Response, A::Error>,
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
@ -180,12 +178,12 @@ where
b: Option<B::Service>,
}
impl<A, B> ThenServiceFactoryResponse<A, B>
impl<A, B, Req> ThenServiceFactoryResponse<A, B, Req>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
B: ServiceFactory<
Result<A::Response, A::Error>,
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
@ -200,17 +198,17 @@ where
}
}
impl<A, B> Future for ThenServiceFactoryResponse<A, B>
impl<A, B, Req> Future for ThenServiceFactoryResponse<A, B, Req>
where
A: ServiceFactory,
A: ServiceFactory<Req>,
B: ServiceFactory<
Result<A::Response, A::Error>,
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{
type Output = Result<ThenService<A::Service, B::Service>, A::InitError>;
type Output = Result<ThenService<A::Service, B::Service, Req>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
@ -249,8 +247,7 @@ mod tests {
#[derive(Clone)]
struct Srv1(Rc<Cell<usize>>);
impl Service for Srv1 {
type Request = Result<&'static str, &'static str>;
impl Service<Result<&'static str, &'static str>> for Srv1 {
type Response = &'static str;
type Error = ();
type Future = Ready<Result<Self::Response, Self::Error>>;
@ -270,8 +267,7 @@ mod tests {
struct Srv2(Rc<Cell<usize>>);
impl Service for Srv2 {
type Request = Result<&'static str, ()>;
impl Service<Result<&'static str, ()>> for Srv2 {
type Response = (&'static str, &'static str);
type Error = ();
type Future = Ready<Result<Self::Response, ()>>;

View File

@ -1,18 +1,18 @@
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::{future::Future, marker::PhantomData};
use crate::transform_err::TransformMapInitErr;
use crate::{IntoServiceFactory, Service, ServiceFactory};
/// Apply transform to a service.
pub fn apply<T, S, U>(t: T, factory: U) -> ApplyTransform<T, S>
pub fn apply<T, S, I, Req>(t: T, factory: I) -> ApplyTransform<T, S, Req>
where
S: ServiceFactory,
T: Transform<S::Service, InitError = S::InitError>,
U: IntoServiceFactory<S>,
I: IntoServiceFactory<S, Req>,
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
{
ApplyTransform::new(t, factory.into_factory())
}
@ -89,10 +89,7 @@ where
/// }
/// }
/// ```
pub trait Transform<S> {
/// Requests handled by the service.
type Request;
pub trait Transform<S, Req> {
/// Responses given by the service.
type Response;
@ -100,11 +97,7 @@ pub trait Transform<S> {
type Error;
/// The `TransformService` value created by this factory
type Transform: Service<
Request = Self::Request,
Response = Self::Response,
Error = Self::Error,
>;
type Transform: Service<Req, Response = Self::Response, Error = Self::Error>;
/// Errors produced while building a transform service.
type InitError;
@ -117,7 +110,7 @@ pub trait Transform<S> {
/// Map this transform's factory error to a different error,
/// returning a new transform service factory.
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, Req, F, E>
where
Self: Sized,
F: Fn(Self::InitError) -> E + Clone,
@ -126,11 +119,10 @@ pub trait Transform<S> {
}
}
impl<T, S> Transform<S> for Rc<T>
impl<T, S, Req> Transform<S, Req> for Rc<T>
where
T: Transform<S>,
T: Transform<S, Req>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
@ -142,11 +134,10 @@ where
}
}
impl<T, S> Transform<S> for Arc<T>
impl<T, S, Req> Transform<S, Req> for Arc<T>
where
T: Transform<S>,
T: Transform<S, Req>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
@ -159,38 +150,37 @@ where
}
/// `Apply` transform to new service
pub struct ApplyTransform<T, S>(Rc<(T, S)>);
pub struct ApplyTransform<T, S, Req>(Rc<(T, S)>, PhantomData<Req>);
impl<T, S> ApplyTransform<T, S>
impl<T, S, Req> ApplyTransform<T, S, Req>
where
S: ServiceFactory,
T: Transform<S::Service, InitError = S::InitError>,
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
{
/// Create new `ApplyTransform` new service instance
fn new(t: T, service: S) -> Self {
Self(Rc::new((t, service)))
Self(Rc::new((t, service)), PhantomData)
}
}
impl<T, S> Clone for ApplyTransform<T, S> {
impl<T, S, Req> Clone for ApplyTransform<T, S, Req> {
fn clone(&self) -> Self {
ApplyTransform(self.0.clone())
ApplyTransform(self.0.clone(), PhantomData)
}
}
impl<T, S> ServiceFactory for ApplyTransform<T, S>
impl<T, S, Req> ServiceFactory<Req> for ApplyTransform<T, S, Req>
where
S: ServiceFactory,
T: Transform<S::Service, InitError = S::InitError>,
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Config = S::Config;
type Service = T::Transform;
type InitError = T::InitError;
type Future = ApplyTransformFuture<T, S>;
type Future = ApplyTransformFuture<T, S, Req>;
fn new_service(&self, cfg: S::Config) -> Self::Future {
ApplyTransformFuture {
@ -201,30 +191,30 @@ where
}
#[pin_project::pin_project]
pub struct ApplyTransformFuture<T, S>
pub struct ApplyTransformFuture<T, S, Req>
where
S: ServiceFactory,
T: Transform<S::Service, InitError = S::InitError>,
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
{
store: Rc<(T, S)>,
#[pin]
state: ApplyTransformFutureState<T, S>,
state: ApplyTransformFutureState<T, S, Req>,
}
#[pin_project::pin_project(project = ApplyTransformFutureStateProj)]
pub enum ApplyTransformFutureState<T, S>
pub enum ApplyTransformFutureState<T, S, Req>
where
S: ServiceFactory,
T: Transform<S::Service, InitError = S::InitError>,
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
{
A(#[pin] S::Future),
B(#[pin] T::Future),
}
impl<T, S> Future for ApplyTransformFuture<T, S>
impl<T, S, Req> Future for ApplyTransformFuture<T, S, Req>
where
S: ServiceFactory,
T: Transform<S::Service, InitError = S::InitError>,
S: ServiceFactory<Req>,
T: Transform<S::Service, Req, InitError = S::InitError>,
{
type Output = Result<T::Transform, T::InitError>;

View File

@ -9,65 +9,64 @@ use super::Transform;
/// transform's init error.
///
/// This is created by the `Transform::map_init_err` method.
pub struct TransformMapInitErr<T, S, F, E> {
t: T,
f: F,
e: PhantomData<(S, E)>,
pub struct TransformMapInitErr<T, S, Req, F, E> {
transform: T,
mapper: F,
_phantom: PhantomData<(S, Req, E)>,
}
impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
impl<T, S, F, E, Req> TransformMapInitErr<T, S, Req, F, E> {
pub(crate) fn new(t: T, f: F) -> Self
where
T: Transform<S>,
T: Transform<S, Req>,
F: Fn(T::InitError) -> E,
{
Self {
t,
f,
e: PhantomData,
transform: t,
mapper: f,
_phantom: PhantomData,
}
}
}
impl<T, S, F, E> Clone for TransformMapInitErr<T, S, F, E>
impl<T, S, Req, F, E> Clone for TransformMapInitErr<T, S, Req, F, E>
where
T: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
t: self.t.clone(),
f: self.f.clone(),
e: PhantomData,
transform: self.transform.clone(),
mapper: self.mapper.clone(),
_phantom: PhantomData,
}
}
}
impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
impl<T, S, F, E, Req> Transform<S, Req> for TransformMapInitErr<T, S, Req, F, E>
where
T: Transform<S>,
T: Transform<S, Req>,
F: Fn(T::InitError) -> E + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = E;
type Future = TransformMapInitErrFuture<T, S, F, E>;
type Future = TransformMapInitErrFuture<T, S, F, E, Req>;
fn new_transform(&self, service: S) -> Self::Future {
TransformMapInitErrFuture {
fut: self.t.new_transform(service),
f: self.f.clone(),
fut: self.transform.new_transform(service),
f: self.mapper.clone(),
}
}
}
#[pin_project::pin_project]
pub struct TransformMapInitErrFuture<T, S, F, E>
pub struct TransformMapInitErrFuture<T, S, F, E, Req>
where
T: Transform<S>,
T: Transform<S, Req>,
F: Fn(T::InitError) -> E,
{
#[pin]
@ -75,9 +74,9 @@ where
f: F,
}
impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
impl<T, S, F, E, Req> Future for TransformMapInitErrFuture<T, S, F, E, Req>
where
T: Transform<S>,
T: Transform<S, Req>,
F: Fn(T::InitError) -> E + Clone,
{
type Output = Result<T::Transform, E>;

View File

@ -27,12 +27,11 @@ impl<S, F> TracingService<S, F> {
}
}
impl<S, F> Service for TracingService<S, F>
impl<S, Req, F> Service<Req> for TracingService<S, F>
where
S: Service,
F: Fn(&S::Request) -> Option<tracing::Span>,
S: Service<Req>,
F: Fn(&Req) -> Option<tracing::Span>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = Either<S::Future, Instrumented<S::Future>>;
@ -41,7 +40,7 @@ where
self.inner.poll_ready(ctx)
}
fn call(&mut self, req: Self::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
let span = (self.make_span)(&req);
let _enter = span.as_ref().map(|s| s.enter());
@ -74,18 +73,12 @@ impl<S, U, F> TracingTransform<S, U, F> {
}
}
impl<S, U, F> Transform<S> for TracingTransform<S, U, F>
impl<S, Req, U, F> Transform<S, Req> for TracingTransform<S, U, F>
where
S: Service,
U: ServiceFactory<
Request = S::Request,
Response = S::Response,
Error = S::Error,
Service = S,
>,
F: Fn(&S::Request) -> Option<tracing::Span> + Clone,
S: Service<Req>,
U: ServiceFactory<Req, Response = S::Response, Error = S::Error, Service = S>,
F: Fn(&Req) -> Option<tracing::Span> + Clone,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Transform = TracingService<S, F>;
@ -110,14 +103,14 @@ where
/// |req: &Request| Some(span!(Level::INFO, "request", req.id))
/// );
/// ```
pub fn trace<S, U, F>(
service_factory: U,
pub fn trace<S, Req, I, F>(
service_factory: I,
make_span: F,
) -> ApplyTransform<TracingTransform<S::Service, S, F>, S>
) -> ApplyTransform<TracingTransform<S::Service, S, F>, S, Req>
where
S: ServiceFactory,
F: Fn(&S::Request) -> Option<tracing::Span> + Clone,
U: IntoServiceFactory<S>,
I: IntoServiceFactory<S, Req>,
S: ServiceFactory<Req>,
F: Fn(&Req) -> Option<tracing::Span> + Clone,
{
apply(
TracingTransform::new(make_span),

View File

@ -1,6 +1,6 @@
[package]
name = "actix-utils"
version = "3.0.0"
version = "2.0.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Various network related services and utilities for the Actix ecosystem."
keywords = ["network", "framework", "async", "futures"]

View File

@ -1,4 +1,4 @@
//! Framed dispatcher service and related utilities
//! Framed dispatcher service and related utilities.
#![allow(type_alias_bounds)]
@ -11,6 +11,7 @@ use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
use actix_service::{IntoService, Service};
use futures_core::stream::Stream;
use log::debug;
use pin_project_lite::pin_project;
use crate::mpsc;
@ -62,12 +63,12 @@ pub enum Message<T> {
Close,
}
pin_project_lite::pin_project! {
pin_project! {
/// Dispatcher is a future that reads frames from Framed object
/// and passes them to the service.
pub struct Dispatcher<S, T, U, I>
where
S: Service<Request = <U as Decoder>::Item, Response = I>,
S: Service<<U as Decoder>::Item, Response = I>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead,
@ -86,7 +87,11 @@ pin_project_lite::pin_project! {
}
}
enum State<S: Service, U: Encoder<I> + Decoder, I> {
enum State<S, U, I>
where
S: Service<<U as Decoder>::Item>,
U: Encoder<I> + Decoder,
{
Processing,
Error(DispatcherError<S::Error, U, I>),
FramedError(DispatcherError<S::Error, U, I>),
@ -94,7 +99,11 @@ enum State<S: Service, U: Encoder<I> + Decoder, I> {
Stopping,
}
impl<S: Service, U: Encoder<I> + Decoder, I> State<S, U, I> {
impl<S, U, I> State<S, U, I>
where
S: Service<<U as Decoder>::Item>,
U: Encoder<I> + Decoder,
{
fn take_error(&mut self) -> DispatcherError<S::Error, U, I> {
match mem::replace(self, State::Processing) {
State::Error(err) => err,
@ -112,7 +121,7 @@ impl<S: Service, U: Encoder<I> + Decoder, I> State<S, U, I> {
impl<S, T, U, I> Dispatcher<S, T, U, I>
where
S: Service<Request = <U as Decoder>::Item, Response = I>,
S: Service<<U as Decoder>::Item, Response = I>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -121,7 +130,10 @@ where
<U as Decoder>::Error: fmt::Debug,
<U as Encoder<I>>::Error: fmt::Debug,
{
pub fn new<F: IntoService<S>>(framed: Framed<T, U>, service: F) -> Self {
pub fn new<F>(framed: Framed<T, U>, service: F) -> Self
where
F: IntoService<S, <U as Decoder>::Item>,
{
let (tx, rx) = mpsc::channel();
Dispatcher {
framed,
@ -133,11 +145,14 @@ where
}
/// Construct new `Dispatcher` instance with customer `mpsc::Receiver`
pub fn with_rx<F: IntoService<S>>(
pub fn with_rx<F>(
framed: Framed<T, U>,
service: F,
rx: mpsc::Receiver<Result<Message<I>, S::Error>>,
) -> Self {
) -> Self
where
F: IntoService<S, <U as Decoder>::Item>,
{
let tx = rx.sender();
Dispatcher {
framed,
@ -176,7 +191,7 @@ where
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool
where
S: Service<Request = <U as Decoder>::Item, Response = I>,
S: Service<<U as Decoder>::Item, Response = I>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -220,7 +235,7 @@ where
/// write to framed object
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool
where
S: Service<Request = <U as Decoder>::Item, Response = I>,
S: Service<<U as Decoder>::Item, Response = I>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -271,7 +286,7 @@ where
impl<S, T, U, I> Future for Dispatcher<S, T, U, I>
where
S: Service<Request = <U as Decoder>::Item, Response = I>,
S: Service<<U as Decoder>::Item, Response = I>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,

View File

@ -2,6 +2,7 @@
//!
//! If the response does not complete within the specified timeout, the response
//! will be aborted.
use core::future::Future;
use core::marker::PhantomData;
use core::pin::Pin;
@ -10,6 +11,7 @@ use core::{fmt, time};
use actix_rt::time::{delay_for, Delay};
use actix_service::{IntoService, Service, Transform};
use pin_project_lite::pin_project;
/// Applies a timeout to requests.
#[derive(Debug)]
@ -77,21 +79,21 @@ impl<E> Clone for Timeout<E> {
}
}
impl<S, E> Transform<S> for Timeout<E>
impl<S, E, Req> Transform<S, Req> for Timeout<E>
where
S: Service,
S: Service<Req>,
{
type Request = S::Request;
type Response = S::Response;
type Error = TimeoutError<S::Error>;
type Transform = TimeoutService<S>;
type InitError = E;
type Transform = TimeoutService<S, Req>;
type Future = TimeoutFuture<Self::Transform, Self::InitError>;
fn new_transform(&self, service: S) -> Self::Future {
let service = TimeoutService {
service,
timeout: self.timeout,
_phantom: PhantomData,
};
TimeoutFuture {
@ -118,40 +120,41 @@ impl<T, E> Future for TimeoutFuture<T, E> {
/// Applies a timeout to requests.
#[derive(Debug, Clone)]
pub struct TimeoutService<S> {
pub struct TimeoutService<S, Req> {
service: S,
timeout: time::Duration,
_phantom: PhantomData<Req>,
}
impl<S> TimeoutService<S>
impl<S, Req> TimeoutService<S, Req>
where
S: Service,
S: Service<Req>,
{
pub fn new<U>(timeout: time::Duration, service: U) -> Self
where
U: IntoService<S>,
U: IntoService<S, Req>,
{
TimeoutService {
timeout,
service: service.into_service(),
_phantom: PhantomData,
}
}
}
impl<S> Service for TimeoutService<S>
impl<S, Req> Service<Req> for TimeoutService<S, Req>
where
S: Service,
S: Service<Req>,
{
type Request = S::Request;
type Response = S::Response;
type Error = TimeoutError<S::Error>;
type Future = TimeoutServiceResponse<S>;
type Future = TimeoutServiceResponse<S, Req>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx).map_err(TimeoutError::Service)
}
fn call(&mut self, request: S::Request) -> Self::Future {
fn call(&mut self, request: Req) -> Self::Future {
TimeoutServiceResponse {
fut: self.service.call(request),
sleep: delay_for(self.timeout),
@ -159,21 +162,24 @@ where
}
}
pin_project_lite::pin_project! {
pin_project! {
/// `TimeoutService` response future
#[derive(Debug)]
pub struct TimeoutServiceResponse<T: Service> {
pub struct TimeoutServiceResponse<S, Req>
where
S: Service<Req>
{
#[pin]
fut: T::Future,
fut: S::Future,
sleep: Delay,
}
}
impl<T> Future for TimeoutServiceResponse<T>
impl<S, Req> Future for TimeoutServiceResponse<S, Req>
where
T: Service,
S: Service<Req>,
{
type Output = Result<T::Response, TimeoutError<T::Error>>;
type Output = Result<S::Response, TimeoutError<S::Error>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
@ -204,8 +210,7 @@ mod tests {
struct SleepService(Duration);
impl Service for SleepService {
type Request = ();
impl Service<()> for SleepService {
type Response = ();
type Error = ();
type Future = LocalBoxFuture<'static, Result<(), ()>>;