1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-22 16:16:40 +00:00

use From/Into instead of custom IntoService and IntoNewService traits

This commit is contained in:
Nikolay Kim 2018-09-04 09:30:52 -07:00
parent 42a49da199
commit 9441624827
7 changed files with 25 additions and 72 deletions

View file

@ -63,7 +63,7 @@ mod worker;
pub use configurable::{IntoNewConfigurableService, NewConfigurableService}; pub use configurable::{IntoNewConfigurableService, NewConfigurableService};
pub use server::Server; pub use server::Server;
pub use service::{IntoNewService, IntoService, NewServiceExt, ServiceExt}; pub use service::{NewServiceExt, ServiceExt};
/// Pause accepting incoming connections /// Pause accepting incoming connections
/// ///

View file

@ -4,8 +4,6 @@ use std::rc::Rc;
use futures::{Async, Future, Poll}; use futures::{Async, Future, Poll};
use tower_service::{NewService, Service}; use tower_service::{NewService, Service};
use super::IntoNewService;
/// `AndThen` service combinator /// `AndThen` service combinator
pub struct AndThen<A, B> { pub struct AndThen<A, B> {
a: A, a: A,
@ -114,11 +112,8 @@ where
B: NewService, B: NewService,
{ {
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new<F: IntoNewService<B>>(a: A, f: F) -> Self { pub fn new<F: Into<B>>(a: A, f: F) -> Self {
Self { Self { a, b: f.into() }
a,
b: f.into_new_service(),
}
} }
} }

View file

@ -1,7 +1,8 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use futures::{Async, Future, IntoFuture, Poll}; use futures::{Async, Future, IntoFuture, Poll};
use {IntoNewService, NewService, Service};
use {NewService, Service};
/// `Apply` service combinator /// `Apply` service combinator
pub struct Apply<T, F, R, Req> { pub struct Apply<T, F, R, Req> {
@ -62,10 +63,10 @@ where
R: IntoFuture, R: IntoFuture,
{ {
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new<F1: IntoNewService<T>>(f: F, service: F1) -> Self { pub fn new<F1: Into<T>>(f: F, service: F1) -> Self {
Self { Self {
f, f,
service: service.into_new_service(), service: service.into(),
r: PhantomData, r: PhantomData,
} }
} }

View file

@ -6,8 +6,6 @@ use futures::{
}; };
use tower_service::{NewService, Service}; use tower_service::{NewService, Service};
use super::IntoNewService;
pub struct FnService<F, Req, Resp, E, Fut> pub struct FnService<F, Req, Resp, E, Fut>
where where
F: Fn(Req) -> Fut, F: Fn(Req) -> Fut,
@ -113,14 +111,13 @@ where
} }
} }
impl<F, Req, Resp, Err, IErr, Fut> IntoNewService<FnNewService<F, Req, Resp, Err, IErr, Fut>> impl<F, Req, Resp, Err, IErr, Fut> From<F> for FnNewService<F, Req, Resp, Err, IErr, Fut>
for F
where where
F: Fn(Req) -> Fut + Clone + 'static, F: Fn(Req) -> Fut + Clone + 'static,
Fut: IntoFuture<Item = Resp, Error = Err>, Fut: IntoFuture<Item = Resp, Error = Err>,
{ {
fn into_new_service(self) -> FnNewService<F, Req, Resp, Err, IErr, Fut> { fn from(f: F) -> FnNewService<F, Req, Resp, Err, IErr, Fut> {
FnNewService::new(self) FnNewService::new(f)
} }
} }

View file

@ -3,8 +3,6 @@ use std::marker;
use futures::{Async, Future, IntoFuture, Poll}; use futures::{Async, Future, IntoFuture, Poll};
use tower_service::{NewService, Service}; use tower_service::{NewService, Service};
use super::IntoNewService;
pub struct FnStateService<S, F, Req, Resp, Err, Fut> pub struct FnStateService<S, F, Req, Resp, Err, Fut>
where where
F: Fn(&mut S, Req) -> Fut, F: Fn(&mut S, Req) -> Fut,
@ -113,8 +111,8 @@ where
} }
} }
impl<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2> impl<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2> From<(F1, F2)>
IntoNewService<FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2>> for (F1, F2) for FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2>
where where
S: 'static, S: 'static,
F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static, F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static,
@ -126,10 +124,8 @@ where
Err1: 'static, Err1: 'static,
Err2: 'static, Err2: 'static,
{ {
fn into_new_service( fn from(data: (F1, F2)) -> FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2> {
self, FnStateNewService::new(data.0, data.1)
) -> FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2> {
FnStateNewService::new(self.0, self.1)
} }
} }

View file

@ -33,10 +33,10 @@ pub trait ServiceExt: Service {
fn and_then<F, B>(self, service: F) -> AndThen<Self, B> fn and_then<F, B>(self, service: F) -> AndThen<Self, B>
where where
Self: Sized, Self: Sized,
F: IntoService<B>, F: Into<B>,
B: Service<Request = Self::Response, Error = Self::Error>, B: Service<Request = Self::Response, Error = Self::Error>,
{ {
AndThen::new(self, service.into_service()) AndThen::new(self, service.into())
} }
fn map<F, R>(self, f: F) -> Map<Self, F, R> fn map<F, R>(self, f: F) -> Map<Self, F, R>
@ -70,7 +70,7 @@ pub trait NewServiceExt: NewService {
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B> fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
where where
Self: Sized, Self: Sized,
F: IntoNewService<B>, F: Into<B>,
B: NewService< B: NewService<
Request = Self::Response, Request = Self::Response,
Error = Self::Error, Error = Self::Error,
@ -113,51 +113,15 @@ pub trait NewServiceExt: NewService {
} }
} }
impl<T: Service> ServiceExt for T {} impl<T: ?Sized> ServiceExt for T where T: Service {}
impl<T: NewService> NewServiceExt for T {} impl<T: ?Sized> NewServiceExt for T where T: NewService {}
/// Trait for types that can be converted to a Service impl<F, Req, Resp, Err, Fut> From<F> for FnService<F, Req, Resp, Err, Fut>
pub trait IntoService<T>
where
T: Service,
{
/// Create service
fn into_service(self) -> T;
}
/// Trait for types that can be converted to a Service
pub trait IntoNewService<T>
where
T: NewService,
{
/// Create service
fn into_new_service(self) -> T;
}
impl<T> IntoService<T> for T
where
T: Service,
{
fn into_service(self) -> T {
self
}
}
impl<T> IntoNewService<T> for T
where
T: NewService,
{
fn into_new_service(self) -> T {
self
}
}
impl<F, Req, Resp, Err, Fut> IntoService<FnService<F, Req, Resp, Err, Fut>> for F
where where
F: Fn(Req) -> Fut + 'static, F: Fn(Req) -> Fut + 'static,
Fut: IntoFuture<Item = Resp, Error = Err>, Fut: IntoFuture<Item = Resp, Error = Err>,
{ {
fn into_service(self) -> FnService<F, Req, Resp, Err, Fut> { fn from(f: F) -> FnService<F, Req, Resp, Err, Fut> {
FnService::new(self) FnService::new(f)
} }
} }

View file

@ -2,7 +2,7 @@ use futures::unsync::mpsc;
use futures::{Async, Future, Poll, Stream}; use futures::{Async, Future, Poll, Stream};
use tokio::executor::current_thread::spawn; use tokio::executor::current_thread::spawn;
use super::{IntoService, Service}; use super::Service;
pub struct StreamDispatcher<S: Stream, T> { pub struct StreamDispatcher<S: Stream, T> {
stream: S, stream: S,
@ -18,12 +18,12 @@ where
T: Service<Request = Result<S::Item, S::Error>, Response = (), Error = ()>, T: Service<Request = Result<S::Item, S::Error>, Response = (), Error = ()>,
T::Future: 'static, T::Future: 'static,
{ {
pub fn new<F: IntoService<T>>(stream: S, service: F) -> Self { pub fn new<F: Into<T>>(stream: S, service: F) -> Self {
let (stop_tx, stop_rx) = mpsc::unbounded(); let (stop_tx, stop_rx) = mpsc::unbounded();
StreamDispatcher { StreamDispatcher {
stream, stream,
item: None, item: None,
service: service.into_service(), service: service.into(),
stop_rx, stop_rx,
stop_tx, stop_tx,
} }