Skip to content

Commit

Permalink
service trait takes request type parameter (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede authored Dec 27, 2020
1 parent 518bf3f commit 3ab8c3e
Show file tree
Hide file tree
Showing 28 changed files with 1,135 additions and 1,129 deletions.
6 changes: 2 additions & 4 deletions actix-connect/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down Expand Up @@ -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)]
Expand Down
12 changes: 6 additions & 6 deletions actix-connect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ 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())
}

/// Create TCP connector service factory.
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 = (),
Expand All @@ -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 = (),
Expand Down
6 changes: 2 additions & 4 deletions actix-connect/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down Expand Up @@ -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)]
Expand Down
17 changes: 7 additions & 10 deletions actix-connect/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand All @@ -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>;
Expand All @@ -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> {
Expand Down Expand Up @@ -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>;
Expand All @@ -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> {
Expand Down
28 changes: 14 additions & 14 deletions actix-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<_, ()>(())
}))),
));
};
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -237,8 +238,8 @@ impl ServiceRuntime {
}

type BoxedNewService = Box<
dyn actix::ServiceFactory<
Request = (Option<CounterGuard>, StdStream),
dyn BaseServiceFactory<
(Option<CounterGuard>, StdStream),
Response = (),
Error = (),
InitError = (),
Expand All @@ -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 = ();
Expand Down
31 changes: 17 additions & 14 deletions actix-server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
}
Expand All @@ -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<(), ()>>;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions actix-service/CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading

0 comments on commit 3ab8c3e

Please sign in to comment.