1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-22 08:07:18 +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 server::Server;
pub use service::{IntoNewService, IntoService, NewServiceExt, ServiceExt};
pub use service::{NewServiceExt, ServiceExt};
/// Pause accepting incoming connections
///

View file

@ -4,8 +4,6 @@ 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,
@ -114,11 +112,8 @@ where
B: NewService,
{
/// Create new `AndThen` combinator
pub fn new<F: IntoNewService<B>>(a: A, f: F) -> Self {
Self {
a,
b: f.into_new_service(),
}
pub fn new<F: Into<B>>(a: A, f: F) -> Self {
Self { a, b: f.into() }
}
}

View file

@ -1,7 +1,8 @@
use std::marker::PhantomData;
use futures::{Async, Future, IntoFuture, Poll};
use {IntoNewService, NewService, Service};
use {NewService, Service};
/// `Apply` service combinator
pub struct Apply<T, F, R, Req> {
@ -62,10 +63,10 @@ where
R: IntoFuture,
{
/// 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 {
f,
service: service.into_new_service(),
service: service.into(),
r: PhantomData,
}
}

View file

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

View file

@ -3,8 +3,6 @@ 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,
@ -113,8 +111,8 @@ where
}
}
impl<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2>
IntoNewService<FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2>> for (F1, F2)
impl<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2> From<(F1, F2)>
for FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2>
where
S: 'static,
F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static,
@ -126,10 +124,8 @@ where
Err1: 'static,
Err2: 'static,
{
fn into_new_service(
self,
) -> FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2> {
FnStateNewService::new(self.0, self.1)
fn from(data: (F1, F2)) -> FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2> {
FnStateNewService::new(data.0, data.1)
}
}

View file

@ -33,10 +33,10 @@ pub trait ServiceExt: Service {
fn and_then<F, B>(self, service: F) -> AndThen<Self, B>
where
Self: Sized,
F: IntoService<B>,
F: Into<B>,
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>
@ -70,7 +70,7 @@ pub trait NewServiceExt: NewService {
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
where
Self: Sized,
F: IntoNewService<B>,
F: Into<B>,
B: NewService<
Request = Self::Response,
Error = Self::Error,
@ -113,51 +113,15 @@ pub trait NewServiceExt: NewService {
}
}
impl<T: Service> ServiceExt for T {}
impl<T: NewService> NewServiceExt for T {}
impl<T: ?Sized> ServiceExt for T where T: Service {}
impl<T: ?Sized> NewServiceExt for T where T: NewService {}
/// 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
impl<F, Req, Resp, Err, Fut> From<F> for FnService<F, Req, Resp, Err, Fut>
where
F: Fn(Req) -> Fut + 'static,
Fut: IntoFuture<Item = Resp, Error = Err>,
{
fn into_service(self) -> FnService<F, Req, Resp, Err, Fut> {
FnService::new(self)
fn from(f: F) -> FnService<F, Req, Resp, Err, Fut> {
FnService::new(f)
}
}

View file

@ -2,7 +2,7 @@ use futures::unsync::mpsc;
use futures::{Async, Future, Poll, Stream};
use tokio::executor::current_thread::spawn;
use super::{IntoService, Service};
use super::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: 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();
StreamDispatcher {
stream,
item: None,
service: service.into_service(),
service: service.into(),
stop_rx,
stop_tx,
}