1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-08-13 11:58:23 +02:00

Compare commits

..

25 Commits

Author SHA1 Message Date
Nikolay Kim
b407c65f4c add FramedParts::with_read_buf method 2019-03-06 22:53:55 -08:00
Nikolay Kim
51bd7d2721 update actix-rt 2019-03-06 10:39:53 -08:00
Nikolay Kim
c03d869694 return io::Result from run method, remove Handle 2019-03-06 10:24:58 -08:00
Nikolay Kim
25f1eae51f add ResourceDef::root_prefix, insert slash to the beggining of the pattern 2019-03-05 21:03:53 -08:00
Nikolay Kim
1153715149 change generics order for Transform trait 2019-03-05 09:49:08 -08:00
Nikolay Kim
aa2967c653 fix feature gated code 2019-03-05 07:41:41 -08:00
Nikolay Kim
dfbb77f98d make service Request type generic 2019-03-05 07:35:26 -08:00
Nikolay Kim
e8a49801eb revert IntoFuture change 2019-03-04 21:37:06 -08:00
Nikolay Kim
03f2046a42 add ApplyTransform new service 2019-03-04 21:25:50 -08:00
Nikolay Kim
2e18ca805c use IntoFuture 2019-03-04 20:40:38 -08:00
Nikolay Kim
15dafeff3d use IntoFuture instead of Future 2019-03-04 20:37:03 -08:00
Nikolay Kim
ed14e6b8ea change to IntoFuture 2019-03-04 20:29:35 -08:00
Nikolay Kim
700abc997e prepare actix-utils release 2019-03-04 19:45:17 -08:00
Nikolay Kim
8c48bf4de7 simplify transform trait 2019-03-04 19:38:11 -08:00
Nikolay Kim
9bc492cf6c add SslError 2019-03-04 16:16:39 -08:00
Nikolay Kim
d2a223e69e update changes 2019-03-04 15:42:25 -08:00
Nikolay Kim
e9657a399a add maxconnrate 2019-03-04 15:41:16 -08:00
Nikolay Kim
9f25fdf929 rename StreamServiceFactory to ServiceFactory 2019-03-04 14:31:46 -08:00
Nikolay Kim
82930de8e7 use default type for RouterBuilder 2019-03-04 14:03:46 -08:00
Nikolay Kim
04a3e59bd5 update tests 2019-03-04 12:41:39 -08:00
Nikolay Kim
0ff0daa795 allow custom checks for resource selection 2019-03-04 11:47:03 -08:00
Nikolay Kim
fb43940824 allow empty pattern 2019-03-03 21:00:58 -08:00
Nikolay Kim
0410f59cf5 prep release 2019-03-02 14:55:22 -08:00
Nikolay Kim
400023a07b prepare actix-connector release 2019-03-02 14:47:52 -08:00
Nikolay Kim
2e4c84dbb6 prepare actix-server release 2019-03-02 14:42:31 -08:00
59 changed files with 1134 additions and 1626 deletions

View File

@@ -1,5 +1,10 @@
# Changes # Changes
## [0.1.0] - 2019-03-06
* Added `FramedParts::with_read_buffer()` method.
## [0.1.0] - 2018-12-09 ## [0.1.0] - 2018-12-09
* Move codec to separate crate * Move codec to separate crate

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "actix-codec" name = "actix-codec"
version = "0.1.0" version = "0.1.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Utilities for encoding and decoding frames" description = "Utilities for encoding and decoding frames"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]

View File

@@ -349,4 +349,17 @@ impl<T, U> FramedParts<T, U> {
_priv: (), _priv: (),
} }
} }
/// Create a new `FramedParts` with read buffer
pub fn with_read_buf(io: T, codec: U, read_buf: BytesMut) -> FramedParts<T, U> {
FramedParts {
io,
codec,
read_buf,
write_buf: BytesMut::new(),
write_buf_lw: LW,
write_buf_hw: HW,
_priv: (),
}
}
} }

View File

@@ -1,8 +1,15 @@
# Changes # Changes
## [0.3.0] - 2019-03-02
### Changed
* Migrate to actix-service 0.3
## [0.2.0] - 2019-02-01 ## [0.2.0] - 2019-02-01
### Changes ### Changed
* Migrate to actix-service 0.2 * Migrate to actix-service 0.2

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "actix-connector" name = "actix-connector"
version = "0.2.0" version = "0.3.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix Connector - tcp connector service" description = "Actix Connector - tcp connector service"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]
@@ -11,7 +11,7 @@ categories = ["network-programming", "asynchronous"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"] exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
edition = "2018" edition = "2018"
workspace = "../" workspace = ".."
[package.metadata.docs.rs] [package.metadata.docs.rs]
features = ["ssl"] features = ["ssl"]
@@ -27,7 +27,7 @@ default = []
ssl = ["openssl", "tokio-openssl"] ssl = ["openssl", "tokio-openssl"]
[dependencies] [dependencies]
#actix-service = "0.2.0" #actix-service = "0.3.0"
actix-service = { path="../actix-service" } actix-service = { path="../actix-service" }
actix-codec = "0.1.0" actix-codec = "0.1.0"
futures = "0.1" futures = "0.1"

View File

@@ -167,8 +167,8 @@ impl Connector {
/// Create new connector with custom resolver /// Create new connector with custom resolver
pub fn with_resolver( pub fn with_resolver(
resolver: Resolver<Connect>, resolver: Resolver<Connect>,
) -> impl Service<Request = Connect, Response = (Connect, TcpStream), Error = ConnectorError> ) -> impl Service<Connect, Response = (Connect, TcpStream), Error = ConnectorError> + Clone
+ Clone { {
Connector { resolver } Connector { resolver }
} }
@@ -177,8 +177,8 @@ impl Connector {
cfg: ResolverConfig, cfg: ResolverConfig,
opts: ResolverOpts, opts: ResolverOpts,
) -> impl NewService< ) -> impl NewService<
Connect,
(), (),
Request = Connect,
Response = (Connect, TcpStream), Response = (Connect, TcpStream),
Error = ConnectorError, Error = ConnectorError,
InitError = E, InitError = E,
@@ -195,8 +195,7 @@ impl Clone for Connector {
} }
} }
impl Service for Connector { impl Service<Connect> for Connector {
type Request = Connect;
type Response = (Connect, TcpStream); type Response = (Connect, TcpStream);
type Error = ConnectorError; type Error = ConnectorError;
type Future = Either<ConnectorFuture, ConnectorTcpFuture>; type Future = Either<ConnectorFuture, ConnectorTcpFuture>;
@@ -273,8 +272,7 @@ impl<T: RequestPort> Default for TcpConnector<T> {
} }
} }
impl<T: RequestPort> Service for TcpConnector<T> { impl<T: RequestPort> Service<(T, VecDeque<IpAddr>)> for TcpConnector<T> {
type Request = (T, VecDeque<IpAddr>);
type Response = (T, TcpStream); type Response = (T, TcpStream);
type Error = io::Error; type Error = io::Error;
type Future = TcpConnectorResponse<T>; type Future = TcpConnectorResponse<T>;
@@ -354,8 +352,7 @@ impl DefaultConnector {
} }
} }
impl Service for DefaultConnector { impl Service<Connect> for DefaultConnector {
type Request = Connect;
type Response = TcpStream; type Response = TcpStream;
type Error = ConnectorError; type Error = ConnectorError;
type Future = DefaultConnectorFuture; type Future = DefaultConnectorFuture;

View File

@@ -67,8 +67,7 @@ impl<T> Clone for Resolver<T> {
} }
} }
impl<T: RequestHost> Service for Resolver<T> { impl<T: RequestHost> Service<T> for Resolver<T> {
type Request = T;
type Response = (T, VecDeque<IpAddr>); type Response = (T, VecDeque<IpAddr>);
type Error = ResolveError; type Error = ResolveError;
type Future = ResolverFuture<T>; type Future = ResolverFuture<T>;

View File

@@ -26,8 +26,7 @@ impl<R, T, E> OpensslConnector<R, T, E> {
impl<R: RequestHost, T: AsyncRead + AsyncWrite> OpensslConnector<R, T, ()> { impl<R: RequestHost, T: AsyncRead + AsyncWrite> OpensslConnector<R, T, ()> {
pub fn service( pub fn service(
connector: SslConnector, connector: SslConnector,
) -> impl Service<Request = (R, T), Response = (R, SslStream<T>), Error = HandshakeError<T>> ) -> impl Service<(R, T), Response = (R, SslStream<T>), Error = HandshakeError<T>> {
{
OpensslConnectorService { OpensslConnectorService {
connector: connector, connector: connector,
_t: PhantomData, _t: PhantomData,
@@ -44,10 +43,9 @@ impl<R, T, E> Clone for OpensslConnector<R, T, E> {
} }
} }
impl<R: RequestHost, T: AsyncRead + AsyncWrite, E> NewService<()> impl<R: RequestHost, T: AsyncRead + AsyncWrite, E> NewService<(R, T), ()>
for OpensslConnector<R, T, E> for OpensslConnector<R, T, E>
{ {
type Request = (R, T);
type Response = (R, SslStream<T>); type Response = (R, SslStream<T>);
type Error = HandshakeError<T>; type Error = HandshakeError<T>;
type Service = OpensslConnectorService<R, T>; type Service = OpensslConnectorService<R, T>;
@@ -67,8 +65,9 @@ pub struct OpensslConnectorService<R, T> {
_t: PhantomData<(R, T)>, _t: PhantomData<(R, T)>,
} }
impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service for OpensslConnectorService<R, T> { impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service<(R, T)>
type Request = (R, T); for OpensslConnectorService<R, T>
{
type Response = (R, SslStream<T>); type Response = (R, SslStream<T>);
type Error = HandshakeError<T>; type Error = HandshakeError<T>;
type Future = ConnectAsyncExt<R, T>; type Future = ConnectAsyncExt<R, T>;

View File

@@ -1,5 +1,11 @@
# Changes # Changes
## [0.2.0] - 2019-03-06
* `run` method returns `io::Result<()>`
* Removed `Handle`
## [0.1.0] - 2018-12-09 ## [0.1.0] - 2018-12-09
* Initial release * Initial release

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "actix-rt" name = "actix-rt"
version = "0.1.0" version = "0.2.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix runtime" description = "Actix runtime"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]
@@ -20,7 +20,7 @@ path = "src/lib.rs"
[dependencies] [dependencies]
log = "0.4" log = "0.4"
bytes = "0.4" bytes = "0.4"
futures = "0.1.24" futures = "0.1.25"
tokio-current-thread = "0.1" tokio-current-thread = "0.1"
tokio-executor = "0.1.5" tokio-executor = "0.1.5"
tokio-reactor = "0.1.7" tokio-reactor = "0.1.7"

View File

@@ -72,7 +72,7 @@ impl Builder {
/// This function will start tokio runtime and will finish once the /// This function will start tokio runtime and will finish once the
/// `System::stop()` message get called. /// `System::stop()` message get called.
/// Function `f` get called within tokio runtime context. /// Function `f` get called within tokio runtime context.
pub fn run<F>(self, f: F) -> i32 pub fn run<F>(self, f: F) -> io::Result<()>
where where
F: FnOnce() + 'static, F: FnOnce() + 'static,
{ {
@@ -140,7 +140,7 @@ pub struct SystemRunner {
impl SystemRunner { impl SystemRunner {
/// This function will start event loop and will finish once the /// This function will start event loop and will finish once the
/// `System::stop()` function is called. /// `System::stop()` function is called.
pub fn run(self) -> i32 { pub fn run(self) -> io::Result<()> {
let SystemRunner { mut rt, stop, .. } = self; let SystemRunner { mut rt, stop, .. } = self;
// run loop // run loop
@@ -148,12 +148,21 @@ impl SystemRunner {
Arbiter::run_system(); Arbiter::run_system();
Ok::<_, ()>(()) Ok::<_, ()>(())
})); }));
let code = match rt.block_on(stop) { let result = match rt.block_on(stop) {
Ok(code) => code, Ok(code) => {
Err(_) => 1, if code != 0 {
Err(io::Error::new(
io::ErrorKind::Other,
format!("Non-zero exit code: {}", code),
))
} else {
Ok(())
}
}
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
}; };
Arbiter::stop_system(); Arbiter::stop_system();
code result
} }
/// Execute a future and wait for result. /// Execute a future and wait for result.

View File

@@ -7,7 +7,7 @@ mod system;
pub use self::arbiter::Arbiter; pub use self::arbiter::Arbiter;
pub use self::builder::{Builder, SystemRunner}; pub use self::builder::{Builder, SystemRunner};
pub use self::runtime::{Handle, Runtime}; pub use self::runtime::Runtime;
pub use self::system::System; pub use self::system::System;
/// Spawns a future on the current arbiter. /// Spawns a future on the current arbiter.

View File

@@ -1,9 +1,7 @@
use std::error::Error; use std::error::Error;
use std::fmt; use std::{fmt, io};
use std::io;
use futures::{future, Future}; use futures::Future;
use tokio_current_thread::Handle as ExecutorHandle;
use tokio_current_thread::{self as current_thread, CurrentThread}; use tokio_current_thread::{self as current_thread, CurrentThread};
use tokio_executor; use tokio_executor;
use tokio_reactor::{self, Reactor}; use tokio_reactor::{self, Reactor};
@@ -26,58 +24,6 @@ pub struct Runtime {
executor: CurrentThread<Timer<Reactor>>, executor: CurrentThread<Timer<Reactor>>,
} }
/// Handle to spawn a future on the corresponding `CurrentThread` runtime instance
#[derive(Debug, Clone)]
pub struct Handle(ExecutorHandle);
impl Handle {
/// Spawn a future onto the `CurrentThread` runtime instance corresponding to this handle
///
/// # Panics
///
/// This function panics if the spawn fails. Failure occurs if the `CurrentThread`
/// instance of the `Handle` does not exist anymore.
pub fn spawn<F>(&self, future: F) -> Result<(), tokio_executor::SpawnError>
where
F: Future<Item = (), Error = ()> + Send + 'static,
{
self.0.spawn(future)
}
/// Provides a best effort **hint** to whether or not `spawn` will succeed.
///
/// This function may return both false positives **and** false negatives.
/// If `status` returns `Ok`, then a call to `spawn` will *probably*
/// succeed, but may fail. If `status` returns `Err`, a call to `spawn` will
/// *probably* fail, but may succeed.
///
/// This allows a caller to avoid creating the task if the call to `spawn`
/// has a high likelihood of failing.
pub fn status(&self) -> Result<(), tokio_executor::SpawnError> {
self.0.status()
}
}
impl<T> future::Executor<T> for Handle
where
T: Future<Item = (), Error = ()> + Send + 'static,
{
fn execute(&self, future: T) -> Result<(), future::ExecuteError<T>> {
if let Err(e) = self.status() {
let kind = if e.is_at_capacity() {
future::ExecuteErrorKind::NoCapacity
} else {
future::ExecuteErrorKind::Shutdown
};
return Err(future::ExecuteError::new(kind, future));
}
let _ = self.spawn(future);
Ok(())
}
}
/// Error returned by the `run` function. /// Error returned by the `run` function.
#[derive(Debug)] #[derive(Debug)]
pub struct RunError { pub struct RunError {
@@ -120,14 +66,6 @@ impl Runtime {
} }
} }
/// Get a new handle to spawn futures on the single-threaded Tokio runtime
///
/// Different to the runtime itself, the handle can be sent to different
/// threads.
pub fn handle(&self) -> Handle {
Handle(self.executor.handle().clone())
}
/// Spawn a future onto the single-threaded Tokio runtime. /// Spawn a future onto the single-threaded Tokio runtime.
/// ///
/// See [module level][mod] documentation for more details. /// See [module level][mod] documentation for more details.

View File

@@ -1,4 +1,5 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::io;
use futures::sync::mpsc::UnboundedSender; use futures::sync::mpsc::UnboundedSender;
@@ -109,7 +110,7 @@ impl System {
/// This function will start tokio runtime and will finish once the /// This function will start tokio runtime and will finish once the
/// `System::stop()` message get called. /// `System::stop()` message get called.
/// Function `f` get called within tokio runtime context. /// Function `f` get called within tokio runtime context.
pub fn run<F>(f: F) -> i32 pub fn run<F>(f: F) -> io::Result<()>
where where
F: FnOnce() + 'static, F: FnOnce() + 'static,
{ {

View File

@@ -1,15 +1,43 @@
# Changes # Changes
## [0.4.0] - 2019-03-xx
* Upgrade actix-service
## [0.3.1] - 2019-03-04
### Added
* Add `ServerBuilder::maxconnrate` sets the maximum per-worker number of concurrent connections
* Add helper ssl error `SslError`
### Changed
* Rename `StreamServiceFactory` to `ServiceFactory`
* Deprecate `StreamServiceFactory`
## [0.3.0] - 2019-03-02
### Changed
* Use new `NewService` trait
## [0.2.1] - 2019-02-09 ## [0.2.1] - 2019-02-09
### Changes ### Changed
* Drop service response * Drop service response
## [0.2.0] - 2019-02-01 ## [0.2.0] - 2019-02-01
### Changes ### Changed
* Migrate to actix-service 0.2 * Migrate to actix-service 0.2

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "actix-server" name = "actix-server"
version = "0.2.1" version = "0.3.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix server - General purpose tcp server" description = "Actix server - General purpose tcp server"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]
@@ -11,7 +11,7 @@ categories = ["network-programming", "asynchronous"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"] exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
edition = "2018" edition = "2018"
workspace = "../" workspace = ".."
[package.metadata.docs.rs] [package.metadata.docs.rs]
features = ["ssl", "tls", "rust-tls"] features = ["ssl", "tls", "rust-tls"]
@@ -33,17 +33,15 @@ ssl = ["openssl", "tokio-openssl"]
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"] rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
[dependencies] [dependencies]
#actix-service = "0.2.1" #actix-service = "0.3.2"
actix-service = { path="../actix-service" } actix-service = { path="../actix-service" }
actix-rt = "0.1.0" actix-rt = "0.2.0"
log = "0.4" log = "0.4"
num_cpus = "1.0" num_cpus = "1.0"
# io
mio = "^0.6.13" mio = "^0.6.13"
net2 = "0.2" net2 = "0.2"
bytes = "0.4"
futures = "0.1" futures = "0.1"
slab = "0.4" slab = "0.4"
tokio-io = "0.1" tokio-io = "0.1"

View File

@@ -14,10 +14,10 @@ use tokio_timer::sleep;
use crate::accept::{AcceptLoop, AcceptNotify, Command}; use crate::accept::{AcceptLoop, AcceptNotify, Command};
use crate::config::{ConfiguredService, ServiceConfig}; use crate::config::{ConfiguredService, ServiceConfig};
use crate::server::{Server, ServerCommand}; use crate::server::{Server, ServerCommand};
use crate::services::{InternalServiceFactory, StreamNewService, StreamServiceFactory}; use crate::services::{InternalServiceFactory, ServiceFactory, StreamNewService};
use crate::signals::{Signal, Signals}; use crate::signals::{Signal, Signals};
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient}; use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};
use crate::Token; use crate::{ssl, Token};
/// Server builder /// Server builder
pub struct ServerBuilder { pub struct ServerBuilder {
@@ -81,9 +81,18 @@ impl ServerBuilder {
self self
} }
/// Stop actix system. /// Sets the maximum per-worker concurrent connection establish process.
/// ///
/// `SystemExit` message stops currently running system. /// All listeners will stop accepting connections when this limit is reached. It
/// can be used to limit the global SSL CPU usage.
///
/// By default max connections is set to a 256.
pub fn maxconnrate(self, num: usize) -> Self {
ssl::max_concurrent_ssl_connect(num);
self
}
/// Stop actix system.
pub fn system_exit(mut self) -> Self { pub fn system_exit(mut self) -> Self {
self.exit = true; self.exit = true;
self self
@@ -137,7 +146,7 @@ impl ServerBuilder {
/// Add new service to the server. /// Add new service to the server.
pub fn bind<F, U, N: AsRef<str>>(mut self, name: N, addr: U, factory: F) -> io::Result<Self> pub fn bind<F, U, N: AsRef<str>>(mut self, name: N, addr: U, factory: F) -> io::Result<Self>
where where
F: StreamServiceFactory, F: ServiceFactory,
U: net::ToSocketAddrs, U: net::ToSocketAddrs,
{ {
let sockets = bind_addr(addr)?; let sockets = bind_addr(addr)?;
@@ -163,7 +172,7 @@ impl ServerBuilder {
factory: F, factory: F,
) -> Self ) -> Self
where where
F: StreamServiceFactory, F: ServiceFactory,
{ {
let token = self.token.next(); let token = self.token.next();
self.services.push(StreamNewService::create( self.services.push(StreamNewService::create(
@@ -183,9 +192,6 @@ impl ServerBuilder {
/// This methods panics if no socket addresses get bound. /// This methods panics if no socket addresses get bound.
/// ///
/// ```rust,ignore /// ```rust,ignore
/// # extern crate futures;
/// # extern crate actix_web;
/// # use futures::Future;
/// use actix_web::*; /// use actix_web::*;
/// ///
/// fn main() { /// fn main() {

View File

@@ -169,8 +169,8 @@ impl ServiceRuntime {
pub fn service<T, F>(&mut self, name: &str, service: F) pub fn service<T, F>(&mut self, name: &str, service: F)
where where
F: IntoNewService<T>, F: IntoNewService<T, TcpStream>,
T: NewService<Request = TcpStream, Response = ()> + 'static, T: NewService<TcpStream, Response = ()> + 'static,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
T::InitError: fmt::Debug, T::InitError: fmt::Debug,
@@ -191,7 +191,7 @@ impl ServiceRuntime {
type BoxedNewService = Box< type BoxedNewService = Box<
NewService< NewService<
Request = (Option<CounterGuard>, ServerMessage), (Option<CounterGuard>, ServerMessage),
Response = (), Response = (),
Error = (), Error = (),
InitError = (), InitError = (),
@@ -204,15 +204,14 @@ struct ServiceFactory<T> {
inner: T, inner: T,
} }
impl<T> NewService for ServiceFactory<T> impl<T> NewService<(Option<CounterGuard>, ServerMessage)> for ServiceFactory<T>
where where
T: NewService<Request = TcpStream, Response = ()>, T: NewService<TcpStream, Response = ()>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
T::Error: 'static, T::Error: 'static,
T::InitError: fmt::Debug + 'static, T::InitError: fmt::Debug + 'static,
{ {
type Request = (Option<CounterGuard>, ServerMessage);
type Response = (); type Response = ();
type Error = (); type Error = ();
type InitError = (); type InitError = ();

View File

@@ -13,7 +13,10 @@ mod worker;
pub use self::builder::ServerBuilder; pub use self::builder::ServerBuilder;
pub use self::config::{ServiceConfig, ServiceRuntime}; pub use self::config::{ServiceConfig, ServiceRuntime};
pub use self::server::Server; pub use self::server::Server;
pub use self::services::StreamServiceFactory; pub use self::services::ServiceFactory;
#[doc(hidden)]
pub use self::services::ServiceFactory as StreamServiceFactory;
/// Socket id token /// Socket id token
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -28,6 +31,6 @@ impl Token {
} }
/// Start server building process /// Start server building process
pub fn build() -> ServerBuilder { pub fn new() -> ServerBuilder {
ServerBuilder::default() ServerBuilder::default()
} }

View File

@@ -22,8 +22,8 @@ pub(crate) enum ServerMessage {
ForceShutdown, ForceShutdown,
} }
pub trait StreamServiceFactory: Send + Clone + 'static { pub trait ServiceFactory: Send + Clone + 'static {
type NewService: NewService<Request = TcpStream>; type NewService: NewService<TcpStream>;
fn create(&self) -> Self::NewService; fn create(&self) -> Self::NewService;
} }
@@ -38,7 +38,7 @@ pub(crate) trait InternalServiceFactory: Send {
pub(crate) type BoxedServerService = Box< pub(crate) type BoxedServerService = Box<
Service< Service<
Request = (Option<CounterGuard>, ServerMessage), (Option<CounterGuard>, ServerMessage),
Response = (), Response = (),
Error = (), Error = (),
Future = FutureResult<(), ()>, Future = FutureResult<(), ()>,
@@ -55,13 +55,12 @@ impl<T> StreamService<T> {
} }
} }
impl<T> Service for StreamService<T> impl<T> Service<(Option<CounterGuard>, ServerMessage)> for StreamService<T>
where where
T: Service<Request = TcpStream>, T: Service<TcpStream>,
T::Future: 'static, T::Future: 'static,
T::Error: 'static, T::Error: 'static,
{ {
type Request = (Option<CounterGuard>, ServerMessage);
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;
@@ -92,7 +91,7 @@ where
} }
} }
pub(crate) struct StreamNewService<F: StreamServiceFactory> { pub(crate) struct StreamNewService<F: ServiceFactory> {
name: String, name: String,
inner: F, inner: F,
token: Token, token: Token,
@@ -100,7 +99,7 @@ pub(crate) struct StreamNewService<F: StreamServiceFactory> {
impl<F> StreamNewService<F> impl<F> StreamNewService<F>
where where
F: StreamServiceFactory, F: ServiceFactory,
{ {
pub(crate) fn create(name: String, token: Token, inner: F) -> Box<InternalServiceFactory> { pub(crate) fn create(name: String, token: Token, inner: F) -> Box<InternalServiceFactory> {
Box::new(Self { name, token, inner }) Box::new(Self { name, token, inner })
@@ -109,7 +108,7 @@ where
impl<F> InternalServiceFactory for StreamNewService<F> impl<F> InternalServiceFactory for StreamNewService<F>
where where
F: StreamServiceFactory, F: ServiceFactory,
{ {
fn name(&self, _: Token) -> &str { fn name(&self, _: Token) -> &str {
&self.name &self.name
@@ -152,10 +151,10 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
} }
} }
impl<F, T> StreamServiceFactory for F impl<F, T> ServiceFactory for F
where where
F: Fn() -> T + Send + Clone + 'static, F: Fn() -> T + Send + Clone + 'static,
T: NewService<Request = TcpStream>, T: NewService<TcpStream>,
{ {
type NewService = T; type NewService = T;

View File

@@ -33,3 +33,9 @@ pub(crate) static MAX_CONN: AtomicUsize = AtomicUsize::new(256);
thread_local! { thread_local! {
static MAX_CONN_COUNTER: Counter = Counter::new(MAX_CONN.load(Ordering::Relaxed)); static MAX_CONN_COUNTER: Counter = Counter::new(MAX_CONN.load(Ordering::Relaxed));
} }
/// Ssl error combinded with service error.
pub enum SslError<E1, E2> {
Ssl(E1),
Service(E2),
}

View File

@@ -36,8 +36,7 @@ impl<T: AsyncRead + AsyncWrite> Clone for NativeTlsAcceptor<T> {
} }
} }
impl<T: AsyncRead + AsyncWrite> NewService for NativeTlsAcceptor<T> { impl<T: AsyncRead + AsyncWrite> NewService<T> for NativeTlsAcceptor<T> {
type Request = T;
type Response = TlsStream<T>; type Response = TlsStream<T>;
type Error = Error; type Error = Error;
type Service = NativeTlsAcceptorService<T>; type Service = NativeTlsAcceptorService<T>;
@@ -61,8 +60,7 @@ pub struct NativeTlsAcceptorService<T> {
conns: Counter, conns: Counter,
} }
impl<T: AsyncRead + AsyncWrite> Service for NativeTlsAcceptorService<T> { impl<T: AsyncRead + AsyncWrite> Service<T> for NativeTlsAcceptorService<T> {
type Request = T;
type Response = TlsStream<T>; type Response = TlsStream<T>;
type Error = Error; type Error = Error;
type Future = Accept<T>; type Future = Accept<T>;

View File

@@ -36,8 +36,7 @@ impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
} }
} }
impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> { impl<T: AsyncRead + AsyncWrite> NewService<T> for OpensslAcceptor<T> {
type Request = T;
type Response = SslStream<T>; type Response = SslStream<T>;
type Error = HandshakeError<T>; type Error = HandshakeError<T>;
type Service = OpensslAcceptorService<T>; type Service = OpensslAcceptorService<T>;
@@ -61,8 +60,7 @@ pub struct OpensslAcceptorService<T> {
conns: Counter, conns: Counter,
} }
impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> { impl<T: AsyncRead + AsyncWrite> Service<T> for OpensslAcceptorService<T> {
type Request = T;
type Response = SslStream<T>; type Response = SslStream<T>;
type Error = HandshakeError<T>; type Error = HandshakeError<T>;
type Future = OpensslAcceptorServiceFut<T>; type Future = OpensslAcceptorServiceFut<T>;

View File

@@ -38,8 +38,7 @@ impl<T> Clone for RustlsAcceptor<T> {
} }
} }
impl<T: AsyncRead + AsyncWrite> NewService for RustlsAcceptor<T> { impl<T: AsyncRead + AsyncWrite> NewService<T> for RustlsAcceptor<T> {
type Request = T;
type Response = TlsStream<T, ServerSession>; type Response = TlsStream<T, ServerSession>;
type Error = io::Error; type Error = io::Error;
type Service = RustlsAcceptorService<T>; type Service = RustlsAcceptorService<T>;
@@ -63,8 +62,7 @@ pub struct RustlsAcceptorService<T> {
conns: Counter, conns: Counter,
} }
impl<T: AsyncRead + AsyncWrite> Service for RustlsAcceptorService<T> { impl<T: AsyncRead + AsyncWrite> Service<T> for RustlsAcceptorService<T> {
type Request = T;
type Response = TlsStream<T, ServerSession>; type Response = TlsStream<T, ServerSession>;
type Error = io::Error; type Error = io::Error;
type Future = RustlsAcceptorServiceFut<T>; type Future = RustlsAcceptorServiceFut<T>;

View File

@@ -1,5 +1,32 @@
# Changes # Changes
## [0.3.2] - 2019-03-xx
### Added
* Add `ApplyTransform` new service for transform and new service.
### Changed
* Revert IntoFuture change
## [0.3.2] - 2019-03-04
### Changed
* Change `NewService::Future` and `Transform::Future` to the `IntoFuture` trait.
* Export `AndThenTransform` type
## [0.3.1] - 2019-03-04
### Changed
* Simplify Transform trait
## [0.3.0] - 2019-03-02 ## [0.3.0] - 2019-03-02
## Added ## Added

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "actix-service" name = "actix-service"
version = "0.3.0" version = "0.3.2"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix Service" description = "Actix Service"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]
@@ -25,3 +25,6 @@ path = "src/lib.rs"
[dependencies] [dependencies]
futures = "0.1.24" futures = "0.1.24"
void = "1.0.2" void = "1.0.2"
[dev-dependencies]
actix-rt = "0.1"

View File

@@ -1,5 +1,3 @@
use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll}; use futures::{try_ready, Async, Future, Poll};
use super::{IntoNewService, NewService, Service}; use super::{IntoNewService, NewService, Service};
@@ -16,10 +14,10 @@ pub struct AndThen<A, B> {
impl<A, B> AndThen<A, B> { impl<A, B> AndThen<A, B> {
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new(a: A, b: B) -> Self pub fn new<R>(a: A, b: B) -> Self
where where
A: Service, A: Service<R>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
Self { a, b: Cell::new(b) } Self { a, b: Cell::new(b) }
} }
@@ -37,40 +35,39 @@ where
} }
} }
impl<A, B> Service for AndThen<A, B> impl<A, B, R> Service<R> for AndThen<A, B>
where where
A: Service, A: Service<R>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Future = AndThenFuture<A, B>; type Future = AndThenFuture<A, B, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.poll_ready()); try_ready!(self.a.poll_ready());
self.b.get_mut().poll_ready() self.b.get_mut().poll_ready()
} }
fn call(&mut self, req: A::Request) -> Self::Future { fn call(&mut self, req: R) -> Self::Future {
AndThenFuture::new(self.a.call(req), self.b.clone()) AndThenFuture::new(self.a.call(req), self.b.clone())
} }
} }
pub struct AndThenFuture<A, B> pub struct AndThenFuture<A, B, R>
where where
A: Service, A: Service<R>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
b: Cell<B>, b: Cell<B>,
fut_b: Option<B::Future>, fut_b: Option<B::Future>,
fut_a: Option<A::Future>, fut_a: Option<A::Future>,
} }
impl<A, B> AndThenFuture<A, B> impl<A, B, R> AndThenFuture<A, B, R>
where where
A: Service, A: Service<R>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
fn new(a: A::Future, b: Cell<B>) -> Self { fn new(a: A::Future, b: Cell<B>) -> Self {
AndThenFuture { AndThenFuture {
@@ -81,10 +78,10 @@ where
} }
} }
impl<A, B> Future for AndThenFuture<A, B> impl<A, B, R> Future for AndThenFuture<A, B, R>
where where
A: Service, A: Service<R>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
type Item = B::Response; type Item = B::Response;
type Error = A::Error; type Error = A::Error;
@@ -107,46 +104,43 @@ where
} }
/// `AndThenNewService` new service combinator /// `AndThenNewService` new service combinator
pub struct AndThenNewService<A, B, C> { pub struct AndThenNewService<A, B> {
a: A, a: A,
b: B, b: B,
_t: PhantomData<C>,
} }
impl<A, B, C> AndThenNewService<A, B, C> { impl<A, B> AndThenNewService<A, B> {
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new<F: IntoNewService<B, C>>(a: A, f: F) -> Self pub fn new<R, C, F: IntoNewService<B, A::Response, C>>(a: A, f: F) -> Self
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>, B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
{ {
Self { Self {
a, a,
b: f.into_new_service(), b: f.into_new_service(),
_t: PhantomData,
} }
} }
} }
impl<A, B, C> NewService<C> for AndThenNewService<A, B, C> impl<A, B, R, C> NewService<R, C> for AndThenNewService<A, B>
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>, B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Service = AndThen<A::Service, B::Service>; type Service = AndThen<A::Service, B::Service>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = AndThenNewServiceFuture<A, B, C>; type Future = AndThenNewServiceFuture<A, B, R, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
AndThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg)) AndThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg))
} }
} }
impl<A, B, C> Clone for AndThenNewService<A, B, C> impl<A, B> Clone for AndThenNewService<A, B>
where where
A: Clone, A: Clone,
B: Clone, B: Clone,
@@ -155,15 +149,14 @@ where
Self { Self {
a: self.a.clone(), a: self.a.clone(),
b: self.b.clone(), b: self.b.clone(),
_t: PhantomData,
} }
} }
} }
pub struct AndThenNewServiceFuture<A, B, C> pub struct AndThenNewServiceFuture<A, B, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService<C, Request = A::Response>, B: NewService<A::Response, C>,
{ {
fut_b: B::Future, fut_b: B::Future,
fut_a: A::Future, fut_a: A::Future,
@@ -171,10 +164,10 @@ where
b: Option<B::Service>, b: Option<B::Service>,
} }
impl<A, B, C> AndThenNewServiceFuture<A, B, C> impl<A, B, R, C> AndThenNewServiceFuture<A, B, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService<C, Request = A::Response>, B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
{ {
fn new(fut_a: A::Future, fut_b: B::Future) -> Self { fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
AndThenNewServiceFuture { AndThenNewServiceFuture {
@@ -186,10 +179,10 @@ where
} }
} }
impl<A, B, C> Future for AndThenNewServiceFuture<A, B, C> impl<A, B, R, C> Future for AndThenNewServiceFuture<A, B, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>, B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
{ {
type Item = AndThen<A::Service, B::Service>; type Item = AndThen<A::Service, B::Service>;
type Error = A::InitError; type Error = A::InitError;
@@ -229,8 +222,7 @@ mod tests {
use crate::{NewService, Service, ServiceExt}; use crate::{NewService, Service, ServiceExt};
struct Srv1(Rc<Cell<usize>>); struct Srv1(Rc<Cell<usize>>);
impl Service for Srv1 { impl Service<&'static str> for Srv1 {
type Request = &'static str;
type Response = &'static str; type Response = &'static str;
type Error = (); type Error = ();
type Future = FutureResult<Self::Response, ()>; type Future = FutureResult<Self::Response, ()>;
@@ -248,8 +240,7 @@ mod tests {
#[derive(Clone)] #[derive(Clone)]
struct Srv2(Rc<Cell<usize>>); struct Srv2(Rc<Cell<usize>>);
impl Service for Srv2 { impl Service<&'static str> for Srv2 {
type Request = &'static str;
type Response = (&'static str, &'static str); type Response = (&'static str, &'static str);
type Error = (); type Error = ();
type Future = FutureResult<Self::Response, ()>; type Future = FutureResult<Self::Response, ()>;

View File

@@ -1,159 +1,42 @@
use std::marker::PhantomData;
use std::rc::Rc;
use futures::{Async, Future, Poll}; use futures::{Async, Future, Poll};
use super::{NewService, NewTransform, Service, Transform}; use crate::and_then::AndThen;
use crate::cell::Cell; use crate::from_err::FromErr;
use crate::{NewService, Transform};
/// `Apply` service combinator
pub struct AndThenTransform<T, A, B>
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
a: A,
b: Cell<B>,
t: Cell<T>,
}
impl<T, A, B> AndThenTransform<T, A, B>
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
/// Create new `Apply` combinator
pub fn new(t: T, a: A, b: B) -> Self {
Self {
a,
b: Cell::new(b),
t: Cell::new(t),
}
}
}
impl<T, A, B> Clone for AndThenTransform<T, A, B>
where
A: Service + Clone,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
fn clone(&self) -> Self {
AndThenTransform {
a: self.a.clone(),
b: self.b.clone(),
t: self.t.clone(),
}
}
}
impl<T, A, B> Service for AndThenTransform<T, A, B>
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
type Request = A::Request;
type Response = T::Response;
type Error = T::Error;
type Future = AndThenTransformFuture<T, A, B>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
let notready = Async::NotReady == self.a.poll_ready()?;
let notready = Async::NotReady == self.b.get_mut().poll_ready()? || notready;
let notready = Async::NotReady == self.t.get_mut().poll_ready()? || notready;
if notready {
Ok(Async::NotReady)
} else {
Ok(Async::Ready(()))
}
}
fn call(&mut self, req: A::Request) -> Self::Future {
AndThenTransformFuture {
b: self.b.clone(),
t: self.t.clone(),
fut_t: None,
fut_a: Some(self.a.call(req)),
}
}
}
pub struct AndThenTransformFuture<T, A, B>
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
b: Cell<B>,
t: Cell<T>,
fut_a: Option<A::Future>,
fut_t: Option<T::Future>,
}
impl<T, A, B> Future for AndThenTransformFuture<T, A, B>
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
type Item = T::Response;
type Error = T::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if let Some(ref mut fut) = self.fut_t {
return fut.poll();
}
match self.fut_a.as_mut().expect("Bug in actix-service").poll() {
Ok(Async::Ready(resp)) => {
let _ = self.fut_a.take();
self.fut_t = Some(self.t.get_mut().call(resp, self.b.get_mut()));
self.poll()
}
Ok(Async::NotReady) => Ok(Async::NotReady),
Err(err) => Err(err.into()),
}
}
}
/// `Apply` new service combinator /// `Apply` new service combinator
pub struct AndThenTransformNewService<T, A, B, C> { pub struct AndThenTransform<T, A, B, BR> {
a: A, a: A,
b: B, b: B,
t: T, t: Rc<T>,
_t: std::marker::PhantomData<C>, _t: PhantomData<BR>,
} }
impl<T, A, B, C> AndThenTransformNewService<T, A, B, C> impl<T, A, B, BR> AndThenTransform<T, A, B, BR> {
where
A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new(t: T, a: A, b: B) -> Self { pub fn new<AR, C>(t: T, a: A, b: B) -> Self
where
A: NewService<AR, C>,
B: NewService<BR, C, InitError = A::InitError>,
T: Transform<B::Service, A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
Self { Self {
a, a,
b, b,
t, t: Rc::new(t),
_t: std::marker::PhantomData, _t: std::marker::PhantomData,
} }
} }
} }
impl<T, A, B, C> Clone for AndThenTransformNewService<T, A, B, C> impl<T, A, B, BR> Clone for AndThenTransform<T, A, B, BR>
where where
A: Clone, A: Clone,
B: Clone, B: Clone,
T: Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
@@ -165,62 +48,61 @@ where
} }
} }
impl<T, A, B, C> NewService<C> for AndThenTransformNewService<T, A, B, C> impl<T, A, B, AR, BR, C> NewService<AR, C> for AndThenTransform<T, A, B, BR>
where where
A: NewService<C>, A: NewService<AR, C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>, B: NewService<BR, C, InitError = A::InitError>,
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>, T: Transform<B::Service, A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
type Request = A::Request;
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type InitError = T::InitError; type InitError = T::InitError;
type Service = AndThenTransform<T::Transform, A::Service, B::Service>; type Service = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
type Future = AndThenTransformNewServiceFuture<T, A, B, C>; type Future = AndThenTransformFuture<T, A, B, AR, BR, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
AndThenTransformNewServiceFuture { AndThenTransformFuture {
a: None, a: None,
b: None,
t: None, t: None,
t_cell: self.t.clone(),
fut_a: self.a.new_service(cfg), fut_a: self.a.new_service(cfg),
fut_b: self.b.new_service(cfg), fut_b: self.b.new_service(cfg),
fut_t: self.t.new_transform(cfg), fut_t: None,
} }
} }
} }
pub struct AndThenTransformNewServiceFuture<T, A, B, C> pub struct AndThenTransformFuture<T, A, B, AR, BR, C>
where where
A: NewService<C>, A: NewService<AR, C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>, B: NewService<BR, C, InitError = A::InitError>,
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>, T: Transform<B::Service, A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
fut_b: B::Future,
fut_a: A::Future, fut_a: A::Future,
fut_t: T::Future, fut_b: B::Future,
fut_t: Option<T::Future>,
a: Option<A::Service>, a: Option<A::Service>,
b: Option<B::Service>,
t: Option<T::Transform>, t: Option<T::Transform>,
t_cell: Rc<T>,
} }
impl<T, A, B, C> Future for AndThenTransformNewServiceFuture<T, A, B, C> impl<T, A, B, AR, BR, C> Future for AndThenTransformFuture<T, A, B, AR, BR, C>
where where
A: NewService<C>, A: NewService<AR, C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>, B: NewService<BR, C, InitError = A::InitError>,
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>, T: Transform<B::Service, A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
type Item = AndThenTransform<T::Transform, A::Service, B::Service>; type Item = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
type Error = T::InitError; type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if self.t.is_none() { if self.fut_t.is_none() {
if let Async::Ready(transform) = self.fut_t.poll()? { if let Async::Ready(service) = self.fut_b.poll()? {
self.t = Some(transform); self.fut_t = Some(self.t_cell.new_transform(service));
} }
} }
@@ -230,18 +112,17 @@ where
} }
} }
if self.b.is_none() { if let Some(ref mut fut) = self.fut_t {
if let Async::Ready(service) = self.fut_b.poll()? { if let Async::Ready(transform) = fut.poll()? {
self.b = Some(service); self.t = Some(transform);
} }
} }
if self.a.is_some() && self.b.is_some() && self.t.is_some() { if self.a.is_some() && self.t.is_some() {
Ok(Async::Ready(AndThenTransform { Ok(Async::Ready(AndThen::new(
a: self.a.take().unwrap(), FromErr::new(self.a.take().unwrap()),
t: Cell::new(self.t.take().unwrap()), self.t.take().unwrap(),
b: Cell::new(self.b.take().unwrap()), )))
}))
} else { } else {
Ok(Async::NotReady) Ok(Async::NotReady)
} }
@@ -257,8 +138,7 @@ mod tests {
#[derive(Clone)] #[derive(Clone)]
struct Srv; struct Srv;
impl Service for Srv { impl Service<()> for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;
@@ -276,10 +156,11 @@ mod tests {
fn test_apply() { fn test_apply() {
let blank = |req| Ok(req); let blank = |req| Ok(req);
let mut srv = blank.into_service().apply( let mut srv = blank
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)), .into_service()
Srv, .apply_fn(Srv, |req: &'static str, srv: &mut Srv| {
); srv.call(()).map(move |res| (req, res))
});
assert!(srv.poll_ready().is_ok()); assert!(srv.poll_ready().is_ok());
let res = srv.call("srv").poll(); let res = srv.call("srv").poll();
assert!(res.is_ok()); assert!(res.is_ok());

View File

@@ -6,30 +6,23 @@ use super::{IntoNewService, IntoService, NewService, Service};
use crate::cell::Cell; use crate::cell::Cell;
/// `Apply` service combinator /// `Apply` service combinator
pub struct AndThenApply<A, B, F, Out> pub struct AndThenApply<A, B, F, Out, AReq, BReq> {
where
A: Service,
B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
a: A, a: A,
b: Cell<B>, b: Cell<B>,
f: Cell<F>, f: Cell<F>,
r: PhantomData<(Out,)>, r: PhantomData<(Out, AReq, BReq)>,
} }
impl<A, B, F, Out> AndThenApply<A, B, F, Out> impl<A, B, F, Out, AReq, BReq> AndThenApply<A, B, F, Out, AReq, BReq>
where where
A: Service, A: Service<AReq>,
B: Service<Error = A::Error>, B: Service<BReq, Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out, F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
/// Create new `Apply` combinator /// Create new `Apply` combinator
pub fn new<A1: IntoService<A>, B1: IntoService<B>>(a: A1, b: B1, f: F) -> Self { pub fn new<A1: IntoService<A, AReq>, B1: IntoService<B, BReq>>(a: A1, b: B1, f: F) -> Self {
Self { Self {
f: Cell::new(f), f: Cell::new(f),
a: a.into_service(), a: a.into_service(),
@@ -39,13 +32,9 @@ where
} }
} }
impl<A, B, F, Out> Clone for AndThenApply<A, B, F, Out> impl<A, B, F, Out, AReq, BReq> Clone for AndThenApply<A, B, F, Out, AReq, BReq>
where where
A: Service + Clone, A: Clone,
B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
AndThenApply { AndThenApply {
@@ -57,38 +46,38 @@ where
} }
} }
impl<A, B, F, Out> Service for AndThenApply<A, B, F, Out> impl<A, B, F, Out, AReq, BReq> Service<AReq> for AndThenApply<A, B, F, Out, AReq, BReq>
where where
A: Service, A: Service<AReq>,
B: Service<Error = A::Error>, B: Service<BReq, Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out, F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
type Request = A::Request;
type Response = Out::Item; type Response = Out::Item;
type Error = A::Error; type Error = A::Error;
type Future = AndThenApplyFuture<A, B, F, Out>; type Future = AndThenApplyFuture<A, B, F, Out, AReq, BReq>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.poll_ready()); try_ready!(self.a.poll_ready());
self.b.get_mut().poll_ready() self.b.get_mut().poll_ready()
} }
fn call(&mut self, req: A::Request) -> Self::Future { fn call(&mut self, req: AReq) -> Self::Future {
AndThenApplyFuture { AndThenApplyFuture {
b: self.b.clone(), b: self.b.clone(),
f: self.f.clone(), f: self.f.clone(),
fut_b: None, fut_b: None,
fut_a: Some(self.a.call(req)), fut_a: Some(self.a.call(req)),
_t: PhantomData,
} }
} }
} }
pub struct AndThenApplyFuture<A, B, F, Out> pub struct AndThenApplyFuture<A, B, F, Out, AReq, BReq>
where where
A: Service, A: Service<AReq>,
B: Service<Error = A::Error>, B: Service<BReq, Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out, F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
@@ -97,12 +86,13 @@ where
f: Cell<F>, f: Cell<F>,
fut_a: Option<A::Future>, fut_a: Option<A::Future>,
fut_b: Option<Out::Future>, fut_b: Option<Out::Future>,
_t: PhantomData<(AReq, BReq)>,
} }
impl<A, B, F, Out> Future for AndThenApplyFuture<A, B, F, Out> impl<A, B, F, Out, AReq, BReq> Future for AndThenApplyFuture<A, B, F, Out, AReq, BReq>
where where
A: Service, A: Service<AReq>,
B: Service<Error = A::Error>, B: Service<BReq, Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out, F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
@@ -129,23 +119,23 @@ where
} }
/// `ApplyNewService` new service combinator /// `ApplyNewService` new service combinator
pub struct AndThenApplyNewService<A, B, F, Out, Cfg> { pub struct AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg> {
a: A, a: A,
b: B, b: B,
f: Cell<F>, f: Cell<F>,
r: PhantomData<(Out, Cfg)>, r: PhantomData<(Out, AReq, BReq, Cfg)>,
} }
impl<A, B, F, Out, Cfg> AndThenApplyNewService<A, B, F, Out, Cfg> impl<A, B, F, Out, AReq, BReq, Cfg> AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg>
where where
A: NewService<Cfg>, A: NewService<AReq, Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>, B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new<A1: IntoNewService<A, Cfg>, B1: IntoNewService<B, Cfg>>( pub fn new<A1: IntoNewService<A, AReq, Cfg>, B1: IntoNewService<B, BReq, Cfg>>(
a: A1, a: A1,
b: B1, b: B1,
f: F, f: F,
@@ -159,7 +149,8 @@ where
} }
} }
impl<A, B, F, Out, Cfg> Clone for AndThenApplyNewService<A, B, F, Out, Cfg> impl<A, B, F, Out, AReq, BReq, Cfg> Clone
for AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg>
where where
A: Clone, A: Clone,
B: Clone, B: Clone,
@@ -174,37 +165,37 @@ where
} }
} }
impl<A, B, F, Out, Cfg> NewService<Cfg> for AndThenApplyNewService<A, B, F, Out, Cfg> impl<A, B, F, Out, AReq, BReq, Cfg> NewService<AReq, Cfg>
for AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg>
where where
A: NewService<Cfg>, A: NewService<AReq, Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>, B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
type Request = A::Request;
type Response = Out::Item; type Response = Out::Item;
type Error = A::Error; type Error = A::Error;
type Service = AndThenApply<A::Service, B::Service, F, Out>; type Service = AndThenApply<A::Service, B::Service, F, Out, AReq, BReq>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = AndThenApplyNewServiceFuture<A, B, F, Out, Cfg>; type Future = AndThenApplyNewServiceFuture<A, B, F, Out, AReq, BReq, Cfg>;
fn new_service(&self, cfg: &Cfg) -> Self::Future { fn new_service(&self, cfg: &Cfg) -> Self::Future {
AndThenApplyNewServiceFuture { AndThenApplyNewServiceFuture {
a: None, a: None,
b: None, b: None,
f: self.f.clone(), f: self.f.clone(),
fut_a: self.a.new_service(cfg), fut_a: self.a.new_service(cfg).into_future(),
fut_b: self.b.new_service(cfg), fut_b: self.b.new_service(cfg).into_future(),
} }
} }
} }
pub struct AndThenApplyNewServiceFuture<A, B, F, Out, Cfg> pub struct AndThenApplyNewServiceFuture<A, B, F, Out, AReq, BReq, Cfg>
where where
A: NewService<Cfg>, A: NewService<AReq, Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>, B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
@@ -216,15 +207,16 @@ where
b: Option<B::Service>, b: Option<B::Service>,
} }
impl<A, B, F, Out, Cfg> Future for AndThenApplyNewServiceFuture<A, B, F, Out, Cfg> impl<A, B, F, Out, AReq, BReq, Cfg> Future
for AndThenApplyNewServiceFuture<A, B, F, Out, AReq, BReq, Cfg>
where where
A: NewService<Cfg>, A: NewService<AReq, Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>, B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out, F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
type Item = AndThenApply<A::Service, B::Service, F, Out>; type Item = AndThenApply<A::Service, B::Service, F, Out, AReq, BReq>;
type Error = A::InitError; type Error = A::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@@ -263,8 +255,7 @@ mod tests {
#[derive(Clone)] #[derive(Clone)]
struct Srv; struct Srv;
impl Service for Srv { impl Service<()> for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;

View File

@@ -1,218 +1,165 @@
use futures::{try_ready, Async, Future, IntoFuture, Poll}; use std::marker::PhantomData;
use super::{FnNewTransform, FnTransform}; use futures::{Async, Future, IntoFuture, Poll};
use super::{
IntoNewService, IntoNewTransform, IntoService, IntoTransform, NewService, NewTransform, use super::{IntoNewService, IntoService, NewService, Service};
Service, Transform,
};
/// `Apply` service combinator /// `Apply` service combinator
pub struct Apply<T, S> pub struct Apply<T, R, F, In, Out> {
where service: T,
T: Transform<S>, f: F,
T::Error: From<S::Error>, r: PhantomData<(R, In, Out)>,
S: Service,
{
transform: T,
service: S,
} }
impl<T, S> Apply<T, S> impl<T, R, F, In, Out> Apply<T, R, F, In, Out>
where where
T: Transform<S>, F: FnMut(In, &mut T) -> Out,
T::Error: From<S::Error>,
S: Service,
{ {
/// Create new `Apply` combinator /// Create new `Apply` combinator
pub fn new<T1: IntoTransform<T, S>, S1: IntoService<S>>( pub fn new<I: IntoService<T, R>>(service: I, f: F) -> Self
transform: T1, where
service: S1, T: Service<R>,
) -> Self { Out: IntoFuture,
Out::Error: From<T::Error>,
{
Self { Self {
transform: transform.into_transform(),
service: service.into_service(), service: service.into_service(),
f,
r: PhantomData,
} }
} }
} }
impl<F, S, Req, Out> Apply<FnTransform<F, S, Req, Out>, S> impl<T, R, F, In, Out> Clone for Apply<T, R, F, In, Out>
where where
F: FnMut(Req, &mut S) -> Out, T: Clone,
Out: IntoFuture, F: Clone,
Out::Error: From<S::Error>,
S: Service,
{
/// Create new `Apply` combinator
pub fn new_fn<S1: IntoService<S>>(service: S1, transform: F) -> Self {
Self {
service: service.into_service(),
transform: transform.into_transform(),
}
}
}
impl<T, S> Clone for Apply<T, S>
where
S: Service + Clone,
T::Error: From<S::Error>,
T: Transform<S> + Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Apply { Apply {
service: self.service.clone(), service: self.service.clone(),
transform: self.transform.clone(), f: self.f.clone(),
r: PhantomData,
} }
} }
} }
impl<T, S> Service for Apply<T, S> impl<T, R, F, In, Out> Service<In> for Apply<T, R, F, In, Out>
where where
T: Transform<S>, T: Service<R>,
T::Error: From<S::Error>, F: FnMut(In, &mut T) -> Out,
S: Service, Out: IntoFuture,
Out::Error: From<T::Error>,
{ {
type Request = T::Request; type Response = Out::Item;
type Response = T::Response; type Error = Out::Error;
type Error = T::Error; type Future = Out::Future;
type Future = T::Future;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.service.poll_ready()); self.service.poll_ready().map_err(|e| e.into())
self.transform.poll_ready()
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: In) -> Self::Future {
self.transform.call(req, &mut self.service).into_future() (self.f)(req, &mut self.service).into_future()
} }
} }
/// `ApplyNewService` new service combinator /// `ApplyNewService` new service combinator
pub struct ApplyNewService<T, S, C> pub struct ApplyNewService<T, F, In, Out, Req> {
where service: T,
T: NewTransform<S::Service, C, InitError = S::InitError>, f: F,
T::Error: From<S::Error>, r: PhantomData<(In, Out, Req)>,
S: NewService<C>,
{
transform: T,
service: S,
_t: std::marker::PhantomData<C>,
} }
impl<T, S, C> ApplyNewService<T, S, C> impl<T, F, In, Out, Req> ApplyNewService<T, F, In, Out, Req> {
where
T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>,
S: NewService<C>,
{
/// Create new `ApplyNewService` new service instance /// Create new `ApplyNewService` new service instance
pub fn new<T1: IntoNewTransform<T, S::Service, C>, S1: IntoNewService<S, C>>( pub fn new<Cfg, F1: IntoNewService<T, Req, Cfg>>(service: F1, f: F) -> Self
transform: T1, where
service: S1, T: NewService<Req, Cfg>,
) -> Self { F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
Self { Self {
transform: transform.into_new_transform(), f,
service: service.into_new_service(), service: service.into_new_service(),
_t: std::marker::PhantomData, r: PhantomData,
} }
} }
} }
impl<F, S, In, Out, Cfg> impl<T, F, In, Out, Req> Clone for ApplyNewService<T, F, In, Out, Req>
ApplyNewService<FnNewTransform<F, S::Service, In, Out, S::InitError, Cfg>, S, Cfg>
where where
F: FnMut(In, &mut S::Service) -> Out + Clone, T: Clone,
Out: IntoFuture, F: Clone,
Out::Error: From<S::Error>,
S: NewService<Cfg>,
{
/// Create new `Apply` combinator factory
pub fn new_fn<S1: IntoNewService<S, Cfg>>(service: S1, transform: F) -> Self {
Self {
service: service.into_new_service(),
transform: FnNewTransform::new(transform),
_t: std::marker::PhantomData,
}
}
}
impl<T, S, C> Clone for ApplyNewService<T, S, C>
where
T: NewTransform<S::Service, C, InitError = S::InitError> + Clone,
T::Error: From<S::Error>,
S: NewService<C> + Clone,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
service: self.service.clone(), service: self.service.clone(),
transform: self.transform.clone(), f: self.f.clone(),
_t: std::marker::PhantomData, r: PhantomData,
} }
} }
} }
impl<T, S, C> NewService<C> for ApplyNewService<T, S, C> impl<T, F, In, Out, Req, Cfg> NewService<In, Cfg> for ApplyNewService<T, F, In, Out, Req>
where where
T: NewTransform<S::Service, C, InitError = S::InitError>, T: NewService<Req, Cfg>,
T::Error: From<S::Error>, F: FnMut(In, &mut T::Service) -> Out + Clone,
S: NewService<C>, Out: IntoFuture,
Out::Error: From<T::Error>,
{ {
type Request = T::Request; type Response = Out::Item;
type Response = T::Response; type Error = Out::Error;
type Error = T::Error; type Service = Apply<T::Service, Req, F, In, Out>;
type Service = Apply<T::Transform, S::Service>;
type InitError = T::InitError; type InitError = T::InitError;
type Future = ApplyNewServiceFuture<T, S, C>; type Future = ApplyNewServiceFuture<T, F, In, Out, Req, Cfg>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &Cfg) -> Self::Future {
ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone())
}
}
pub struct ApplyNewServiceFuture<T, F, In, Out, Req, Cfg>
where
T: NewService<Req, Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
fut: T::Future,
f: Option<F>,
r: PhantomData<(In, Out)>,
}
impl<T, F, In, Out, Req, Cfg> ApplyNewServiceFuture<T, F, In, Out, Req, Cfg>
where
T: NewService<Req, Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
fn new(fut: T::Future, f: F) -> Self {
ApplyNewServiceFuture { ApplyNewServiceFuture {
fut_t: self.transform.new_transform(cfg), f: Some(f),
fut_s: self.service.new_service(cfg), fut,
service: None, r: PhantomData,
transform: None,
} }
} }
} }
pub struct ApplyNewServiceFuture<T, S, C> impl<T, F, In, Out, Req, Cfg> Future for ApplyNewServiceFuture<T, F, In, Out, Req, Cfg>
where where
T: NewTransform<S::Service, C, InitError = S::InitError>, T: NewService<Req, Cfg>,
T::Error: From<S::Error>, F: FnMut(In, &mut T::Service) -> Out + Clone,
S: NewService<C>, Out: IntoFuture,
Out::Error: From<T::Error>,
{ {
fut_s: S::Future, type Item = Apply<T::Service, Req, F, In, Out>;
fut_t: T::Future,
service: Option<S::Service>,
transform: Option<T::Transform>,
}
impl<T, S, C> Future for ApplyNewServiceFuture<T, S, C>
where
T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>,
S: NewService<C>,
{
type Item = Apply<T::Transform, S::Service>;
type Error = T::InitError; type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if self.transform.is_none() { if let Async::Ready(service) = self.fut.poll()? {
if let Async::Ready(transform) = self.fut_t.poll()? { Ok(Async::Ready(Apply::new(service, self.f.take().unwrap())))
self.transform = Some(transform);
}
}
if self.service.is_none() {
if let Async::Ready(service) = self.fut_s.poll()? {
self.service = Some(service);
}
}
if self.transform.is_some() && self.service.is_some() {
Ok(Async::Ready(Apply {
service: self.service.take().unwrap(),
transform: self.transform.take().unwrap(),
}))
} else { } else {
Ok(Async::NotReady) Ok(Async::NotReady)
} }
@@ -225,12 +172,11 @@ mod tests {
use futures::{Async, Future, Poll}; use futures::{Async, Future, Poll};
use super::*; use super::*;
use crate::{NewService, Service}; use crate::{IntoService, NewService, Service, ServiceExt};
#[derive(Clone)] #[derive(Clone)]
struct Srv; struct Srv;
impl Service for Srv { impl Service<()> for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;
@@ -245,10 +191,14 @@ mod tests {
} }
#[test] #[test]
fn test_apply() { fn test_call() {
let mut srv = Apply::new_fn(Srv, |req: &'static str, srv| { let blank = |req| Ok(req);
srv.call(()).map(move |res| (req, res))
}); let mut srv = blank
.into_service()
.apply_fn(Srv, |req: &'static str, srv| {
srv.call(()).map(move |res| (req, res))
});
assert!(srv.poll_ready().is_ok()); assert!(srv.poll_ready().is_ok());
let res = srv.call("srv").poll(); let res = srv.call("srv").poll();
assert!(res.is_ok()); assert!(res.is_ok());
@@ -258,22 +208,6 @@ mod tests {
#[test] #[test]
fn test_new_service() { fn test_new_service() {
let new_srv = ApplyNewService::new( let new_srv = ApplyNewService::new(
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|| Ok::<_, ()>(Srv),
);
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
assert!(srv.poll_ready().is_ok());
let res = srv.call("srv").poll();
assert!(res.is_ok());
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
} else {
panic!()
}
}
#[test]
fn test_new_service_fn() {
let new_srv = ApplyNewService::new_fn(
|| Ok::<_, ()>(Srv), || Ok::<_, ()>(Srv),
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)), |req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
); );

View File

@@ -30,8 +30,7 @@ impl<R, E> Default for Blank<R, E> {
} }
} }
impl<R, E> Service for Blank<R, E> { impl<R, E> Service<R> for Blank<R, E> {
type Request = R;
type Response = R; type Response = R;
type Error = E; type Error = E;
type Future = FutureResult<R, E>; type Future = FutureResult<R, E>;
@@ -68,8 +67,7 @@ impl<R, E1, E2> Default for BlankNewService<R, E1, E2> {
} }
} }
impl<R, E1, E2> NewService<()> for BlankNewService<R, E1, E2> { impl<R, E1, E2> NewService<R, ()> for BlankNewService<R, E1, E2> {
type Request = R;
type Response = R; type Response = R;
type Error = E1; type Error = E1;
type Service = Blank<R, E1>; type Service = Blank<R, E1>;

View File

@@ -1,48 +1,49 @@
use std::marker::PhantomData;
use crate::{NewService, Service}; use crate::{NewService, Service};
use futures::{Future, Poll}; use futures::{Future, IntoFuture, Poll};
pub type BoxedService<Req, Res, Err> = Box< pub type BoxedService<Req, Res, Err> = Box<
Service< Service<Req, Response = Res, Error = Err, Future = Box<Future<Item = Res, Error = Err>>>,
Request = Req,
Response = Res,
Error = Err,
Future = Box<Future<Item = Res, Error = Err>>,
>,
>; >;
/// Create boxed new service /// Create boxed new service
pub fn new_service<T, C>( pub fn new_service<T, R, C>(
service: T, service: T,
) -> BoxedNewService<C, T::Request, T::Response, T::Error, T::InitError> ) -> BoxedNewService<C, R, T::Response, T::Error, T::InitError>
where where
C: 'static, C: 'static,
T: NewService<C> + 'static, T: NewService<R, C> + 'static,
T::Request: 'static,
T::Response: 'static, T::Response: 'static,
T::Service: 'static, T::Service: 'static,
T::Future: 'static, T::Future: 'static,
T::Error: 'static, T::Error: 'static,
T::InitError: 'static, T::InitError: 'static,
R: 'static,
{ {
BoxedNewService(Box::new(NewServiceWrapper { BoxedNewService(Box::new(NewServiceWrapper {
service, service,
_t: std::marker::PhantomData, _t: PhantomData,
})) }))
} }
/// Create boxed service /// Create boxed service
pub fn service<T>(service: T) -> BoxedService<T::Request, T::Response, T::Error> pub fn service<T, R>(service: T) -> BoxedService<R, T::Response, T::Error>
where where
T: Service + 'static, T: Service<R> + 'static,
T::Future: 'static, T::Future: 'static,
R: 'static,
{ {
Box::new(ServiceWrapper(service)) Box::new(ServiceWrapper {
service,
_t: PhantomData,
})
} }
type Inner<C, Req, Res, Err, InitErr> = Box< type Inner<C, Req, Res, Err, InitErr> = Box<
NewService< NewService<
Req,
C, C,
Request = Req,
Response = Res, Response = Res,
Error = Err, Error = Err,
InitError = InitErr, InitError = InitErr,
@@ -53,14 +54,14 @@ type Inner<C, Req, Res, Err, InitErr> = Box<
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>); pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
impl<C, Req, Res, Err, InitErr> NewService<C> for BoxedNewService<C, Req, Res, Err, InitErr> impl<C, Req, Res, Err, InitErr> NewService<Req, C>
for BoxedNewService<C, Req, Res, Err, InitErr>
where where
Req: 'static, Req: 'static,
Res: 'static, Res: 'static,
Err: 'static, Err: 'static,
InitErr: 'static, InitErr: 'static,
{ {
type Request = Req;
type Response = Res; type Response = Res;
type Error = Err; type Error = Err;
type InitError = InitErr; type InitError = InitErr;
@@ -72,23 +73,22 @@ where
} }
} }
struct NewServiceWrapper<C, T: NewService<C>> { struct NewServiceWrapper<T: NewService<R, C>, R, C> {
service: T, service: T,
_t: std::marker::PhantomData<C>, _t: std::marker::PhantomData<(R, C)>,
} }
impl<C, T, Req, Res, Err, InitErr> NewService<C> for NewServiceWrapper<C, T> impl<C, T, Req, Res, Err, InitErr> NewService<Req, C> for NewServiceWrapper<T, Req, C>
where where
Req: 'static, Req: 'static,
Res: 'static, Res: 'static,
Err: 'static, Err: 'static,
InitErr: 'static, InitErr: 'static,
T: NewService<C, Request = Req, Response = Res, Error = Err, InitError = InitErr>, T: NewService<Req, C, Response = Res, Error = Err, InitError = InitErr>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
<T::Service as Service>::Future: 'static, <T::Service as Service<Req>>::Future: 'static,
{ {
type Request = Req;
type Response = Res; type Response = Res;
type Error = Err; type Error = Err;
type InitError = InitErr; type InitError = InitErr;
@@ -99,38 +99,46 @@ where
Box::new( Box::new(
self.service self.service
.new_service(cfg) .new_service(cfg)
.map(|service| ServiceWrapper::boxed(service)), .into_future()
.map(ServiceWrapper::boxed),
) )
} }
} }
struct ServiceWrapper<T: Service>(T); struct ServiceWrapper<T: Service<R>, R> {
service: T,
_t: PhantomData<R>,
}
impl<T> ServiceWrapper<T> impl<T, R> ServiceWrapper<T, R>
where where
T: Service + 'static, T: Service<R> + 'static,
T::Future: 'static, T::Future: 'static,
R: 'static,
{ {
fn boxed(service: T) -> BoxedService<T::Request, T::Response, T::Error> { fn boxed(service: T) -> BoxedService<R, T::Response, T::Error> {
Box::new(ServiceWrapper(service)) Box::new(ServiceWrapper {
service,
_t: PhantomData,
})
} }
} }
impl<T, Req, Res, Err> Service for ServiceWrapper<T> impl<T, Req, Res, Err> Service<Req> for ServiceWrapper<T, Req>
where where
T: Service<Request = Req, Response = Res, Error = Err>, T: Service<Req, Response = Res, Error = Err>,
T::Future: 'static, T::Future: 'static,
Req: 'static,
{ {
type Request = Req;
type Response = Res; type Response = Res;
type Error = Err; type Error = Err;
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>; type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.0.poll_ready() self.service.poll_ready()
} }
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: Req) -> Self::Future {
Box::new(self.0.call(req)) Box::new(self.service.call(req))
} }
} }

View File

@@ -15,21 +15,21 @@ where
} }
/// Create `NewService` for function that can produce services /// Create `NewService` for function that can produce services
pub fn fn_factory<F, R, S, E>(f: F) -> FnNewServiceNoConfig<F, R, S, E> pub fn fn_factory<F, R, S, E, Req>(f: F) -> FnNewServiceNoConfig<F, R, S, E, Req>
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
FnNewServiceNoConfig::new(f) FnNewServiceNoConfig::new(f)
} }
/// Create `NewService` for function that can produce services with configuration /// Create `NewService` for function that can produce services with configuration
pub fn fn_cfg_factory<F, C, R, S, E>(f: F) -> FnNewServiceConfig<F, C, R, S, E> pub fn fn_cfg_factory<F, C, R, S, E, Req>(f: F) -> FnNewServiceConfig<F, C, R, S, E, Req>
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
FnNewServiceConfig::new(f) FnNewServiceConfig::new(f)
} }
@@ -66,12 +66,11 @@ where
} }
} }
impl<F, Req, Out> Service for FnService<F, Req, Out> impl<F, Req, Out> Service<Req> for FnService<F, Req, Out>
where where
F: FnMut(Req) -> Out, F: FnMut(Req) -> Out,
Out: IntoFuture, Out: IntoFuture,
{ {
type Request = Req;
type Response = Out::Item; type Response = Out::Item;
type Error = Out::Error; type Error = Out::Error;
type Future = Out::Future; type Future = Out::Future;
@@ -85,7 +84,7 @@ where
} }
} }
impl<F, Req, Out> IntoService<FnService<F, Req, Out>> for F impl<F, Req, Out> IntoService<FnService<F, Req, Out>, Req> for F
where where
F: FnMut(Req) -> Out + 'static, F: FnMut(Req) -> Out + 'static,
Out: IntoFuture, Out: IntoFuture,
@@ -114,12 +113,11 @@ where
} }
} }
impl<F, Req, Out, Cfg> NewService<Cfg> for FnNewService<F, Req, Out, Cfg> impl<F, Req, Out, Cfg> NewService<Req, Cfg> for FnNewService<F, Req, Out, Cfg>
where where
F: FnMut(Req) -> Out + Clone, F: FnMut(Req) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
type Request = Req;
type Response = Out::Item; type Response = Out::Item;
type Error = Out::Error; type Error = Out::Error;
type Service = FnService<F, Req, Out>; type Service = FnService<F, Req, Out>;
@@ -142,7 +140,7 @@ where
} }
} }
impl<F, Req, Out, Cfg> IntoNewService<FnNewService<F, Req, Out, Cfg>, Cfg> for F impl<F, Req, Out, Cfg> IntoNewService<FnNewService<F, Req, Out, Cfg>, Req, Cfg> for F
where where
F: Fn(Req) -> Out + Clone, F: Fn(Req) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
@@ -153,33 +151,33 @@ where
} }
/// Converter for `Fn() -> Future<Service>` fn /// Converter for `Fn() -> Future<Service>` fn
pub struct FnNewServiceNoConfig<F, R, S, E> pub struct FnNewServiceNoConfig<F, R, S, E, Req>
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
f: F, f: F,
_t: PhantomData<Req>,
} }
impl<F, R, S, E> FnNewServiceNoConfig<F, R, S, E> impl<F, R, S, E, Req> FnNewServiceNoConfig<F, R, S, E, Req>
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
pub fn new(f: F) -> Self { pub fn new(f: F) -> Self {
FnNewServiceNoConfig { f } FnNewServiceNoConfig { f, _t: PhantomData }
} }
} }
impl<F, R, S, E> NewService<()> for FnNewServiceNoConfig<F, R, S, E> impl<F, R, S, E, Req> NewService<Req, ()> for FnNewServiceNoConfig<F, R, S, E, Req>
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Service = S; type Service = S;
@@ -192,57 +190,56 @@ where
} }
} }
impl<F, R, S, E> Clone for FnNewServiceNoConfig<F, R, S, E> impl<F, R, S, E, Req> Clone for FnNewServiceNoConfig<F, R, S, E, Req>
where where
F: Fn() -> R + Clone, F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self::new(self.f.clone()) Self::new(self.f.clone())
} }
} }
impl<F, R, S, E> IntoNewService<FnNewServiceNoConfig<F, R, S, E>, ()> for F impl<F, R, S, E, Req> IntoNewService<FnNewServiceNoConfig<F, R, S, E, Req>, Req, ()> for F
where where
F: Fn() -> R, F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
fn into_new_service(self) -> FnNewServiceNoConfig<F, R, S, E> { fn into_new_service(self) -> FnNewServiceNoConfig<F, R, S, E, Req> {
FnNewServiceNoConfig::new(self) FnNewServiceNoConfig::new(self)
} }
} }
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService /// Convert `Fn(&Config) -> Future<Service>` fn to NewService
pub struct FnNewServiceConfig<F, C, R, S, E> pub struct FnNewServiceConfig<F, C, R, S, E, Req>
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
f: F, f: F,
_t: PhantomData<(C, R, S, E)>, _t: PhantomData<(C, R, S, E, Req)>,
} }
impl<F, C, R, S, E> FnNewServiceConfig<F, C, R, S, E> impl<F, C, R, S, E, Req> FnNewServiceConfig<F, C, R, S, E, Req>
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
pub fn new(f: F) -> Self { pub fn new(f: F) -> Self {
FnNewServiceConfig { f, _t: PhantomData } FnNewServiceConfig { f, _t: PhantomData }
} }
} }
impl<F, C, R, S, E> NewService<C> for FnNewServiceConfig<F, C, R, S, E> impl<F, C, R, S, E, Req> NewService<Req, C> for FnNewServiceConfig<F, C, R, S, E, Req>
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Service = S; type Service = S;
@@ -255,24 +252,42 @@ where
} }
} }
impl<F, C, R, S, E> Clone for FnNewServiceConfig<F, C, R, S, E> impl<F, C, R, S, E, Req> Clone for FnNewServiceConfig<F, C, R, S, E, Req>
where where
F: Fn(&C) -> R + Clone, F: Fn(&C) -> R + Clone,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self::new(self.f.clone()) Self::new(self.f.clone())
} }
} }
impl<F, C, R, S, E> IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E>, C> for F impl<F, C, R, S, E, Req>
IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E, Req>, Req, C> for F
where where
F: Fn(&C) -> R, F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>, R: IntoFuture<Item = S, Error = E>,
S: Service, S: Service<Req>,
{ {
fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E> { fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E, Req> {
FnNewServiceConfig::new(self) FnNewServiceConfig::new(self)
} }
} }
#[cfg(test)]
mod tests {
use crate::{IntoService, Service, ServiceExt};
#[test]
fn test_fn_service() {
let mut rt = actix_rt::Runtime::new().unwrap();
let srv = (|_t: &str| -> Result<usize, ()> { Ok(1) }).into_service();
let mut srv = srv.and_then(|test: usize| Ok(test));
let s = "HELLO".to_owned();
let res = rt.block_on(srv.call(&s)).unwrap();
assert_eq!(res, 1);
}
}

View File

@@ -1,122 +1,63 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use futures::future::{ok, FutureResult}; use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll}; use futures::IntoFuture;
use crate::{IntoNewTransform, IntoTransform, NewTransform, Transform}; use crate::{Apply, IntoTransform, Service, Transform};
pub struct FnTransform<F, S, Req, Res> pub struct FnTransform<F, S, R, In, Out, Err>
where where
F: FnMut(Req, &mut S) -> Res, F: FnMut(In, &mut S) -> Out + Clone,
Res: IntoFuture, Out: IntoFuture,
{ {
f: F, f: F,
_t: PhantomData<(S, Req, Res)>, _t: PhantomData<(S, R, In, Out, Err)>,
} }
impl<F, S, Req, Res> FnTransform<F, S, Req, Res> impl<F, S, R, In, Out, Err> FnTransform<F, S, R, In, Out, Err>
where where
F: FnMut(Req, &mut S) -> Res, F: FnMut(In, &mut S) -> Out + Clone,
Res: IntoFuture, Out: IntoFuture,
{ {
pub fn new(f: F) -> Self { pub fn new(f: F) -> Self {
FnTransform { f, _t: PhantomData } FnTransform { f, _t: PhantomData }
} }
} }
impl<F, S, Req, Res> Clone for FnTransform<F, S, Req, Res> impl<F, S, R, In, Out, Err> Transform<S, In> for FnTransform<F, S, R, In, Out, Err>
where where
F: FnMut(Req, &mut S) -> Res + Clone, S: Service<R>,
Res: IntoFuture, F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<S::Error>,
{ {
fn clone(&self) -> Self { type Response = Out::Item;
FnTransform { type Error = Out::Error;
f: self.f.clone(), type Transform = Apply<S, R, F, In, Out>;
_t: PhantomData, type InitError = Err;
} type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, service: S) -> Self::Future {
ok(Apply::new(service, self.f.clone()))
} }
} }
impl<F, S, Req, Res> Transform<S> for FnTransform<F, S, Req, Res> impl<F, S, R, In, Out, Err> IntoTransform<FnTransform<F, S, R, In, Out, Err>, S, In> for F
where where
F: FnMut(Req, &mut S) -> Res, S: Service<R>,
Res: IntoFuture, F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<S::Error>,
{ {
type Request = Req; fn into_transform(self) -> FnTransform<F, S, R, In, Out, Err> {
type Response = Res::Item;
type Error = Res::Error;
type Future = Res::Future;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, request: Req, service: &mut S) -> Self::Future {
(self.f)(request, service).into_future()
}
}
impl<F, S, Req, Res> IntoTransform<FnTransform<F, S, Req, Res>, S> for F
where
F: FnMut(Req, &mut S) -> Res,
Res: IntoFuture,
{
fn into_transform(self) -> FnTransform<F, S, Req, Res> {
FnTransform::new(self) FnTransform::new(self)
} }
} }
pub struct FnNewTransform<F, S, Req, Out, Err, Cfg> impl<F, S, R, In, Out, Err> Clone for FnTransform<F, S, R, In, Out, Err>
where where
F: FnMut(Req, &mut S) -> Out + Clone, F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{
f: F,
_t: PhantomData<(S, Req, Out, Err, Cfg)>,
}
impl<F, S, Req, Res, Err, Cfg> FnNewTransform<F, S, Req, Res, Err, Cfg>
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
pub fn new(f: F) -> Self {
FnNewTransform { f, _t: PhantomData }
}
}
impl<F, S, Req, Res, Err, Cfg> NewTransform<S, Cfg> for FnNewTransform<F, S, Req, Res, Err, Cfg>
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
type Request = Req;
type Response = Res::Item;
type Error = Res::Error;
type Transform = FnTransform<F, S, Req, Res>;
type InitError = Err;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, _: &Cfg) -> Self::Future {
ok(FnTransform::new(self.f.clone()))
}
}
impl<F, S, Req, Res, Err, Cfg>
IntoNewTransform<FnNewTransform<F, S, Req, Res, Err, Cfg>, S, Cfg> for F
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
fn into_new_transform(self) -> FnNewTransform<F, S, Req, Res, Err, Cfg> {
FnNewTransform::new(self)
}
}
impl<F, S, Req, Res, Err, Cfg> Clone for FnNewTransform<F, S, Req, Res, Err, Cfg>
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self::new(self.f.clone()) Self::new(self.f.clone())

View File

@@ -13,9 +13,9 @@ pub struct FromErr<A, E> {
} }
impl<A, E> FromErr<A, E> { impl<A, E> FromErr<A, E> {
pub(crate) fn new(service: A) -> Self pub(crate) fn new<R>(service: A) -> Self
where where
A: Service, A: Service<R>,
E: From<A::Error>, E: From<A::Error>,
{ {
FromErr { FromErr {
@@ -37,21 +37,20 @@ where
} }
} }
impl<A, E> Service for FromErr<A, E> impl<A, E, R> Service<R> for FromErr<A, E>
where where
A: Service, A: Service<R>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Future = FromErrFuture<A, E>; type Future = FromErrFuture<A, R, E>;
fn poll_ready(&mut self) -> Poll<(), E> { fn poll_ready(&mut self) -> Poll<(), E> {
self.service.poll_ready().map_err(E::from) self.service.poll_ready().map_err(E::from)
} }
fn call(&mut self, req: A::Request) -> Self::Future { fn call(&mut self, req: R) -> Self::Future {
FromErrFuture { FromErrFuture {
fut: self.service.call(req), fut: self.service.call(req),
f: PhantomData, f: PhantomData,
@@ -59,14 +58,14 @@ where
} }
} }
pub struct FromErrFuture<A: Service, E> { pub struct FromErrFuture<A: Service<R>, R, E> {
fut: A::Future, fut: A::Future,
f: PhantomData<E>, f: PhantomData<E>,
} }
impl<A, E> Future for FromErrFuture<A, E> impl<A, R, E> Future for FromErrFuture<A, R, E>
where where
A: Service, A: Service<R>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Item = A::Response; type Item = A::Response;
@@ -88,9 +87,9 @@ pub struct FromErrNewService<A, E, C> {
impl<A, E, C> FromErrNewService<A, E, C> { impl<A, E, C> FromErrNewService<A, E, C> {
/// Create new `FromErr` new service instance /// Create new `FromErr` new service instance
pub fn new(a: A) -> Self pub fn new<R>(a: A) -> Self
where where
A: NewService<C>, A: NewService<R, C>,
E: From<A::Error>, E: From<A::Error>,
{ {
Self { a, e: PhantomData } Self { a, e: PhantomData }
@@ -109,18 +108,17 @@ where
} }
} }
impl<A, E, C> NewService<C> for FromErrNewService<A, E, C> impl<A, E, C, R> NewService<R, C> for FromErrNewService<A, E, C>
where where
A: NewService<C>, A: NewService<R, C>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Service = FromErr<A::Service, E>; type Service = FromErr<A::Service, E>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = FromErrNewServiceFuture<A, E, C>; type Future = FromErrNewServiceFuture<A, E, C, R>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
FromErrNewServiceFuture { FromErrNewServiceFuture {
@@ -130,18 +128,18 @@ where
} }
} }
pub struct FromErrNewServiceFuture<A, E, C> pub struct FromErrNewServiceFuture<A, E, C, R>
where where
A: NewService<C>, A: NewService<R, C>,
E: From<A::Error>, E: From<A::Error>,
{ {
fut: A::Future, fut: A::Future,
e: PhantomData<E>, e: PhantomData<E>,
} }
impl<A, E, C> Future for FromErrNewServiceFuture<A, E, C> impl<A, E, C, R> Future for FromErrNewServiceFuture<A, E, C, R>
where where
A: NewService<C>, A: NewService<R, C>,
E: From<A::Error>, E: From<A::Error>,
{ {
type Item = FromErr<A::Service, E>; type Item = FromErr<A::Service, E>;
@@ -164,8 +162,7 @@ mod tests {
use crate::{IntoNewService, NewService, Service, ServiceExt}; use crate::{IntoNewService, NewService, Service, ServiceExt};
struct Srv; struct Srv;
impl Service for Srv { impl Service<()> for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;

View File

@@ -20,27 +20,25 @@ mod map_err;
mod map_init_err; mod map_init_err;
mod then; mod then;
mod transform; mod transform;
mod transform_map_err;
mod transform_map_init_err; mod transform_map_init_err;
pub use self::and_then::{AndThen, AndThenNewService}; pub use self::and_then::{AndThen, AndThenNewService};
use self::and_then_apply::{AndThenTransform, AndThenTransformNewService}; use self::and_then_apply::AndThenTransform;
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService}; use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
pub use self::apply::{Apply, ApplyNewService}; pub use self::apply::{Apply, ApplyNewService};
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService}; pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
pub use self::fn_transform::{FnNewTransform, FnTransform}; pub use self::fn_transform::FnTransform;
pub use self::from_err::{FromErr, FromErrNewService}; pub use self::from_err::{FromErr, FromErrNewService};
pub use self::map::{Map, MapNewService}; pub use self::map::{Map, MapNewService};
pub use self::map_err::{MapErr, MapErrNewService}; pub use self::map_err::{MapErr, MapErrNewService};
pub use self::map_init_err::MapInitErr; pub use self::map_init_err::MapInitErr;
pub use self::then::{Then, ThenNewService}; pub use self::then::{Then, ThenNewService};
pub use self::transform::{IntoNewTransform, IntoTransform, NewTransform, Transform}; pub use self::transform::{ApplyTransform, IntoTransform, Transform};
/// An asynchronous function from `Request` to a `Response`. /// An asynchronous function from `Request` to a `Response`.
pub trait Service { ///
/// Requests handled by the service. /// `Request` - requests handled by the service.
type Request; pub trait Service<Request> {
/// Responses given by the service. /// Responses given by the service.
type Response; type Response;
@@ -70,36 +68,26 @@ pub trait Service {
/// ///
/// Calling `call` without calling `poll_ready` is permitted. The /// Calling `call` without calling `poll_ready` is permitted. The
/// implementation must be resilient to this fact. /// implementation must be resilient to this fact.
fn call(&mut self, req: Self::Request) -> Self::Future; fn call(&mut self, req: Request) -> Self::Future;
} }
/// An extension trait for `Service`s that provides a variety of convenient /// An extension trait for `Service`s that provides a variety of convenient
/// adapters /// adapters
pub trait ServiceExt: Service { pub trait ServiceExt<Request>: Service<Request> {
/// Apply tranformation to specified service and use it as a next service in
/// chain.
fn apply<T, T1, B, B1>(self, transform: T1, service: B1) -> AndThenTransform<T, Self, B>
where
Self: Sized,
T: Transform<B, Request = Self::Response>,
T::Error: From<Self::Error>,
T1: IntoTransform<T, B>,
B: Service<Error = Self::Error>,
B1: IntoService<B>,
{
AndThenTransform::new(transform.into_transform(), self, service.into_service())
}
/// Apply function to specified service and use it as a next service in /// Apply function to specified service and use it as a next service in
/// chain. /// chain.
fn apply_fn<F, B, B1, Out>(self, service: B1, f: F) -> AndThenApply<Self, B, F, Out> fn apply_fn<F, B, B1, Out, Req>(
self,
service: B1,
f: F,
) -> AndThenApply<Self, B, F, Out, Request, Req>
where where
Self: Sized, Self: Sized,
F: FnMut(Self::Response, &mut B) -> Out, F: FnMut(Self::Response, &mut B) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<Self::Error>, Out::Error: Into<Self::Error>,
B: Service<Error = Self::Error>, B: Service<Req, Error = Self::Error>,
B1: IntoService<B>, B1: IntoService<B, Req>,
{ {
AndThenApply::new(self, service, f) AndThenApply::new(self, service, f)
} }
@@ -116,8 +104,8 @@ 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: IntoService<B, Self::Response>,
B: Service<Request = Self::Response, Error = Self::Error>, B: Service<Self::Response, Error = Self::Error>,
{ {
AndThen::new(self, service.into_service()) AndThen::new(self, service.into_service())
} }
@@ -143,7 +131,7 @@ pub trait ServiceExt: Service {
fn then<B>(self, service: B) -> Then<Self, B> fn then<B>(self, service: B) -> Then<Self, B>
where where
Self: Sized, Self: Sized,
B: Service<Request = Result<Self::Response, Self::Error>, Error = Self::Error>, B: Service<Result<Self::Response, Self::Error>, Error = Self::Error>,
{ {
Then::new(self, service) Then::new(self, service)
} }
@@ -182,7 +170,7 @@ pub trait ServiceExt: Service {
} }
} }
impl<T: ?Sized> ServiceExt for T where T: Service {} impl<T: ?Sized, R> ServiceExt<R> for T where T: Service<R> {}
/// Creates new `Service` values. /// Creates new `Service` values.
/// ///
@@ -192,11 +180,9 @@ impl<T: ?Sized> ServiceExt for T where T: Service {}
/// `NewService` trait, and uses that new `Service` value to process inbound /// `NewService` trait, and uses that new `Service` value to process inbound
/// requests on that new TCP stream. /// requests on that new TCP stream.
/// ///
/// `Config` is a service factory configuration type. /// * `Request` - requests handled by the service.
pub trait NewService<Config = ()> { /// * `Config` - is a service factory configuration type.
/// Requests handled by the service. pub trait NewService<Request, Config = ()> {
type Request;
/// Responses given by the service /// Responses given by the service
type Response; type Response;
@@ -204,11 +190,7 @@ pub trait NewService<Config = ()> {
type Error; type Error;
/// The `Service` value created by this factory /// The `Service` value created by this factory
type Service: Service< type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
Request = Self::Request,
Response = Self::Response,
Error = Self::Error,
>;
/// Errors produced while building a service. /// Errors produced while building a service.
type InitError; type InitError;
@@ -221,37 +203,33 @@ pub trait NewService<Config = ()> {
/// Apply function to specified service and use it as a next service in /// Apply function to specified service and use it as a next service in
/// chain. /// chain.
fn apply<T, T1, B, B1>( fn apply<T, T1, B, B1, Req>(
self, self,
transform: T1, transform: T1,
service: B1, service: B1,
) -> AndThenTransformNewService<T, Self, B, Config> ) -> AndThenTransform<T, Self, B, Req>
where where
Self: Sized, Self: Sized,
T: NewTransform<B::Service, Request = Self::Response, InitError = Self::InitError>, T: Transform<B::Service, Self::Response, InitError = Self::InitError>,
T::Error: From<Self::Error>, T::Error: From<Self::Error>,
T1: IntoNewTransform<T, B::Service>, T1: IntoTransform<T, B::Service, Self::Response>,
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>, B: NewService<Req, Config, InitError = Self::InitError>,
B1: IntoNewService<B, Config>, B1: IntoNewService<B, Req, Config>,
{ {
AndThenTransformNewService::new( AndThenTransform::new(transform.into_transform(), self, service.into_new_service())
transform.into_new_transform(),
self,
service.into_new_service(),
)
} }
/// Apply function to specified service and use it as a next service in /// Apply function to specified service and use it as a next service in
/// chain. /// chain.
fn apply_fn<B, I, F, Out>( fn apply_fn<B, I, F, Out, Req>(
self, self,
service: I, service: I,
f: F, f: F,
) -> AndThenApplyNewService<Self, B, F, Out, Config> ) -> AndThenApplyNewService<Self, B, F, Out, Request, Req, Config>
where where
Self: Sized, Self: Sized,
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>, B: NewService<Req, Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Config>, I: IntoNewService<B, Req, Config>,
F: FnMut(Self::Response, &mut B::Service) -> Out, F: FnMut(Self::Response, &mut B::Service) -> Out,
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<Self::Error>, Out::Error: Into<Self::Error>,
@@ -260,16 +238,11 @@ pub trait NewService<Config = ()> {
} }
/// Call another service after call to this one has resolved successfully. /// Call another service after call to this one has resolved successfully.
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B, Config> fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
where where
Self: Sized, Self: Sized,
F: IntoNewService<B, Config>, F: IntoNewService<B, Self::Response, Config>,
B: NewService< B: NewService<Self::Response, Config, Error = Self::Error, InitError = Self::InitError>,
Config,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
>,
{ {
AndThenNewService::new(self, new_service) AndThenNewService::new(self, new_service)
} }
@@ -297,15 +270,15 @@ pub trait NewService<Config = ()> {
fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B, Config> fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B, Config>
where where
Self: Sized, Self: Sized,
F: IntoNewService<B, Config>, F: IntoNewService<B, Result<Self::Response, Self::Error>, Config>,
B: NewService< B: NewService<
Result<Self::Response, Self::Error>,
Config, Config,
Request = Result<Self::Response, Self::Error>,
Error = Self::Error, Error = Self::Error,
InitError = Self::InitError, InitError = Self::InitError,
>, >,
{ {
ThenNewService::new(self, new_service) ThenNewService::new(self, new_service.into_new_service())
} }
/// Map this service's output to a different type, returning a new service /// Map this service's output to a different type, returning a new service
@@ -337,11 +310,10 @@ pub trait NewService<Config = ()> {
} }
} }
impl<'a, S> Service for &'a mut S impl<'a, S, R> Service<R> for &'a mut S
where where
S: Service + 'a, S: Service<R> + 'a,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Future = S::Future; type Future = S::Future;
@@ -350,16 +322,15 @@ where
(**self).poll_ready() (**self).poll_ready()
} }
fn call(&mut self, request: Self::Request) -> S::Future { fn call(&mut self, request: R) -> S::Future {
(**self).call(request) (**self).call(request)
} }
} }
impl<S> Service for Box<S> impl<S, R> Service<R> for Box<S>
where where
S: Service + ?Sized, S: Service<R> + ?Sized,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Future = S::Future; type Future = S::Future;
@@ -368,16 +339,15 @@ where
(**self).poll_ready() (**self).poll_ready()
} }
fn call(&mut self, request: Self::Request) -> S::Future { fn call(&mut self, request: R) -> S::Future {
(**self).call(request) (**self).call(request)
} }
} }
impl<S, C> NewService<C> for Rc<S> impl<S, R, C> NewService<R, C> for Rc<S>
where where
S: NewService<C>, S: NewService<R, C>,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Service = S::Service; type Service = S::Service;
@@ -389,11 +359,10 @@ where
} }
} }
impl<S, C> NewService<C> for Arc<S> impl<S, R, C> NewService<R, C> for Arc<S>
where where
S: NewService<C>, S: NewService<R, C>,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Service = S::Service; type Service = S::Service;
@@ -406,35 +375,35 @@ where
} }
/// Trait for types that can be converted to a `Service` /// Trait for types that can be converted to a `Service`
pub trait IntoService<T> pub trait IntoService<T, R>
where where
T: Service, T: Service<R>,
{ {
/// Convert to a `Service` /// Convert to a `Service`
fn into_service(self) -> T; fn into_service(self) -> T;
} }
/// Trait for types that can be converted to a `NewService` /// Trait for types that can be converted to a `NewService`
pub trait IntoNewService<T, C = ()> pub trait IntoNewService<T, R, C = ()>
where where
T: NewService<C>, T: NewService<R, C>,
{ {
/// Convert to an `NewService` /// Convert to an `NewService`
fn into_new_service(self) -> T; fn into_new_service(self) -> T;
} }
impl<T> IntoService<T> for T impl<T, R> IntoService<T, R> for T
where where
T: Service, T: Service<R>,
{ {
fn into_service(self) -> T { fn into_service(self) -> T {
self self
} }
} }
impl<T, C> IntoNewService<T, C> for T impl<T, R, C> IntoNewService<T, R, C> for T
where where
T: NewService<C>, T: NewService<R, C>,
{ {
fn into_new_service(self) -> T { fn into_new_service(self) -> T {
self self
@@ -442,17 +411,17 @@ where
} }
/// Trait for types that can be converted to a configurable `NewService` /// Trait for types that can be converted to a configurable `NewService`
pub trait IntoConfigurableNewService<T, C> pub trait IntoConfigurableNewService<T, R, C>
where where
T: NewService<C>, T: NewService<R, C>,
{ {
/// Convert to an `NewService` /// Convert to an `NewService`
fn into_new_service(self) -> T; fn into_new_service(self) -> T;
} }
impl<T, C> IntoConfigurableNewService<T, C> for T impl<T, R, C> IntoConfigurableNewService<T, R, C> for T
where where
T: NewService<C>, T: NewService<R, C>,
{ {
fn into_new_service(self) -> T { fn into_new_service(self) -> T {
self self

View File

@@ -15,9 +15,9 @@ pub struct Map<A, F, Response> {
impl<A, F, Response> Map<A, F, Response> { impl<A, F, Response> Map<A, F, Response> {
/// Create new `Map` combinator /// Create new `Map` combinator
pub fn new(service: A, f: F) -> Self pub fn new<R>(service: A, f: F) -> Self
where where
A: Service, A: Service<R>,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Response,
{ {
Self { Self {
@@ -42,37 +42,36 @@ where
} }
} }
impl<A, F, Response> Service for Map<A, F, Response> impl<A, F, R, Response> Service<R> for Map<A, F, Response>
where where
A: Service, A: Service<R>,
F: FnMut(A::Response) -> Response + Clone, F: FnMut(A::Response) -> Response + Clone,
{ {
type Request = A::Request;
type Response = Response; type Response = Response;
type Error = A::Error; type Error = A::Error;
type Future = MapFuture<A, F, Response>; type Future = MapFuture<A, F, R, Response>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready() self.service.poll_ready()
} }
fn call(&mut self, req: A::Request) -> Self::Future { fn call(&mut self, req: R) -> Self::Future {
MapFuture::new(self.service.call(req), self.f.clone()) MapFuture::new(self.service.call(req), self.f.clone())
} }
} }
pub struct MapFuture<A, F, Response> pub struct MapFuture<A, F, R, Response>
where where
A: Service, A: Service<R>,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Response,
{ {
f: F, f: F,
fut: A::Future, fut: A::Future,
} }
impl<A, F, Response> MapFuture<A, F, Response> impl<A, F, R, Response> MapFuture<A, F, R, Response>
where where
A: Service, A: Service<R>,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Response,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@@ -80,9 +79,9 @@ where
} }
} }
impl<A, F, Response> Future for MapFuture<A, F, Response> impl<A, F, R, Response> Future for MapFuture<A, F, R, Response>
where where
A: Service, A: Service<R>,
F: FnMut(A::Response) -> Response, F: FnMut(A::Response) -> Response,
{ {
type Item = Response; type Item = Response;
@@ -105,9 +104,9 @@ pub struct MapNewService<A, F, Res, Cfg> {
impl<A, F, Res, Cfg> MapNewService<A, F, Res, Cfg> { impl<A, F, Res, Cfg> MapNewService<A, F, Res, Cfg> {
/// Create new `Map` new service instance /// Create new `Map` new service instance
pub fn new(a: A, f: F) -> Self pub fn new<Req>(a: A, f: F) -> Self
where where
A: NewService<Cfg>, A: NewService<Req, Cfg>,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
Self { Self {
@@ -132,36 +131,35 @@ where
} }
} }
impl<A, F, Res, Cfg> NewService<Cfg> for MapNewService<A, F, Res, Cfg> impl<A, F, Req, Res, Cfg> NewService<Req, Cfg> for MapNewService<A, F, Res, Cfg>
where where
A: NewService<Cfg>, A: NewService<Req, Cfg>,
F: FnMut(A::Response) -> Res + Clone, F: FnMut(A::Response) -> Res + Clone,
{ {
type Request = A::Request;
type Response = Res; type Response = Res;
type Error = A::Error; type Error = A::Error;
type Service = Map<A::Service, F, Res>; type Service = Map<A::Service, F, Res>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = MapNewServiceFuture<A, F, Res, Cfg>; type Future = MapNewServiceFuture<A, F, Req, Res, Cfg>;
fn new_service(&self, cfg: &Cfg) -> Self::Future { fn new_service(&self, cfg: &Cfg) -> Self::Future {
MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
} }
} }
pub struct MapNewServiceFuture<A, F, Res, Cfg> pub struct MapNewServiceFuture<A, F, Req, Res, Cfg>
where where
A: NewService<Cfg>, A: NewService<Req, Cfg>,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
fut: A::Future, fut: A::Future,
f: Option<F>, f: Option<F>,
} }
impl<A, F, Res, Cfg> MapNewServiceFuture<A, F, Res, Cfg> impl<A, F, Req, Res, Cfg> MapNewServiceFuture<A, F, Req, Res, Cfg>
where where
A: NewService<Cfg>, A: NewService<Req, Cfg>,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@@ -169,9 +167,9 @@ where
} }
} }
impl<A, F, Res, Cfg> Future for MapNewServiceFuture<A, F, Res, Cfg> impl<A, F, Req, Res, Cfg> Future for MapNewServiceFuture<A, F, Req, Res, Cfg>
where where
A: NewService<Cfg>, A: NewService<Req, Cfg>,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
type Item = Map<A::Service, F, Res>; type Item = Map<A::Service, F, Res>;
@@ -194,8 +192,7 @@ mod tests {
use crate::{IntoNewService, Service, ServiceExt}; use crate::{IntoNewService, Service, ServiceExt};
struct Srv; struct Srv;
impl Service for Srv { impl Service<()> for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;

View File

@@ -16,9 +16,9 @@ pub struct MapErr<A, F, E> {
impl<A, F, E> MapErr<A, F, E> { impl<A, F, E> MapErr<A, F, E> {
/// Create new `MapErr` combinator /// Create new `MapErr` combinator
pub fn new(service: A, f: F) -> Self pub fn new<R>(service: A, f: F) -> Self
where where
A: Service, A: Service<R>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
Self { Self {
@@ -43,47 +43,39 @@ where
} }
} }
impl<A, F, E> Service for MapErr<A, F, E> impl<A, F, E, R> Service<R> for MapErr<A, F, E>
where where
A: Service, A: Service<R>,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Future = MapErrFuture<A, F, E>; type Future = MapErrFuture<A, F, E, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready().map_err(&self.f) self.service.poll_ready().map_err(&self.f)
} }
fn call(&mut self, req: A::Request) -> Self::Future { fn call(&mut self, req: R) -> Self::Future {
MapErrFuture::new(self.service.call(req), self.f.clone()) MapErrFuture {
fut: self.service.call(req),
f: self.f.clone(),
}
} }
} }
pub struct MapErrFuture<A, F, E> pub struct MapErrFuture<A, F, E, R>
where where
A: Service, A: Service<R>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
f: F, f: F,
fut: A::Future, fut: A::Future,
} }
impl<A, F, E> MapErrFuture<A, F, E> impl<A, F, E, R> Future for MapErrFuture<A, F, E, R>
where where
A: Service, A: Service<R>,
F: Fn(A::Error) -> E,
{
fn new(fut: A::Future, f: F) -> Self {
MapErrFuture { f, fut }
}
}
impl<A, F, E> Future for MapErrFuture<A, F, E>
where
A: Service,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
type Item = A::Response; type Item = A::Response;
@@ -106,9 +98,9 @@ pub struct MapErrNewService<A, F, E, C> {
impl<A, F, E, C> MapErrNewService<A, F, E, C> { impl<A, F, E, C> MapErrNewService<A, F, E, C> {
/// Create new `MapErr` new service instance /// Create new `MapErr` new service instance
pub fn new(a: A, f: F) -> Self pub fn new<R>(a: A, f: F) -> Self
where where
A: NewService<C>, A: NewService<R, C>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
Self { Self {
@@ -133,36 +125,35 @@ where
} }
} }
impl<A, F, E, C> NewService<C> for MapErrNewService<A, F, E, C> impl<A, F, E, R, C> NewService<R, C> for MapErrNewService<A, F, E, C>
where where
A: NewService<C>, A: NewService<R, C>,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = E; type Error = E;
type Service = MapErr<A::Service, F, E>; type Service = MapErr<A::Service, F, E>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = MapErrNewServiceFuture<A, F, E, C>; type Future = MapErrNewServiceFuture<A, F, E, R, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
} }
} }
pub struct MapErrNewServiceFuture<A, F, E, C> pub struct MapErrNewServiceFuture<A, F, E, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
fut: A::Future, fut: A::Future,
f: F, f: F,
} }
impl<A, F, E, C> MapErrNewServiceFuture<A, F, E, C> impl<A, F, E, R, C> MapErrNewServiceFuture<A, F, E, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: A::Future, f: F) -> Self {
@@ -170,9 +161,9 @@ where
} }
} }
impl<A, F, E, C> Future for MapErrNewServiceFuture<A, F, E, C> impl<A, F, E, R, C> Future for MapErrNewServiceFuture<A, F, E, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
F: Fn(A::Error) -> E + Clone, F: Fn(A::Error) -> E + Clone,
{ {
type Item = MapErr<A::Service, F, E>; type Item = MapErr<A::Service, F, E>;
@@ -196,8 +187,7 @@ mod tests {
struct Srv; struct Srv;
impl Service for Srv { impl Service<()> for Srv {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = FutureResult<(), ()>; type Future = FutureResult<(), ()>;

View File

@@ -13,9 +13,9 @@ pub struct MapInitErr<A, F, E, C> {
impl<A, F, E, C> MapInitErr<A, F, E, C> { impl<A, F, E, C> MapInitErr<A, F, E, C> {
/// Create new `MapInitErr` combinator /// Create new `MapInitErr` combinator
pub fn new(a: A, f: F) -> Self pub fn new<R>(a: A, f: F) -> Self
where where
A: NewService<C>, A: NewService<R, C>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
Self { Self {
@@ -40,46 +40,38 @@ where
} }
} }
impl<A, F, E, C> NewService<C> for MapInitErr<A, F, E, C> impl<A, F, E, R, C> NewService<R, C> for MapInitErr<A, F, E, C>
where where
A: NewService<C>, A: NewService<R, C>,
F: Fn(A::InitError) -> E + Clone, F: Fn(A::InitError) -> E + Clone,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = A::Error; type Error = A::Error;
type Service = A::Service; type Service = A::Service;
type InitError = E; type InitError = E;
type Future = MapInitErrFuture<A, F, E, C>; type Future = MapInitErrFuture<A, F, E, R, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone()) MapInitErrFuture {
fut: self.a.new_service(cfg),
f: self.f.clone(),
}
} }
} }
pub struct MapInitErrFuture<A, F, E, C> pub struct MapInitErrFuture<A, F, E, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
f: F, f: F,
fut: A::Future, fut: A::Future,
} }
impl<A, F, E, C> MapInitErrFuture<A, F, E, C> impl<A, F, E, R, C> Future for MapInitErrFuture<A, F, E, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
F: Fn(A::InitError) -> E,
{
fn new(fut: A::Future, f: F) -> Self {
MapInitErrFuture { f, fut }
}
}
impl<A, F, E, C> Future for MapInitErrFuture<A, F, E, C>
where
A: NewService<C>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
type Item = A::Service; type Item = A::Service;

View File

@@ -2,7 +2,7 @@ use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll}; use futures::{try_ready, Async, Future, Poll};
use super::{IntoNewService, NewService, Service}; use super::{NewService, Service};
use crate::cell::Cell; use crate::cell::Cell;
/// Service for the `then` combinator, chaining a computation onto the end of /// Service for the `then` combinator, chaining a computation onto the end of
@@ -16,10 +16,10 @@ pub struct Then<A, B> {
impl<A, B> Then<A, B> { impl<A, B> Then<A, B> {
/// Create new `Then` combinator /// Create new `Then` combinator
pub fn new(a: A, b: B) -> Then<A, B> pub fn new<R>(a: A, b: B) -> Then<A, B>
where where
A: Service, A: Service<R>,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>, B: Service<Result<A::Response, A::Error>, Error = A::Error>,
{ {
Then { a, b: Cell::new(b) } Then { a, b: Cell::new(b) }
} }
@@ -37,40 +37,39 @@ where
} }
} }
impl<A, B> Service for Then<A, B> impl<A, B, R> Service<R> for Then<A, B>
where where
A: Service, A: Service<R>,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>, B: Service<Result<A::Response, A::Error>, Error = A::Error>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = B::Error; type Error = B::Error;
type Future = ThenFuture<A, B>; type Future = ThenFuture<A, B, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.poll_ready()); try_ready!(self.a.poll_ready());
self.b.get_mut().poll_ready() self.b.get_mut().poll_ready()
} }
fn call(&mut self, req: A::Request) -> Self::Future { fn call(&mut self, req: R) -> Self::Future {
ThenFuture::new(self.a.call(req), self.b.clone()) ThenFuture::new(self.a.call(req), self.b.clone())
} }
} }
pub struct ThenFuture<A, B> pub struct ThenFuture<A, B, R>
where where
A: Service, A: Service<R>,
B: Service<Request = Result<A::Response, A::Error>>, B: Service<Result<A::Response, A::Error>>,
{ {
b: Cell<B>, b: Cell<B>,
fut_b: Option<B::Future>, fut_b: Option<B::Future>,
fut_a: Option<A::Future>, fut_a: Option<A::Future>,
} }
impl<A, B> ThenFuture<A, B> impl<A, B, R> ThenFuture<A, B, R>
where where
A: Service, A: Service<R>,
B: Service<Request = Result<A::Response, A::Error>>, B: Service<Result<A::Response, A::Error>>,
{ {
fn new(a: A::Future, b: Cell<B>) -> Self { fn new(a: A::Future, b: Cell<B>) -> Self {
ThenFuture { ThenFuture {
@@ -81,10 +80,10 @@ where
} }
} }
impl<A, B> Future for ThenFuture<A, B> impl<A, B, R> Future for ThenFuture<A, B, R>
where where
A: Service, A: Service<R>,
B: Service<Request = Result<A::Response, A::Error>>, B: Service<Result<A::Response, A::Error>>,
{ {
type Item = B::Response; type Item = B::Response;
type Error = B::Error; type Error = B::Error;
@@ -119,42 +118,35 @@ pub struct ThenNewService<A, B, C> {
impl<A, B, C> ThenNewService<A, B, C> { impl<A, B, C> ThenNewService<A, B, C> {
/// Create new `AndThen` combinator /// Create new `AndThen` combinator
pub fn new<F>(a: A, f: F) -> Self pub fn new<R>(a: A, f: B) -> Self
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService< B: NewService<
Result<A::Response, A::Error>,
C, C,
Request = Result<A::Response, A::Error>,
Error = A::Error, Error = A::Error,
InitError = A::InitError, InitError = A::InitError,
>, >,
F: IntoNewService<B, C>,
{ {
Self { Self {
a, a,
b: f.into_new_service(), b: f,
_t: PhantomData, _t: PhantomData,
} }
} }
} }
impl<A, B, C> NewService<C> for ThenNewService<A, B, C> impl<A, B, R, C> NewService<R, C> for ThenNewService<A, B, C>
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService< B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Service = Then<A::Service, B::Service>; type Service = Then<A::Service, B::Service>;
type InitError = A::InitError; type InitError = A::InitError;
type Future = ThenNewServiceFuture<A, B, C>; type Future = ThenNewServiceFuture<A, B, R, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
ThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg)) ThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg))
@@ -175,15 +167,10 @@ where
} }
} }
pub struct ThenNewServiceFuture<A, B, C> pub struct ThenNewServiceFuture<A, B, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService< B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{ {
fut_b: B::Future, fut_b: B::Future,
fut_a: A::Future, fut_a: A::Future,
@@ -191,15 +178,10 @@ where
b: Option<B::Service>, b: Option<B::Service>,
} }
impl<A, B, C> ThenNewServiceFuture<A, B, C> impl<A, B, R, C> ThenNewServiceFuture<A, B, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService< B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{ {
fn new(fut_a: A::Future, fut_b: B::Future) -> Self { fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
ThenNewServiceFuture { ThenNewServiceFuture {
@@ -211,15 +193,10 @@ where
} }
} }
impl<A, B, C> Future for ThenNewServiceFuture<A, B, C> impl<A, B, R, C> Future for ThenNewServiceFuture<A, B, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService< B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
{ {
type Item = Then<A::Service, B::Service>; type Item = Then<A::Service, B::Service>;
type Error = A::InitError; type Error = A::InitError;
@@ -259,8 +236,7 @@ mod tests {
#[derive(Clone)] #[derive(Clone)]
struct Srv1(Rc<Cell<usize>>); struct Srv1(Rc<Cell<usize>>);
impl Service for Srv1 { impl Service<Result<&'static str, &'static str>> for Srv1 {
type Request = Result<&'static str, &'static str>;
type Response = &'static str; type Response = &'static str;
type Error = (); type Error = ();
type Future = FutureResult<Self::Response, Self::Error>; type Future = FutureResult<Self::Response, Self::Error>;
@@ -280,8 +256,7 @@ mod tests {
struct Srv2(Rc<Cell<usize>>); struct Srv2(Rc<Cell<usize>>);
impl Service for Srv2 { impl Service<Result<&'static str, ()>> for Srv2 {
type Request = Result<&'static str, ()>;
type Response = (&'static str, &'static str); type Response = (&'static str, &'static str);
type Error = (); type Error = ();
type Future = FutureResult<Self::Response, ()>; type Future = FutureResult<Self::Response, ()>;

View File

@@ -1,59 +1,18 @@
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use futures::{Future, Poll}; use futures::{Async, Future, IntoFuture, Poll};
use crate::transform_map_err::{TransformMapErr, TransformMapErrNewTransform};
use crate::transform_map_init_err::TransformMapInitErr; use crate::transform_map_init_err::TransformMapInitErr;
use crate::Service; use crate::{NewService, Service};
/// An asynchronous function for transforming service call result. /// `Transform` service factory.
pub trait Transform<Service> {
/// Requests handled by the service.
type Request;
/// Responses given by the service.
type Response;
/// Errors produced by the service.
type Error;
/// The future response value.
type Future: Future<Item = Self::Response, Error = Self::Error>;
/// Returns `Ready` when the service is able to process requests.
///
/// This method is similar to `Service::poll_ready` method.
fn poll_ready(&mut self) -> Poll<(), Self::Error>;
/// Process the request and apply it to provided service,
/// return the response asynchronously.
fn call(&mut self, request: Self::Request, service: &mut Service) -> Self::Future;
/// Map this transform's error to a different error, returning a new transform.
///
/// This function is similar to the `Result::map_err` where it will change
/// the error type of the underlying transform. This is useful for example to
/// ensure that services and transforms have the same error type.
///
/// Note that this function consumes the receiving transform and returns a
/// wrapped version of it.
fn map_err<F, E>(self, f: F) -> TransformMapErr<Self, Service, F, E>
where
Self: Sized,
F: Fn(Self::Error) -> E,
{
TransformMapErr::new(self, f)
}
}
/// `Transform` service factory
/// ///
/// `Config` is a service factory configuration type. /// Transform factory creates service that wraps other services.
pub trait NewTransform<Service, Config = ()> { ///
/// Requests handled by the service. /// * `S` is a wrapped service.
type Request; /// * `R` requests handled by this transform service.
pub trait Transform<S, R> {
/// Responses given by the service. /// Responses given by the service.
type Response; type Response;
@@ -61,12 +20,7 @@ pub trait NewTransform<Service, Config = ()> {
type Error; type Error;
/// The `TransformService` value created by this factory /// The `TransformService` value created by this factory
type Transform: Transform< type Transform: Service<R, Response = Self::Response, Error = Self::Error>;
Service,
Request = Self::Request,
Response = Self::Response,
Error = Self::Error,
>;
/// Errors produced while building a service. /// Errors produced while building a service.
type InitError; type InitError;
@@ -75,21 +29,11 @@ pub trait NewTransform<Service, Config = ()> {
type Future: Future<Item = Self::Transform, Error = Self::InitError>; type Future: Future<Item = Self::Transform, Error = Self::InitError>;
/// Create and return a new service value asynchronously. /// Create and return a new service value asynchronously.
fn new_transform(&self, cfg: &Config) -> Self::Future; fn new_transform(&self, service: S) -> Self::Future;
/// Map this transforms's output to a different type, returning a new transform
/// of the resulting type.
fn map_err<F, E>(self, f: F) -> TransformMapErrNewTransform<Self, Service, Config, F, E>
where
Self: Sized,
F: Fn(Self::Error) -> E,
{
TransformMapErrNewTransform::new(self, f)
}
/// Map this service's factory init error to a different error, /// Map this service's factory init error to a different error,
/// returning a new transform service factory. /// returning a new transform service factory.
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, Service, Config, F, E> fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
where where
Self: Sized, Self: Sized,
F: Fn(Self::InitError) -> E, F: Fn(Self::InitError) -> E,
@@ -98,108 +42,126 @@ pub trait NewTransform<Service, Config = ()> {
} }
} }
impl<'a, T, S> Transform<S> for &'a mut T impl<T, S, R> Transform<S, R> for Rc<T>
where where
T: Transform<S> + 'a, T: Transform<S, R>,
S: Service<Error = T::Error>,
{ {
type Request = T::Request;
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type Future = T::Future;
fn poll_ready(&mut self) -> Poll<(), T::Error> {
(**self).poll_ready()
}
fn call(&mut self, request: Self::Request, service: &mut S) -> T::Future {
(**self).call(request, service)
}
}
impl<T, S> Transform<S> for Box<T>
where
T: Transform<S> + ?Sized,
S: Service<Error = T::Error>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Future = T::Future;
fn poll_ready(&mut self) -> Poll<(), S::Error> {
(**self).poll_ready()
}
fn call(&mut self, request: Self::Request, service: &mut S) -> T::Future {
(**self).call(request, service)
}
}
impl<S, C, T> NewTransform<S, C> for Rc<T>
where
T: NewTransform<S, C>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = T::InitError; type InitError = T::InitError;
type Transform = T::Transform;
type Future = T::Future; type Future = T::Future;
fn new_transform(&self, cfg: &C) -> T::Future { fn new_transform(&self, service: S) -> T::Future {
self.as_ref().new_transform(cfg) self.as_ref().new_transform(service)
} }
} }
impl<S, C, T> NewTransform<S, C> for Arc<T> impl<T, S, R> Transform<S, R> for Arc<T>
where where
T: NewTransform<S, C>, T: Transform<S, R>,
{ {
type Request = T::Request;
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type Transform = T::Transform;
type InitError = T::InitError; type InitError = T::InitError;
type Transform = T::Transform;
type Future = T::Future; type Future = T::Future;
fn new_transform(&self, cfg: &C) -> T::Future { fn new_transform(&self, service: S) -> T::Future {
self.as_ref().new_transform(cfg) self.as_ref().new_transform(service)
} }
} }
/// Trait for types that can be converted to a `TransformService` /// Trait for types that can be converted to a *transform service*
pub trait IntoTransform<T, S> pub trait IntoTransform<T, S, R>
where where
T: Transform<S>, T: Transform<S, R>,
{ {
/// Convert to a `TransformService` /// Convert to a `TransformService`
fn into_transform(self) -> T; fn into_transform(self) -> T;
} }
/// Trait for types that can be converted to a TransfromNewService impl<T, S, R> IntoTransform<T, S, R> for T
pub trait IntoNewTransform<T, S, C = ()>
where where
T: NewTransform<S, C>, T: Transform<S, R>,
{
/// Convert to an `TranformNewService`
fn into_new_transform(self) -> T;
}
impl<T, S> IntoTransform<T, S> for T
where
T: Transform<S>,
{ {
fn into_transform(self) -> T { fn into_transform(self) -> T {
self self
} }
} }
impl<T, S, C> IntoNewTransform<T, S, C> for T /// `Apply` transform new service
#[derive(Clone)]
pub struct ApplyTransform<T, S, R, Req, Cfg> {
a: S,
t: Rc<T>,
_t: std::marker::PhantomData<(R, Req, Cfg)>,
}
impl<T, S, R, Req, Cfg> ApplyTransform<T, S, R, Req, Cfg>
where where
T: NewTransform<S, C>, S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
{ {
fn into_new_transform(self) -> T { /// Create new `ApplyNewService` new service instance
self pub fn new<F: IntoTransform<T, S::Service, R>>(t: F, a: S) -> Self {
Self {
a,
t: Rc::new(t.into_transform()),
_t: std::marker::PhantomData,
}
}
}
impl<T, S, R, Req, Cfg> NewService<R, Cfg> for ApplyTransform<T, S, R, Req, Cfg>
where
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
{
type Response = T::Response;
type Error = T::Error;
type Service = T::Transform;
type InitError = T::InitError;
type Future = ApplyTransformFuture<T, S, R, Req, Cfg>;
fn new_service(&self, cfg: &Cfg) -> Self::Future {
ApplyTransformFuture {
t_cell: self.t.clone(),
fut_a: self.a.new_service(cfg).into_future(),
fut_t: None,
}
}
}
pub struct ApplyTransformFuture<T, S, R, Req, Cfg>
where
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
{
fut_a: S::Future,
fut_t: Option<T::Future>,
t_cell: Rc<T>,
}
impl<T, S, R, Req, Cfg> Future for ApplyTransformFuture<T, S, R, Req, Cfg>
where
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
{
type Item = T::Transform;
type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if self.fut_t.is_none() {
if let Async::Ready(service) = self.fut_a.poll()? {
self.fut_t = Some(self.t_cell.new_transform(service).into_future());
}
}
if let Some(ref mut fut) = self.fut_t {
fut.poll()
} else {
Ok(Async::NotReady)
}
} }
} }

View File

@@ -1,188 +0,0 @@
use std::marker::PhantomData;
use futures::{Async, Future, Poll};
use super::{NewTransform, Transform};
/// Service for the `map_err` combinator, changing the type of a transform's
/// error.
///
/// This is created by the `Transform::map_err` method.
pub struct TransformMapErr<T, S, F, E> {
transform: T,
f: F,
_t: PhantomData<(S, E)>,
}
impl<T, S, F, E> TransformMapErr<T, S, F, E> {
/// Create new `MapErr` combinator
pub fn new(transform: T, f: F) -> Self
where
T: Transform<S>,
F: Fn(T::Error) -> E,
{
Self {
transform,
f,
_t: PhantomData,
}
}
}
impl<T, S, F, E> Clone for TransformMapErr<T, S, F, E>
where
T: Clone,
F: Clone,
{
fn clone(&self) -> Self {
TransformMapErr {
transform: self.transform.clone(),
f: self.f.clone(),
_t: PhantomData,
}
}
}
impl<T, S, F, E> Transform<S> for TransformMapErr<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::Error) -> E + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = E;
type Future = TransformMapErrFuture<T, S, F, E>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.transform.poll_ready().map_err(&self.f)
}
fn call(&mut self, req: T::Request, service: &mut S) -> Self::Future {
TransformMapErrFuture::new(self.transform.call(req, service), self.f.clone())
}
}
pub struct TransformMapErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::Error) -> E,
{
f: F,
fut: T::Future,
}
impl<T, S, F, E> TransformMapErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::Error) -> E,
{
fn new(fut: T::Future, f: F) -> Self {
TransformMapErrFuture { f, fut }
}
}
impl<T, S, F, E> Future for TransformMapErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::Error) -> E,
{
type Item = T::Response;
type Error = E;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.fut.poll().map_err(&self.f)
}
}
/// NewTransform for the `map_err` combinator, changing the type of a new
/// transform's error.
///
/// This is created by the `NewTransform::map_err` method.
pub struct TransformMapErrNewTransform<T, S, C, F, E> {
t: T,
f: F,
e: PhantomData<(S, C, E)>,
}
impl<T, S, C, F, E> TransformMapErrNewTransform<T, S, C, F, E> {
/// Create new `MapErr` new service instance
pub fn new(t: T, f: F) -> Self
where
T: NewTransform<S, C>,
F: Fn(T::Error) -> E,
{
Self {
t,
f,
e: PhantomData,
}
}
}
impl<T, S, C, F, E> Clone for TransformMapErrNewTransform<T, S, C, F, E>
where
T: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
t: self.t.clone(),
f: self.f.clone(),
e: PhantomData,
}
}
}
impl<T, S, C, F, E> NewTransform<S, C> for TransformMapErrNewTransform<T, S, C, F, E>
where
T: NewTransform<S, C>,
F: Fn(T::Error) -> E + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = E;
type Transform = TransformMapErr<T::Transform, S, F, E>;
type InitError = T::InitError;
type Future = TransformMapErrNewTransformFuture<T, S, C, F, E>;
fn new_transform(&self, cfg: &C) -> Self::Future {
TransformMapErrNewTransformFuture::new(self.t.new_transform(cfg), self.f.clone())
}
}
pub struct TransformMapErrNewTransformFuture<T, S, C, F, E>
where
T: NewTransform<S, C>,
F: Fn(T::Error) -> E,
{
fut: T::Future,
f: F,
}
impl<T, S, C, F, E> TransformMapErrNewTransformFuture<T, S, C, F, E>
where
T: NewTransform<S, C>,
F: Fn(T::Error) -> E,
{
fn new(fut: T::Future, f: F) -> Self {
TransformMapErrNewTransformFuture { f, fut }
}
}
impl<T, S, C, F, E> Future for TransformMapErrNewTransformFuture<T, S, C, F, E>
where
T: NewTransform<S, C>,
F: Fn(T::Error) -> E + Clone,
{
type Item = TransformMapErr<T::Transform, S, F, E>;
type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if let Async::Ready(tr) = self.fut.poll()? {
Ok(Async::Ready(TransformMapErr::new(tr, self.f.clone())))
} else {
Ok(Async::NotReady)
}
}
}

View File

@@ -2,23 +2,23 @@ use std::marker::PhantomData;
use futures::{Future, Poll}; use futures::{Future, Poll};
use super::NewTransform; use super::Transform;
/// NewTransform for the `map_init_err` combinator, changing the type of a new /// NewTransform for the `map_init_err` combinator, changing the type of a new
/// transform's error. /// transform's error.
/// ///
/// This is created by the `NewTransform::map_init_err` method. /// This is created by the `NewTransform::map_init_err` method.
pub struct TransformMapInitErr<T, S, C, F, E> { pub struct TransformMapInitErr<T, S, F, E> {
t: T, t: T,
f: F, f: F,
e: PhantomData<(S, C, E)>, e: PhantomData<(S, E)>,
} }
impl<T, S, C, F, E> TransformMapInitErr<T, S, C, F, E> { impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
/// Create new `MapInitErr` new transform instance /// Create new `MapInitErr` new transform instance
pub fn new(t: T, f: F) -> Self pub fn new<R>(t: T, f: F) -> Self
where where
T: NewTransform<S, C>, T: Transform<S, R>,
F: Fn(T::InitError) -> E, F: Fn(T::InitError) -> E,
{ {
Self { Self {
@@ -29,7 +29,7 @@ impl<T, S, C, F, E> TransformMapInitErr<T, S, C, F, E> {
} }
} }
impl<T, S, C, F, E> Clone for TransformMapInitErr<T, S, C, F, E> impl<T, S, F, E> Clone for TransformMapInitErr<T, S, F, E>
where where
T: Clone, T: Clone,
F: Clone, F: Clone,
@@ -43,36 +43,35 @@ where
} }
} }
impl<T, S, C, F, E> NewTransform<S, C> for TransformMapInitErr<T, S, C, F, E> impl<T, R, S, F, E> Transform<S, R> for TransformMapInitErr<T, S, F, E>
where where
T: NewTransform<S, C>, T: Transform<S, R>,
F: Fn(T::InitError) -> E + Clone, F: Fn(T::InitError) -> E + Clone,
{ {
type Request = T::Request;
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type Transform = T::Transform; type Transform = T::Transform;
type InitError = E; type InitError = E;
type Future = TransformMapInitErrFuture<T, S, C, F, E>; type Future = TransformMapInitErrFuture<T, R, S, F, E>;
fn new_transform(&self, cfg: &C) -> Self::Future { fn new_transform(&self, service: S) -> Self::Future {
TransformMapInitErrFuture::new(self.t.new_transform(cfg), self.f.clone()) TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone())
} }
} }
pub struct TransformMapInitErrFuture<T, S, C, F, E> pub struct TransformMapInitErrFuture<T, R, S, F, E>
where where
T: NewTransform<S, C>, T: Transform<S, R>,
F: Fn(T::InitError) -> E, F: Fn(T::InitError) -> E,
{ {
fut: T::Future, fut: T::Future,
f: F, f: F,
} }
impl<T, S, C, F, E> TransformMapInitErrFuture<T, S, C, F, E> impl<T, R, S, F, E> TransformMapInitErrFuture<T, R, S, F, E>
where where
T: NewTransform<S, C>, T: Transform<S, R>,
F: Fn(T::InitError) -> E, F: Fn(T::InitError) -> E,
{ {
fn new(fut: T::Future, f: F) -> Self { fn new(fut: T::Future, f: F) -> Self {
@@ -80,15 +79,15 @@ where
} }
} }
impl<T, S, C, F, E> Future for TransformMapInitErrFuture<T, S, C, F, E> impl<T, R, S, F, E> Future for TransformMapInitErrFuture<T, R, S, F, E>
where where
T: NewTransform<S, C>, T: Transform<S, R>,
F: Fn(T::InitError) -> E + Clone, F: Fn(T::InitError) -> E + Clone,
{ {
type Item = T::Transform; type Item = T::Transform;
type Error = E; type Error = E;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.fut.poll().map_err(|e| (self.f)(e)) self.fut.poll().map_err(&self.f)
} }
} }

View File

@@ -34,12 +34,10 @@ rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
[dependencies] [dependencies]
actix-rt = "0.1.0" actix-rt = "0.1.0"
#actix-server = "0.2.0" #actix-server = "0.3.0"
actix-server = { path="../actix-server" } actix-server = { path="../actix-server" }
log = "0.4" log = "0.4"
# io
net2 = "0.2" net2 = "0.2"
futures = "0.1" futures = "0.1"
tokio-tcp = "0.1" tokio-tcp = "0.1"

View File

@@ -1,5 +1,24 @@
# Changes # Changes
## [0.4.0] - 2019-03-xx
* Upgrade actix-service
## [0.3.2] - 2019-03-04
### Changed
* Use IntoFuture for new services
## [0.3.1] - 2019-03-04
### Changed
* Use new type of transform trait
## [0.3.0] - 2019-03-02 ## [0.3.0] - 2019-03-02
### Changed ### Changed

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "actix-utils" name = "actix-utils"
version = "0.3.0" version = "0.3.2"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix utils - various actix net related services" description = "Actix utils - various actix net related services"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]
@@ -18,7 +18,8 @@ name = "actix_utils"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
actix-service = "0.3.0" #actix-service = "0.3.2"
actix-service = { path="../actix-service" }
actix-codec = "0.1.0" actix-codec = "0.1.0"
bytes = "0.4" bytes = "0.4"
futures = "0.1.24" futures = "0.1.24"

View File

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

View File

@@ -1,6 +1,6 @@
//! Contains `Either` service and related types and functions. //! Contains `Either` service and related types and functions.
use actix_service::{NewService, Service}; use actix_service::{NewService, Service};
use futures::{future, try_ready, Async, Future, Poll}; use futures::{future, try_ready, Async, Future, IntoFuture, Poll};
/// Combine two different service types into a single type. /// Combine two different service types into a single type.
/// ///
@@ -21,12 +21,11 @@ impl<A: Clone, B: Clone> Clone for EitherService<A, B> {
} }
} }
impl<A, B> Service for EitherService<A, B> impl<A, B, R> Service<R> for EitherService<A, B>
where where
A: Service, A: Service<R>,
B: Service<Request = A::Request, Response = A::Response, Error = A::Error>, B: Service<R, Response = A::Response, Error = A::Error>,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = A::Error; type Error = A::Error;
type Future = future::Either<A::Future, B::Future>; type Future = future::Either<A::Future, B::Future>;
@@ -38,7 +37,7 @@ where
} }
} }
fn call(&mut self, req: A::Request) -> Self::Future { fn call(&mut self, req: R) -> Self::Future {
match self { match self {
EitherService::A(ref mut inner) => future::Either::A(inner.call(req)), EitherService::A(ref mut inner) => future::Either::A(inner.call(req)),
EitherService::B(ref mut inner) => future::Either::B(inner.call(req)), EitherService::B(ref mut inner) => future::Either::B(inner.call(req)),
@@ -53,52 +52,33 @@ pub enum Either<A, B> {
} }
impl<A, B> Either<A, B> { impl<A, B> Either<A, B> {
pub fn new_a<C>(srv: A) -> Self pub fn new_a<R, C>(srv: A) -> Self
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService< B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{ {
Either::A(srv) Either::A(srv)
} }
pub fn new_b<C>(srv: B) -> Self pub fn new_b<R, C>(srv: B) -> Self
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService< B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{ {
Either::B(srv) Either::B(srv)
} }
} }
impl<A, B, C> NewService<C> for Either<A, B> impl<A, B, R, C> NewService<R, C> for Either<A, B>
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService< B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{ {
type Request = A::Request;
type Response = A::Response; type Response = A::Response;
type Error = A::Error; type Error = A::Error;
type InitError = A::InitError; type InitError = A::InitError;
type Service = EitherService<A::Service, B::Service>; type Service = EitherService<A::Service, B::Service>;
type Future = EitherNewService<A, B, C>; type Future = EitherNewService<A, B, R, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
match self { match self {
@@ -118,21 +98,15 @@ impl<A: Clone, B: Clone> Clone for Either<A, B> {
} }
#[doc(hidden)] #[doc(hidden)]
pub enum EitherNewService<A: NewService<C>, B: NewService<C>, C> { pub enum EitherNewService<A: NewService<R, C>, B: NewService<R, C>, R, C> {
A(A::Future), A(<A::Future as IntoFuture>::Future),
B(B::Future), B(<B::Future as IntoFuture>::Future),
} }
impl<A, B, C> Future for EitherNewService<A, B, C> impl<A, B, R, C> Future for EitherNewService<A, B, R, C>
where where
A: NewService<C>, A: NewService<R, C>,
B: NewService< B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{ {
type Item = EitherService<A::Service, B::Service>; type Item = EitherService<A::Service, B::Service>;
type Error = A::InitError; type Error = A::InitError;

View File

@@ -7,7 +7,7 @@ use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
use actix_service::{IntoNewService, IntoService, NewService, Service}; use actix_service::{IntoNewService, IntoService, NewService, Service};
use futures::future::{ok, FutureResult}; use futures::future::{ok, FutureResult};
use futures::task::AtomicTask; use futures::task::AtomicTask;
use futures::{Async, Future, Poll, Sink, Stream}; use futures::{Async, Future, IntoFuture, Poll, Sink, Stream};
use log::debug; use log::debug;
use crate::cell::Cell; use crate::cell::Cell;
@@ -23,15 +23,15 @@ pub struct FramedNewService<S, T, U, C> {
impl<S, T, U, C> FramedNewService<S, T, U, C> impl<S, T, U, C> FramedNewService<S, T, U, C>
where where
C: Clone, C: Clone,
S: NewService<C, Request = Request<U>, Response = Response<U>>, S: NewService<Request<U>, C, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
<S::Service as Service>::Future: 'static, <S::Service as Service<Request<U>>>::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug, <U as Encoder>::Error: std::fmt::Debug,
{ {
pub fn new<F1: IntoNewService<S, C>>(factory: F1) -> Self { pub fn new<F1: IntoNewService<S, Request<U>, C>>(factory: F1) -> Self {
Self { Self {
factory: factory.into_new_service(), factory: factory.into_new_service(),
_t: PhantomData, _t: PhantomData,
@@ -51,18 +51,17 @@ where
} }
} }
impl<S, T, U, C> NewService<C> for FramedNewService<S, T, U, C> impl<S, T, U, C> NewService<Framed<T, U>, C> for FramedNewService<S, T, U, C>
where where
C: Clone, C: Clone,
S: NewService<C, Request = Request<U>, Response = Response<U>> + Clone, S: NewService<Request<U>, C, Response = Response<U>> + Clone,
S::Error: 'static, S::Error: 'static,
<S::Service as Service>::Future: 'static, <S::Service as Service<Request<U>>>::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug, <U as Encoder>::Error: std::fmt::Debug,
{ {
type Request = Framed<T, U>;
type Response = FramedTransport<S::Service, T, U>; type Response = FramedTransport<S::Service, T, U>;
type Error = S::InitError; type Error = S::InitError;
type InitError = S::InitError; type InitError = S::InitError;
@@ -98,18 +97,17 @@ where
} }
} }
impl<S, T, U, C> Service for FramedService<S, T, U, C> impl<S, T, U, C> Service<Framed<T, U>> for FramedService<S, T, U, C>
where where
S: NewService<C, Request = Request<U>, Response = Response<U>>, S: NewService<Request<U>, C, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
<S::Service as Service>::Future: 'static, <S::Service as Service<Request<U>>>::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug, <U as Encoder>::Error: std::fmt::Debug,
C: Clone, C: Clone,
{ {
type Request = Framed<T, U>;
type Response = FramedTransport<S::Service, T, U>; type Response = FramedTransport<S::Service, T, U>;
type Error = S::InitError; type Error = S::InitError;
type Future = FramedServiceResponseFuture<S, T, U, C>; type Future = FramedServiceResponseFuture<S, T, U, C>;
@@ -129,23 +127,23 @@ where
#[doc(hidden)] #[doc(hidden)]
pub struct FramedServiceResponseFuture<S, T, U, C> pub struct FramedServiceResponseFuture<S, T, U, C>
where where
S: NewService<C, Request = Request<U>, Response = Response<U>>, S: NewService<Request<U>, C, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
<S::Service as Service>::Future: 'static, <S::Service as Service<Request<U>>>::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug, <U as Encoder>::Error: std::fmt::Debug,
{ {
fut: S::Future, fut: <S::Future as IntoFuture>::Future,
framed: Option<Framed<T, U>>, framed: Option<Framed<T, U>>,
} }
impl<S, T, U, C> Future for FramedServiceResponseFuture<S, T, U, C> impl<S, T, U, C> Future for FramedServiceResponseFuture<S, T, U, C>
where where
S: NewService<C, Request = Request<U>, Response = Response<U>>, S: NewService<Request<U>, C, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
<S::Service as Service>::Future: 'static, <S::Service as Service<Request<U>>>::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
U: Decoder + Encoder, U: Decoder + Encoder,
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
@@ -182,7 +180,7 @@ impl<E, U: Encoder + Decoder> From<E> for FramedTransportError<E, U> {
/// and pass then to the service. /// and pass then to the service.
pub struct FramedTransport<S, T, U> pub struct FramedTransport<S, T, U>
where where
S: Service<Request = Request<U>, Response = Response<U>>, S: Service<Request<U>, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
S::Future: 'static, S::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
@@ -196,7 +194,7 @@ where
inner: Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>, inner: Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>,
} }
enum TransportState<S: Service, U: Encoder + Decoder> { enum TransportState<S: Service<Request<U>>, U: Encoder + Decoder> {
Processing, Processing,
Error(FramedTransportError<S::Error, U>), Error(FramedTransportError<S::Error, U>),
FramedError(FramedTransportError<S::Error, U>), FramedError(FramedTransportError<S::Error, U>),
@@ -210,7 +208,7 @@ struct FramedTransportInner<I, E> {
impl<S, T, U> FramedTransport<S, T, U> impl<S, T, U> FramedTransport<S, T, U>
where where
S: Service<Request = Request<U>, Response = Response<U>>, S: Service<Request<U>, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
S::Future: 'static, S::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
@@ -302,7 +300,7 @@ where
impl<S, T, U> FramedTransport<S, T, U> impl<S, T, U> FramedTransport<S, T, U>
where where
S: Service<Request = Request<U>, Response = Response<U>>, S: Service<Request<U>, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
S::Future: 'static, S::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
@@ -310,7 +308,7 @@ where
<U as Encoder>::Item: 'static, <U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug, <U as Encoder>::Error: std::fmt::Debug,
{ {
pub fn new<F: IntoService<S>>(framed: Framed<T, U>, service: F) -> Self { pub fn new<F: IntoService<S, Request<U>>>(framed: Framed<T, U>, service: F) -> Self {
FramedTransport { FramedTransport {
framed, framed,
service: service.into_service(), service: service.into_service(),
@@ -348,7 +346,7 @@ where
impl<S, T, U> Future for FramedTransport<S, T, U> impl<S, T, U> Future for FramedTransport<S, T, U>
where where
S: Service<Request = Request<U>, Response = Response<U>>, S: Service<Request<U>, Response = Response<U>>,
S::Error: 'static, S::Error: 'static,
S::Future: 'static, S::Future: 'static,
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
@@ -408,13 +406,12 @@ where
} }
} }
impl<T, U, F> NewService<()> for IntoFramed<T, U, F> impl<T, U, F> NewService<T, ()> for IntoFramed<T, U, F>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static, F: Fn() -> U + Send + Clone + 'static,
U: Encoder + Decoder, U: Encoder + Decoder,
{ {
type Request = T;
type Response = Framed<T, U>; type Response = Framed<T, U>;
type Error = (); type Error = ();
type InitError = (); type InitError = ();
@@ -439,13 +436,12 @@ where
_t: PhantomData<(T,)>, _t: PhantomData<(T,)>,
} }
impl<T, U, F> Service for IntoFramedService<T, U, F> impl<T, U, F> Service<T> for IntoFramedService<T, U, F>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static, F: Fn() -> U + Send + Clone + 'static,
U: Encoder + Decoder, U: Encoder + Decoder,
{ {
type Request = T;
type Response = Framed<T, U>; type Response = Framed<T, U>;
type Error = (); type Error = ();
type Future = FutureResult<Self::Response, Self::Error>; type Future = FutureResult<Self::Response, Self::Error>;

View File

@@ -1,4 +1,4 @@
use actix_service::{NewTransform, Service, Transform, Void}; use actix_service::{Service, Transform, Void};
use futures::future::{ok, FutureResult}; use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll}; use futures::{Async, Future, Poll};
@@ -24,41 +24,43 @@ impl Default for InFlight {
} }
} }
impl<T: Service, C> NewTransform<T, C> for InFlight { impl<S: Service<R>, R> Transform<S, R> for InFlight {
type Request = T::Request; type Response = S::Response;
type Response = T::Response; type Error = S::Error;
type Error = T::Error;
type InitError = Void; type InitError = Void;
type Transform = InFlightService; type Transform = InFlightService<S>;
type Future = FutureResult<Self::Transform, Self::InitError>; type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, _: &C) -> Self::Future { fn new_transform(&self, service: S) -> Self::Future {
ok(InFlightService::new(self.max_inflight)) ok(InFlightService::new(self.max_inflight, service))
} }
} }
pub struct InFlightService { pub struct InFlightService<S> {
count: Counter, count: Counter,
service: S,
} }
impl InFlightService { impl<S> InFlightService<S> {
pub fn new(max: usize) -> Self { pub fn new(max: usize, service: S) -> Self {
Self { Self {
service,
count: Counter::new(max), count: Counter::new(max),
} }
} }
} }
impl<T> Transform<T> for InFlightService impl<T, R> Service<R> for InFlightService<T>
where where
T: Service, T: Service<R>,
{ {
type Request = T::Request;
type Response = T::Response; type Response = T::Response;
type Error = T::Error; type Error = T::Error;
type Future = InFlightServiceResponse<T>; type Future = InFlightServiceResponse<T, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready()?;
if !self.count.available() { if !self.count.available() {
log::trace!("InFlight limit exceeded"); log::trace!("InFlight limit exceeded");
Ok(Async::NotReady) Ok(Async::NotReady)
@@ -67,21 +69,21 @@ where
} }
} }
fn call(&mut self, req: T::Request, service: &mut T) -> Self::Future { fn call(&mut self, req: R) -> Self::Future {
InFlightServiceResponse { InFlightServiceResponse {
fut: service.call(req), fut: self.service.call(req),
_guard: self.count.get(), _guard: self.count.get(),
} }
} }
} }
#[doc(hidden)] #[doc(hidden)]
pub struct InFlightServiceResponse<T: Service> { pub struct InFlightServiceResponse<T: Service<R>, R> {
fut: T::Future, fut: T::Future,
_guard: CounterGuard, _guard: CounterGuard,
} }
impl<T: Service> Future for InFlightServiceResponse<T> { impl<T: Service<R>, R> Future for InFlightServiceResponse<T, R> {
type Item = T::Response; type Item = T::Response;
type Error = T::Error; type Error = T::Error;
@@ -103,8 +105,7 @@ mod tests {
struct SleepService(Duration); struct SleepService(Duration);
impl Service for SleepService { impl Service<()> for SleepService {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = Box<Future<Item = (), Error = ()>>; type Future = Box<Future<Item = (), Error = ()>>;
@@ -122,7 +123,8 @@ mod tests {
fn test_transform() { fn test_transform() {
let wait_time = Duration::from_millis(50); let wait_time = Duration::from_millis(50);
let _ = actix_rt::System::new("test").block_on(lazy(|| { let _ = actix_rt::System::new("test").block_on(lazy(|| {
let mut srv = Blank::new().apply(InFlightService::new(1), SleepService(wait_time)); let mut srv =
Blank::new().and_then(InFlightService::new(1, SleepService(wait_time)));
assert_eq!(srv.poll_ready(), Ok(Async::Ready(()))); assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
let mut res = srv.call(()); let mut res = srv.call(());

View File

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

View File

@@ -3,7 +3,7 @@ use std::fmt;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::rc::Rc; use std::rc::Rc;
use actix_service::{NewTransform, Service, Transform, Void}; use actix_service::{Service, Transform, Void};
use futures::future::{ok, FutureResult}; use futures::future::{ok, FutureResult};
use futures::task::AtomicTask; use futures::task::AtomicTask;
use futures::unsync::oneshot; use futures::unsync::oneshot;
@@ -52,105 +52,86 @@ pub struct InOrder<S> {
_t: PhantomData<S>, _t: PhantomData<S>,
} }
impl<S> InOrder<S> impl<S> InOrder<S> {
where pub fn new<R>() -> Self
S: Service, where
S::Response: 'static, S: Service<R>,
S::Future: 'static, S::Response: 'static,
S::Error: 'static, S::Future: 'static,
{ S::Error: 'static,
pub fn new() -> Self { {
Self { _t: PhantomData } Self { _t: PhantomData }
} }
pub fn service() -> impl Transform< pub fn service<R>(service: S) -> InOrderService<S, R>
S, where
Request = S::Request, S: Service<R>,
Response = S::Response, S::Response: 'static,
Error = InOrderError<S::Error>, S::Future: 'static,
> { S::Error: 'static,
InOrderService::new() {
InOrderService::new(service)
} }
} }
impl<S> Default for InOrder<S> impl<S, R> Transform<S, R> for InOrder<S>
where where
S: Service, S: Service<R>,
S::Response: 'static, S::Response: 'static,
S::Future: 'static, S::Future: 'static,
S::Error: 'static, S::Error: 'static,
{ {
fn default() -> Self {
Self::new()
}
}
impl<S, C> NewTransform<S, C> for InOrder<S>
where
S: Service,
S::Response: 'static,
S::Future: 'static,
S::Error: 'static,
{
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = InOrderError<S::Error>; type Error = InOrderError<S::Error>;
type InitError = Void; type InitError = Void;
type Transform = InOrderService<S>; type Transform = InOrderService<S, R>;
type Future = FutureResult<Self::Transform, Self::InitError>; type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, _: &C) -> Self::Future { fn new_transform(&self, service: S) -> Self::Future {
ok(InOrderService::new()) ok(InOrderService::new(service))
} }
} }
pub struct InOrderService<S: Service> { pub struct InOrderService<S: Service<R>, R> {
service: S,
task: Rc<AtomicTask>, task: Rc<AtomicTask>,
acks: VecDeque<Record<S::Response, S::Error>>, acks: VecDeque<Record<S::Response, S::Error>>,
} }
impl<S> InOrderService<S> impl<S, R> InOrderService<S, R>
where where
S: Service, S: Service<R>,
S::Response: 'static, S::Response: 'static,
S::Future: 'static, S::Future: 'static,
S::Error: 'static, S::Error: 'static,
{ {
pub fn new() -> Self { pub fn new(service: S) -> Self {
Self { Self {
service,
acks: VecDeque::new(), acks: VecDeque::new(),
task: Rc::new(AtomicTask::new()), task: Rc::new(AtomicTask::new()),
} }
} }
} }
impl<S> Default for InOrderService<S> impl<S, R> Service<R> for InOrderService<S, R>
where where
S: Service, S: Service<R>,
S::Response: 'static, S::Response: 'static,
S::Future: 'static, S::Future: 'static,
S::Error: 'static, S::Error: 'static,
{ {
fn default() -> Self {
Self::new()
}
}
impl<S> Transform<S> for InOrderService<S>
where
S: Service,
S::Response: 'static,
S::Future: 'static,
S::Error: 'static,
{
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = InOrderError<S::Error>; type Error = InOrderError<S::Error>;
type Future = InOrderServiceResponse<S>; type Future = InOrderServiceResponse<S, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
// poll_ready could be called from different task
self.task.register(); self.task.register();
// check nested service
self.service.poll_ready().map_err(InOrderError::Service)?;
// check acks // check acks
while !self.acks.is_empty() { while !self.acks.is_empty() {
let rec = self.acks.front_mut().unwrap(); let rec = self.acks.front_mut().unwrap();
@@ -167,13 +148,13 @@ where
Ok(Async::Ready(())) Ok(Async::Ready(()))
} }
fn call(&mut self, request: S::Request, service: &mut S) -> Self::Future { fn call(&mut self, request: R) -> Self::Future {
let (tx1, rx1) = oneshot::channel(); let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = oneshot::channel(); let (tx2, rx2) = oneshot::channel();
self.acks.push_back(Record { rx: rx1, tx: tx2 }); self.acks.push_back(Record { rx: rx1, tx: tx2 });
let task = self.task.clone(); let task = self.task.clone();
tokio_current_thread::spawn(service.call(request).then(move |res| { tokio_current_thread::spawn(self.service.call(request).then(move |res| {
task.notify(); task.notify();
let _ = tx1.send(res); let _ = tx1.send(res);
Ok(()) Ok(())
@@ -184,11 +165,11 @@ where
} }
#[doc(hidden)] #[doc(hidden)]
pub struct InOrderServiceResponse<S: Service> { pub struct InOrderServiceResponse<S: Service<R>, R> {
rx: oneshot::Receiver<Result<S::Response, S::Error>>, rx: oneshot::Receiver<Result<S::Response, S::Error>>,
} }
impl<S: Service> Future for InOrderServiceResponse<S> { impl<S: Service<R>, R> Future for InOrderServiceResponse<S, R> {
type Item = S::Response; type Item = S::Response;
type Error = InOrderError<S::Error>; type Error = InOrderError<S::Error>;
@@ -215,8 +196,7 @@ mod tests {
struct Srv; struct Srv;
impl Service for Srv { impl Service<oneshot::Receiver<usize>> for Srv {
type Request = oneshot::Receiver<usize>;
type Response = usize; type Response = usize;
type Error = (); type Error = ();
type Future = Box<Future<Item = usize, Error = ()>>; type Future = Box<Future<Item = usize, Error = ()>>;
@@ -230,11 +210,11 @@ mod tests {
} }
} }
struct SrvPoll<S: Service> { struct SrvPoll<S: Service<oneshot::Receiver<usize>>> {
s: S, s: S,
} }
impl<S: Service> Future for SrvPoll<S> { impl<S: Service<oneshot::Receiver<usize>>> Future for SrvPoll<S> {
type Item = (); type Item = ();
type Error = (); type Error = ();
@@ -257,7 +237,7 @@ mod tests {
let rx3 = rx3; let rx3 = rx3;
let tx_stop = tx_stop; let tx_stop = tx_stop;
let _ = actix_rt::System::new("test").block_on(lazy(move || { let _ = actix_rt::System::new("test").block_on(lazy(move || {
let mut srv = Blank::new().apply(InOrderService::new(), Srv); let mut srv = Blank::new().and_then(InOrderService::new(Srv));
let res1 = srv.call(rx1); let res1 = srv.call(rx1);
let res2 = srv.call(rx2); let res2 = srv.call(rx2);

View File

@@ -38,12 +38,12 @@ impl<S, T, E, C> StreamNewService<S, T, E, C>
where where
C: Clone, C: Clone,
S: IntoStream, S: IntoStream,
T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>, T: NewService<Request<S>, C, Response = (), Error = E, InitError = E>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
<T::Service as Service>::Future: 'static, <T::Service as Service<Request<S>>>::Future: 'static,
{ {
pub fn new<F: IntoNewService<T, C>>(factory: F) -> Self { pub fn new<F: IntoNewService<T, Request<S>, C>>(factory: F) -> Self {
Self { Self {
factory: Rc::new(factory.into_new_service()), factory: Rc::new(factory.into_new_service()),
_t: PhantomData, _t: PhantomData,
@@ -60,16 +60,15 @@ impl<S, T, E, C> Clone for StreamNewService<S, T, E, C> {
} }
} }
impl<S, T, E, C> NewService<C> for StreamNewService<S, T, E, C> impl<S, T, E, C> NewService<S, C> for StreamNewService<S, T, E, C>
where where
C: Clone, C: Clone,
S: IntoStream + 'static, S: IntoStream + 'static,
T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>, T: NewService<Request<S>, C, Response = (), Error = E, InitError = E>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
<T::Service as Service>::Future: 'static, <T::Service as Service<Request<S>>>::Future: 'static,
{ {
type Request = S;
type Response = (); type Response = ();
type Error = E; type Error = E;
type InitError = E; type InitError = E;
@@ -91,16 +90,15 @@ pub struct StreamService<S, T, E, C = ()> {
_t: PhantomData<(S, E)>, _t: PhantomData<(S, E)>,
} }
impl<S, T, E, C> Service for StreamService<S, T, E, C> impl<S, T, E, C> Service<S> for StreamService<S, T, E, C>
where where
S: IntoStream + 'static, S: IntoStream + 'static,
T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>, T: NewService<Request<S>, C, Response = (), Error = E, InitError = E>,
T::Future: 'static, T::Future: 'static,
T::Service: 'static, T::Service: 'static,
<T::Service as Service>::Future: 'static, <T::Service as Service<Request<S>>>::Future: 'static,
C: Clone, C: Clone,
{ {
type Request = S;
type Response = (); type Response = ();
type Error = E; type Error = E;
type Future = Box<Future<Item = (), Error = E>>; type Future = Box<Future<Item = (), Error = E>>;
@@ -121,7 +119,7 @@ where
pub struct StreamDispatcher<S, T> pub struct StreamDispatcher<S, T>
where where
S: IntoStream + 'static, S: IntoStream + 'static,
T: Service<Request = Request<S>, Response = ()> + 'static, T: Service<Request<S>, Response = ()> + 'static,
T::Future: 'static, T::Future: 'static,
{ {
stream: S, stream: S,
@@ -133,13 +131,13 @@ where
impl<S, T> StreamDispatcher<S, T> impl<S, T> StreamDispatcher<S, T>
where where
S: Stream, S: Stream,
T: Service<Request = Request<S>, Response = ()>, T: Service<Request<S>, Response = ()>,
T::Future: 'static, T::Future: 'static,
{ {
pub fn new<F1, F2>(stream: F1, service: F2) -> Self pub fn new<F1, F2>(stream: F1, service: F2) -> Self
where where
F1: IntoStream<Stream = S, Item = S::Item, Error = S::Error>, F1: IntoStream<Stream = S, Item = S::Item, Error = S::Error>,
F2: IntoService<T>, F2: IntoService<T, Request<S>>,
{ {
let (err_tx, err_rx) = mpsc::unbounded(); let (err_tx, err_rx) = mpsc::unbounded();
StreamDispatcher { StreamDispatcher {
@@ -154,7 +152,7 @@ where
impl<S, T> Future for StreamDispatcher<S, T> impl<S, T> Future for StreamDispatcher<S, T>
where where
S: Stream, S: Stream,
T: Service<Request = Request<S>, Response = ()>, T: Service<Request<S>, Response = ()>,
T::Future: 'static, T::Future: 'static,
{ {
type Item = (); type Item = ();
@@ -232,8 +230,7 @@ impl<T> Clone for TakeItem<T> {
} }
} }
impl<T: Stream> NewService<()> for TakeItem<T> { impl<T: Stream> NewService<T, ()> for TakeItem<T> {
type Request = T;
type Response = (Option<T::Item>, T); type Response = (Option<T::Item>, T);
type Error = T::Error; type Error = T::Error;
type InitError = (); type InitError = ();
@@ -256,8 +253,7 @@ impl<T> Clone for TakeItemService<T> {
} }
} }
impl<T: Stream> Service for TakeItemService<T> { impl<T: Stream> Service<T> for TakeItemService<T> {
type Request = T;
type Response = (Option<T::Item>, T); type Response = (Option<T::Item>, T);
type Error = T::Error; type Error = T::Error;
type Future = TakeItemServiceResponse<T>; type Future = TakeItemServiceResponse<T>;

View File

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

View File

@@ -6,7 +6,7 @@ use std::fmt;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::time::Duration; use std::time::Duration;
use actix_service::{NewTransform, Service, Transform}; use actix_service::{Service, Transform};
use futures::future::{ok, FutureResult}; use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll}; use futures::{Async, Future, Poll};
use tokio_timer::{clock, Delay}; use tokio_timer::{clock, Delay};
@@ -80,19 +80,19 @@ impl<E> Clone for Timeout<E> {
} }
} }
impl<S, C, E> NewTransform<S, C> for Timeout<E> impl<S, R, E> Transform<S, R> for Timeout<E>
where where
S: Service, S: Service<R>,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = TimeoutError<S::Error>; type Error = TimeoutError<S::Error>;
type InitError = E; type InitError = E;
type Transform = TimeoutService; type Transform = TimeoutService<S>;
type Future = FutureResult<Self::Transform, Self::InitError>; type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, _: &C) -> Self::Future { fn new_transform(&self, service: S) -> Self::Future {
ok(TimeoutService { ok(TimeoutService {
service,
timeout: self.timeout, timeout: self.timeout,
}) })
} }
@@ -100,32 +100,32 @@ where
/// Applies a timeout to requests. /// Applies a timeout to requests.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TimeoutService { pub struct TimeoutService<S> {
service: S,
timeout: Duration, timeout: Duration,
} }
impl TimeoutService { impl<S> TimeoutService<S> {
pub fn new(timeout: Duration) -> Self { pub fn new(timeout: Duration, service: S) -> Self {
TimeoutService { timeout } TimeoutService { service, timeout }
} }
} }
impl<S> Transform<S> for TimeoutService impl<S, R> Service<R> for TimeoutService<S>
where where
S: Service, S: Service<R>,
{ {
type Request = S::Request;
type Response = S::Response; type Response = S::Response;
type Error = TimeoutError<S::Error>; type Error = TimeoutError<S::Error>;
type Future = TimeoutServiceResponse<S>; type Future = TimeoutServiceResponse<S, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(())) self.service.poll_ready().map_err(TimeoutError::Service)
} }
fn call(&mut self, request: S::Request, service: &mut S) -> Self::Future { fn call(&mut self, request: R) -> Self::Future {
TimeoutServiceResponse { TimeoutServiceResponse {
fut: service.call(request), fut: self.service.call(request),
sleep: Delay::new(clock::now() + self.timeout), sleep: Delay::new(clock::now() + self.timeout),
} }
} }
@@ -133,14 +133,14 @@ where
/// `TimeoutService` response future /// `TimeoutService` response future
#[derive(Debug)] #[derive(Debug)]
pub struct TimeoutServiceResponse<T: Service> { pub struct TimeoutServiceResponse<T: Service<R>, R> {
fut: T::Future, fut: T::Future,
sleep: Delay, sleep: Delay,
} }
impl<T> Future for TimeoutServiceResponse<T> impl<T, R> Future for TimeoutServiceResponse<T, R>
where where
T: Service, T: Service<R>,
{ {
type Item = T::Response; type Item = T::Response;
type Error = TimeoutError<T::Error>; type Error = TimeoutError<T::Error>;
@@ -175,8 +175,7 @@ mod tests {
struct SleepService(Duration); struct SleepService(Duration);
impl Service for SleepService { impl Service<()> for SleepService {
type Request = ();
type Response = (); type Response = ();
type Error = (); type Error = ();
type Future = Box<Future<Item = (), Error = ()>>; type Future = Box<Future<Item = (), Error = ()>>;
@@ -197,7 +196,7 @@ mod tests {
let res = actix_rt::System::new("test").block_on(lazy(|| { let res = actix_rt::System::new("test").block_on(lazy(|| {
let mut timeout = Blank::default() let mut timeout = Blank::default()
.apply(TimeoutService::new(resolution), SleepService(wait_time)); .and_then(TimeoutService::new(resolution, SleepService(wait_time)));
timeout.call(()) timeout.call(())
})); }));
assert_eq!(res, Ok(())); assert_eq!(res, Ok(()));
@@ -210,7 +209,7 @@ mod tests {
let res = actix_rt::System::new("test").block_on(lazy(|| { let res = actix_rt::System::new("test").block_on(lazy(|| {
let mut timeout = Blank::default() let mut timeout = Blank::default()
.apply(TimeoutService::new(resolution), SleepService(wait_time)); .and_then(TimeoutService::new(resolution, SleepService(wait_time)));
timeout.call(()) timeout.call(())
})); }));
assert_eq!(res, Err(TimeoutError::Timeout)); assert_eq!(res, Err(TimeoutError::Timeout));
@@ -222,7 +221,7 @@ mod tests {
let wait_time = Duration::from_millis(150); let wait_time = Duration::from_millis(150);
let res = actix_rt::System::new("test").block_on(lazy(|| { let res = actix_rt::System::new("test").block_on(lazy(|| {
let timeout = BlankNewService::<_, _, ()>::default() let timeout = BlankNewService::<(), (), ()>::default()
.apply(Timeout::new(resolution), || Ok(SleepService(wait_time))); .apply(Timeout::new(resolution), || Ok(SleepService(wait_time)));
if let Async::Ready(mut to) = timeout.new_service(&()).poll().unwrap() { if let Async::Ready(mut to) = timeout.new_service(&()).poll().unwrap() {
to.call(()) to.call(())

View File

@@ -9,6 +9,10 @@ pub use self::path::Path;
pub use self::resource::ResourceDef; pub use self::resource::ResourceDef;
pub use self::router::{ResourceInfo, Router, RouterBuilder}; pub use self::router::{ResourceInfo, Router, RouterBuilder};
pub trait Resource<T: ResourcePath> {
fn resource_path(&mut self) -> &mut Path<T>;
}
pub trait ResourcePath { pub trait ResourcePath {
fn path(&self) -> &str; fn path(&self) -> &str;
} }

View File

@@ -4,7 +4,7 @@ use std::rc::Rc;
use serde::de; use serde::de;
use crate::de::PathDeserializer; use crate::de::PathDeserializer;
use crate::ResourcePath; use crate::{Resource, ResourcePath};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub(crate) enum PathItem { pub(crate) enum PathItem {
@@ -202,3 +202,9 @@ impl<T: ResourcePath> Index<usize> for Path<T> {
} }
} }
} }
impl<T: ResourcePath> Resource<T> for Path<T> {
fn resource_path(&mut self) -> &mut Self {
self
}
}

View File

@@ -64,6 +64,17 @@ impl ResourceDef {
ResourceDef::with_prefix(path, true) ResourceDef::with_prefix(path, true)
} }
/// Parse path pattern and create new `Pattern` instance.
/// Inserts `/` to begging of the pattern.
///
///
/// Use `prefix` type instead of `static`.
///
/// Panics if path regex pattern is wrong.
pub fn root_prefix(path: &str) -> Self {
ResourceDef::with_prefix(&insert_slash(path), true)
}
/// Construct external resource def /// Construct external resource def
/// ///
/// Panics if path pattern is malformed. /// Panics if path pattern is malformed.
@@ -313,6 +324,14 @@ impl From<String> for ResourceDef {
} }
} }
pub(crate) fn insert_slash(path: &str) -> String {
let mut path = path.to_owned();
if !path.is_empty() && !path.starts_with('/') {
path.insert(0, '/');
};
path
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@@ -408,6 +427,11 @@ mod tests {
assert!(re.is_match("/name/")); assert!(re.is_match("/name/"));
assert!(re.is_match("/name/gs")); assert!(re.is_match("/name/gs"));
assert!(!re.is_match("/name")); assert!(!re.is_match("/name"));
let re = ResourceDef::root_prefix("name/");
assert!(re.is_match("/name/"));
assert!(re.is_match("/name/gs"));
assert!(!re.is_match("/name"));
} }
#[test] #[test]

View File

@@ -1,9 +1,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use crate::path::Path;
use crate::resource::ResourceDef; use crate::resource::ResourceDef;
use crate::ResourcePath; use crate::{Resource, ResourcePath};
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) enum ResourceId { pub(crate) enum ResourceId {
@@ -26,14 +25,14 @@ pub(crate) struct ResourceMap {
} }
/// Resource router. /// Resource router.
pub struct Router<T> { pub struct Router<T, U = ()> {
rmap: Rc<ResourceMap>, rmap: Rc<ResourceMap>,
named: HashMap<String, ResourceDef>, named: HashMap<String, ResourceDef>,
resources: Vec<T>, resources: Vec<(T, Option<U>)>,
} }
impl<T> Router<T> { impl<T, U> Router<T, U> {
pub fn build() -> RouterBuilder<T> { pub fn build() -> RouterBuilder<T, U> {
RouterBuilder { RouterBuilder {
rmap: ResourceMap::default(), rmap: ResourceMap::default(),
named: HashMap::new(), named: HashMap::new(),
@@ -41,52 +40,71 @@ impl<T> Router<T> {
} }
} }
pub fn recognize<U: ResourcePath>(&self, path: &mut Path<U>) -> Option<(&T, ResourceInfo)> { pub fn recognize<R: Resource<P>, P: ResourcePath>(
if !path.path().is_empty() { &self,
for (idx, resource) in self.rmap.patterns.iter().enumerate() { res: &mut R,
if resource.match_path(path) { ) -> Option<(&T, ResourceInfo)> {
let info = ResourceInfo { for (idx, resource) in self.rmap.patterns.iter().enumerate() {
rmap: self.rmap.clone(), if resource.match_path(res.resource_path()) {
resource: ResourceId::Normal(idx as u16), let info = ResourceInfo {
}; rmap: self.rmap.clone(),
return Some((&self.resources[idx], info)); resource: ResourceId::Normal(idx as u16),
} };
return Some((&self.resources[idx].0, info));
} }
} }
None None
} }
pub fn recognize_mut<U: ResourcePath>( pub fn recognize_mut<R: Resource<P>, P: ResourcePath>(
&mut self, &mut self,
path: &mut Path<U>, res: &mut R,
) -> Option<(&mut T, ResourceInfo)> { ) -> Option<(&mut T, ResourceInfo)> {
if !path.path().is_empty() { for (idx, resource) in self.rmap.patterns.iter().enumerate() {
for (idx, resource) in self.rmap.patterns.iter().enumerate() { if resource.match_path(res.resource_path()) {
if resource.match_path(path) { let info = ResourceInfo {
let info = ResourceInfo { rmap: self.rmap.clone(),
rmap: self.rmap.clone(), resource: ResourceId::Normal(idx as u16),
resource: ResourceId::Normal(idx as u16), };
}; return Some((&mut self.resources[idx].0, info));
return Some((&mut self.resources[idx], info)); }
} }
None
}
pub fn recognize_mut_checked<R: Resource<P>, P: ResourcePath, F>(
&mut self,
res: &mut R,
check: F,
) -> Option<(&mut T, ResourceInfo)>
where
F: Fn(&R, &Option<U>) -> bool,
{
for (idx, resource) in self.rmap.patterns.iter().enumerate() {
if resource.match_path(res.resource_path()) && check(res, &self.resources[idx].1) {
let info = ResourceInfo {
rmap: self.rmap.clone(),
resource: ResourceId::Normal(idx as u16),
};
return Some((&mut self.resources[idx].0, info));
} }
} }
None None
} }
} }
impl<'a, T> IntoIterator for &'a Router<T> { impl<'a, T, U> IntoIterator for &'a Router<T, U> {
type Item = &'a T; type Item = &'a (T, Option<U>);
type IntoIter = std::slice::Iter<'a, T>; type IntoIter = std::slice::Iter<'a, (T, Option<U>)>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
self.resources.iter() self.resources.iter()
} }
} }
impl<'a, T> IntoIterator for &'a mut Router<T> { impl<'a, T, U> IntoIterator for &'a mut Router<T, U> {
type Item = &'a mut T; type Item = &'a mut (T, Option<U>);
type IntoIter = std::slice::IterMut<'a, T>; type IntoIter = std::slice::IterMut<'a, (T, Option<U>)>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
self.resources.iter_mut() self.resources.iter_mut()
@@ -108,33 +126,40 @@ impl ResourceMap {
} }
} }
pub struct RouterBuilder<T> { pub struct RouterBuilder<T, U = ()> {
rmap: ResourceMap, rmap: ResourceMap,
named: HashMap<String, ResourceDef>, named: HashMap<String, ResourceDef>,
resources: Vec<T>, resources: Vec<(T, Option<U>)>,
} }
impl<T> RouterBuilder<T> { impl<T, U> RouterBuilder<T, U> {
/// Register resource for specified path. /// Register resource for specified path.
pub fn path(&mut self, path: &str, resource: T) { pub fn path(&mut self, path: &str, resource: T) {
self.rmap.register(ResourceDef::new(path)); self.rmap.register(ResourceDef::new(path));
self.resources.push(resource); self.resources.push((resource, None));
} }
/// Register resource for specified path prefix. /// Register resource for specified path prefix.
pub fn prefix(&mut self, prefix: &str, resource: T) { pub fn prefix(&mut self, prefix: &str, resource: T) {
self.rmap.register(ResourceDef::prefix(prefix)); self.rmap.register(ResourceDef::prefix(prefix));
self.resources.push(resource); self.resources.push((resource, None));
} }
/// Register resource for ResourceDef /// Register resource for ResourceDef
pub fn rdef(&mut self, rdef: ResourceDef, resource: T) { pub fn rdef(&mut self, rdef: ResourceDef, resource: T) {
self.rmap.register(rdef); self.rmap.register(rdef);
self.resources.push(resource); self.resources.push((resource, None));
}
/// Method attachs user data to lastly added resource.
///
/// This panics if no resources were added.
pub fn set_user_data(&mut self, userdata: Option<U>) {
self.resources.last_mut().unwrap().1 = userdata;
} }
/// Finish configuration and create router instance. /// Finish configuration and create router instance.
pub fn finish(self) -> Router<T> { pub fn finish(self) -> Router<T, U> {
Router { Router {
rmap: Rc::new(self.rmap), rmap: Rc::new(self.rmap),
named: self.named, named: self.named,