1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-21 23:56:35 +00:00

Revert "use From/Into instead of custom IntoService and IntoNewService traits"

This reverts commit 9441624827.
This commit is contained in:
Nikolay Kim 2018-09-04 09:49:21 -07:00
parent dfa08b3bf1
commit 0390ff37d3
7 changed files with 72 additions and 25 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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