1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-08-12 14:27:05 +02:00

Compare commits

..

11 Commits

Author SHA1 Message Date
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
48 changed files with 789 additions and 835 deletions

View File

@@ -27,7 +27,8 @@ default = []
ssl = ["openssl", "tokio-openssl"]
[dependencies]
actix-service = "0.3.0"
#actix-service = "0.3.0"
actix-service = { path="../actix-service" }
actix-codec = "0.1.0"
futures = "0.1"
tokio-tcp = "0.1"

View File

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

View File

@@ -67,8 +67,7 @@ impl<T> Clone for Resolver<T> {
}
}
impl<T: RequestHost> Service for Resolver<T> {
type Request = T;
impl<T: RequestHost> Service<T> for Resolver<T> {
type Response = (T, VecDeque<IpAddr>);
type Error = ResolveError;
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, ()> {
pub fn service(
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 {
connector: connector,
_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>
{
type Request = (R, T);
type Response = (R, SslStream<T>);
type Error = HandshakeError<T>;
type Service = OpensslConnectorService<R, T>;
@@ -67,8 +65,9 @@ pub struct OpensslConnectorService<R, T> {
_t: PhantomData<(R, T)>,
}
impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service for OpensslConnectorService<R, T> {
type Request = (R, T);
impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service<(R, T)>
for OpensslConnectorService<R, T>
{
type Response = (R, SslStream<T>);
type Error = HandshakeError<T>;
type Future = ConnectAsyncExt<R, T>;

View File

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

View File

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

View File

@@ -72,7 +72,7 @@ impl Builder {
/// This function will start tokio runtime and will finish once the
/// `System::stop()` message get called.
/// 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
F: FnOnce() + 'static,
{
@@ -140,7 +140,7 @@ pub struct SystemRunner {
impl SystemRunner {
/// This function will start event loop and will finish once the
/// `System::stop()` function is called.
pub fn run(self) -> i32 {
pub fn run(self) -> io::Result<()> {
let SystemRunner { mut rt, stop, .. } = self;
// run loop
@@ -148,12 +148,21 @@ impl SystemRunner {
Arbiter::run_system();
Ok::<_, ()>(())
}));
let code = match rt.block_on(stop) {
Ok(code) => code,
Err(_) => 1,
let result = match rt.block_on(stop) {
Ok(code) => {
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();
code
result
}
/// Execute a future and wait for result.

View File

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

View File

@@ -1,9 +1,7 @@
use std::error::Error;
use std::fmt;
use std::io;
use std::{fmt, io};
use futures::{future, Future};
use tokio_current_thread::Handle as ExecutorHandle;
use futures::Future;
use tokio_current_thread::{self as current_thread, CurrentThread};
use tokio_executor;
use tokio_reactor::{self, Reactor};
@@ -26,58 +24,6 @@ pub struct Runtime {
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.
#[derive(Debug)]
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.
///
/// See [module level][mod] documentation for more details.

View File

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

View File

@@ -1,6 +1,11 @@
# Changes
## [0.3.1] - 2019-03-xx
## [0.4.0] - 2019-03-xx
* Upgrade actix-service
## [0.3.1] - 2019-03-04
### Added

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-server"
version = "0.3.0"
version = "0.3.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix server - General purpose tcp server"
keywords = ["network", "framework", "async", "futures"]
@@ -33,7 +33,8 @@ ssl = ["openssl", "tokio-openssl"]
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
[dependencies]
actix-service = "0.3.0"
#actix-service = "0.3.2"
actix-service = { path="../actix-service" }
actix-rt = "0.1.0"
log = "0.4"

View File

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

View File

@@ -23,7 +23,7 @@ pub(crate) enum ServerMessage {
}
pub trait ServiceFactory: Send + Clone + 'static {
type NewService: NewService<Request = TcpStream>;
type NewService: NewService<TcpStream>;
fn create(&self) -> Self::NewService;
}
@@ -38,7 +38,7 @@ pub(crate) trait InternalServiceFactory: Send {
pub(crate) type BoxedServerService = Box<
Service<
Request = (Option<CounterGuard>, ServerMessage),
(Option<CounterGuard>, ServerMessage),
Response = (),
Error = (),
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
T: Service<Request = TcpStream>,
T: Service<TcpStream>,
T::Future: 'static,
T::Error: 'static,
{
type Request = (Option<CounterGuard>, ServerMessage);
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;
@@ -155,7 +154,7 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
impl<F, T> ServiceFactory for F
where
F: Fn() -> T + Send + Clone + 'static,
T: NewService<Request = TcpStream>,
T: NewService<TcpStream>,
{
type NewService = T;

View File

@@ -36,8 +36,7 @@ impl<T: AsyncRead + AsyncWrite> Clone for NativeTlsAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> NewService for NativeTlsAcceptor<T> {
type Request = T;
impl<T: AsyncRead + AsyncWrite> NewService<T> for NativeTlsAcceptor<T> {
type Response = TlsStream<T>;
type Error = Error;
type Service = NativeTlsAcceptorService<T>;
@@ -61,8 +60,7 @@ pub struct NativeTlsAcceptorService<T> {
conns: Counter,
}
impl<T: AsyncRead + AsyncWrite> Service for NativeTlsAcceptorService<T> {
type Request = T;
impl<T: AsyncRead + AsyncWrite> Service<T> for NativeTlsAcceptorService<T> {
type Response = TlsStream<T>;
type Error = Error;
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> {
type Request = T;
impl<T: AsyncRead + AsyncWrite> NewService<T> for OpensslAcceptor<T> {
type Response = SslStream<T>;
type Error = HandshakeError<T>;
type Service = OpensslAcceptorService<T>;
@@ -61,8 +60,7 @@ pub struct OpensslAcceptorService<T> {
conns: Counter,
}
impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
type Request = T;
impl<T: AsyncRead + AsyncWrite> Service<T> for OpensslAcceptorService<T> {
type Response = SslStream<T>;
type Error = HandshakeError<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> {
type Request = T;
impl<T: AsyncRead + AsyncWrite> NewService<T> for RustlsAcceptor<T> {
type Response = TlsStream<T, ServerSession>;
type Error = io::Error;
type Service = RustlsAcceptorService<T>;
@@ -63,8 +62,7 @@ pub struct RustlsAcceptorService<T> {
conns: Counter,
}
impl<T: AsyncRead + AsyncWrite> Service for RustlsAcceptorService<T> {
type Request = T;
impl<T: AsyncRead + AsyncWrite> Service<T> for RustlsAcceptorService<T> {
type Response = TlsStream<T, ServerSession>;
type Error = io::Error;
type Future = RustlsAcceptorServiceFut<T>;

View File

@@ -1,5 +1,25 @@
# 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

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-service"
version = "0.3.1"
version = "0.3.2"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix Service"
keywords = ["network", "framework", "async", "futures"]
@@ -25,3 +25,6 @@ path = "src/lib.rs"
[dependencies]
futures = "0.1.24"
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 super::{IntoNewService, NewService, Service};
@@ -16,10 +14,10 @@ pub struct AndThen<A, B> {
impl<A, B> AndThen<A, B> {
/// Create new `AndThen` combinator
pub fn new(a: A, b: B) -> Self
pub fn new<R>(a: A, b: B) -> Self
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<R>,
B: Service<A::Response, Error = A::Error>,
{
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
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<R>,
B: Service<A::Response, Error = A::Error>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Future = AndThenFuture<A, B>;
type Future = AndThenFuture<A, B, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.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())
}
}
pub struct AndThenFuture<A, B>
pub struct AndThenFuture<A, B, R>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<R>,
B: Service<A::Response, Error = A::Error>,
{
b: Cell<B>,
fut_b: Option<B::Future>,
fut_a: Option<A::Future>,
}
impl<A, B> AndThenFuture<A, B>
impl<A, B, R> AndThenFuture<A, B, R>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<R>,
B: Service<A::Response, Error = A::Error>,
{
fn new(a: A::Future, b: Cell<B>) -> Self {
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
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<R>,
B: Service<A::Response, Error = A::Error>,
{
type Item = B::Response;
type Error = A::Error;
@@ -107,46 +104,43 @@ where
}
/// `AndThenNewService` new service combinator
pub struct AndThenNewService<A, B, C> {
pub struct AndThenNewService<A, B> {
a: A,
b: B,
_t: PhantomData<C>,
}
impl<A, B, C> AndThenNewService<A, B, C> {
impl<A, B> AndThenNewService<A, B> {
/// 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
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService<R, C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
{
Self {
a,
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
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService<R, C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Service = AndThen<A::Service, B::Service>;
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 {
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
A: Clone,
B: Clone,
@@ -155,15 +149,14 @@ where
Self {
a: self.a.clone(),
b: self.b.clone(),
_t: PhantomData,
}
}
}
pub struct AndThenNewServiceFuture<A, B, C>
pub struct AndThenNewServiceFuture<A, B, R, C>
where
A: NewService<C>,
B: NewService<C, Request = A::Response>,
A: NewService<R, C>,
B: NewService<A::Response, C>,
{
fut_b: B::Future,
fut_a: A::Future,
@@ -171,10 +164,10 @@ where
b: Option<B::Service>,
}
impl<A, B, C> AndThenNewServiceFuture<A, B, C>
impl<A, B, R, C> AndThenNewServiceFuture<A, B, R, C>
where
A: NewService<C>,
B: NewService<C, Request = A::Response>,
A: NewService<R, C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
{
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
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
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService<R, C>,
B: NewService<A::Response, C, Error = A::Error, InitError = A::InitError>,
{
type Item = AndThen<A::Service, B::Service>;
type Error = A::InitError;
@@ -229,8 +222,7 @@ mod tests {
use crate::{NewService, Service, ServiceExt};
struct Srv1(Rc<Cell<usize>>);
impl Service for Srv1 {
type Request = &'static str;
impl Service<&'static str> for Srv1 {
type Response = &'static str;
type Error = ();
type Future = FutureResult<Self::Response, ()>;
@@ -248,8 +240,7 @@ mod tests {
#[derive(Clone)]
struct Srv2(Rc<Cell<usize>>);
impl Service for Srv2 {
type Request = &'static str;
impl Service<&'static str> for Srv2 {
type Response = (&'static str, &'static str);
type Error = ();
type Future = FutureResult<Self::Response, ()>;

View File

@@ -1,3 +1,4 @@
use std::marker::PhantomData;
use std::rc::Rc;
use futures::{Async, Future, Poll};
@@ -7,22 +8,22 @@ use crate::from_err::FromErr;
use crate::{NewService, Transform};
/// `Apply` new service combinator
pub struct AndThenTransformNewService<T, A, B, C> {
pub struct AndThenTransform<T, A, B, BR> {
a: A,
b: B,
t: Rc<T>,
_t: std::marker::PhantomData<C>,
_t: PhantomData<BR>,
}
impl<T, A, B, C> AndThenTransformNewService<T, A, B, C>
where
A: NewService<C>,
B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
impl<T, A, B, BR> AndThenTransform<T, A, B, BR> {
/// 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 {
a,
b,
@@ -32,7 +33,7 @@ where
}
}
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
A: Clone,
B: Clone,
@@ -47,23 +48,22 @@ 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
A: NewService<C>,
B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
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>,
{
type Request = A::Request;
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
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 {
AndThenTransformNewServiceFuture {
AndThenTransformFuture {
a: None,
t: None,
t_cell: self.t.clone(),
@@ -74,11 +74,11 @@ where
}
}
pub struct AndThenTransformNewServiceFuture<T, A, B, C>
pub struct AndThenTransformFuture<T, A, B, AR, BR, C>
where
A: NewService<C>,
B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
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>,
{
fut_a: A::Future,
@@ -89,11 +89,11 @@ where
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
A: NewService<C>,
B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
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>,
{
type Item = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
@@ -138,8 +138,7 @@ mod tests {
#[derive(Clone)]
struct Srv;
impl Service for Srv {
type Request = ();
impl Service<()> for Srv {
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;

View File

@@ -6,30 +6,23 @@ use super::{IntoNewService, IntoService, NewService, Service};
use crate::cell::Cell;
/// `Apply` service combinator
pub struct AndThenApply<A, B, F, Out>
where
A: Service,
B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
pub struct AndThenApply<A, B, F, Out, AReq, BReq> {
a: A,
b: Cell<B>,
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
A: Service,
B: Service<Error = A::Error>,
A: Service<AReq>,
B: Service<BReq, Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
/// 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 {
f: Cell::new(f),
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
A: Service + Clone,
B: Service<Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
A: Clone,
{
fn clone(&self) -> Self {
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
A: Service,
B: Service<Error = A::Error>,
A: Service<AReq>,
B: Service<BReq, Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
type Request = A::Request;
type Response = Out::Item;
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> {
try_ready!(self.a.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 {
b: self.b.clone(),
f: self.f.clone(),
fut_b: None,
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
A: Service,
B: Service<Error = A::Error>,
A: Service<AReq>,
B: Service<BReq, Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
@@ -97,12 +86,13 @@ where
f: Cell<F>,
fut_a: Option<A::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
A: Service,
B: Service<Error = A::Error>,
A: Service<AReq>,
B: Service<BReq, Error = A::Error>,
F: FnMut(A::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
@@ -129,23 +119,23 @@ where
}
/// `ApplyNewService` new service combinator
pub struct AndThenApplyNewService<A, B, F, Out, Cfg> {
pub struct AndThenApplyNewService<A, B, F, Out, AReq, BReq, Cfg> {
a: A,
b: B,
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
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService<AReq, Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
/// 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,
b: B1,
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
A: 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
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService<AReq, Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
{
type Request = A::Request;
type Response = Out::Item;
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 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 {
AndThenApplyNewServiceFuture {
a: None,
b: None,
f: self.f.clone(),
fut_a: self.a.new_service(cfg),
fut_b: self.b.new_service(cfg),
fut_a: self.a.new_service(cfg).into_future(),
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
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService<AReq, Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<A::Error>,
@@ -216,15 +207,16 @@ where
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
A: NewService<Cfg>,
B: NewService<Cfg, Error = A::Error, InitError = A::InitError>,
A: NewService<AReq, Cfg>,
B: NewService<BReq, Cfg, Error = A::Error, InitError = A::InitError>,
F: FnMut(A::Response, &mut B::Service) -> Out,
Out: IntoFuture,
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;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@@ -263,8 +255,7 @@ mod tests {
#[derive(Clone)]
struct Srv;
impl Service for Srv {
type Request = ();
impl Service<()> for Srv {
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;

View File

@@ -5,24 +5,23 @@ use futures::{Async, Future, IntoFuture, Poll};
use super::{IntoNewService, IntoService, NewService, Service};
/// `Apply` service combinator
pub struct Apply<T, F, In, Out>
where
T: Service,
{
pub struct Apply<T, R, F, In, Out> {
service: T,
f: F,
r: PhantomData<(In, Out)>,
r: PhantomData<(R, In, Out)>,
}
impl<T, F, In, Out> Apply<T, F, In, Out>
impl<T, R, F, In, Out> Apply<T, R, F, In, Out>
where
T: Service,
F: FnMut(In, &mut T) -> Out,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
/// Create new `Apply` combinator
pub fn new<I: IntoService<T>>(service: I, f: F) -> Self {
pub fn new<I: IntoService<T, R>>(service: I, f: F) -> Self
where
T: Service<R>,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
Self {
service: service.into_service(),
f,
@@ -31,9 +30,9 @@ where
}
}
impl<T, F, In, Out> Clone for Apply<T, F, In, Out>
impl<T, R, F, In, Out> Clone for Apply<T, R, F, In, Out>
where
T: Service + Clone,
T: Clone,
F: Clone,
{
fn clone(&self) -> Self {
@@ -45,14 +44,13 @@ where
}
}
impl<T, F, In, Out> Service for Apply<T, F, In, Out>
impl<T, R, F, In, Out> Service<In> for Apply<T, R, F, In, Out>
where
T: Service,
T: Service<R>,
F: FnMut(In, &mut T) -> Out,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
type Request = In;
type Response = Out::Item;
type Error = Out::Error;
type Future = Out::Future;
@@ -67,24 +65,21 @@ where
}
/// `ApplyNewService` new service combinator
pub struct ApplyNewService<T, F, In, Out, Cfg>
where
T: NewService<Cfg>,
{
pub struct ApplyNewService<T, F, In, Out, Req> {
service: T,
f: F,
r: PhantomData<(In, Out, Cfg)>,
r: PhantomData<(In, Out, Req)>,
}
impl<T, F, In, Out, Cfg> ApplyNewService<T, F, In, Out, Cfg>
where
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
impl<T, F, In, Out, Req> ApplyNewService<T, F, In, Out, Req> {
/// Create new `ApplyNewService` new service instance
pub fn new<F1: IntoNewService<T, Cfg>>(service: F1, f: F) -> Self {
pub fn new<Cfg, F1: IntoNewService<T, Req, Cfg>>(service: F1, f: F) -> Self
where
T: NewService<Req, Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
Self {
f,
service: service.into_new_service(),
@@ -93,11 +88,10 @@ where
}
}
impl<T, F, In, Out, Cfg> Clone for ApplyNewService<T, F, In, Out, Cfg>
impl<T, F, In, Out, Req> Clone for ApplyNewService<T, F, In, Out, Req>
where
T: NewService<Cfg> + Clone,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
T: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
@@ -108,29 +102,28 @@ where
}
}
impl<T, F, In, Out, Cfg> NewService<Cfg> for ApplyNewService<T, F, In, Out, Cfg>
impl<T, F, In, Out, Req, Cfg> NewService<In, Cfg> for ApplyNewService<T, F, In, Out, Req>
where
T: NewService<Cfg>,
T: NewService<Req, Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
type Request = In;
type Response = Out::Item;
type Error = Out::Error;
type Service = Apply<T::Service, F, In, Out>;
type Service = Apply<T::Service, Req, F, In, Out>;
type InitError = T::InitError;
type Future = ApplyNewServiceFuture<T, F, In, Out, Cfg>;
type Future = ApplyNewServiceFuture<T, F, In, Out, Req, Cfg>;
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, Cfg>
pub struct ApplyNewServiceFuture<T, F, In, Out, Req, Cfg>
where
T: NewService<Cfg>,
T: NewService<Req, Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
@@ -139,9 +132,9 @@ where
r: PhantomData<(In, Out)>,
}
impl<T, F, In, Out, Cfg> ApplyNewServiceFuture<T, F, In, Out, Cfg>
impl<T, F, In, Out, Req, Cfg> ApplyNewServiceFuture<T, F, In, Out, Req, Cfg>
where
T: NewService<Cfg>,
T: NewService<Req, Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
@@ -154,14 +147,14 @@ where
}
}
impl<T, F, In, Out, Cfg> Future for ApplyNewServiceFuture<T, F, In, Out, Cfg>
impl<T, F, In, Out, Req, Cfg> Future for ApplyNewServiceFuture<T, F, In, Out, Req, Cfg>
where
T: NewService<Cfg>,
T: NewService<Req, Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
type Item = Apply<T::Service, F, In, Out>;
type Item = Apply<T::Service, Req, F, In, Out>;
type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@@ -183,8 +176,7 @@ mod tests {
#[derive(Clone)]
struct Srv;
impl Service for Srv {
type Request = ();
impl Service<()> for Srv {
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;

View File

@@ -30,8 +30,7 @@ impl<R, E> Default for Blank<R, E> {
}
}
impl<R, E> Service for Blank<R, E> {
type Request = R;
impl<R, E> Service<R> for Blank<R, E> {
type Response = R;
type Error = 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> {
type Request = R;
impl<R, E1, E2> NewService<R, ()> for BlankNewService<R, E1, E2> {
type Response = R;
type Error = E1;
type Service = Blank<R, E1>;

View File

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

View File

@@ -15,21 +15,21 @@ where
}
/// 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
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
S: Service<Req>,
{
FnNewServiceNoConfig::new(f)
}
/// 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
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
S: Service<Req>,
{
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
F: FnMut(Req) -> Out,
Out: IntoFuture,
{
type Request = Req;
type Response = Out::Item;
type Error = Out::Error;
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
F: FnMut(Req) -> Out + 'static,
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
F: FnMut(Req) -> Out + Clone,
Out: IntoFuture,
{
type Request = Req;
type Response = Out::Item;
type Error = Out::Error;
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
F: Fn(Req) -> Out + Clone,
Out: IntoFuture,
@@ -153,33 +151,33 @@ where
}
/// Converter for `Fn() -> Future<Service>` fn
pub struct FnNewServiceNoConfig<F, R, S, E>
pub struct FnNewServiceNoConfig<F, R, S, E, Req>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
S: Service<Req>,
{
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
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
S: Service<Req>,
{
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
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
S: Service<Req>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
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
F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service,
S: Service<Req>,
{
fn clone(&self) -> Self {
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
F: Fn() -> R,
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)
}
}
/// 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
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
S: Service<Req>,
{
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
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
S: Service<Req>,
{
pub fn new(f: F) -> Self {
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
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service,
S: Service<Req>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
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
F: Fn(&C) -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service,
S: Service<Req>,
{
fn clone(&self) -> Self {
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
F: Fn(&C) -> R,
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)
}
}
#[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

@@ -5,16 +5,16 @@ use futures::IntoFuture;
use crate::{Apply, IntoTransform, Service, Transform};
pub struct FnTransform<F, S, In, Out, Err>
pub struct FnTransform<F, S, R, In, Out, Err>
where
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
{
f: F,
_t: PhantomData<(S, In, Out, Err)>,
_t: PhantomData<(S, R, In, Out, Err)>,
}
impl<F, S, In, Out, Err> FnTransform<F, S, In, Out, Err>
impl<F, S, R, In, Out, Err> FnTransform<F, S, R, In, Out, Err>
where
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
@@ -24,17 +24,16 @@ where
}
}
impl<F, S, In, Out, Err> Transform<S> for FnTransform<F, S, In, Out, Err>
impl<F, S, R, In, Out, Err> Transform<S, In> for FnTransform<F, S, R, In, Out, Err>
where
S: Service,
S: Service<R>,
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<S::Error>,
{
type Request = In;
type Response = Out::Item;
type Error = Out::Error;
type Transform = Apply<S, F, In, Out>;
type Transform = Apply<S, R, F, In, Out>;
type InitError = Err;
type Future = FutureResult<Self::Transform, Self::InitError>;
@@ -43,19 +42,19 @@ where
}
}
impl<F, S, In, Out, Err> IntoTransform<FnTransform<F, S, In, Out, Err>, S> for F
impl<F, S, R, In, Out, Err> IntoTransform<FnTransform<F, S, R, In, Out, Err>, S, In> for F
where
S: Service,
S: Service<R>,
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<S::Error>,
{
fn into_transform(self) -> FnTransform<F, S, In, Out, Err> {
fn into_transform(self) -> FnTransform<F, S, R, In, Out, Err> {
FnTransform::new(self)
}
}
impl<F, S, In, Out, Err> Clone for FnTransform<F, S, In, Out, Err>
impl<F, S, R, In, Out, Err> Clone for FnTransform<F, S, R, In, Out, Err>
where
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,

View File

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

View File

@@ -23,7 +23,7 @@ mod transform;
mod transform_map_init_err;
pub use self::and_then::{AndThen, AndThenNewService};
use self::and_then_apply::AndThenTransformNewService;
use self::and_then_apply::AndThenTransform;
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
pub use self::apply::{Apply, ApplyNewService};
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
@@ -33,13 +33,12 @@ pub use self::map::{Map, MapNewService};
pub use self::map_err::{MapErr, MapErrNewService};
pub use self::map_init_err::MapInitErr;
pub use self::then::{Then, ThenNewService};
pub use self::transform::{IntoTransform, Transform};
pub use self::transform::{ApplyTransform, IntoTransform, Transform};
/// An asynchronous function from `Request` to a `Response`.
pub trait Service {
/// Requests handled by the service.
type Request;
///
/// `Request` - requests handled by the service.
pub trait Service<Request> {
/// Responses given by the service.
type Response;
@@ -69,22 +68,26 @@ pub trait Service {
///
/// Calling `call` without calling `poll_ready` is permitted. The
/// 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
/// adapters
pub trait ServiceExt: Service {
pub trait ServiceExt<Request>: Service<Request> {
/// Apply function to specified service and use it as a next service in
/// 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
Self: Sized,
F: FnMut(Self::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<Self::Error>,
B: Service<Error = Self::Error>,
B1: IntoService<B>,
B: Service<Req, Error = Self::Error>,
B1: IntoService<B, Req>,
{
AndThenApply::new(self, service, f)
}
@@ -101,8 +104,8 @@ pub trait ServiceExt: Service {
fn and_then<F, B>(self, service: F) -> AndThen<Self, B>
where
Self: Sized,
F: IntoService<B>,
B: Service<Request = Self::Response, Error = Self::Error>,
F: IntoService<B, Self::Response>,
B: Service<Self::Response, Error = Self::Error>,
{
AndThen::new(self, service.into_service())
}
@@ -128,7 +131,7 @@ pub trait ServiceExt: Service {
fn then<B>(self, service: B) -> Then<Self, B>
where
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)
}
@@ -167,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.
///
@@ -177,11 +180,9 @@ impl<T: ?Sized> ServiceExt for T where T: Service {}
/// `NewService` trait, and uses that new `Service` value to process inbound
/// requests on that new TCP stream.
///
/// `Config` is a service factory configuration type.
pub trait NewService<Config = ()> {
/// Requests handled by the service.
type Request;
/// * `Request` - requests handled by the service.
/// * `Config` - is a service factory configuration type.
pub trait NewService<Request, Config = ()> {
/// Responses given by the service
type Response;
@@ -189,11 +190,7 @@ pub trait NewService<Config = ()> {
type Error;
/// The `Service` value created by this factory
type Service: Service<
Request = Self::Request,
Response = Self::Response,
Error = Self::Error,
>;
type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
/// Errors produced while building a service.
type InitError;
@@ -206,37 +203,33 @@ pub trait NewService<Config = ()> {
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply<T, T1, B, B1>(
fn apply<T, T1, B, B1, Req>(
self,
transform: T1,
service: B1,
) -> AndThenTransformNewService<T, Self, B, Config>
) -> AndThenTransform<T, Self, B, Req>
where
Self: Sized,
T: Transform<B::Service, Request = Self::Response, InitError = Self::InitError>,
T: Transform<B::Service, Self::Response, InitError = Self::InitError>,
T::Error: From<Self::Error>,
T1: IntoTransform<T, B::Service>,
B: NewService<Config, InitError = Self::InitError>,
B1: IntoNewService<B, Config>,
T1: IntoTransform<T, B::Service, Self::Response>,
B: NewService<Req, Config, InitError = Self::InitError>,
B1: IntoNewService<B, Req, Config>,
{
AndThenTransformNewService::new(
transform.into_transform(),
self,
service.into_new_service(),
)
AndThenTransform::new(transform.into_transform(), self, service.into_new_service())
}
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply_fn<B, I, F, Out>(
fn apply_fn<B, I, F, Out, Req>(
self,
service: I,
f: F,
) -> AndThenApplyNewService<Self, B, F, Out, Config>
) -> AndThenApplyNewService<Self, B, F, Out, Request, Req, Config>
where
Self: Sized,
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Config>,
B: NewService<Req, Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Req, Config>,
F: FnMut(Self::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<Self::Error>,
@@ -245,16 +238,11 @@ pub trait NewService<Config = ()> {
}
/// 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
Self: Sized,
F: IntoNewService<B, Config>,
B: NewService<
Config,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
>,
F: IntoNewService<B, Self::Response, Config>,
B: NewService<Self::Response, Config, Error = Self::Error, InitError = Self::InitError>,
{
AndThenNewService::new(self, new_service)
}
@@ -282,15 +270,15 @@ pub trait NewService<Config = ()> {
fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B, Config>
where
Self: Sized,
F: IntoNewService<B, Config>,
F: IntoNewService<B, Result<Self::Response, Self::Error>, Config>,
B: NewService<
Result<Self::Response, Self::Error>,
Config,
Request = Result<Self::Response, Self::Error>,
Error = Self::Error,
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
@@ -322,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
S: Service + 'a,
S: Service<R> + 'a,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@@ -335,16 +322,15 @@ where
(**self).poll_ready()
}
fn call(&mut self, request: Self::Request) -> S::Future {
fn call(&mut self, request: R) -> S::Future {
(**self).call(request)
}
}
impl<S> Service for Box<S>
impl<S, R> Service<R> for Box<S>
where
S: Service + ?Sized,
S: Service<R> + ?Sized,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@@ -353,16 +339,15 @@ where
(**self).poll_ready()
}
fn call(&mut self, request: Self::Request) -> S::Future {
fn call(&mut self, request: R) -> S::Future {
(**self).call(request)
}
}
impl<S, C> NewService<C> for Rc<S>
impl<S, R, C> NewService<R, C> for Rc<S>
where
S: NewService<C>,
S: NewService<R, C>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S::Service;
@@ -374,11 +359,10 @@ where
}
}
impl<S, C> NewService<C> for Arc<S>
impl<S, R, C> NewService<R, C> for Arc<S>
where
S: NewService<C>,
S: NewService<R, C>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S::Service;
@@ -391,35 +375,35 @@ where
}
/// Trait for types that can be converted to a `Service`
pub trait IntoService<T>
pub trait IntoService<T, R>
where
T: Service,
T: Service<R>,
{
/// Convert to a `Service`
fn into_service(self) -> T;
}
/// Trait for types that can be converted to a `NewService`
pub trait IntoNewService<T, C = ()>
pub trait IntoNewService<T, R, C = ()>
where
T: NewService<C>,
T: NewService<R, C>,
{
/// Convert to an `NewService`
fn into_new_service(self) -> T;
}
impl<T> IntoService<T> for T
impl<T, R> IntoService<T, R> for T
where
T: Service,
T: Service<R>,
{
fn into_service(self) -> T {
self
}
}
impl<T, C> IntoNewService<T, C> for T
impl<T, R, C> IntoNewService<T, R, C> for T
where
T: NewService<C>,
T: NewService<R, C>,
{
fn into_new_service(self) -> T {
self
@@ -427,17 +411,17 @@ where
}
/// Trait for types that can be converted to a configurable `NewService`
pub trait IntoConfigurableNewService<T, C>
pub trait IntoConfigurableNewService<T, R, C>
where
T: NewService<C>,
T: NewService<R, C>,
{
/// Convert to an `NewService`
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
T: NewService<C>,
T: NewService<R, C>,
{
fn into_new_service(self) -> T {
self

View File

@@ -15,9 +15,9 @@ pub struct Map<A, F, Response> {
impl<A, F, Response> Map<A, F, Response> {
/// Create new `Map` combinator
pub fn new(service: A, f: F) -> Self
pub fn new<R>(service: A, f: F) -> Self
where
A: Service,
A: Service<R>,
F: FnMut(A::Response) -> Response,
{
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
A: Service,
A: Service<R>,
F: FnMut(A::Response) -> Response + Clone,
{
type Request = A::Request;
type Response = Response;
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> {
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())
}
}
pub struct MapFuture<A, F, Response>
pub struct MapFuture<A, F, R, Response>
where
A: Service,
A: Service<R>,
F: FnMut(A::Response) -> Response,
{
f: F,
fut: A::Future,
}
impl<A, F, Response> MapFuture<A, F, Response>
impl<A, F, R, Response> MapFuture<A, F, R, Response>
where
A: Service,
A: Service<R>,
F: FnMut(A::Response) -> Response,
{
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
A: Service,
A: Service<R>,
F: FnMut(A::Response) -> 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> {
/// Create new `Map` new service instance
pub fn new(a: A, f: F) -> Self
pub fn new<Req>(a: A, f: F) -> Self
where
A: NewService<Cfg>,
A: NewService<Req, Cfg>,
F: FnMut(A::Response) -> Res,
{
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
A: NewService<Cfg>,
A: NewService<Req, Cfg>,
F: FnMut(A::Response) -> Res + Clone,
{
type Request = A::Request;
type Response = Res;
type Error = A::Error;
type Service = Map<A::Service, F, Res>;
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 {
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
A: NewService<Cfg>,
A: NewService<Req, Cfg>,
F: FnMut(A::Response) -> Res,
{
fut: A::Future,
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
A: NewService<Cfg>,
A: NewService<Req, Cfg>,
F: FnMut(A::Response) -> Res,
{
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
A: NewService<Cfg>,
A: NewService<Req, Cfg>,
F: FnMut(A::Response) -> Res,
{
type Item = Map<A::Service, F, Res>;
@@ -194,8 +192,7 @@ mod tests {
use crate::{IntoNewService, Service, ServiceExt};
struct Srv;
impl Service for Srv {
type Request = ();
impl Service<()> for Srv {
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;

View File

@@ -16,9 +16,9 @@ pub struct MapErr<A, F, E> {
impl<A, F, E> MapErr<A, F, E> {
/// Create new `MapErr` combinator
pub fn new(service: A, f: F) -> Self
pub fn new<R>(service: A, f: F) -> Self
where
A: Service,
A: Service<R>,
F: Fn(A::Error) -> E,
{
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
A: Service,
A: Service<R>,
F: Fn(A::Error) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = E;
type Future = MapErrFuture<A, F, E>;
type Future = MapErrFuture<A, F, E, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready().map_err(&self.f)
}
fn call(&mut self, req: A::Request) -> Self::Future {
MapErrFuture::new(self.service.call(req), self.f.clone())
fn call(&mut self, req: R) -> Self::Future {
MapErrFuture {
fut: self.service.call(req),
f: self.f.clone(),
}
}
}
pub struct MapErrFuture<A, F, E>
pub struct MapErrFuture<A, F, E, R>
where
A: Service,
A: Service<R>,
F: Fn(A::Error) -> E,
{
f: F,
fut: A::Future,
}
impl<A, F, E> MapErrFuture<A, F, E>
impl<A, F, E, R> Future for MapErrFuture<A, F, E, R>
where
A: Service,
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,
A: Service<R>,
F: Fn(A::Error) -> E,
{
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> {
/// Create new `MapErr` new service instance
pub fn new(a: A, f: F) -> Self
pub fn new<R>(a: A, f: F) -> Self
where
A: NewService<C>,
A: NewService<R, C>,
F: Fn(A::Error) -> E,
{
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
A: NewService<C>,
A: NewService<R, C>,
F: Fn(A::Error) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = E;
type Service = MapErr<A::Service, F, E>;
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 {
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
A: NewService<C>,
A: NewService<R, C>,
F: Fn(A::Error) -> E,
{
fut: A::Future,
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
A: NewService<C>,
A: NewService<R, C>,
F: Fn(A::Error) -> E,
{
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
A: NewService<C>,
A: NewService<R, C>,
F: Fn(A::Error) -> E + Clone,
{
type Item = MapErr<A::Service, F, E>;
@@ -196,8 +187,7 @@ mod tests {
struct Srv;
impl Service for Srv {
type Request = ();
impl Service<()> for Srv {
type Response = ();
type Error = ();
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> {
/// Create new `MapInitErr` combinator
pub fn new(a: A, f: F) -> Self
pub fn new<R>(a: A, f: F) -> Self
where
A: NewService<C>,
A: NewService<R, C>,
F: Fn(A::InitError) -> E,
{
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
A: NewService<C>,
A: NewService<R, C>,
F: Fn(A::InitError) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type Service = A::Service;
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 {
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
A: NewService<C>,
A: NewService<R, C>,
F: Fn(A::InitError) -> E,
{
f: F,
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
A: NewService<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>,
A: NewService<R, C>,
F: Fn(A::InitError) -> E,
{
type Item = A::Service;

View File

@@ -2,7 +2,7 @@ use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll};
use super::{IntoNewService, NewService, Service};
use super::{NewService, Service};
use crate::cell::Cell;
/// 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> {
/// 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
A: Service,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
A: Service<R>,
B: Service<Result<A::Response, A::Error>, Error = A::Error>,
{
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
A: Service,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
A: Service<R>,
B: Service<Result<A::Response, A::Error>, Error = A::Error>,
{
type Request = A::Request;
type Response = B::Response;
type Error = B::Error;
type Future = ThenFuture<A, B>;
type Future = ThenFuture<A, B, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.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())
}
}
pub struct ThenFuture<A, B>
pub struct ThenFuture<A, B, R>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
A: Service<R>,
B: Service<Result<A::Response, A::Error>>,
{
b: Cell<B>,
fut_b: Option<B::Future>,
fut_a: Option<A::Future>,
}
impl<A, B> ThenFuture<A, B>
impl<A, B, R> ThenFuture<A, B, R>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
A: Service<R>,
B: Service<Result<A::Response, A::Error>>,
{
fn new(a: A::Future, b: Cell<B>) -> Self {
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
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
A: Service<R>,
B: Service<Result<A::Response, A::Error>>,
{
type Item = B::Response;
type Error = B::Error;
@@ -119,42 +118,35 @@ pub struct ThenNewService<A, B, C> {
impl<A, B, C> ThenNewService<A, B, C> {
/// Create new `AndThen` combinator
pub fn new<F>(a: A, f: F) -> Self
pub fn new<R>(a: A, f: B) -> Self
where
A: NewService<C>,
A: NewService<R, C>,
B: NewService<
Result<A::Response, A::Error>,
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
F: IntoNewService<B, C>,
{
Self {
a,
b: f.into_new_service(),
b: f,
_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
A: NewService<C>,
B: NewService<
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
A: NewService<R, C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Service = Then<A::Service, B::Service>;
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 {
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
A: NewService<C>,
B: NewService<
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
A: NewService<R, C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
{
fut_b: B::Future,
fut_a: A::Future,
@@ -191,15 +178,10 @@ where
b: Option<B::Service>,
}
impl<A, B, C> ThenNewServiceFuture<A, B, C>
impl<A, B, R, C> ThenNewServiceFuture<A, B, R, C>
where
A: NewService<C>,
B: NewService<
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
A: NewService<R, C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
{
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
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
A: NewService<C>,
B: NewService<
C,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
A: NewService<R, C>,
B: NewService<Result<A::Response, A::Error>, C, Error = A::Error, InitError = A::InitError>,
{
type Item = Then<A::Service, B::Service>;
type Error = A::InitError;
@@ -259,8 +236,7 @@ mod tests {
#[derive(Clone)]
struct Srv1(Rc<Cell<usize>>);
impl Service for Srv1 {
type Request = Result<&'static str, &'static str>;
impl Service<Result<&'static str, &'static str>> for Srv1 {
type Response = &'static str;
type Error = ();
type Future = FutureResult<Self::Response, Self::Error>;
@@ -280,8 +256,7 @@ mod tests {
struct Srv2(Rc<Cell<usize>>);
impl Service for Srv2 {
type Request = Result<&'static str, ()>;
impl Service<Result<&'static str, ()>> for Srv2 {
type Response = (&'static str, &'static str);
type Error = ();
type Future = FutureResult<Self::Response, ()>;

View File

@@ -1,19 +1,18 @@
use std::rc::Rc;
use std::sync::Arc;
use futures::Future;
use futures::{Async, Future, IntoFuture, Poll};
use crate::transform_map_init_err::TransformMapInitErr;
use crate::Service;
use crate::{NewService, Service};
/// `Transform` service factory.
///
/// Transform factory creates service that wraps other services.
/// `Config` is a service factory configuration type.
pub trait Transform<S> {
/// Requests handled by the service.
type Request;
///
/// * `S` is a wrapped service.
/// * `R` requests handled by this transform service.
pub trait Transform<S, R> {
/// Responses given by the service.
type Response;
@@ -21,11 +20,7 @@ pub trait Transform<S> {
type Error;
/// The `TransformService` value created by this factory
type Transform: Service<
Request = Self::Request,
Response = Self::Response,
Error = Self::Error,
>;
type Transform: Service<R, Response = Self::Response, Error = Self::Error>;
/// Errors produced while building a service.
type InitError;
@@ -47,11 +42,10 @@ pub trait Transform<S> {
}
}
impl<T, S> Transform<S> for Rc<T>
impl<T, S, R> Transform<S, R> for Rc<T>
where
T: Transform<S>,
T: Transform<S, R>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
@@ -63,11 +57,10 @@ where
}
}
impl<T, S> Transform<S> for Arc<T>
impl<T, S, R> Transform<S, R> for Arc<T>
where
T: Transform<S>,
T: Transform<S, R>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
@@ -80,19 +73,95 @@ where
}
/// Trait for types that can be converted to a *transform service*
pub trait IntoTransform<T, S>
pub trait IntoTransform<T, S, R>
where
T: Transform<S>,
T: Transform<S, R>,
{
/// Convert to a `TransformService`
fn into_transform(self) -> T;
}
impl<T, S> IntoTransform<T, S> for T
impl<T, S, R> IntoTransform<T, S, R> for T
where
T: Transform<S>,
T: Transform<S, R>,
{
fn into_transform(self) -> T {
self
}
}
/// `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
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
{
/// Create new `ApplyNewService` new service instance
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

@@ -16,9 +16,9 @@ pub struct TransformMapInitErr<T, S, F, E> {
impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
/// Create new `MapInitErr` new transform instance
pub fn new(t: T, f: F) -> Self
pub fn new<R>(t: T, f: F) -> Self
where
T: Transform<S>,
T: Transform<S, R>,
F: Fn(T::InitError) -> E,
{
Self {
@@ -43,36 +43,35 @@ where
}
}
impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
impl<T, R, S, F, E> Transform<S, R> for TransformMapInitErr<T, S, F, E>
where
T: Transform<S>,
T: Transform<S, R>,
F: Fn(T::InitError) -> E + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = E;
type Future = TransformMapInitErrFuture<T, S, F, E>;
type Future = TransformMapInitErrFuture<T, R, S, F, E>;
fn new_transform(&self, service: S) -> Self::Future {
TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone())
}
}
pub struct TransformMapInitErrFuture<T, S, F, E>
pub struct TransformMapInitErrFuture<T, R, S, F, E>
where
T: Transform<S>,
T: Transform<S, R>,
F: Fn(T::InitError) -> E,
{
fut: T::Future,
f: F,
}
impl<T, S, F, E> TransformMapInitErrFuture<T, S, F, E>
impl<T, R, S, F, E> TransformMapInitErrFuture<T, R, S, F, E>
where
T: Transform<S>,
T: Transform<S, R>,
F: Fn(T::InitError) -> E,
{
fn new(fut: T::Future, f: F) -> Self {
@@ -80,9 +79,9 @@ where
}
}
impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
impl<T, R, S, F, E> Future for TransformMapInitErrFuture<T, R, S, F, E>
where
T: Transform<S>,
T: Transform<S, R>,
F: Fn(T::InitError) -> E + Clone,
{
type Item = T::Transform;

View File

@@ -34,7 +34,8 @@ rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
[dependencies]
actix-rt = "0.1.0"
actix-server = "0.3.0"
#actix-server = "0.3.0"
actix-server = { path="../actix-server" }
log = "0.4"
net2 = "0.2"

View File

@@ -1,5 +1,17 @@
# 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

View File

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

View File

@@ -13,9 +13,9 @@ pub struct CloneableService<T: 'static> {
}
impl<T: 'static> CloneableService<T> {
pub fn new(service: T) -> Self
pub fn new<R>(service: T) -> Self
where
T: Service,
T: Service<R>,
{
Self {
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
T: Service + 'static,
T: Service<R> + 'static,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Future = T::Future;
@@ -46,7 +45,7 @@ where
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)
}
}

View File

@@ -1,6 +1,6 @@
//! Contains `Either` service and related types and functions.
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.
///
@@ -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
A: Service,
B: Service<Request = A::Request, Response = A::Response, Error = A::Error>,
A: Service<R>,
B: Service<R, Response = A::Response, Error = A::Error>,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
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 {
EitherService::A(ref mut inner) => future::Either::A(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> {
pub fn new_a<C>(srv: A) -> Self
pub fn new_a<R, C>(srv: A) -> Self
where
A: NewService<C>,
B: NewService<
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
{
Either::A(srv)
}
pub fn new_b<C>(srv: B) -> Self
pub fn new_b<R, C>(srv: B) -> Self
where
A: NewService<C>,
B: NewService<
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
{
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
A: NewService<C>,
B: NewService<
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type InitError = A::InitError;
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 {
match self {
@@ -118,21 +98,15 @@ impl<A: Clone, B: Clone> Clone for Either<A, B> {
}
#[doc(hidden)]
pub enum EitherNewService<A: NewService<C>, B: NewService<C>, C> {
A(A::Future),
B(B::Future),
pub enum EitherNewService<A: NewService<R, C>, B: NewService<R, C>, R, C> {
A(<A::Future as IntoFuture>::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
A: NewService<C>,
B: NewService<
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
{
type Item = EitherService<A::Service, B::Service>;
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 futures::future::{ok, FutureResult};
use futures::task::AtomicTask;
use futures::{Async, Future, Poll, Sink, Stream};
use futures::{Async, Future, IntoFuture, Poll, Sink, Stream};
use log::debug;
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>
where
C: Clone,
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S: NewService<Request<U>, C, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service>::Future: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<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 {
factory: factory.into_new_service(),
_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
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::Service as Service>::Future: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
type Request = Framed<T, U>;
type Response = FramedTransport<S::Service, T, U>;
type Error = 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
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S: NewService<Request<U>, C, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service>::Future: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
C: Clone,
{
type Request = Framed<T, U>;
type Response = FramedTransport<S::Service, T, U>;
type Error = S::InitError;
type Future = FramedServiceResponseFuture<S, T, U, C>;
@@ -129,23 +127,23 @@ where
#[doc(hidden)]
pub struct FramedServiceResponseFuture<S, T, U, C>
where
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S: NewService<Request<U>, C, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service>::Future: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
fut: S::Future,
fut: <S::Future as IntoFuture>::Future,
framed: Option<Framed<T, U>>,
}
impl<S, T, U, C> Future for FramedServiceResponseFuture<S, T, U, C>
where
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S: NewService<Request<U>, C, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service>::Future: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<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.
pub struct FramedTransport<S, T, U>
where
S: Service<Request = Request<U>, Response = Response<U>>,
S: Service<Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@@ -196,7 +194,7 @@ where
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,
Error(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>
where
S: Service<Request = Request<U>, Response = Response<U>>,
S: Service<Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@@ -302,7 +300,7 @@ where
impl<S, T, U> FramedTransport<S, T, U>
where
S: Service<Request = Request<U>, Response = Response<U>>,
S: Service<Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@@ -310,7 +308,7 @@ where
<U as Encoder>::Item: 'static,
<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 {
framed,
service: service.into_service(),
@@ -348,7 +346,7 @@ where
impl<S, T, U> Future for FramedTransport<S, T, U>
where
S: Service<Request = Request<U>, Response = Response<U>>,
S: Service<Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
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
T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static,
U: Encoder + Decoder,
{
type Request = T;
type Response = Framed<T, U>;
type Error = ();
type InitError = ();
@@ -439,13 +436,12 @@ where
_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
T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static,
U: Encoder + Decoder,
{
type Request = T;
type Response = Framed<T, U>;
type Error = ();
type Future = FutureResult<Self::Response, Self::Error>;

View File

@@ -24,8 +24,7 @@ impl Default for InFlight {
}
}
impl<S: Service> Transform<S> for InFlight {
type Request = S::Request;
impl<S: Service<R>, R> Transform<S, R> for InFlight {
type Response = S::Response;
type Error = S::Error;
type InitError = Void;
@@ -51,14 +50,13 @@ impl<S> InFlightService<S> {
}
}
impl<T> Service for InFlightService<T>
impl<T, R> Service<R> for InFlightService<T>
where
T: Service,
T: Service<R>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Future = InFlightServiceResponse<T>;
type Future = InFlightServiceResponse<T, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready()?;
@@ -71,7 +69,7 @@ where
}
}
fn call(&mut self, req: T::Request) -> Self::Future {
fn call(&mut self, req: R) -> Self::Future {
InFlightServiceResponse {
fut: self.service.call(req),
_guard: self.count.get(),
@@ -80,12 +78,12 @@ where
}
#[doc(hidden)]
pub struct InFlightServiceResponse<T: Service> {
pub struct InFlightServiceResponse<T: Service<R>, R> {
fut: T::Future,
_guard: CounterGuard,
}
impl<T: Service> Future for InFlightServiceResponse<T> {
impl<T: Service<R>, R> Future for InFlightServiceResponse<T, R> {
type Item = T::Response;
type Error = T::Error;
@@ -107,8 +105,7 @@ mod tests {
struct SleepService(Duration);
impl Service for SleepService {
type Request = ();
impl Service<()> for SleepService {
type Response = ();
type Error = ();
type Future = Box<Future<Item = (), Error = ()>>;

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
F: Fn() -> E + Clone,
{
type Request = R;
type Response = R;
type Error = E;
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
F: Fn() -> E,
{
type Request = R;
type Response = R;
type Error = E;
type Future = FutureResult<R, E>;

View File

@@ -52,46 +52,39 @@ pub struct InOrder<S> {
_t: PhantomData<S>,
}
impl<S> InOrder<S>
where
S: Service,
S::Response: 'static,
S::Future: 'static,
S::Error: 'static,
{
pub fn new() -> Self {
impl<S> InOrder<S> {
pub fn new<R>() -> Self
where
S: Service<R>,
S::Response: 'static,
S::Future: 'static,
S::Error: 'static,
{
Self { _t: PhantomData }
}
pub fn service(service: S) -> InOrderService<S> {
pub fn service<R>(service: S) -> InOrderService<S, R>
where
S: Service<R>,
S::Response: 'static,
S::Future: 'static,
S::Error: 'static,
{
InOrderService::new(service)
}
}
impl<S> Default for InOrder<S>
impl<S, R> Transform<S, R> for InOrder<S>
where
S: Service,
S: Service<R>,
S::Response: 'static,
S::Future: 'static,
S::Error: 'static,
{
fn default() -> Self {
Self::new()
}
}
impl<S> Transform<S> for InOrder<S>
where
S: Service,
S::Response: 'static,
S::Future: 'static,
S::Error: 'static,
{
type Request = S::Request;
type Response = S::Response;
type Error = InOrderError<S::Error>;
type InitError = Void;
type Transform = InOrderService<S>;
type Transform = InOrderService<S, R>;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, service: S) -> Self::Future {
@@ -99,15 +92,15 @@ where
}
}
pub struct InOrderService<S: Service> {
pub struct InOrderService<S: Service<R>, R> {
service: S,
task: Rc<AtomicTask>,
acks: VecDeque<Record<S::Response, S::Error>>,
}
impl<S> InOrderService<S>
impl<S, R> InOrderService<S, R>
where
S: Service,
S: Service<R>,
S::Response: 'static,
S::Future: 'static,
S::Error: 'static,
@@ -121,17 +114,16 @@ where
}
}
impl<S> Service for InOrderService<S>
impl<S, R> Service<R> for InOrderService<S, R>
where
S: Service,
S: Service<R>,
S::Response: 'static,
S::Future: 'static,
S::Error: 'static,
{
type Request = S::Request;
type Response = S::Response;
type Error = InOrderError<S::Error>;
type Future = InOrderServiceResponse<S>;
type Future = InOrderServiceResponse<S, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
// poll_ready could be called from different task
@@ -156,7 +148,7 @@ where
Ok(Async::Ready(()))
}
fn call(&mut self, request: S::Request) -> Self::Future {
fn call(&mut self, request: R) -> Self::Future {
let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = oneshot::channel();
self.acks.push_back(Record { rx: rx1, tx: tx2 });
@@ -173,11 +165,11 @@ where
}
#[doc(hidden)]
pub struct InOrderServiceResponse<S: Service> {
pub struct InOrderServiceResponse<S: Service<R>, R> {
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 Error = InOrderError<S::Error>;
@@ -204,8 +196,7 @@ mod tests {
struct Srv;
impl Service for Srv {
type Request = oneshot::Receiver<usize>;
impl Service<oneshot::Receiver<usize>> for Srv {
type Response = usize;
type Error = ();
type Future = Box<Future<Item = usize, Error = ()>>;
@@ -219,11 +210,11 @@ mod tests {
}
}
struct SrvPoll<S: Service> {
struct SrvPoll<S: Service<oneshot::Receiver<usize>>> {
s: S,
}
impl<S: Service> Future for SrvPoll<S> {
impl<S: Service<oneshot::Receiver<usize>>> Future for SrvPoll<S> {
type Item = ();
type Error = ();

View File

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

View File

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

View File

@@ -80,11 +80,10 @@ impl<E> Clone for Timeout<E> {
}
}
impl<S, E> Transform<S> for Timeout<E>
impl<S, R, E> Transform<S, R> for Timeout<E>
where
S: Service,
S: Service<R>,
{
type Request = S::Request;
type Response = S::Response;
type Error = TimeoutError<S::Error>;
type InitError = E;
@@ -112,20 +111,19 @@ impl<S> TimeoutService<S> {
}
}
impl<S> Service for TimeoutService<S>
impl<S, R> Service<R> for TimeoutService<S>
where
S: Service,
S: Service<R>,
{
type Request = S::Request;
type Response = S::Response;
type Error = TimeoutError<S::Error>;
type Future = TimeoutServiceResponse<S>;
type Future = TimeoutServiceResponse<S, R>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready().map_err(TimeoutError::Service)
}
fn call(&mut self, request: S::Request) -> Self::Future {
fn call(&mut self, request: R) -> Self::Future {
TimeoutServiceResponse {
fut: self.service.call(request),
sleep: Delay::new(clock::now() + self.timeout),
@@ -135,14 +133,14 @@ where
/// `TimeoutService` response future
#[derive(Debug)]
pub struct TimeoutServiceResponse<T: Service> {
pub struct TimeoutServiceResponse<T: Service<R>, R> {
fut: T::Future,
sleep: Delay,
}
impl<T> Future for TimeoutServiceResponse<T>
impl<T, R> Future for TimeoutServiceResponse<T, R>
where
T: Service,
T: Service<R>,
{
type Item = T::Response;
type Error = TimeoutError<T::Error>;
@@ -177,8 +175,7 @@ mod tests {
struct SleepService(Duration);
impl Service for SleepService {
type Request = ();
impl Service<()> for SleepService {
type Response = ();
type Error = ();
type Future = Box<Future<Item = (), Error = ()>>;
@@ -224,7 +221,7 @@ mod tests {
let wait_time = Duration::from_millis(150);
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)));
if let Async::Ready(mut to) = timeout.new_service(&()).poll().unwrap() {
to.call(())

View File

@@ -64,6 +64,17 @@ impl ResourceDef {
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
///
/// 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)]
mod tests {
use super::*;
@@ -408,6 +427,11 @@ mod tests {
assert!(re.is_match("/name/"));
assert!(re.is_match("/name/gs"));
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]