1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-27 00:27:43 +02:00

Use associated type instead of generic for Service definition

This commit is contained in:
Nikolay Kim
2019-02-01 19:53:13 -08:00
parent 8cdbf49187
commit d83bf95304
35 changed files with 544 additions and 421 deletions

View File

@ -13,9 +13,9 @@ pub struct CloneableService<T: 'static> {
}
impl<T: 'static> CloneableService<T> {
pub fn new<Request>(service: T) -> Self
pub fn new(service: T) -> Self
where
T: Service<Request>,
T: Service,
{
Self {
service: Cell::new(service),
@ -33,10 +33,11 @@ impl<T: 'static> Clone for CloneableService<T> {
}
}
impl<T: 'static, Request> Service<Request> for CloneableService<T>
impl<T> Service for CloneableService<T>
where
T: Service<Request>,
T: Service + 'static,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Future = T::Future;
@ -45,7 +46,7 @@ where
self.service.get_mut().poll_ready()
}
fn call(&mut self, req: Request) -> Self::Future {
fn call(&mut self, req: T::Request) -> Self::Future {
self.service.get_mut().call(req)
}
}

View File

@ -21,11 +21,12 @@ impl<A: Clone, B: Clone> Clone for EitherService<A, B> {
}
}
impl<A, B, Request> Service<Request> for EitherService<A, B>
impl<A, B> Service for EitherService<A, B>
where
A: Service<Request>,
B: Service<Request, Response = A::Response, Error = A::Error>,
A: Service,
B: Service<Request = A::Request, Response = A::Response, Error = A::Error>,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type Future = future::Either<A::Future, B::Future>;
@ -37,7 +38,7 @@ where
}
}
fn call(&mut self, req: Request) -> Self::Future {
fn call(&mut self, req: A::Request) -> Self::Future {
match self {
EitherService::A(ref mut inner) => future::Either::A(inner.call(req)),
EitherService::B(ref mut inner) => future::Either::B(inner.call(req)),
@ -52,11 +53,11 @@ pub enum Either<A, B> {
}
impl<A, B> Either<A, B> {
pub fn new_a<Request>(srv: A) -> Self
pub fn new_a(srv: A) -> Self
where
A: NewService<Request>,
A: NewService,
B: NewService<
Request,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
@ -65,11 +66,11 @@ impl<A, B> Either<A, B> {
Either::A(srv)
}
pub fn new_b<Request>(srv: B) -> Self
pub fn new_b(srv: B) -> Self
where
A: NewService<Request>,
A: NewService,
B: NewService<
Request,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
@ -79,16 +80,22 @@ impl<A, B> Either<A, B> {
}
}
impl<A, B, Request> NewService<Request> for Either<A, B>
impl<A, B> NewService for Either<A, B>
where
A: NewService<Request>,
B: NewService<Request, Response = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService,
B: NewService<
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type InitError = A::InitError;
type Service = EitherService<A::Service, B::Service>;
type Future = EitherNewService<A, B, Request>;
type Future = EitherNewService<A, B>;
fn new_service(&self) -> Self::Future {
match self {
@ -108,15 +115,20 @@ impl<A: Clone, B: Clone> Clone for Either<A, B> {
}
#[doc(hidden)]
pub enum EitherNewService<A: NewService<R>, B: NewService<R>, R> {
pub enum EitherNewService<A: NewService, B: NewService> {
A(A::Future),
B(B::Future),
}
impl<A, B, Request> Future for EitherNewService<A, B, Request>
impl<A, B> Future for EitherNewService<A, B>
where
A: NewService<Request>,
B: NewService<Request, Response = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService,
B: NewService<
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{
type Item = EitherService<A::Service, B::Service>;
type Error = A::InitError;

View File

@ -22,15 +22,15 @@ pub struct FramedNewService<S, T, U> {
impl<S, T, U> FramedNewService<S, T, U>
where
S: NewService<Request<U>, Response = Response<U>>,
S: NewService<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
pub fn new<F1: IntoNewService<S, Request<U>>>(factory: F1) -> Self {
pub fn new<F1: IntoNewService<S>>(factory: F1) -> Self {
Self {
factory: factory.into_new_service(),
_t: PhantomData,
@ -50,16 +50,17 @@ where
}
}
impl<S, T, U> NewService<Framed<T, U>> for FramedNewService<S, T, U>
impl<S, T, U> NewService for FramedNewService<S, T, U>
where
S: NewService<Request<U>, Response = Response<U>> + Clone,
S: NewService<Request = Request<U>, Response = Response<U>> + Clone,
S::Error: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
type Request = Framed<T, U>;
type Response = FramedTransport<S::Service, T, U>;
type Error = S::InitError;
type InitError = S::InitError;
@ -91,16 +92,17 @@ where
}
}
impl<S, T, U> Service<Framed<T, U>> for FramedService<S, T, U>
impl<S, T, U> Service for FramedService<S, T, U>
where
S: NewService<Request<U>, Response = Response<U>>,
S: NewService<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
type Request = Framed<T, U>;
type Response = FramedTransport<S::Service, T, U>;
type Error = S::InitError;
type Future = FramedServiceResponseFuture<S, T, U>;
@ -121,9 +123,9 @@ where
#[doc(hidden)]
pub struct FramedServiceResponseFuture<S, T, U>
where
S: NewService<Request<U>, Response = Response<U>>,
S: NewService<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
@ -135,9 +137,9 @@ where
impl<S, T, U> Future for FramedServiceResponseFuture<S, T, U>
where
S: NewService<Request<U>, Response = Response<U>>,
S: NewService<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
@ -174,7 +176,7 @@ impl<E, U: Encoder + Decoder> From<E> for FramedTransportError<E, U> {
/// and pass then to the service.
pub struct FramedTransport<S, T, U>
where
S: Service<Request<U>, Response = Response<U>>,
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -188,7 +190,7 @@ where
inner: Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>,
}
enum TransportState<S: Service<Request<U>>, U: Encoder + Decoder> {
enum TransportState<S: Service, U: Encoder + Decoder> {
Processing,
Error(FramedTransportError<S::Error, U>),
FramedError(FramedTransportError<S::Error, U>),
@ -202,7 +204,7 @@ struct FramedTransportInner<I, E> {
impl<S, T, U> FramedTransport<S, T, U>
where
S: Service<Request<U>, Response = Response<U>>,
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -294,7 +296,7 @@ where
impl<S, T, U> FramedTransport<S, T, U>
where
S: Service<Request<U>, Response = Response<U>>,
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -302,7 +304,7 @@ where
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
pub fn new<F: IntoService<S, Request<U>>>(framed: Framed<T, U>, service: F) -> Self {
pub fn new<F: IntoService<S>>(framed: Framed<T, U>, service: F) -> Self {
FramedTransport {
framed,
service: service.into_service(),
@ -340,7 +342,7 @@ where
impl<S, T, U> Future for FramedTransport<S, T, U>
where
S: Service<Request<U>, Response = Response<U>>,
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -400,12 +402,13 @@ where
}
}
impl<T, U, F> NewService<T> for IntoFramed<T, U, F>
impl<T, U, F> NewService for IntoFramed<T, U, F>
where
T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static,
U: Encoder + Decoder,
{
type Request = T;
type Response = Framed<T, U>;
type Error = ();
type InitError = ();
@ -430,12 +433,13 @@ where
_t: PhantomData<(T,)>,
}
impl<T, U, F> Service<T> for IntoFramedService<T, U, F>
impl<T, U, F> Service for IntoFramedService<T, U, F>
where
T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static,
U: Encoder + Decoder,
{
type Request = T;
type Response = Framed<T, U>;
type Error = ();
type Future = FutureResult<Self::Response, Self::Error>;

View File

@ -13,10 +13,10 @@ pub struct InFlight<T> {
}
impl<T> InFlight<T> {
pub fn new<F, Request>(factory: F) -> Self
pub fn new<F>(factory: F) -> Self
where
T: NewService<Request>,
F: IntoNewService<T, Request>,
T: NewService,
F: IntoNewService<T>,
{
Self {
factory: factory.into_new_service(),
@ -33,15 +33,16 @@ impl<T> InFlight<T> {
}
}
impl<T, Request> NewService<Request> for InFlight<T>
impl<T> NewService for InFlight<T>
where
T: NewService<Request>,
T: NewService,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
type Service = InFlightService<T::Service>;
type Future = InFlightResponseFuture<T, Request>;
type Future = InFlightResponseFuture<T>;
fn new_service(&self) -> Self::Future {
InFlightResponseFuture {
@ -51,12 +52,12 @@ where
}
}
pub struct InFlightResponseFuture<T: NewService<Request>, Request> {
pub struct InFlightResponseFuture<T: NewService> {
fut: T::Future,
max_inflight: usize,
}
impl<T: NewService<Request>, Request> Future for InFlightResponseFuture<T, Request> {
impl<T: NewService> Future for InFlightResponseFuture<T> {
type Item = InFlightService<T::Service>;
type Error = T::InitError;
@ -74,10 +75,10 @@ pub struct InFlightService<T> {
}
impl<T> InFlightService<T> {
pub fn new<F, Request>(service: F) -> Self
pub fn new<F>(service: F) -> Self
where
T: Service<Request>,
F: IntoService<T, Request>,
T: Service,
F: IntoService<T>,
{
Self {
service: service.into_service(),
@ -85,10 +86,10 @@ impl<T> InFlightService<T> {
}
}
pub fn with_max_inflight<F, Request>(max: usize, service: F) -> Self
pub fn with_max_inflight<F>(max: usize, service: F) -> Self
where
T: Service<Request>,
F: IntoService<T, Request>,
T: Service,
F: IntoService<T>,
{
Self {
service: service.into_service(),
@ -97,13 +98,14 @@ impl<T> InFlightService<T> {
}
}
impl<T, Request> Service<Request> for InFlightService<T>
impl<T> Service for InFlightService<T>
where
T: Service<Request>,
T: Service,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Future = InFlightServiceResponse<T, Request>;
type Future = InFlightServiceResponse<T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
let res = self.service.poll_ready()?;
@ -114,7 +116,7 @@ where
Ok(res)
}
fn call(&mut self, req: Request) -> Self::Future {
fn call(&mut self, req: T::Request) -> Self::Future {
InFlightServiceResponse {
fut: self.service.call(req),
_guard: self.count.get(),
@ -123,12 +125,12 @@ where
}
#[doc(hidden)]
pub struct InFlightServiceResponse<T: Service<Request>, Request> {
pub struct InFlightServiceResponse<T: Service> {
fut: T::Future,
_guard: CounterGuard,
}
impl<T: Service<Request>, Request> Future for InFlightServiceResponse<T, Request> {
impl<T: Service> Future for InFlightServiceResponse<T> {
type Item = T::Response;
type Error = T::Error;

View File

@ -44,10 +44,11 @@ where
}
}
impl<R, E, F> NewService<R> for KeepAlive<R, E, F>
impl<R, E, F> NewService for KeepAlive<R, E, F>
where
F: Fn() -> E + Clone,
{
type Request = R;
type Response = R;
type Error = E;
type InitError = Never;
@ -89,10 +90,11 @@ where
}
}
impl<R, E, F> Service<R> for KeepAliveService<R, E, F>
impl<R, E, F> Service for KeepAliveService<R, E, F>
where
F: Fn() -> E,
{
type Request = R;
type Response = R;
type Error = E;
type Future = FutureResult<R, E>;

View File

@ -37,12 +37,12 @@ pub struct StreamNewService<S, T, E> {
impl<S, T, E> StreamNewService<S, T, E>
where
S: IntoStream,
T: NewService<Request<S>, Response = (), Error = E, InitError = E>,
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>,
T::Future: 'static,
T::Service: 'static,
<T::Service as Service<Request<S>>>::Future: 'static,
<T::Service as Service>::Future: 'static,
{
pub fn new<F: IntoNewService<T, Request<S>>>(factory: F) -> Self {
pub fn new<F: IntoNewService<T>>(factory: F) -> Self {
Self {
factory: Rc::new(factory.into_new_service()),
_t: PhantomData,
@ -59,14 +59,15 @@ impl<S, T, E> Clone for StreamNewService<S, T, E> {
}
}
impl<S, T, E> NewService<S> for StreamNewService<S, T, E>
impl<S, T, E> NewService for StreamNewService<S, T, E>
where
S: IntoStream + 'static,
T: NewService<Request<S>, Response = (), Error = E, InitError = E>,
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>,
T::Future: 'static,
T::Service: 'static,
<T::Service as Service<Request<S>>>::Future: 'static,
<T::Service as Service>::Future: 'static,
{
type Request = S;
type Response = ();
type Error = E;
type InitError = E;
@ -86,14 +87,15 @@ pub struct StreamService<S, T, E> {
_t: PhantomData<(S, E)>,
}
impl<S, T, E> Service<S> for StreamService<S, T, E>
impl<S, T, E> Service for StreamService<S, T, E>
where
S: IntoStream + 'static,
T: NewService<Request<S>, Response = (), Error = E, InitError = E>,
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>,
T::Future: 'static,
T::Service: 'static,
<T::Service as Service<Request<S>>>::Future: 'static,
<T::Service as Service>::Future: 'static,
{
type Request = S;
type Response = ();
type Error = E;
type Future = Box<Future<Item = (), Error = E>>;
@ -114,7 +116,7 @@ where
pub struct StreamDispatcher<S, T>
where
S: IntoStream + 'static,
T: Service<Request<S>, Response = ()> + 'static,
T: Service<Request = Request<S>, Response = ()> + 'static,
T::Future: 'static,
{
stream: S,
@ -126,13 +128,13 @@ where
impl<S, T> StreamDispatcher<S, T>
where
S: Stream,
T: Service<Request<S>, Response = ()>,
T: Service<Request = Request<S>, Response = ()>,
T::Future: 'static,
{
pub fn new<F1, F2>(stream: F1, service: F2) -> Self
where
F1: IntoStream<Stream = S, Item = S::Item, Error = S::Error>,
F2: IntoService<T, Request<S>>,
F2: IntoService<T>,
{
let (err_tx, err_rx) = mpsc::unbounded();
StreamDispatcher {
@ -147,7 +149,7 @@ where
impl<S, T> Future for StreamDispatcher<S, T>
where
S: Stream,
T: Service<Request<S>, Response = ()>,
T: Service<Request = Request<S>, Response = ()>,
T::Future: 'static,
{
type Item = ();
@ -225,7 +227,8 @@ impl<T> Clone for TakeItem<T> {
}
}
impl<T: Stream> NewService<T> for TakeItem<T> {
impl<T: Stream> NewService for TakeItem<T> {
type Request = T;
type Response = (Option<T::Item>, T);
type Error = T::Error;
type InitError = ();
@ -248,7 +251,8 @@ impl<T> Clone for TakeItemService<T> {
}
}
impl<T: Stream> Service<T> for TakeItemService<T> {
impl<T: Stream> Service for TakeItemService<T> {
type Request = T;
type Response = (Option<T::Item>, T);
type Error = T::Error;
type Future = TakeItemServiceResponse<T>;

View File

@ -42,7 +42,8 @@ impl Default for LowResTime {
}
}
impl NewService<()> for LowResTime {
impl NewService for LowResTime {
type Request = ();
type Response = Instant;
type Error = Never;
type InitError = Never;
@ -88,7 +89,8 @@ impl LowResTimeService {
}
}
impl Service<()> for LowResTimeService {
impl Service for LowResTimeService {
type Request = ();
type Response = Instant;
type Error = Never;
type Future = FutureResult<Self::Response, Self::Error>;

View File

@ -35,9 +35,9 @@ impl<E: fmt::Debug> fmt::Debug for TimeoutError<E> {
}
impl<T> Timeout<T> {
pub fn new<Request>(timeout: Duration, inner: T) -> Self
pub fn new(timeout: Duration, inner: T) -> Self
where
T: NewService<Request> + Clone,
T: NewService + Clone,
{
Timeout { inner, timeout }
}
@ -55,15 +55,16 @@ where
}
}
impl<T, Request> NewService<Request> for Timeout<T>
impl<T> NewService for Timeout<T>
where
T: NewService<Request> + Clone,
T: NewService + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = TimeoutError<T::Error>;
type InitError = T::InitError;
type Service = TimeoutService<T::Service>;
type Future = TimeoutFut<T, Request>;
type Future = TimeoutFut<T>;
fn new_service(&self) -> Self::Future {
TimeoutFut {
@ -75,14 +76,14 @@ where
/// `Timeout` response future
#[derive(Debug)]
pub struct TimeoutFut<T: NewService<Request>, Request> {
pub struct TimeoutFut<T: NewService> {
fut: T::Future,
timeout: Duration,
}
impl<T, Request> Future for TimeoutFut<T, Request>
impl<T> Future for TimeoutFut<T>
where
T: NewService<Request>,
T: NewService,
{
type Item = TimeoutService<T::Service>;
type Error = T::InitError;
@ -101,9 +102,9 @@ pub struct TimeoutService<T> {
}
impl<T> TimeoutService<T> {
pub fn new<Request>(timeout: Duration, inner: T) -> Self
pub fn new(timeout: Duration, inner: T) -> Self
where
T: Service<Request>,
T: Service,
{
TimeoutService { inner, timeout }
}
@ -118,19 +119,20 @@ impl<T: Clone> Clone for TimeoutService<T> {
}
}
impl<T, Request> Service<Request> for TimeoutService<T>
impl<T> Service for TimeoutService<T>
where
T: Service<Request>,
T: Service,
{
type Request = T::Request;
type Response = T::Response;
type Error = TimeoutError<T::Error>;
type Future = TimeoutServiceResponse<T, Request>;
type Future = TimeoutServiceResponse<T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.inner.poll_ready().map_err(TimeoutError::Service)
}
fn call(&mut self, request: Request) -> Self::Future {
fn call(&mut self, request: T::Request) -> Self::Future {
TimeoutServiceResponse {
fut: self.inner.call(request),
sleep: Delay::new(clock::now() + self.timeout),
@ -140,14 +142,14 @@ where
/// `TimeoutService` response future
#[derive(Debug)]
pub struct TimeoutServiceResponse<T: Service<Request>, Request> {
pub struct TimeoutServiceResponse<T: Service> {
fut: T::Future,
sleep: Delay,
}
impl<T, Request> Future for TimeoutServiceResponse<T, Request>
impl<T> Future for TimeoutServiceResponse<T>
where
T: Service<Request>,
T: Service,
{
type Item = T::Response;
type Error = TimeoutError<T::Error>;