mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-27 18:02:58 +01:00
Use associated type instead of generic for Service definition
This commit is contained in:
parent
8cdbf49187
commit
d83bf95304
@ -26,7 +26,8 @@ members = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-service = "0.1.1"
|
#actix-service = "0.2.0"
|
||||||
|
actix-service = { path="actix-service" }
|
||||||
actix-codec = "0.1.0"
|
actix-codec = "0.1.0"
|
||||||
actix-rt = { path="actix-rt" }
|
actix-rt = { path="actix-rt" }
|
||||||
actix-server = { path="actix-server", features=["ssl"] }
|
actix-server = { path="actix-server", features=["ssl"] }
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [0.1.2] - 2019-01-xx
|
## [0.2.0] - 2019-01-xx
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
* Migrate to actix-service 0.2
|
||||||
|
|
||||||
* Upgrade trust-dns-resolver
|
* Upgrade trust-dns-resolver
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-connector"
|
name = "actix-connector"
|
||||||
version = "0.1.2"
|
version = "0.2.0"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix Connector - tcp connector service"
|
description = "Actix Connector - tcp connector service"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -27,12 +27,13 @@ default = []
|
|||||||
ssl = ["openssl", "tokio-openssl"]
|
ssl = ["openssl", "tokio-openssl"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "0.1.6"
|
#actix-service = "0.1.6"
|
||||||
|
actix-service = { path="../actix-service" }
|
||||||
actix-codec = "0.1.0"
|
actix-codec = "0.1.0"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
tokio-tcp = "0.1"
|
tokio-tcp = "0.1"
|
||||||
tokio-current-thread = "0.1"
|
tokio-current-thread = "0.1"
|
||||||
trust-dns-resolver = { version="0.11.0-alpha.1", default-features = false }
|
trust-dns-resolver = { version="0.11.0-alpha.2", default-features = false }
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
openssl = { version="0.10", optional = true }
|
openssl = { version="0.10", optional = true }
|
||||||
|
@ -167,8 +167,8 @@ impl Connector {
|
|||||||
/// Create new connector with custom resolver
|
/// Create new connector with custom resolver
|
||||||
pub fn with_resolver(
|
pub fn with_resolver(
|
||||||
resolver: Resolver<Connect>,
|
resolver: Resolver<Connect>,
|
||||||
) -> impl Service<Connect, Response = (Connect, TcpStream), Error = ConnectorError> + Clone
|
) -> impl Service<Request = Connect, Response = (Connect, TcpStream), Error = ConnectorError>
|
||||||
{
|
+ Clone {
|
||||||
Connector { resolver }
|
Connector { resolver }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,7 +177,7 @@ impl Connector {
|
|||||||
cfg: ResolverConfig,
|
cfg: ResolverConfig,
|
||||||
opts: ResolverOpts,
|
opts: ResolverOpts,
|
||||||
) -> impl NewService<
|
) -> impl NewService<
|
||||||
Connect,
|
Request = Connect,
|
||||||
Response = (Connect, TcpStream),
|
Response = (Connect, TcpStream),
|
||||||
Error = ConnectorError,
|
Error = ConnectorError,
|
||||||
InitError = E,
|
InitError = E,
|
||||||
@ -194,7 +194,8 @@ impl Clone for Connector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service<Connect> for Connector {
|
impl Service for Connector {
|
||||||
|
type Request = Connect;
|
||||||
type Response = (Connect, TcpStream);
|
type Response = (Connect, TcpStream);
|
||||||
type Error = ConnectorError;
|
type Error = ConnectorError;
|
||||||
type Future = Either<ConnectorFuture, ConnectorTcpFuture>;
|
type Future = Either<ConnectorFuture, ConnectorTcpFuture>;
|
||||||
@ -271,7 +272,8 @@ impl<T: RequestPort> Default for TcpConnector<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: RequestPort> Service<(T, VecDeque<IpAddr>)> for TcpConnector<T> {
|
impl<T: RequestPort> Service for TcpConnector<T> {
|
||||||
|
type Request = (T, VecDeque<IpAddr>);
|
||||||
type Response = (T, TcpStream);
|
type Response = (T, TcpStream);
|
||||||
type Error = io::Error;
|
type Error = io::Error;
|
||||||
type Future = TcpConnectorResponse<T>;
|
type Future = TcpConnectorResponse<T>;
|
||||||
@ -317,7 +319,7 @@ impl<T: RequestPort> Future for TcpConnectorResponse<T> {
|
|||||||
if let Some(new) = self.stream.as_mut() {
|
if let Some(new) = self.stream.as_mut() {
|
||||||
match new.poll() {
|
match new.poll() {
|
||||||
Ok(Async::Ready(sock)) => {
|
Ok(Async::Ready(sock)) => {
|
||||||
return Ok(Async::Ready((self.req.take().unwrap(), sock)))
|
return Ok(Async::Ready((self.req.take().unwrap(), sock)));
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@ -351,7 +353,8 @@ impl DefaultConnector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service<Connect> for DefaultConnector {
|
impl Service for DefaultConnector {
|
||||||
|
type Request = Connect;
|
||||||
type Response = TcpStream;
|
type Response = TcpStream;
|
||||||
type Error = ConnectorError;
|
type Error = ConnectorError;
|
||||||
type Future = DefaultConnectorFuture;
|
type Future = DefaultConnectorFuture;
|
||||||
|
@ -67,7 +67,8 @@ impl<T> Clone for Resolver<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: RequestHost> Service<T> for Resolver<T> {
|
impl<T: RequestHost> Service for Resolver<T> {
|
||||||
|
type Request = T;
|
||||||
type Response = (T, VecDeque<IpAddr>);
|
type Response = (T, VecDeque<IpAddr>);
|
||||||
type Error = ResolveError;
|
type Error = ResolveError;
|
||||||
type Future = ResolverFuture<T>;
|
type Future = ResolverFuture<T>;
|
||||||
|
@ -26,7 +26,8 @@ impl<R, T, E> OpensslConnector<R, T, E> {
|
|||||||
impl<R: RequestHost, T: AsyncRead + AsyncWrite> OpensslConnector<R, T, ()> {
|
impl<R: RequestHost, T: AsyncRead + AsyncWrite> OpensslConnector<R, T, ()> {
|
||||||
pub fn service(
|
pub fn service(
|
||||||
connector: SslConnector,
|
connector: SslConnector,
|
||||||
) -> impl Service<(R, T), Response = (R, SslStream<T>), Error = HandshakeError<T>> {
|
) -> impl Service<Request = (R, T), Response = (R, SslStream<T>), Error = HandshakeError<T>>
|
||||||
|
{
|
||||||
OpensslConnectorService {
|
OpensslConnectorService {
|
||||||
connector: connector,
|
connector: connector,
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
@ -43,9 +44,8 @@ impl<R, T, E> Clone for OpensslConnector<R, T, E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: RequestHost, T: AsyncRead + AsyncWrite, E> NewService<(R, T)>
|
impl<R: RequestHost, T: AsyncRead + AsyncWrite, E> NewService for OpensslConnector<R, T, E> {
|
||||||
for OpensslConnector<R, T, E>
|
type Request = (R, T);
|
||||||
{
|
|
||||||
type Response = (R, SslStream<T>);
|
type Response = (R, SslStream<T>);
|
||||||
type Error = HandshakeError<T>;
|
type Error = HandshakeError<T>;
|
||||||
type Service = OpensslConnectorService<R, T>;
|
type Service = OpensslConnectorService<R, T>;
|
||||||
@ -65,9 +65,8 @@ pub struct OpensslConnectorService<R, T> {
|
|||||||
_t: PhantomData<(R, T)>,
|
_t: PhantomData<(R, T)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service<(R, T)>
|
impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service for OpensslConnectorService<R, T> {
|
||||||
for OpensslConnectorService<R, T>
|
type Request = (R, T);
|
||||||
{
|
|
||||||
type Response = (R, SslStream<T>);
|
type Response = (R, SslStream<T>);
|
||||||
type Error = HandshakeError<T>;
|
type Error = HandshakeError<T>;
|
||||||
type Future = ConnectAsyncExt<R, T>;
|
type Future = ConnectAsyncExt<R, T>;
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [0.1.4] - 2018-12-xx
|
## [0.2.0] - 2019-02-xx
|
||||||
|
|
||||||
## Changes
|
## Changes
|
||||||
|
|
||||||
|
* Migrate to actix-service 0.2
|
||||||
|
|
||||||
* Updated rustls dependency
|
* Updated rustls dependency
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-server"
|
name = "actix-server"
|
||||||
version = "0.1.4"
|
version = "0.2.0"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix server - General purpose tcp server"
|
description = "Actix server - General purpose tcp server"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -33,7 +33,8 @@ ssl = ["openssl", "tokio-openssl"]
|
|||||||
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "0.1.1"
|
#actix-service = "0.2.0"
|
||||||
|
actix-service = { path="../actix-service" }
|
||||||
actix-rt = "0.1.0"
|
actix-rt = "0.1.0"
|
||||||
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
@ -169,8 +169,8 @@ impl ServiceRuntime {
|
|||||||
|
|
||||||
pub fn service<T, F>(&mut self, name: &str, service: F)
|
pub fn service<T, F>(&mut self, name: &str, service: F)
|
||||||
where
|
where
|
||||||
F: IntoNewService<T, TcpStream>,
|
F: IntoNewService<T>,
|
||||||
T: NewService<TcpStream, Response = ()> + 'static,
|
T: NewService<Request = TcpStream, Response = ()> + 'static,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Service: 'static,
|
T::Service: 'static,
|
||||||
T::InitError: fmt::Debug,
|
T::InitError: fmt::Debug,
|
||||||
@ -191,7 +191,7 @@ impl ServiceRuntime {
|
|||||||
|
|
||||||
type BoxedNewService = Box<
|
type BoxedNewService = Box<
|
||||||
NewService<
|
NewService<
|
||||||
(Option<CounterGuard>, ServerMessage),
|
Request = (Option<CounterGuard>, ServerMessage),
|
||||||
Response = (),
|
Response = (),
|
||||||
Error = (),
|
Error = (),
|
||||||
InitError = (),
|
InitError = (),
|
||||||
@ -204,14 +204,15 @@ struct ServiceFactory<T> {
|
|||||||
inner: T,
|
inner: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> NewService<(Option<CounterGuard>, ServerMessage)> for ServiceFactory<T>
|
impl<T> NewService for ServiceFactory<T>
|
||||||
where
|
where
|
||||||
T: NewService<TcpStream, Response = ()>,
|
T: NewService<Request = TcpStream, Response = ()>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Service: 'static,
|
T::Service: 'static,
|
||||||
T::Error: 'static,
|
T::Error: 'static,
|
||||||
T::InitError: fmt::Debug + 'static,
|
T::InitError: fmt::Debug + 'static,
|
||||||
{
|
{
|
||||||
|
type Request = (Option<CounterGuard>, ServerMessage);
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
|
@ -23,13 +23,13 @@ pub enum ServerMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait StreamServiceFactory: Send + Clone + 'static {
|
pub trait StreamServiceFactory: Send + Clone + 'static {
|
||||||
type NewService: NewService<TcpStream, Response = ()>;
|
type NewService: NewService<Request = TcpStream, Response = ()>;
|
||||||
|
|
||||||
fn create(&self) -> Self::NewService;
|
fn create(&self) -> Self::NewService;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ServiceFactory: Send + Clone + 'static {
|
pub trait ServiceFactory: Send + Clone + 'static {
|
||||||
type NewService: NewService<ServerMessage, Response = ()>;
|
type NewService: NewService<Request = ServerMessage, Response = ()>;
|
||||||
|
|
||||||
fn create(&self) -> Self::NewService;
|
fn create(&self) -> Self::NewService;
|
||||||
}
|
}
|
||||||
@ -44,7 +44,7 @@ pub(crate) trait InternalServiceFactory: Send {
|
|||||||
|
|
||||||
pub(crate) type BoxedServerService = Box<
|
pub(crate) type BoxedServerService = Box<
|
||||||
Service<
|
Service<
|
||||||
(Option<CounterGuard>, ServerMessage),
|
Request = (Option<CounterGuard>, ServerMessage),
|
||||||
Response = (),
|
Response = (),
|
||||||
Error = (),
|
Error = (),
|
||||||
Future = FutureResult<(), ()>,
|
Future = FutureResult<(), ()>,
|
||||||
@ -61,12 +61,13 @@ impl<T> StreamService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Service<(Option<CounterGuard>, ServerMessage)> for StreamService<T>
|
impl<T> Service for StreamService<T>
|
||||||
where
|
where
|
||||||
T: Service<TcpStream, Response = ()>,
|
T: Service<Request = TcpStream, Response = ()>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Error: 'static,
|
T::Error: 'static,
|
||||||
{
|
{
|
||||||
|
type Request = (Option<CounterGuard>, ServerMessage);
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<(), ()>;
|
type Future = FutureResult<(), ()>;
|
||||||
@ -107,12 +108,13 @@ impl<T> ServerService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Service<(Option<CounterGuard>, ServerMessage)> for ServerService<T>
|
impl<T> Service for ServerService<T>
|
||||||
where
|
where
|
||||||
T: Service<ServerMessage, Response = ()>,
|
T: Service<Request = ServerMessage, Response = ()>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Error: 'static,
|
T::Error: 'static,
|
||||||
{
|
{
|
||||||
|
type Request = (Option<CounterGuard>, ServerMessage);
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<(), ()>;
|
type Future = FutureResult<(), ()>;
|
||||||
@ -239,7 +241,7 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
|
|||||||
impl<F, T> ServiceFactory for F
|
impl<F, T> ServiceFactory for F
|
||||||
where
|
where
|
||||||
F: Fn() -> T + Send + Clone + 'static,
|
F: Fn() -> T + Send + Clone + 'static,
|
||||||
T: NewService<ServerMessage, Response = ()>,
|
T: NewService<Request = ServerMessage, Response = ()>,
|
||||||
{
|
{
|
||||||
type NewService = T;
|
type NewService = T;
|
||||||
|
|
||||||
@ -251,7 +253,7 @@ where
|
|||||||
impl<F, T> StreamServiceFactory for F
|
impl<F, T> StreamServiceFactory for F
|
||||||
where
|
where
|
||||||
F: Fn() -> T + Send + Clone + 'static,
|
F: Fn() -> T + Send + Clone + 'static,
|
||||||
T: NewService<TcpStream, Response = ()>,
|
T: NewService<Request = TcpStream, Response = ()>,
|
||||||
{
|
{
|
||||||
type NewService = T;
|
type NewService = T;
|
||||||
|
|
||||||
|
@ -36,7 +36,8 @@ impl<T: AsyncRead + AsyncWrite> Clone for NativeTlsAcceptor<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> NewService<T> for NativeTlsAcceptor<T> {
|
impl<T: AsyncRead + AsyncWrite> NewService for NativeTlsAcceptor<T> {
|
||||||
|
type Request = T;
|
||||||
type Response = TlsStream<T>;
|
type Response = TlsStream<T>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Service = NativeTlsAcceptorService<T>;
|
type Service = NativeTlsAcceptorService<T>;
|
||||||
@ -60,7 +61,8 @@ pub struct NativeTlsAcceptorService<T> {
|
|||||||
conns: Counter,
|
conns: Counter,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> Service<T> for NativeTlsAcceptorService<T> {
|
impl<T: AsyncRead + AsyncWrite> Service for NativeTlsAcceptorService<T> {
|
||||||
|
type Request = T;
|
||||||
type Response = TlsStream<T>;
|
type Response = TlsStream<T>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Accept<T>;
|
type Future = Accept<T>;
|
||||||
|
@ -36,7 +36,8 @@ impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> NewService<T> for OpensslAcceptor<T> {
|
impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> {
|
||||||
|
type Request = T;
|
||||||
type Response = SslStream<T>;
|
type Response = SslStream<T>;
|
||||||
type Error = HandshakeError<T>;
|
type Error = HandshakeError<T>;
|
||||||
type Service = OpensslAcceptorService<T>;
|
type Service = OpensslAcceptorService<T>;
|
||||||
@ -60,7 +61,8 @@ pub struct OpensslAcceptorService<T> {
|
|||||||
conns: Counter,
|
conns: Counter,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> Service<T> for OpensslAcceptorService<T> {
|
impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
|
||||||
|
type Request = T;
|
||||||
type Response = SslStream<T>;
|
type Response = SslStream<T>;
|
||||||
type Error = HandshakeError<T>;
|
type Error = HandshakeError<T>;
|
||||||
type Future = OpensslAcceptorServiceFut<T>;
|
type Future = OpensslAcceptorServiceFut<T>;
|
||||||
|
@ -38,7 +38,8 @@ impl<T> Clone for RustlsAcceptor<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> NewService<T> for RustlsAcceptor<T> {
|
impl<T: AsyncRead + AsyncWrite> NewService for RustlsAcceptor<T> {
|
||||||
|
type Request = T;
|
||||||
type Response = TlsStream<T, ServerSession>;
|
type Response = TlsStream<T, ServerSession>;
|
||||||
type Error = io::Error;
|
type Error = io::Error;
|
||||||
type Service = RustlsAcceptorService<T>;
|
type Service = RustlsAcceptorService<T>;
|
||||||
@ -62,7 +63,8 @@ pub struct RustlsAcceptorService<T> {
|
|||||||
conns: Counter,
|
conns: Counter,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite> Service<T> for RustlsAcceptorService<T> {
|
impl<T: AsyncRead + AsyncWrite> Service for RustlsAcceptorService<T> {
|
||||||
|
type Request = T;
|
||||||
type Response = TlsStream<T, ServerSession>;
|
type Response = TlsStream<T, ServerSession>;
|
||||||
type Error = io::Error;
|
type Error = io::Error;
|
||||||
type Future = RustlsAcceptorServiceFut<T>;
|
type Future = RustlsAcceptorServiceFut<T>;
|
||||||
|
@ -1,5 +1,30 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.0] - 2019-02-01
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Use associated type instead of generic for Service definition.
|
||||||
|
|
||||||
|
* Before:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Service<Request> for Client {
|
||||||
|
type Response = Response;
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* After:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Service for Client {
|
||||||
|
type Request = Request;
|
||||||
|
type Response = Response;
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## [0.1.6] - 2019-01-24
|
## [0.1.6] - 2019-01-24
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-service"
|
name = "actix-service"
|
||||||
version = "0.1.6"
|
version = "0.2.0"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix Service"
|
description = "Actix Service"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -15,7 +15,7 @@ workspace = "../"
|
|||||||
|
|
||||||
[badges]
|
[badges]
|
||||||
travis-ci = { repository = "actix/actix-service", branch = "master" }
|
travis-ci = { repository = "actix/actix-service", branch = "master" }
|
||||||
# appveyor = { repository = "fafhrd91/actix-web-hdy9d" }
|
appveyor = { repository = "actix/actix-net" }
|
||||||
codecov = { repository = "actix/actix-service", branch = "master", service = "github" }
|
codecov = { repository = "actix/actix-service", branch = "master", service = "github" }
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
@ -14,10 +14,10 @@ pub struct AndThen<A, B> {
|
|||||||
|
|
||||||
impl<A, B> AndThen<A, B> {
|
impl<A, B> AndThen<A, B> {
|
||||||
/// Create new `AndThen` combinator
|
/// Create new `AndThen` combinator
|
||||||
pub fn new<Request>(a: A, b: B) -> Self
|
pub fn new(a: A, b: B) -> Self
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
B: Service<A::Response, Error = A::Error>,
|
B: Service<Request = A::Response, Error = A::Error>,
|
||||||
{
|
{
|
||||||
Self { a, b: Cell::new(b) }
|
Self { a, b: Cell::new(b) }
|
||||||
}
|
}
|
||||||
@ -35,39 +35,40 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> Service<Request> for AndThen<A, B>
|
impl<A, B> Service for AndThen<A, B>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
B: Service<A::Response, Error = A::Error>,
|
B: Service<Request = A::Response, Error = A::Error>,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = B::Response;
|
type Response = B::Response;
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type Future = AndThenFuture<A, B, Request>;
|
type Future = AndThenFuture<A, B>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
try_ready!(self.a.poll_ready());
|
try_ready!(self.a.poll_ready());
|
||||||
self.b.get_mut().poll_ready()
|
self.b.get_mut().poll_ready()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Request) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
AndThenFuture::new(self.a.call(req), self.b.clone())
|
AndThenFuture::new(self.a.call(req), self.b.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AndThenFuture<A, B, Request>
|
pub struct AndThenFuture<A, B>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
B: Service<A::Response, Error = A::Error>,
|
B: Service<Request = A::Response, Error = A::Error>,
|
||||||
{
|
{
|
||||||
b: Cell<B>,
|
b: Cell<B>,
|
||||||
fut_b: Option<B::Future>,
|
fut_b: Option<B::Future>,
|
||||||
fut_a: Option<A::Future>,
|
fut_a: Option<A::Future>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> AndThenFuture<A, B, Request>
|
impl<A, B> AndThenFuture<A, B>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
B: Service<A::Response, Error = A::Error>,
|
B: Service<Request = A::Response, Error = A::Error>,
|
||||||
{
|
{
|
||||||
fn new(a: A::Future, b: Cell<B>) -> Self {
|
fn new(a: A::Future, b: Cell<B>) -> Self {
|
||||||
AndThenFuture {
|
AndThenFuture {
|
||||||
@ -78,10 +79,10 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> Future for AndThenFuture<A, B, Request>
|
impl<A, B> Future for AndThenFuture<A, B>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
B: Service<A::Response, Error = A::Error>,
|
B: Service<Request = A::Response, Error = A::Error>,
|
||||||
{
|
{
|
||||||
type Item = B::Response;
|
type Item = B::Response;
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
@ -111,10 +112,10 @@ pub struct AndThenNewService<A, B> {
|
|||||||
|
|
||||||
impl<A, B> AndThenNewService<A, B> {
|
impl<A, B> AndThenNewService<A, B> {
|
||||||
/// Create new `AndThen` combinator
|
/// Create new `AndThen` combinator
|
||||||
pub fn new<Request, F: IntoNewService<B, A::Response>>(a: A, f: F) -> Self
|
pub fn new<F: IntoNewService<B>>(a: A, f: F) -> Self
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<A::Response, Error = A::Error, InitError = A::InitError>,
|
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
a,
|
a,
|
||||||
@ -123,17 +124,18 @@ impl<A, B> AndThenNewService<A, B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> NewService<Request> for AndThenNewService<A, B>
|
impl<A, B> NewService for AndThenNewService<A, B>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<A::Response, Error = A::Error, InitError = A::InitError>,
|
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = B::Response;
|
type Response = B::Response;
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type Service = AndThen<A::Service, B::Service>;
|
type Service = AndThen<A::Service, B::Service>;
|
||||||
|
|
||||||
type InitError = A::InitError;
|
type InitError = A::InitError;
|
||||||
type Future = AndThenNewServiceFuture<A, B, Request>;
|
type Future = AndThenNewServiceFuture<A, B>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
AndThenNewServiceFuture::new(self.a.new_service(), self.b.new_service())
|
AndThenNewServiceFuture::new(self.a.new_service(), self.b.new_service())
|
||||||
@ -153,10 +155,10 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AndThenNewServiceFuture<A, B, Request>
|
pub struct AndThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<A::Response>,
|
B: NewService<Request = A::Response>,
|
||||||
{
|
{
|
||||||
fut_b: B::Future,
|
fut_b: B::Future,
|
||||||
fut_a: A::Future,
|
fut_a: A::Future,
|
||||||
@ -164,10 +166,10 @@ where
|
|||||||
b: Option<B::Service>,
|
b: Option<B::Service>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> AndThenNewServiceFuture<A, B, Request>
|
impl<A, B> AndThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<A::Response>,
|
B: NewService<Request = A::Response>,
|
||||||
{
|
{
|
||||||
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
||||||
AndThenNewServiceFuture {
|
AndThenNewServiceFuture {
|
||||||
@ -179,10 +181,10 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> Future for AndThenNewServiceFuture<A, B, Request>
|
impl<A, B> Future for AndThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<A::Response, Error = A::Error, InitError = A::InitError>,
|
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
||||||
{
|
{
|
||||||
type Item = AndThen<A::Service, B::Service>;
|
type Item = AndThen<A::Service, B::Service>;
|
||||||
type Error = A::InitError;
|
type Error = A::InitError;
|
||||||
@ -222,7 +224,8 @@ mod tests {
|
|||||||
use crate::{NewService, Service, ServiceExt};
|
use crate::{NewService, Service, ServiceExt};
|
||||||
|
|
||||||
struct Srv1(Rc<Cell<usize>>);
|
struct Srv1(Rc<Cell<usize>>);
|
||||||
impl Service<&'static str> for Srv1 {
|
impl Service for Srv1 {
|
||||||
|
type Request = &'static str;
|
||||||
type Response = &'static str;
|
type Response = &'static str;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<Self::Response, ()>;
|
type Future = FutureResult<Self::Response, ()>;
|
||||||
@ -240,7 +243,8 @@ mod tests {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Srv2(Rc<Cell<usize>>);
|
struct Srv2(Rc<Cell<usize>>);
|
||||||
|
|
||||||
impl Service<&'static str> for Srv2 {
|
impl Service for Srv2 {
|
||||||
|
type Request = &'static str;
|
||||||
type Response = (&'static str, &'static str);
|
type Response = (&'static str, &'static str);
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<Self::Response, ()>;
|
type Future = FutureResult<Self::Response, ()>;
|
||||||
|
@ -6,10 +6,10 @@ use super::{IntoNewService, IntoService, NewService, Service};
|
|||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
|
|
||||||
/// `Apply` service combinator
|
/// `Apply` service combinator
|
||||||
pub struct AndThenApply<A, B, F, Out, Req1, Req2>
|
pub struct AndThenApply<A, B, F, Out>
|
||||||
where
|
where
|
||||||
A: Service<Req1>,
|
A: Service,
|
||||||
B: Service<Req2, Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<A::Error>,
|
Out::Error: Into<A::Error>,
|
||||||
@ -17,19 +17,19 @@ where
|
|||||||
a: A,
|
a: A,
|
||||||
b: Cell<B>,
|
b: Cell<B>,
|
||||||
f: Cell<F>,
|
f: Cell<F>,
|
||||||
r: PhantomData<(Out, Req1, Req2)>,
|
r: PhantomData<(Out,)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out, Req1, Req2> AndThenApply<A, B, F, Out, Req1, Req2>
|
impl<A, B, F, Out> AndThenApply<A, B, F, Out>
|
||||||
where
|
where
|
||||||
A: Service<Req1>,
|
A: Service,
|
||||||
B: Service<Req2, Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<A::Error>,
|
Out::Error: Into<A::Error>,
|
||||||
{
|
{
|
||||||
/// Create new `Apply` combinator
|
/// Create new `Apply` combinator
|
||||||
pub fn new<A1: IntoService<A, Req1>, B1: IntoService<B, Req2>>(a: A1, b: B1, f: F) -> Self {
|
pub fn new<A1: IntoService<A>, B1: IntoService<B>>(a: A1, b: B1, f: F) -> Self {
|
||||||
Self {
|
Self {
|
||||||
f: Cell::new(f),
|
f: Cell::new(f),
|
||||||
a: a.into_service(),
|
a: a.into_service(),
|
||||||
@ -39,10 +39,10 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out, Req1, Req2> Clone for AndThenApply<A, B, F, Out, Req1, Req2>
|
impl<A, B, F, Out> Clone for AndThenApply<A, B, F, Out>
|
||||||
where
|
where
|
||||||
A: Service<Req1> + Clone,
|
A: Service + Clone,
|
||||||
B: Service<Req2, Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<A::Error>,
|
Out::Error: Into<A::Error>,
|
||||||
@ -57,38 +57,38 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out, Req1, Req2> Service<Req1> for AndThenApply<A, B, F, Out, Req1, Req2>
|
impl<A, B, F, Out> Service for AndThenApply<A, B, F, Out>
|
||||||
where
|
where
|
||||||
A: Service<Req1>,
|
A: Service,
|
||||||
B: Service<Req2, Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<A::Error>,
|
Out::Error: Into<A::Error>,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = Out::Item;
|
type Response = Out::Item;
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type Future = AndThenApplyFuture<A, B, F, Out, Req1, Req2>;
|
type Future = AndThenApplyFuture<A, B, F, Out>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
try_ready!(self.a.poll_ready());
|
try_ready!(self.a.poll_ready());
|
||||||
self.b.get_mut().poll_ready().map_err(|e| e.into())
|
self.b.get_mut().poll_ready().map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Req1) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
AndThenApplyFuture {
|
AndThenApplyFuture {
|
||||||
b: self.b.clone(),
|
b: self.b.clone(),
|
||||||
f: self.f.clone(),
|
f: self.f.clone(),
|
||||||
fut_b: None,
|
fut_b: None,
|
||||||
fut_a: Some(self.a.call(req)),
|
fut_a: Some(self.a.call(req)),
|
||||||
_t: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AndThenApplyFuture<A, B, F, Out, Req1, Req2>
|
pub struct AndThenApplyFuture<A, B, F, Out>
|
||||||
where
|
where
|
||||||
A: Service<Req1>,
|
A: Service,
|
||||||
B: Service<Req2, Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<A::Error>,
|
Out::Error: Into<A::Error>,
|
||||||
@ -97,13 +97,12 @@ where
|
|||||||
f: Cell<F>,
|
f: Cell<F>,
|
||||||
fut_a: Option<A::Future>,
|
fut_a: Option<A::Future>,
|
||||||
fut_b: Option<Out::Future>,
|
fut_b: Option<Out::Future>,
|
||||||
_t: PhantomData<Req2>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out, Req1, Req2> Future for AndThenApplyFuture<A, B, F, Out, Req1, Req2>
|
impl<A, B, F, Out> Future for AndThenApplyFuture<A, B, F, Out>
|
||||||
where
|
where
|
||||||
A: Service<Req1>,
|
A: Service,
|
||||||
B: Service<Req2, Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<A::Error>,
|
Out::Error: Into<A::Error>,
|
||||||
@ -130,27 +129,23 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `ApplyNewService` new service combinator
|
/// `ApplyNewService` new service combinator
|
||||||
pub struct AndThenApplyNewService<A, B, F, Out, Req1, Req2> {
|
pub struct AndThenApplyNewService<A, B, F, Out> {
|
||||||
a: A,
|
a: A,
|
||||||
b: B,
|
b: B,
|
||||||
f: Cell<F>,
|
f: Cell<F>,
|
||||||
r: PhantomData<(Out, Req1, Req2)>,
|
r: PhantomData<(Out)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out, Req1, Req2> AndThenApplyNewService<A, B, F, Out, Req1, Req2>
|
impl<A, B, F, Out> AndThenApplyNewService<A, B, F, Out>
|
||||||
where
|
where
|
||||||
A: NewService<Req1>,
|
A: NewService,
|
||||||
B: NewService<Req2, Error = A::Error, InitError = A::InitError>,
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
F: FnMut(A::Response, &mut B::Service) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<A::Error>,
|
Out::Error: Into<A::Error>,
|
||||||
{
|
{
|
||||||
/// Create new `ApplyNewService` new service instance
|
/// Create new `ApplyNewService` new service instance
|
||||||
pub fn new<A1: IntoNewService<A, Req1>, B1: IntoNewService<B, Req2>>(
|
pub fn new<A1: IntoNewService<A>, B1: IntoNewService<B>>(a: A1, b: B1, f: F) -> Self {
|
||||||
a: A1,
|
|
||||||
b: B1,
|
|
||||||
f: F,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
f: Cell::new(f),
|
f: Cell::new(f),
|
||||||
a: a.into_new_service(),
|
a: a.into_new_service(),
|
||||||
@ -160,7 +155,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out, Req1, Req2> Clone for AndThenApplyNewService<A, B, F, Out, Req1, Req2>
|
impl<A, B, F, Out> Clone for AndThenApplyNewService<A, B, F, Out>
|
||||||
where
|
where
|
||||||
A: Clone,
|
A: Clone,
|
||||||
B: Clone,
|
B: Clone,
|
||||||
@ -175,21 +170,21 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out, Req1, Req2> NewService<Req1>
|
impl<A, B, F, Out> NewService for AndThenApplyNewService<A, B, F, Out>
|
||||||
for AndThenApplyNewService<A, B, F, Out, Req1, Req2>
|
|
||||||
where
|
where
|
||||||
A: NewService<Req1>,
|
A: NewService,
|
||||||
B: NewService<Req2, Error = A::Error, InitError = A::InitError>,
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
F: FnMut(A::Response, &mut B::Service) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<A::Error>,
|
Out::Error: Into<A::Error>,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = Out::Item;
|
type Response = Out::Item;
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
|
|
||||||
type InitError = A::InitError;
|
type InitError = A::InitError;
|
||||||
type Service = AndThenApply<A::Service, B::Service, F, Out, Req1, Req2>;
|
type Service = AndThenApply<A::Service, B::Service, F, Out>;
|
||||||
type Future = AndThenApplyNewServiceFuture<A, B, F, Out, Req1, Req2>;
|
type Future = AndThenApplyNewServiceFuture<A, B, F, Out>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
AndThenApplyNewServiceFuture {
|
AndThenApplyNewServiceFuture {
|
||||||
@ -202,10 +197,10 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AndThenApplyNewServiceFuture<A, B, F, Out, Req1, Req2>
|
pub struct AndThenApplyNewServiceFuture<A, B, F, Out>
|
||||||
where
|
where
|
||||||
A: NewService<Req1>,
|
A: NewService,
|
||||||
B: NewService<Req2, Error = A::Error, InitError = A::InitError>,
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
F: FnMut(A::Response, &mut B::Service) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<A::Error>,
|
Out::Error: Into<A::Error>,
|
||||||
@ -217,15 +212,15 @@ where
|
|||||||
b: Option<B::Service>,
|
b: Option<B::Service>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out, Req1, Req2> Future for AndThenApplyNewServiceFuture<A, B, F, Out, Req1, Req2>
|
impl<A, B, F, Out> Future for AndThenApplyNewServiceFuture<A, B, F, Out>
|
||||||
where
|
where
|
||||||
A: NewService<Req1>,
|
A: NewService,
|
||||||
B: NewService<Req2, Error = A::Error, InitError = A::InitError>,
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
F: FnMut(A::Response, &mut B::Service) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<A::Error>,
|
Out::Error: Into<A::Error>,
|
||||||
{
|
{
|
||||||
type Item = AndThenApply<A::Service, B::Service, F, Out, Req1, Req2>;
|
type Item = AndThenApply<A::Service, B::Service, F, Out>;
|
||||||
type Error = A::InitError;
|
type Error = A::InitError;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
@ -263,7 +258,8 @@ mod tests {
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Srv;
|
struct Srv;
|
||||||
impl Service<()> for Srv {
|
impl Service for Srv {
|
||||||
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<(), ()>;
|
type Future = FutureResult<(), ()>;
|
||||||
|
@ -5,24 +5,24 @@ use futures::{Async, Future, IntoFuture, Poll};
|
|||||||
use super::{IntoNewService, IntoService, NewService, Service};
|
use super::{IntoNewService, IntoService, NewService, Service};
|
||||||
|
|
||||||
/// `Apply` service combinator
|
/// `Apply` service combinator
|
||||||
pub struct Apply<T, F, In, Out, Request>
|
pub struct Apply<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
{
|
{
|
||||||
service: T,
|
service: T,
|
||||||
f: F,
|
f: F,
|
||||||
r: PhantomData<(In, Out, Request)>,
|
r: PhantomData<(In, Out)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out, Request> Apply<T, F, In, Out, Request>
|
impl<T, F, In, Out> Apply<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
F: FnMut(In, &mut T) -> Out,
|
F: FnMut(In, &mut T) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: From<T::Error>,
|
Out::Error: From<T::Error>,
|
||||||
{
|
{
|
||||||
/// Create new `Apply` combinator
|
/// Create new `Apply` combinator
|
||||||
pub fn new<I: IntoService<T, Request>>(service: I, f: F) -> Self {
|
pub fn new<I: IntoService<T>>(service: I, f: F) -> Self {
|
||||||
Self {
|
Self {
|
||||||
service: service.into_service(),
|
service: service.into_service(),
|
||||||
f,
|
f,
|
||||||
@ -31,9 +31,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out, Request> Clone for Apply<T, F, In, Out, Request>
|
impl<T, F, In, Out> Clone for Apply<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: Service<Request> + Clone,
|
T: Service + Clone,
|
||||||
F: Clone,
|
F: Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
@ -45,13 +45,14 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out, Request> Service<In> for Apply<T, F, In, Out, Request>
|
impl<T, F, In, Out> Service for Apply<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
F: FnMut(In, &mut T) -> Out,
|
F: FnMut(In, &mut T) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: From<T::Error>,
|
Out::Error: From<T::Error>,
|
||||||
{
|
{
|
||||||
|
type Request = In;
|
||||||
type Response = Out::Item;
|
type Response = Out::Item;
|
||||||
type Error = Out::Error;
|
type Error = Out::Error;
|
||||||
type Future = Out::Future;
|
type Future = Out::Future;
|
||||||
@ -66,24 +67,24 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `ApplyNewService` new service combinator
|
/// `ApplyNewService` new service combinator
|
||||||
pub struct ApplyNewService<T, F, In, Out, Request>
|
pub struct ApplyNewService<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: NewService<Request>,
|
T: NewService,
|
||||||
{
|
{
|
||||||
service: T,
|
service: T,
|
||||||
f: F,
|
f: F,
|
||||||
r: PhantomData<(In, Out, Request)>,
|
r: PhantomData<(In, Out)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out, Request> ApplyNewService<T, F, In, Out, Request>
|
impl<T, F, In, Out> ApplyNewService<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: NewService<Request>,
|
T: NewService,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: From<T::Error>,
|
Out::Error: From<T::Error>,
|
||||||
{
|
{
|
||||||
/// Create new `ApplyNewService` new service instance
|
/// Create new `ApplyNewService` new service instance
|
||||||
pub fn new<F1: IntoNewService<T, Request>>(service: F1, f: F) -> Self {
|
pub fn new<F1: IntoNewService<T>>(service: F1, f: F) -> Self {
|
||||||
Self {
|
Self {
|
||||||
f,
|
f,
|
||||||
service: service.into_new_service(),
|
service: service.into_new_service(),
|
||||||
@ -92,9 +93,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out, Request> Clone for ApplyNewService<T, F, In, Out, Request>
|
impl<T, F, In, Out> Clone for ApplyNewService<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: NewService<Request> + Clone,
|
T: NewService + Clone,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
@ -107,28 +108,29 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out, Request> NewService<In> for ApplyNewService<T, F, In, Out, Request>
|
impl<T, F, In, Out> NewService for ApplyNewService<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: NewService<Request>,
|
T: NewService,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: From<T::Error>,
|
Out::Error: From<T::Error>,
|
||||||
{
|
{
|
||||||
|
type Request = In;
|
||||||
type Response = Out::Item;
|
type Response = Out::Item;
|
||||||
type Error = Out::Error;
|
type Error = Out::Error;
|
||||||
type Service = Apply<T::Service, F, In, Out, Request>;
|
type Service = Apply<T::Service, F, In, Out>;
|
||||||
|
|
||||||
type InitError = T::InitError;
|
type InitError = T::InitError;
|
||||||
type Future = ApplyNewServiceFuture<T, F, In, Out, Request>;
|
type Future = ApplyNewServiceFuture<T, F, In, Out>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
ApplyNewServiceFuture::new(self.service.new_service(), self.f.clone())
|
ApplyNewServiceFuture::new(self.service.new_service(), self.f.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ApplyNewServiceFuture<T, F, In, Out, Request>
|
pub struct ApplyNewServiceFuture<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: NewService<Request>,
|
T: NewService,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
@ -137,9 +139,9 @@ where
|
|||||||
r: PhantomData<(In, Out)>,
|
r: PhantomData<(In, Out)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out, Request> ApplyNewServiceFuture<T, F, In, Out, Request>
|
impl<T, F, In, Out> ApplyNewServiceFuture<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: NewService<Request>,
|
T: NewService,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
@ -152,14 +154,14 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out, Request> Future for ApplyNewServiceFuture<T, F, In, Out, Request>
|
impl<T, F, In, Out> Future for ApplyNewServiceFuture<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: NewService<Request>,
|
T: NewService,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: From<T::Error>,
|
Out::Error: From<T::Error>,
|
||||||
{
|
{
|
||||||
type Item = Apply<T::Service, F, In, Out, Request>;
|
type Item = Apply<T::Service, F, In, Out>;
|
||||||
type Error = T::InitError;
|
type Error = T::InitError;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
@ -180,7 +182,8 @@ mod tests {
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Srv;
|
struct Srv;
|
||||||
impl Service<()> for Srv {
|
impl Service for Srv {
|
||||||
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<(), ()>;
|
type Future = FutureResult<(), ()>;
|
||||||
|
@ -42,11 +42,12 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, E, Fut> Service<Req> for FnService<F, Req, Resp, E, Fut>
|
impl<F, Req, Resp, E, Fut> Service for FnService<F, Req, Resp, E, Fut>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut,
|
F: FnMut(Req) -> Fut,
|
||||||
Fut: IntoFuture<Item = Resp, Error = E>,
|
Fut: IntoFuture<Item = Resp, Error = E>,
|
||||||
{
|
{
|
||||||
|
type Request = Req;
|
||||||
type Response = Resp;
|
type Response = Resp;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Future = Fut::Future;
|
type Future = Fut::Future;
|
||||||
@ -60,7 +61,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut> IntoService<FnService<F, Req, Resp, Err, Fut>, Req> for F
|
impl<F, Req, Resp, Err, Fut> IntoService<FnService<F, Req, Resp, Err, Fut>> for F
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut + 'static,
|
F: FnMut(Req) -> Fut + 'static,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||||
@ -92,11 +93,12 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut> NewService<Req> for FnNewService<F, Req, Resp, Err, Fut>
|
impl<F, Req, Resp, Err, Fut> NewService for FnNewService<F, Req, Resp, Err, Fut>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut + Clone,
|
F: FnMut(Req) -> Fut + Clone,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||||
{
|
{
|
||||||
|
type Request = Req;
|
||||||
type Response = Resp;
|
type Response = Resp;
|
||||||
type Error = Err;
|
type Error = Err;
|
||||||
type Service = FnService<F, Req, Resp, Err, Fut>;
|
type Service = FnService<F, Req, Resp, Err, Fut>;
|
||||||
@ -108,7 +110,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut> IntoNewService<FnNewService<F, Req, Resp, Err, Fut>, Req> for F
|
impl<F, Req, Resp, Err, Fut> IntoNewService<FnNewService<F, Req, Resp, Err, Fut>> for F
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut + Clone + 'static,
|
F: FnMut(Req) -> Fut + Clone + 'static,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||||
|
@ -13,9 +13,9 @@ pub struct FromErr<A, E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<A, E> FromErr<A, E> {
|
impl<A, E> FromErr<A, E> {
|
||||||
pub(crate) fn new<Request>(service: A) -> Self
|
pub(crate) fn new(service: A) -> Self
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
E: From<A::Error>,
|
E: From<A::Error>,
|
||||||
{
|
{
|
||||||
FromErr {
|
FromErr {
|
||||||
@ -37,20 +37,21 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, E, Request> Service<Request> for FromErr<A, E>
|
impl<A, E> Service for FromErr<A, E>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
E: From<A::Error>,
|
E: From<A::Error>,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = A::Response;
|
type Response = A::Response;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Future = FromErrFuture<A, E, Request>;
|
type Future = FromErrFuture<A, E>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), E> {
|
fn poll_ready(&mut self) -> Poll<(), E> {
|
||||||
self.service.poll_ready().map_err(E::from)
|
self.service.poll_ready().map_err(E::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Request) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
FromErrFuture {
|
FromErrFuture {
|
||||||
fut: self.service.call(req),
|
fut: self.service.call(req),
|
||||||
f: PhantomData,
|
f: PhantomData,
|
||||||
@ -58,14 +59,14 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FromErrFuture<A: Service<Request>, E, Request> {
|
pub struct FromErrFuture<A: Service, E> {
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
f: PhantomData<E>,
|
f: PhantomData<E>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, E, Request> Future for FromErrFuture<A, E, Request>
|
impl<A, E> Future for FromErrFuture<A, E>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
E: From<A::Error>,
|
E: From<A::Error>,
|
||||||
{
|
{
|
||||||
type Item = A::Response;
|
type Item = A::Response;
|
||||||
@ -87,9 +88,9 @@ pub struct FromErrNewService<A, E> {
|
|||||||
|
|
||||||
impl<A, E> FromErrNewService<A, E> {
|
impl<A, E> FromErrNewService<A, E> {
|
||||||
/// Create new `FromErr` new service instance
|
/// Create new `FromErr` new service instance
|
||||||
pub fn new<Request>(a: A) -> Self
|
pub fn new(a: A) -> Self
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
E: From<A::Error>,
|
E: From<A::Error>,
|
||||||
{
|
{
|
||||||
Self { a, e: PhantomData }
|
Self { a, e: PhantomData }
|
||||||
@ -108,17 +109,18 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, E, Request> NewService<Request> for FromErrNewService<A, E>
|
impl<A, E> NewService for FromErrNewService<A, E>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
E: From<A::Error>,
|
E: From<A::Error>,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = A::Response;
|
type Response = A::Response;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Service = FromErr<A::Service, E>;
|
type Service = FromErr<A::Service, E>;
|
||||||
|
|
||||||
type InitError = A::InitError;
|
type InitError = A::InitError;
|
||||||
type Future = FromErrNewServiceFuture<A, E, Request>;
|
type Future = FromErrNewServiceFuture<A, E>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
FromErrNewServiceFuture {
|
FromErrNewServiceFuture {
|
||||||
@ -128,18 +130,18 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FromErrNewServiceFuture<A, E, Request>
|
pub struct FromErrNewServiceFuture<A, E>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
E: From<A::Error>,
|
E: From<A::Error>,
|
||||||
{
|
{
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
e: PhantomData<E>,
|
e: PhantomData<E>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, E, Request> Future for FromErrNewServiceFuture<A, E, Request>
|
impl<A, E> Future for FromErrNewServiceFuture<A, E>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
E: From<A::Error>,
|
E: From<A::Error>,
|
||||||
{
|
{
|
||||||
type Item = FromErr<A::Service, E>;
|
type Item = FromErr<A::Service, E>;
|
||||||
@ -162,7 +164,8 @@ mod tests {
|
|||||||
use crate::{IntoNewService, NewService, Service, ServiceExt};
|
use crate::{IntoNewService, NewService, Service, ServiceExt};
|
||||||
|
|
||||||
struct Srv;
|
struct Srv;
|
||||||
impl Service<()> for Srv {
|
impl Service for Srv {
|
||||||
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<(), ()>;
|
type Future = FutureResult<(), ()>;
|
||||||
|
@ -22,7 +22,10 @@ pub use self::map_init_err::MapInitErr;
|
|||||||
pub use self::then::{Then, ThenNewService};
|
pub use self::then::{Then, ThenNewService};
|
||||||
|
|
||||||
/// An asynchronous function from `Request` to a `Response`.
|
/// An asynchronous function from `Request` to a `Response`.
|
||||||
pub trait Service<Request> {
|
pub trait Service {
|
||||||
|
/// Requests handled by the service.
|
||||||
|
type Request;
|
||||||
|
|
||||||
/// Responses given by the service.
|
/// Responses given by the service.
|
||||||
type Response;
|
type Response;
|
||||||
|
|
||||||
@ -52,23 +55,19 @@ pub trait Service<Request> {
|
|||||||
///
|
///
|
||||||
/// Calling `call` without calling `poll_ready` is permitted. The
|
/// Calling `call` without calling `poll_ready` is permitted. The
|
||||||
/// implementation must be resilient to this fact.
|
/// implementation must be resilient to this fact.
|
||||||
fn call(&mut self, req: Request) -> Self::Future;
|
fn call(&mut self, req: Self::Request) -> Self::Future;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An extension trait for `Service`s that provides a variety of convenient
|
/// An extension trait for `Service`s that provides a variety of convenient
|
||||||
/// adapters
|
/// adapters
|
||||||
pub trait ServiceExt<Request>: Service<Request> {
|
pub trait ServiceExt: Service {
|
||||||
/// Apply function to specified service and use it as a next service in
|
/// Apply function to specified service and use it as a next service in
|
||||||
/// chain.
|
/// chain.
|
||||||
fn apply<B, I, F, Out, Req>(
|
fn apply<B, I, F, Out, Req>(self, service: I, f: F) -> AndThenApply<Self, B, F, Out>
|
||||||
self,
|
|
||||||
service: I,
|
|
||||||
f: F,
|
|
||||||
) -> AndThenApply<Self, B, F, Out, Request, Req>
|
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
B: Service<Req, Error = Self::Error>,
|
B: Service<Request = Req, Error = Self::Error>,
|
||||||
I: IntoService<B, Req>,
|
I: IntoService<B>,
|
||||||
F: FnMut(Self::Response, &mut B) -> Out,
|
F: FnMut(Self::Response, &mut B) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<Self::Error>,
|
Out::Error: Into<Self::Error>,
|
||||||
@ -88,8 +87,8 @@ pub trait ServiceExt<Request>: Service<Request> {
|
|||||||
fn and_then<F, B>(self, service: F) -> AndThen<Self, B>
|
fn and_then<F, B>(self, service: F) -> AndThen<Self, B>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
F: IntoService<B, Self::Response>,
|
F: IntoService<B>,
|
||||||
B: Service<Self::Response, Error = Self::Error>,
|
B: Service<Request = Self::Response, Error = Self::Error>,
|
||||||
{
|
{
|
||||||
AndThen::new(self, service.into_service())
|
AndThen::new(self, service.into_service())
|
||||||
}
|
}
|
||||||
@ -115,7 +114,7 @@ pub trait ServiceExt<Request>: Service<Request> {
|
|||||||
fn then<B>(self, service: B) -> Then<Self, B>
|
fn then<B>(self, service: B) -> Then<Self, B>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
B: Service<Result<Self::Response, Self::Error>, Error = Self::Error>,
|
B: Service<Request = Result<Self::Response, Self::Error>, Error = Self::Error>,
|
||||||
{
|
{
|
||||||
Then::new(self, service)
|
Then::new(self, service)
|
||||||
}
|
}
|
||||||
@ -154,7 +153,7 @@ pub trait ServiceExt<Request>: Service<Request> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ?Sized, Request> ServiceExt<Request> for T where T: Service<Request> {}
|
impl<T: ?Sized> ServiceExt for T where T: Service {}
|
||||||
|
|
||||||
/// Creates new `Service` values.
|
/// Creates new `Service` values.
|
||||||
///
|
///
|
||||||
@ -163,9 +162,10 @@ impl<T: ?Sized, Request> ServiceExt<Request> for T where T: Service<Request> {}
|
|||||||
/// accepts new TCP streams, obtains a new `Service` value using the
|
/// accepts new TCP streams, obtains a new `Service` value using the
|
||||||
/// `NewService` trait, and uses that new `Service` value to process inbound
|
/// `NewService` trait, and uses that new `Service` value to process inbound
|
||||||
/// requests on that new TCP stream.
|
/// requests on that new TCP stream.
|
||||||
///
|
pub trait NewService {
|
||||||
/// Request - request handled by the service
|
/// Requests handled by the service.
|
||||||
pub trait NewService<Request> {
|
type Request;
|
||||||
|
|
||||||
/// Responses given by the service
|
/// Responses given by the service
|
||||||
type Response;
|
type Response;
|
||||||
|
|
||||||
@ -173,7 +173,11 @@ pub trait NewService<Request> {
|
|||||||
type Error;
|
type Error;
|
||||||
|
|
||||||
/// The `Service` value created by this factory
|
/// The `Service` value created by this factory
|
||||||
type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
|
type Service: Service<
|
||||||
|
Request = Self::Request,
|
||||||
|
Response = Self::Response,
|
||||||
|
Error = Self::Error,
|
||||||
|
>;
|
||||||
|
|
||||||
/// Errors produced while building a service.
|
/// Errors produced while building a service.
|
||||||
type InitError;
|
type InitError;
|
||||||
@ -190,11 +194,11 @@ pub trait NewService<Request> {
|
|||||||
self,
|
self,
|
||||||
service: I,
|
service: I,
|
||||||
f: F,
|
f: F,
|
||||||
) -> AndThenApplyNewService<Self, B, F, Out, Request, Req>
|
) -> AndThenApplyNewService<Self, B, F, Out>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
B: NewService<Req, Error = Self::Error, InitError = Self::InitError>,
|
B: NewService<Request = Req, Error = Self::Error, InitError = Self::InitError>,
|
||||||
I: IntoNewService<B, Req>,
|
I: IntoNewService<B>,
|
||||||
F: FnMut(Self::Response, &mut B::Service) -> Out,
|
F: FnMut(Self::Response, &mut B::Service) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<Self::Error>,
|
Out::Error: Into<Self::Error>,
|
||||||
@ -206,8 +210,12 @@ pub trait NewService<Request> {
|
|||||||
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
|
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
F: IntoNewService<B, Self::Response>,
|
F: IntoNewService<B>,
|
||||||
B: NewService<Self::Response, Error = Self::Error, InitError = Self::InitError>,
|
B: NewService<
|
||||||
|
Request = Self::Response,
|
||||||
|
Error = Self::Error,
|
||||||
|
InitError = Self::InitError,
|
||||||
|
>,
|
||||||
{
|
{
|
||||||
AndThenNewService::new(self, new_service)
|
AndThenNewService::new(self, new_service)
|
||||||
}
|
}
|
||||||
@ -235,9 +243,9 @@ pub trait NewService<Request> {
|
|||||||
fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B>
|
fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
F: IntoNewService<B, Result<Self::Response, Self::Error>>,
|
F: IntoNewService<B>,
|
||||||
B: NewService<
|
B: NewService<
|
||||||
Result<Self::Response, Self::Error>,
|
Request = Result<Self::Response, Self::Error>,
|
||||||
Error = Self::Error,
|
Error = Self::Error,
|
||||||
InitError = Self::InitError,
|
InitError = Self::InitError,
|
||||||
>,
|
>,
|
||||||
@ -274,10 +282,11 @@ pub trait NewService<Request> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S, Request> Service<Request> for &'a mut S
|
impl<'a, S> Service for &'a mut S
|
||||||
where
|
where
|
||||||
S: Service<Request> + 'a,
|
S: Service + 'a,
|
||||||
{
|
{
|
||||||
|
type Request = S::Request;
|
||||||
type Response = S::Response;
|
type Response = S::Response;
|
||||||
type Error = S::Error;
|
type Error = S::Error;
|
||||||
type Future = S::Future;
|
type Future = S::Future;
|
||||||
@ -286,15 +295,16 @@ where
|
|||||||
(**self).poll_ready()
|
(**self).poll_ready()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, request: Request) -> S::Future {
|
fn call(&mut self, request: Self::Request) -> S::Future {
|
||||||
(**self).call(request)
|
(**self).call(request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, Request> Service<Request> for Box<S>
|
impl<S> Service for Box<S>
|
||||||
where
|
where
|
||||||
S: Service<Request> + ?Sized,
|
S: Service + ?Sized,
|
||||||
{
|
{
|
||||||
|
type Request = S::Request;
|
||||||
type Response = S::Response;
|
type Response = S::Response;
|
||||||
type Error = S::Error;
|
type Error = S::Error;
|
||||||
type Future = S::Future;
|
type Future = S::Future;
|
||||||
@ -303,17 +313,18 @@ where
|
|||||||
(**self).poll_ready()
|
(**self).poll_ready()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, request: Request) -> S::Future {
|
fn call(&mut self, request: Self::Request) -> S::Future {
|
||||||
(**self).call(request)
|
(**self).call(request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, R, E, S, Request> NewService<Request> for F
|
impl<F, R, E, S> NewService for F
|
||||||
where
|
where
|
||||||
F: Fn() -> R,
|
F: Fn() -> R,
|
||||||
R: IntoFuture<Item = S, Error = E>,
|
R: IntoFuture<Item = S, Error = E>,
|
||||||
S: Service<Request>,
|
S: Service,
|
||||||
{
|
{
|
||||||
|
type Request = S::Request;
|
||||||
type Response = S::Response;
|
type Response = S::Response;
|
||||||
type Error = S::Error;
|
type Error = S::Error;
|
||||||
type Service = S;
|
type Service = S;
|
||||||
@ -326,35 +337,35 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Trait for types that can be converted to a `Service`
|
/// Trait for types that can be converted to a `Service`
|
||||||
pub trait IntoService<T, Request>
|
pub trait IntoService<T>
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
{
|
{
|
||||||
/// Convert to a `Service`
|
/// Convert to a `Service`
|
||||||
fn into_service(self) -> T;
|
fn into_service(self) -> T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait for types that can be converted to a Service
|
/// Trait for types that can be converted to a Service
|
||||||
pub trait IntoNewService<T, Request>
|
pub trait IntoNewService<T>
|
||||||
where
|
where
|
||||||
T: NewService<Request>,
|
T: NewService,
|
||||||
{
|
{
|
||||||
/// Convert to an `NewService`
|
/// Convert to an `NewService`
|
||||||
fn into_new_service(self) -> T;
|
fn into_new_service(self) -> T;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, Request> IntoService<T, Request> for T
|
impl<T> IntoService<T> for T
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
{
|
{
|
||||||
fn into_service(self) -> T {
|
fn into_service(self) -> T {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, Request> IntoNewService<T, Request> for T
|
impl<T> IntoNewService<T> for T
|
||||||
where
|
where
|
||||||
T: NewService<Request>,
|
T: NewService,
|
||||||
{
|
{
|
||||||
fn into_new_service(self) -> T {
|
fn into_new_service(self) -> T {
|
||||||
self
|
self
|
||||||
|
@ -15,9 +15,9 @@ pub struct Map<A, F, Response> {
|
|||||||
|
|
||||||
impl<A, F, Response> Map<A, F, Response> {
|
impl<A, F, Response> Map<A, F, Response> {
|
||||||
/// Create new `Map` combinator
|
/// Create new `Map` combinator
|
||||||
pub fn new<Request>(service: A, f: F) -> Self
|
pub fn new(service: A, f: F) -> Self
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
F: FnMut(A::Response) -> Response,
|
F: FnMut(A::Response) -> Response,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
@ -42,36 +42,37 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, Request, Response> Service<Request> for Map<A, F, Response>
|
impl<A, F, Response> Service for Map<A, F, Response>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
F: FnMut(A::Response) -> Response + Clone,
|
F: FnMut(A::Response) -> Response + Clone,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = Response;
|
type Response = Response;
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type Future = MapFuture<A, F, Request, Response>;
|
type Future = MapFuture<A, F, Response>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
self.service.poll_ready()
|
self.service.poll_ready()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Request) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
MapFuture::new(self.service.call(req), self.f.clone())
|
MapFuture::new(self.service.call(req), self.f.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MapFuture<A, F, Request, Response>
|
pub struct MapFuture<A, F, Response>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
F: FnMut(A::Response) -> Response,
|
F: FnMut(A::Response) -> Response,
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, Request, Response> MapFuture<A, F, Request, Response>
|
impl<A, F, Response> MapFuture<A, F, Response>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
F: FnMut(A::Response) -> Response,
|
F: FnMut(A::Response) -> Response,
|
||||||
{
|
{
|
||||||
fn new(fut: A::Future, f: F) -> Self {
|
fn new(fut: A::Future, f: F) -> Self {
|
||||||
@ -79,9 +80,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, Request, Response> Future for MapFuture<A, F, Request, Response>
|
impl<A, F, Response> Future for MapFuture<A, F, Response>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
F: FnMut(A::Response) -> Response,
|
F: FnMut(A::Response) -> Response,
|
||||||
{
|
{
|
||||||
type Item = Response;
|
type Item = Response;
|
||||||
@ -104,9 +105,9 @@ pub struct MapNewService<A, F, Response> {
|
|||||||
|
|
||||||
impl<A, F, Response> MapNewService<A, F, Response> {
|
impl<A, F, Response> MapNewService<A, F, Response> {
|
||||||
/// Create new `Map` new service instance
|
/// Create new `Map` new service instance
|
||||||
pub fn new<Request>(a: A, f: F) -> Self
|
pub fn new(a: A, f: F) -> Self
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: FnMut(A::Response) -> Response,
|
F: FnMut(A::Response) -> Response,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
@ -131,35 +132,36 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, Request, Response> NewService<Request> for MapNewService<A, F, Response>
|
impl<A, F, Response> NewService for MapNewService<A, F, Response>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: FnMut(A::Response) -> Response + Clone,
|
F: FnMut(A::Response) -> Response + Clone,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = Response;
|
type Response = Response;
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type Service = Map<A::Service, F, Response>;
|
type Service = Map<A::Service, F, Response>;
|
||||||
|
|
||||||
type InitError = A::InitError;
|
type InitError = A::InitError;
|
||||||
type Future = MapNewServiceFuture<A, F, Request, Response>;
|
type Future = MapNewServiceFuture<A, F, Response>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
MapNewServiceFuture::new(self.a.new_service(), self.f.clone())
|
MapNewServiceFuture::new(self.a.new_service(), self.f.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MapNewServiceFuture<A, F, Request, Response>
|
pub struct MapNewServiceFuture<A, F, Response>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: FnMut(A::Response) -> Response,
|
F: FnMut(A::Response) -> Response,
|
||||||
{
|
{
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
f: Option<F>,
|
f: Option<F>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, Request, Response> MapNewServiceFuture<A, F, Request, Response>
|
impl<A, F, Response> MapNewServiceFuture<A, F, Response>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: FnMut(A::Response) -> Response,
|
F: FnMut(A::Response) -> Response,
|
||||||
{
|
{
|
||||||
fn new(fut: A::Future, f: F) -> Self {
|
fn new(fut: A::Future, f: F) -> Self {
|
||||||
@ -167,9 +169,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, Request, Response> Future for MapNewServiceFuture<A, F, Request, Response>
|
impl<A, F, Response> Future for MapNewServiceFuture<A, F, Response>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: FnMut(A::Response) -> Response,
|
F: FnMut(A::Response) -> Response,
|
||||||
{
|
{
|
||||||
type Item = Map<A::Service, F, Response>;
|
type Item = Map<A::Service, F, Response>;
|
||||||
@ -192,7 +194,8 @@ mod tests {
|
|||||||
use crate::{IntoNewService, Service, ServiceExt};
|
use crate::{IntoNewService, Service, ServiceExt};
|
||||||
|
|
||||||
struct Srv;
|
struct Srv;
|
||||||
impl Service<()> for Srv {
|
impl Service for Srv {
|
||||||
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<(), ()>;
|
type Future = FutureResult<(), ()>;
|
||||||
|
@ -16,9 +16,9 @@ pub struct MapErr<A, F, E> {
|
|||||||
|
|
||||||
impl<A, F, E> MapErr<A, F, E> {
|
impl<A, F, E> MapErr<A, F, E> {
|
||||||
/// Create new `MapErr` combinator
|
/// Create new `MapErr` combinator
|
||||||
pub fn new<Request>(service: A, f: F) -> Self
|
pub fn new(service: A, f: F) -> Self
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
F: Fn(A::Error) -> E,
|
F: Fn(A::Error) -> E,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
@ -43,36 +43,37 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E, Request> Service<Request> for MapErr<A, F, E>
|
impl<A, F, E> Service for MapErr<A, F, E>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
F: Fn(A::Error) -> E + Clone,
|
F: Fn(A::Error) -> E + Clone,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = A::Response;
|
type Response = A::Response;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Future = MapErrFuture<A, F, E, Request>;
|
type Future = MapErrFuture<A, F, E>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
self.service.poll_ready().map_err(&self.f)
|
self.service.poll_ready().map_err(&self.f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Request) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
MapErrFuture::new(self.service.call(req), self.f.clone())
|
MapErrFuture::new(self.service.call(req), self.f.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MapErrFuture<A, F, E, Request>
|
pub struct MapErrFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
F: Fn(A::Error) -> E,
|
F: Fn(A::Error) -> E,
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E, Request> MapErrFuture<A, F, E, Request>
|
impl<A, F, E> MapErrFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
F: Fn(A::Error) -> E,
|
F: Fn(A::Error) -> E,
|
||||||
{
|
{
|
||||||
fn new(fut: A::Future, f: F) -> Self {
|
fn new(fut: A::Future, f: F) -> Self {
|
||||||
@ -80,9 +81,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E, Request> Future for MapErrFuture<A, F, E, Request>
|
impl<A, F, E> Future for MapErrFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
F: Fn(A::Error) -> E,
|
F: Fn(A::Error) -> E,
|
||||||
{
|
{
|
||||||
type Item = A::Response;
|
type Item = A::Response;
|
||||||
@ -105,9 +106,9 @@ pub struct MapErrNewService<A, F, E> {
|
|||||||
|
|
||||||
impl<A, F, E> MapErrNewService<A, F, E> {
|
impl<A, F, E> MapErrNewService<A, F, E> {
|
||||||
/// Create new `MapErr` new service instance
|
/// Create new `MapErr` new service instance
|
||||||
pub fn new<Request>(a: A, f: F) -> Self
|
pub fn new(a: A, f: F) -> Self
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: Fn(A::Error) -> E,
|
F: Fn(A::Error) -> E,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
@ -132,35 +133,36 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E, Request> NewService<Request> for MapErrNewService<A, F, E>
|
impl<A, F, E> NewService for MapErrNewService<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: Fn(A::Error) -> E + Clone,
|
F: Fn(A::Error) -> E + Clone,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = A::Response;
|
type Response = A::Response;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Service = MapErr<A::Service, F, E>;
|
type Service = MapErr<A::Service, F, E>;
|
||||||
|
|
||||||
type InitError = A::InitError;
|
type InitError = A::InitError;
|
||||||
type Future = MapErrNewServiceFuture<A, F, E, Request>;
|
type Future = MapErrNewServiceFuture<A, F, E>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
MapErrNewServiceFuture::new(self.a.new_service(), self.f.clone())
|
MapErrNewServiceFuture::new(self.a.new_service(), self.f.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MapErrNewServiceFuture<A, F, E, Request>
|
pub struct MapErrNewServiceFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: Fn(A::Error) -> E,
|
F: Fn(A::Error) -> E,
|
||||||
{
|
{
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
f: F,
|
f: F,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E, Request> MapErrNewServiceFuture<A, F, E, Request>
|
impl<A, F, E> MapErrNewServiceFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: Fn(A::Error) -> E,
|
F: Fn(A::Error) -> E,
|
||||||
{
|
{
|
||||||
fn new(fut: A::Future, f: F) -> Self {
|
fn new(fut: A::Future, f: F) -> Self {
|
||||||
@ -168,9 +170,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E, Request> Future for MapErrNewServiceFuture<A, F, E, Request>
|
impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: Fn(A::Error) -> E + Clone,
|
F: Fn(A::Error) -> E + Clone,
|
||||||
{
|
{
|
||||||
type Item = MapErr<A::Service, F, E>;
|
type Item = MapErr<A::Service, F, E>;
|
||||||
@ -194,7 +196,8 @@ mod tests {
|
|||||||
|
|
||||||
struct Srv;
|
struct Srv;
|
||||||
|
|
||||||
impl Service<()> for Srv {
|
impl Service for Srv {
|
||||||
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<(), ()>;
|
type Future = FutureResult<(), ()>;
|
||||||
|
@ -13,9 +13,9 @@ pub struct MapInitErr<A, F, E> {
|
|||||||
|
|
||||||
impl<A, F, E> MapInitErr<A, F, E> {
|
impl<A, F, E> MapInitErr<A, F, E> {
|
||||||
/// Create new `MapInitErr` combinator
|
/// Create new `MapInitErr` combinator
|
||||||
pub fn new<Request>(a: A, f: F) -> Self
|
pub fn new(a: A, f: F) -> Self
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: Fn(A::InitError) -> E,
|
F: Fn(A::InitError) -> E,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
@ -40,35 +40,36 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E, Request> NewService<Request> for MapInitErr<A, F, E>
|
impl<A, F, E> NewService for MapInitErr<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: Fn(A::InitError) -> E + Clone,
|
F: Fn(A::InitError) -> E + Clone,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = A::Response;
|
type Response = A::Response;
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type Service = A::Service;
|
type Service = A::Service;
|
||||||
|
|
||||||
type InitError = E;
|
type InitError = E;
|
||||||
type Future = MapInitErrFuture<A, F, E, Request>;
|
type Future = MapInitErrFuture<A, F, E>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
MapInitErrFuture::new(self.a.new_service(), self.f.clone())
|
MapInitErrFuture::new(self.a.new_service(), self.f.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MapInitErrFuture<A, F, E, Request>
|
pub struct MapInitErrFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: Fn(A::InitError) -> E,
|
F: Fn(A::InitError) -> E,
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E, Request> MapInitErrFuture<A, F, E, Request>
|
impl<A, F, E> MapInitErrFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: Fn(A::InitError) -> E,
|
F: Fn(A::InitError) -> E,
|
||||||
{
|
{
|
||||||
fn new(fut: A::Future, f: F) -> Self {
|
fn new(fut: A::Future, f: F) -> Self {
|
||||||
@ -76,9 +77,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E, Request> Future for MapInitErrFuture<A, F, E, Request>
|
impl<A, F, E> Future for MapInitErrFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
F: Fn(A::InitError) -> E,
|
F: Fn(A::InitError) -> E,
|
||||||
{
|
{
|
||||||
type Item = A::Service;
|
type Item = A::Service;
|
||||||
|
@ -14,10 +14,10 @@ pub struct Then<A, B> {
|
|||||||
|
|
||||||
impl<A, B> Then<A, B> {
|
impl<A, B> Then<A, B> {
|
||||||
/// Create new `Then` combinator
|
/// Create new `Then` combinator
|
||||||
pub fn new<Request>(a: A, b: B) -> Then<A, B>
|
pub fn new(a: A, b: B) -> Then<A, B>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
B: Service<Result<A::Response, A::Error>, Error = A::Error>,
|
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
|
||||||
{
|
{
|
||||||
Then { a, b: Cell::new(b) }
|
Then { a, b: Cell::new(b) }
|
||||||
}
|
}
|
||||||
@ -35,39 +35,40 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> Service<Request> for Then<A, B>
|
impl<A, B> Service for Then<A, B>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
B: Service<Result<A::Response, A::Error>, Error = A::Error>,
|
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = B::Response;
|
type Response = B::Response;
|
||||||
type Error = B::Error;
|
type Error = B::Error;
|
||||||
type Future = ThenFuture<A, B, Request>;
|
type Future = ThenFuture<A, B>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
try_ready!(self.a.poll_ready());
|
try_ready!(self.a.poll_ready());
|
||||||
self.b.get_mut().poll_ready()
|
self.b.get_mut().poll_ready()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Request) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
ThenFuture::new(self.a.call(req), self.b.clone())
|
ThenFuture::new(self.a.call(req), self.b.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ThenFuture<A, B, Request>
|
pub struct ThenFuture<A, B>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
B: Service<Result<A::Response, A::Error>>,
|
B: Service<Request = Result<A::Response, A::Error>>,
|
||||||
{
|
{
|
||||||
b: Cell<B>,
|
b: Cell<B>,
|
||||||
fut_b: Option<B::Future>,
|
fut_b: Option<B::Future>,
|
||||||
fut_a: Option<A::Future>,
|
fut_a: Option<A::Future>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> ThenFuture<A, B, Request>
|
impl<A, B> ThenFuture<A, B>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
B: Service<Result<A::Response, A::Error>>,
|
B: Service<Request = Result<A::Response, A::Error>>,
|
||||||
{
|
{
|
||||||
fn new(a: A::Future, b: Cell<B>) -> Self {
|
fn new(a: A::Future, b: Cell<B>) -> Self {
|
||||||
ThenFuture {
|
ThenFuture {
|
||||||
@ -78,10 +79,10 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> Future for ThenFuture<A, B, Request>
|
impl<A, B> Future for ThenFuture<A, B>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
B: Service<Result<A::Response, A::Error>>,
|
B: Service<Request = Result<A::Response, A::Error>>,
|
||||||
{
|
{
|
||||||
type Item = B::Response;
|
type Item = B::Response;
|
||||||
type Error = B::Error;
|
type Error = B::Error;
|
||||||
@ -115,15 +116,15 @@ pub struct ThenNewService<A, B> {
|
|||||||
|
|
||||||
impl<A, B> ThenNewService<A, B> {
|
impl<A, B> ThenNewService<A, B> {
|
||||||
/// Create new `AndThen` combinator
|
/// Create new `AndThen` combinator
|
||||||
pub fn new<F, Request>(a: A, f: F) -> Self
|
pub fn new<F>(a: A, f: F) -> Self
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<
|
B: NewService<
|
||||||
Result<A::Response, A::Error>,
|
Request = Result<A::Response, A::Error>,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
InitError = A::InitError,
|
InitError = A::InitError,
|
||||||
>,
|
>,
|
||||||
F: IntoNewService<B, Result<A::Response, A::Error>>,
|
F: IntoNewService<B>,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
a,
|
a,
|
||||||
@ -132,17 +133,22 @@ impl<A, B> ThenNewService<A, B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> NewService<Request> for ThenNewService<A, B>
|
impl<A, B> NewService for ThenNewService<A, B>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
|
B: NewService<
|
||||||
|
Request = Result<A::Response, A::Error>,
|
||||||
|
Error = A::Error,
|
||||||
|
InitError = A::InitError,
|
||||||
|
>,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = B::Response;
|
type Response = B::Response;
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type Service = Then<A::Service, B::Service>;
|
type Service = Then<A::Service, B::Service>;
|
||||||
|
|
||||||
type InitError = A::InitError;
|
type InitError = A::InitError;
|
||||||
type Future = ThenNewServiceFuture<A, B, Request>;
|
type Future = ThenNewServiceFuture<A, B>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
ThenNewServiceFuture::new(self.a.new_service(), self.b.new_service())
|
ThenNewServiceFuture::new(self.a.new_service(), self.b.new_service())
|
||||||
@ -162,10 +168,14 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ThenNewServiceFuture<A, B, Request>
|
pub struct ThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
|
B: NewService<
|
||||||
|
Request = Result<A::Response, A::Error>,
|
||||||
|
Error = A::Error,
|
||||||
|
InitError = A::InitError,
|
||||||
|
>,
|
||||||
{
|
{
|
||||||
fut_b: B::Future,
|
fut_b: B::Future,
|
||||||
fut_a: A::Future,
|
fut_a: A::Future,
|
||||||
@ -173,10 +183,14 @@ where
|
|||||||
b: Option<B::Service>,
|
b: Option<B::Service>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> ThenNewServiceFuture<A, B, Request>
|
impl<A, B> ThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
|
B: NewService<
|
||||||
|
Request = Result<A::Response, A::Error>,
|
||||||
|
Error = A::Error,
|
||||||
|
InitError = A::InitError,
|
||||||
|
>,
|
||||||
{
|
{
|
||||||
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
||||||
ThenNewServiceFuture {
|
ThenNewServiceFuture {
|
||||||
@ -188,10 +202,14 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> Future for ThenNewServiceFuture<A, B, Request>
|
impl<A, B> Future for ThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
|
B: NewService<
|
||||||
|
Request = Result<A::Response, A::Error>,
|
||||||
|
Error = A::Error,
|
||||||
|
InitError = A::InitError,
|
||||||
|
>,
|
||||||
{
|
{
|
||||||
type Item = Then<A::Service, B::Service>;
|
type Item = Then<A::Service, B::Service>;
|
||||||
type Error = A::InitError;
|
type Error = A::InitError;
|
||||||
@ -231,7 +249,8 @@ mod tests {
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Srv1(Rc<Cell<usize>>);
|
struct Srv1(Rc<Cell<usize>>);
|
||||||
impl Service<Result<&'static str, &'static str>> for Srv1 {
|
impl Service for Srv1 {
|
||||||
|
type Request = Result<&'static str, &'static str>;
|
||||||
type Response = &'static str;
|
type Response = &'static str;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<Self::Response, Self::Error>;
|
type Future = FutureResult<Self::Response, Self::Error>;
|
||||||
@ -251,7 +270,8 @@ mod tests {
|
|||||||
|
|
||||||
struct Srv2(Rc<Cell<usize>>);
|
struct Srv2(Rc<Cell<usize>>);
|
||||||
|
|
||||||
impl Service<Result<&'static str, ()>> for Srv2 {
|
impl Service for Srv2 {
|
||||||
|
type Request = Result<&'static str, ()>;
|
||||||
type Response = (&'static str, &'static str);
|
type Response = (&'static str, &'static str);
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<Self::Response, ()>;
|
type Future = FutureResult<Self::Response, ()>;
|
||||||
|
@ -34,7 +34,8 @@ rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "0.1.0"
|
actix-rt = "0.1.0"
|
||||||
actix-server = "0.1.0"
|
#actix-server = "0.1.0"
|
||||||
|
actix-server = { path="../actix-server" }
|
||||||
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-utils"
|
name = "actix-utils"
|
||||||
version = "0.1.1"
|
version = "0.2.0"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix utils - various actix net related services"
|
description = "Actix utils - various actix net related services"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -18,7 +18,8 @@ name = "actix_utils"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "0.1.6"
|
#actix-service = "0.1.6"
|
||||||
|
actix-service = { path="../actix-service" }
|
||||||
actix-codec = "0.1.0"
|
actix-codec = "0.1.0"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
|
@ -13,9 +13,9 @@ pub struct CloneableService<T: 'static> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static> CloneableService<T> {
|
impl<T: 'static> CloneableService<T> {
|
||||||
pub fn new<Request>(service: T) -> Self
|
pub fn new(service: T) -> Self
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
service: Cell::new(service),
|
service: Cell::new(service),
|
||||||
@ -33,10 +33,11 @@ impl<T: 'static> Clone for CloneableService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static, Request> Service<Request> for CloneableService<T>
|
impl<T> Service for CloneableService<T>
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service + 'static,
|
||||||
{
|
{
|
||||||
|
type Request = T::Request;
|
||||||
type Response = T::Response;
|
type Response = T::Response;
|
||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
type Future = T::Future;
|
type Future = T::Future;
|
||||||
@ -45,7 +46,7 @@ where
|
|||||||
self.service.get_mut().poll_ready()
|
self.service.get_mut().poll_ready()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Request) -> Self::Future {
|
fn call(&mut self, req: T::Request) -> Self::Future {
|
||||||
self.service.get_mut().call(req)
|
self.service.get_mut().call(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,11 +21,12 @@ impl<A: Clone, B: Clone> Clone for EitherService<A, B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> Service<Request> for EitherService<A, B>
|
impl<A, B> Service for EitherService<A, B>
|
||||||
where
|
where
|
||||||
A: Service<Request>,
|
A: Service,
|
||||||
B: Service<Request, Response = A::Response, Error = A::Error>,
|
B: Service<Request = A::Request, Response = A::Response, Error = A::Error>,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = A::Response;
|
type Response = A::Response;
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type Future = future::Either<A::Future, B::Future>;
|
type Future = future::Either<A::Future, B::Future>;
|
||||||
@ -37,7 +38,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Request) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
match self {
|
match self {
|
||||||
EitherService::A(ref mut inner) => future::Either::A(inner.call(req)),
|
EitherService::A(ref mut inner) => future::Either::A(inner.call(req)),
|
||||||
EitherService::B(ref mut inner) => future::Either::B(inner.call(req)),
|
EitherService::B(ref mut inner) => future::Either::B(inner.call(req)),
|
||||||
@ -52,11 +53,11 @@ pub enum Either<A, B> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> Either<A, B> {
|
impl<A, B> Either<A, B> {
|
||||||
pub fn new_a<Request>(srv: A) -> Self
|
pub fn new_a(srv: A) -> Self
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<
|
B: NewService<
|
||||||
Request,
|
Request = A::Request,
|
||||||
Response = A::Response,
|
Response = A::Response,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
InitError = A::InitError,
|
InitError = A::InitError,
|
||||||
@ -65,11 +66,11 @@ impl<A, B> Either<A, B> {
|
|||||||
Either::A(srv)
|
Either::A(srv)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_b<Request>(srv: B) -> Self
|
pub fn new_b(srv: B) -> Self
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<
|
B: NewService<
|
||||||
Request,
|
Request = A::Request,
|
||||||
Response = A::Response,
|
Response = A::Response,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
InitError = A::InitError,
|
InitError = A::InitError,
|
||||||
@ -79,16 +80,22 @@ impl<A, B> Either<A, B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> NewService<Request> for Either<A, B>
|
impl<A, B> NewService for Either<A, B>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<Request, Response = A::Response, Error = A::Error, InitError = A::InitError>,
|
B: NewService<
|
||||||
|
Request = A::Request,
|
||||||
|
Response = A::Response,
|
||||||
|
Error = A::Error,
|
||||||
|
InitError = A::InitError,
|
||||||
|
>,
|
||||||
{
|
{
|
||||||
|
type Request = A::Request;
|
||||||
type Response = A::Response;
|
type Response = A::Response;
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type InitError = A::InitError;
|
type InitError = A::InitError;
|
||||||
type Service = EitherService<A::Service, B::Service>;
|
type Service = EitherService<A::Service, B::Service>;
|
||||||
type Future = EitherNewService<A, B, Request>;
|
type Future = EitherNewService<A, B>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
match self {
|
match self {
|
||||||
@ -108,15 +115,20 @@ impl<A: Clone, B: Clone> Clone for Either<A, B> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub enum EitherNewService<A: NewService<R>, B: NewService<R>, R> {
|
pub enum EitherNewService<A: NewService, B: NewService> {
|
||||||
A(A::Future),
|
A(A::Future),
|
||||||
B(B::Future),
|
B(B::Future),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, Request> Future for EitherNewService<A, B, Request>
|
impl<A, B> Future for EitherNewService<A, B>
|
||||||
where
|
where
|
||||||
A: NewService<Request>,
|
A: NewService,
|
||||||
B: NewService<Request, Response = A::Response, Error = A::Error, InitError = A::InitError>,
|
B: NewService<
|
||||||
|
Request = A::Request,
|
||||||
|
Response = A::Response,
|
||||||
|
Error = A::Error,
|
||||||
|
InitError = A::InitError,
|
||||||
|
>,
|
||||||
{
|
{
|
||||||
type Item = EitherService<A::Service, B::Service>;
|
type Item = EitherService<A::Service, B::Service>;
|
||||||
type Error = A::InitError;
|
type Error = A::InitError;
|
||||||
|
@ -22,15 +22,15 @@ pub struct FramedNewService<S, T, U> {
|
|||||||
|
|
||||||
impl<S, T, U> FramedNewService<S, T, U>
|
impl<S, T, U> FramedNewService<S, T, U>
|
||||||
where
|
where
|
||||||
S: NewService<Request<U>, Response = Response<U>>,
|
S: NewService<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
<S::Service as Service<Request<U>>>::Future: 'static,
|
<S::Service as Service>::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
pub fn new<F1: IntoNewService<S, Request<U>>>(factory: F1) -> Self {
|
pub fn new<F1: IntoNewService<S>>(factory: F1) -> Self {
|
||||||
Self {
|
Self {
|
||||||
factory: factory.into_new_service(),
|
factory: factory.into_new_service(),
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
@ -50,16 +50,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, T, U> NewService<Framed<T, U>> for FramedNewService<S, T, U>
|
impl<S, T, U> NewService for FramedNewService<S, T, U>
|
||||||
where
|
where
|
||||||
S: NewService<Request<U>, Response = Response<U>> + Clone,
|
S: NewService<Request = Request<U>, Response = Response<U>> + Clone,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
<S::Service as Service<Request<U>>>::Future: 'static,
|
<S::Service as Service>::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
|
type Request = Framed<T, U>;
|
||||||
type Response = FramedTransport<S::Service, T, U>;
|
type Response = FramedTransport<S::Service, T, U>;
|
||||||
type Error = S::InitError;
|
type Error = S::InitError;
|
||||||
type InitError = S::InitError;
|
type InitError = S::InitError;
|
||||||
@ -91,16 +92,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, T, U> Service<Framed<T, U>> for FramedService<S, T, U>
|
impl<S, T, U> Service for FramedService<S, T, U>
|
||||||
where
|
where
|
||||||
S: NewService<Request<U>, Response = Response<U>>,
|
S: NewService<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
<S::Service as Service<Request<U>>>::Future: 'static,
|
<S::Service as Service>::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
|
type Request = Framed<T, U>;
|
||||||
type Response = FramedTransport<S::Service, T, U>;
|
type Response = FramedTransport<S::Service, T, U>;
|
||||||
type Error = S::InitError;
|
type Error = S::InitError;
|
||||||
type Future = FramedServiceResponseFuture<S, T, U>;
|
type Future = FramedServiceResponseFuture<S, T, U>;
|
||||||
@ -121,9 +123,9 @@ where
|
|||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct FramedServiceResponseFuture<S, T, U>
|
pub struct FramedServiceResponseFuture<S, T, U>
|
||||||
where
|
where
|
||||||
S: NewService<Request<U>, Response = Response<U>>,
|
S: NewService<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
<S::Service as Service<Request<U>>>::Future: 'static,
|
<S::Service as Service>::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
@ -135,9 +137,9 @@ where
|
|||||||
|
|
||||||
impl<S, T, U> Future for FramedServiceResponseFuture<S, T, U>
|
impl<S, T, U> Future for FramedServiceResponseFuture<S, T, U>
|
||||||
where
|
where
|
||||||
S: NewService<Request<U>, Response = Response<U>>,
|
S: NewService<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
<S::Service as Service<Request<U>>>::Future: 'static,
|
<S::Service as Service>::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
@ -174,7 +176,7 @@ impl<E, U: Encoder + Decoder> From<E> for FramedTransportError<E, U> {
|
|||||||
/// and pass then to the service.
|
/// and pass then to the service.
|
||||||
pub struct FramedTransport<S, T, U>
|
pub struct FramedTransport<S, T, U>
|
||||||
where
|
where
|
||||||
S: Service<Request<U>, Response = Response<U>>,
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
@ -188,7 +190,7 @@ where
|
|||||||
inner: Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>,
|
inner: Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum TransportState<S: Service<Request<U>>, U: Encoder + Decoder> {
|
enum TransportState<S: Service, U: Encoder + Decoder> {
|
||||||
Processing,
|
Processing,
|
||||||
Error(FramedTransportError<S::Error, U>),
|
Error(FramedTransportError<S::Error, U>),
|
||||||
FramedError(FramedTransportError<S::Error, U>),
|
FramedError(FramedTransportError<S::Error, U>),
|
||||||
@ -202,7 +204,7 @@ struct FramedTransportInner<I, E> {
|
|||||||
|
|
||||||
impl<S, T, U> FramedTransport<S, T, U>
|
impl<S, T, U> FramedTransport<S, T, U>
|
||||||
where
|
where
|
||||||
S: Service<Request<U>, Response = Response<U>>,
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
@ -294,7 +296,7 @@ where
|
|||||||
|
|
||||||
impl<S, T, U> FramedTransport<S, T, U>
|
impl<S, T, U> FramedTransport<S, T, U>
|
||||||
where
|
where
|
||||||
S: Service<Request<U>, Response = Response<U>>,
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
@ -302,7 +304,7 @@ where
|
|||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
pub fn new<F: IntoService<S, Request<U>>>(framed: Framed<T, U>, service: F) -> Self {
|
pub fn new<F: IntoService<S>>(framed: Framed<T, U>, service: F) -> Self {
|
||||||
FramedTransport {
|
FramedTransport {
|
||||||
framed,
|
framed,
|
||||||
service: service.into_service(),
|
service: service.into_service(),
|
||||||
@ -340,7 +342,7 @@ where
|
|||||||
|
|
||||||
impl<S, T, U> Future for FramedTransport<S, T, U>
|
impl<S, T, U> Future for FramedTransport<S, T, U>
|
||||||
where
|
where
|
||||||
S: Service<Request<U>, Response = Response<U>>,
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
@ -400,12 +402,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U, F> NewService<T> for IntoFramed<T, U, F>
|
impl<T, U, F> NewService for IntoFramed<T, U, F>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
F: Fn() -> U + Send + Clone + 'static,
|
F: Fn() -> U + Send + Clone + 'static,
|
||||||
U: Encoder + Decoder,
|
U: Encoder + Decoder,
|
||||||
{
|
{
|
||||||
|
type Request = T;
|
||||||
type Response = Framed<T, U>;
|
type Response = Framed<T, U>;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
@ -430,12 +433,13 @@ where
|
|||||||
_t: PhantomData<(T,)>,
|
_t: PhantomData<(T,)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U, F> Service<T> for IntoFramedService<T, U, F>
|
impl<T, U, F> Service for IntoFramedService<T, U, F>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
F: Fn() -> U + Send + Clone + 'static,
|
F: Fn() -> U + Send + Clone + 'static,
|
||||||
U: Encoder + Decoder,
|
U: Encoder + Decoder,
|
||||||
{
|
{
|
||||||
|
type Request = T;
|
||||||
type Response = Framed<T, U>;
|
type Response = Framed<T, U>;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<Self::Response, Self::Error>;
|
type Future = FutureResult<Self::Response, Self::Error>;
|
||||||
|
@ -13,10 +13,10 @@ pub struct InFlight<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> InFlight<T> {
|
impl<T> InFlight<T> {
|
||||||
pub fn new<F, Request>(factory: F) -> Self
|
pub fn new<F>(factory: F) -> Self
|
||||||
where
|
where
|
||||||
T: NewService<Request>,
|
T: NewService,
|
||||||
F: IntoNewService<T, Request>,
|
F: IntoNewService<T>,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
factory: factory.into_new_service(),
|
factory: factory.into_new_service(),
|
||||||
@ -33,15 +33,16 @@ impl<T> InFlight<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, Request> NewService<Request> for InFlight<T>
|
impl<T> NewService for InFlight<T>
|
||||||
where
|
where
|
||||||
T: NewService<Request>,
|
T: NewService,
|
||||||
{
|
{
|
||||||
|
type Request = T::Request;
|
||||||
type Response = T::Response;
|
type Response = T::Response;
|
||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
type InitError = T::InitError;
|
type InitError = T::InitError;
|
||||||
type Service = InFlightService<T::Service>;
|
type Service = InFlightService<T::Service>;
|
||||||
type Future = InFlightResponseFuture<T, Request>;
|
type Future = InFlightResponseFuture<T>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
InFlightResponseFuture {
|
InFlightResponseFuture {
|
||||||
@ -51,12 +52,12 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct InFlightResponseFuture<T: NewService<Request>, Request> {
|
pub struct InFlightResponseFuture<T: NewService> {
|
||||||
fut: T::Future,
|
fut: T::Future,
|
||||||
max_inflight: usize,
|
max_inflight: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: NewService<Request>, Request> Future for InFlightResponseFuture<T, Request> {
|
impl<T: NewService> Future for InFlightResponseFuture<T> {
|
||||||
type Item = InFlightService<T::Service>;
|
type Item = InFlightService<T::Service>;
|
||||||
type Error = T::InitError;
|
type Error = T::InitError;
|
||||||
|
|
||||||
@ -74,10 +75,10 @@ pub struct InFlightService<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> InFlightService<T> {
|
impl<T> InFlightService<T> {
|
||||||
pub fn new<F, Request>(service: F) -> Self
|
pub fn new<F>(service: F) -> Self
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
F: IntoService<T, Request>,
|
F: IntoService<T>,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
service: service.into_service(),
|
service: service.into_service(),
|
||||||
@ -85,10 +86,10 @@ impl<T> InFlightService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_max_inflight<F, Request>(max: usize, service: F) -> Self
|
pub fn with_max_inflight<F>(max: usize, service: F) -> Self
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
F: IntoService<T, Request>,
|
F: IntoService<T>,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
service: service.into_service(),
|
service: service.into_service(),
|
||||||
@ -97,13 +98,14 @@ impl<T> InFlightService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, Request> Service<Request> for InFlightService<T>
|
impl<T> Service for InFlightService<T>
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
{
|
{
|
||||||
|
type Request = T::Request;
|
||||||
type Response = T::Response;
|
type Response = T::Response;
|
||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
type Future = InFlightServiceResponse<T, Request>;
|
type Future = InFlightServiceResponse<T>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
let res = self.service.poll_ready()?;
|
let res = self.service.poll_ready()?;
|
||||||
@ -114,7 +116,7 @@ where
|
|||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Request) -> Self::Future {
|
fn call(&mut self, req: T::Request) -> Self::Future {
|
||||||
InFlightServiceResponse {
|
InFlightServiceResponse {
|
||||||
fut: self.service.call(req),
|
fut: self.service.call(req),
|
||||||
_guard: self.count.get(),
|
_guard: self.count.get(),
|
||||||
@ -123,12 +125,12 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct InFlightServiceResponse<T: Service<Request>, Request> {
|
pub struct InFlightServiceResponse<T: Service> {
|
||||||
fut: T::Future,
|
fut: T::Future,
|
||||||
_guard: CounterGuard,
|
_guard: CounterGuard,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Service<Request>, Request> Future for InFlightServiceResponse<T, Request> {
|
impl<T: Service> Future for InFlightServiceResponse<T> {
|
||||||
type Item = T::Response;
|
type Item = T::Response;
|
||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
|
|
||||||
|
@ -44,10 +44,11 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R, E, F> NewService<R> for KeepAlive<R, E, F>
|
impl<R, E, F> NewService for KeepAlive<R, E, F>
|
||||||
where
|
where
|
||||||
F: Fn() -> E + Clone,
|
F: Fn() -> E + Clone,
|
||||||
{
|
{
|
||||||
|
type Request = R;
|
||||||
type Response = R;
|
type Response = R;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type InitError = Never;
|
type InitError = Never;
|
||||||
@ -89,10 +90,11 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R, E, F> Service<R> for KeepAliveService<R, E, F>
|
impl<R, E, F> Service for KeepAliveService<R, E, F>
|
||||||
where
|
where
|
||||||
F: Fn() -> E,
|
F: Fn() -> E,
|
||||||
{
|
{
|
||||||
|
type Request = R;
|
||||||
type Response = R;
|
type Response = R;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Future = FutureResult<R, E>;
|
type Future = FutureResult<R, E>;
|
||||||
|
@ -37,12 +37,12 @@ pub struct StreamNewService<S, T, E> {
|
|||||||
impl<S, T, E> StreamNewService<S, T, E>
|
impl<S, T, E> StreamNewService<S, T, E>
|
||||||
where
|
where
|
||||||
S: IntoStream,
|
S: IntoStream,
|
||||||
T: NewService<Request<S>, Response = (), Error = E, InitError = E>,
|
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Service: 'static,
|
T::Service: 'static,
|
||||||
<T::Service as Service<Request<S>>>::Future: 'static,
|
<T::Service as Service>::Future: 'static,
|
||||||
{
|
{
|
||||||
pub fn new<F: IntoNewService<T, Request<S>>>(factory: F) -> Self {
|
pub fn new<F: IntoNewService<T>>(factory: F) -> Self {
|
||||||
Self {
|
Self {
|
||||||
factory: Rc::new(factory.into_new_service()),
|
factory: Rc::new(factory.into_new_service()),
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
@ -59,14 +59,15 @@ impl<S, T, E> Clone for StreamNewService<S, T, E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, T, E> NewService<S> for StreamNewService<S, T, E>
|
impl<S, T, E> NewService for StreamNewService<S, T, E>
|
||||||
where
|
where
|
||||||
S: IntoStream + 'static,
|
S: IntoStream + 'static,
|
||||||
T: NewService<Request<S>, Response = (), Error = E, InitError = E>,
|
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Service: 'static,
|
T::Service: 'static,
|
||||||
<T::Service as Service<Request<S>>>::Future: 'static,
|
<T::Service as Service>::Future: 'static,
|
||||||
{
|
{
|
||||||
|
type Request = S;
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type InitError = E;
|
type InitError = E;
|
||||||
@ -86,14 +87,15 @@ pub struct StreamService<S, T, E> {
|
|||||||
_t: PhantomData<(S, E)>,
|
_t: PhantomData<(S, E)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, T, E> Service<S> for StreamService<S, T, E>
|
impl<S, T, E> Service for StreamService<S, T, E>
|
||||||
where
|
where
|
||||||
S: IntoStream + 'static,
|
S: IntoStream + 'static,
|
||||||
T: NewService<Request<S>, Response = (), Error = E, InitError = E>,
|
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Service: 'static,
|
T::Service: 'static,
|
||||||
<T::Service as Service<Request<S>>>::Future: 'static,
|
<T::Service as Service>::Future: 'static,
|
||||||
{
|
{
|
||||||
|
type Request = S;
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Future = Box<Future<Item = (), Error = E>>;
|
type Future = Box<Future<Item = (), Error = E>>;
|
||||||
@ -114,7 +116,7 @@ where
|
|||||||
pub struct StreamDispatcher<S, T>
|
pub struct StreamDispatcher<S, T>
|
||||||
where
|
where
|
||||||
S: IntoStream + 'static,
|
S: IntoStream + 'static,
|
||||||
T: Service<Request<S>, Response = ()> + 'static,
|
T: Service<Request = Request<S>, Response = ()> + 'static,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
{
|
{
|
||||||
stream: S,
|
stream: S,
|
||||||
@ -126,13 +128,13 @@ where
|
|||||||
impl<S, T> StreamDispatcher<S, T>
|
impl<S, T> StreamDispatcher<S, T>
|
||||||
where
|
where
|
||||||
S: Stream,
|
S: Stream,
|
||||||
T: Service<Request<S>, Response = ()>,
|
T: Service<Request = Request<S>, Response = ()>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
{
|
{
|
||||||
pub fn new<F1, F2>(stream: F1, service: F2) -> Self
|
pub fn new<F1, F2>(stream: F1, service: F2) -> Self
|
||||||
where
|
where
|
||||||
F1: IntoStream<Stream = S, Item = S::Item, Error = S::Error>,
|
F1: IntoStream<Stream = S, Item = S::Item, Error = S::Error>,
|
||||||
F2: IntoService<T, Request<S>>,
|
F2: IntoService<T>,
|
||||||
{
|
{
|
||||||
let (err_tx, err_rx) = mpsc::unbounded();
|
let (err_tx, err_rx) = mpsc::unbounded();
|
||||||
StreamDispatcher {
|
StreamDispatcher {
|
||||||
@ -147,7 +149,7 @@ where
|
|||||||
impl<S, T> Future for StreamDispatcher<S, T>
|
impl<S, T> Future for StreamDispatcher<S, T>
|
||||||
where
|
where
|
||||||
S: Stream,
|
S: Stream,
|
||||||
T: Service<Request<S>, Response = ()>,
|
T: Service<Request = Request<S>, Response = ()>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
{
|
{
|
||||||
type Item = ();
|
type Item = ();
|
||||||
@ -225,7 +227,8 @@ impl<T> Clone for TakeItem<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Stream> NewService<T> for TakeItem<T> {
|
impl<T: Stream> NewService for TakeItem<T> {
|
||||||
|
type Request = T;
|
||||||
type Response = (Option<T::Item>, T);
|
type Response = (Option<T::Item>, T);
|
||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
@ -248,7 +251,8 @@ impl<T> Clone for TakeItemService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Stream> Service<T> for TakeItemService<T> {
|
impl<T: Stream> Service for TakeItemService<T> {
|
||||||
|
type Request = T;
|
||||||
type Response = (Option<T::Item>, T);
|
type Response = (Option<T::Item>, T);
|
||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
type Future = TakeItemServiceResponse<T>;
|
type Future = TakeItemServiceResponse<T>;
|
||||||
|
@ -42,7 +42,8 @@ impl Default for LowResTime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NewService<()> for LowResTime {
|
impl NewService for LowResTime {
|
||||||
|
type Request = ();
|
||||||
type Response = Instant;
|
type Response = Instant;
|
||||||
type Error = Never;
|
type Error = Never;
|
||||||
type InitError = Never;
|
type InitError = Never;
|
||||||
@ -88,7 +89,8 @@ impl LowResTimeService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service<()> for LowResTimeService {
|
impl Service for LowResTimeService {
|
||||||
|
type Request = ();
|
||||||
type Response = Instant;
|
type Response = Instant;
|
||||||
type Error = Never;
|
type Error = Never;
|
||||||
type Future = FutureResult<Self::Response, Self::Error>;
|
type Future = FutureResult<Self::Response, Self::Error>;
|
||||||
|
@ -35,9 +35,9 @@ impl<E: fmt::Debug> fmt::Debug for TimeoutError<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Timeout<T> {
|
impl<T> Timeout<T> {
|
||||||
pub fn new<Request>(timeout: Duration, inner: T) -> Self
|
pub fn new(timeout: Duration, inner: T) -> Self
|
||||||
where
|
where
|
||||||
T: NewService<Request> + Clone,
|
T: NewService + Clone,
|
||||||
{
|
{
|
||||||
Timeout { inner, timeout }
|
Timeout { inner, timeout }
|
||||||
}
|
}
|
||||||
@ -55,15 +55,16 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, Request> NewService<Request> for Timeout<T>
|
impl<T> NewService for Timeout<T>
|
||||||
where
|
where
|
||||||
T: NewService<Request> + Clone,
|
T: NewService + Clone,
|
||||||
{
|
{
|
||||||
|
type Request = T::Request;
|
||||||
type Response = T::Response;
|
type Response = T::Response;
|
||||||
type Error = TimeoutError<T::Error>;
|
type Error = TimeoutError<T::Error>;
|
||||||
type InitError = T::InitError;
|
type InitError = T::InitError;
|
||||||
type Service = TimeoutService<T::Service>;
|
type Service = TimeoutService<T::Service>;
|
||||||
type Future = TimeoutFut<T, Request>;
|
type Future = TimeoutFut<T>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
TimeoutFut {
|
TimeoutFut {
|
||||||
@ -75,14 +76,14 @@ where
|
|||||||
|
|
||||||
/// `Timeout` response future
|
/// `Timeout` response future
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TimeoutFut<T: NewService<Request>, Request> {
|
pub struct TimeoutFut<T: NewService> {
|
||||||
fut: T::Future,
|
fut: T::Future,
|
||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, Request> Future for TimeoutFut<T, Request>
|
impl<T> Future for TimeoutFut<T>
|
||||||
where
|
where
|
||||||
T: NewService<Request>,
|
T: NewService,
|
||||||
{
|
{
|
||||||
type Item = TimeoutService<T::Service>;
|
type Item = TimeoutService<T::Service>;
|
||||||
type Error = T::InitError;
|
type Error = T::InitError;
|
||||||
@ -101,9 +102,9 @@ pub struct TimeoutService<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> TimeoutService<T> {
|
impl<T> TimeoutService<T> {
|
||||||
pub fn new<Request>(timeout: Duration, inner: T) -> Self
|
pub fn new(timeout: Duration, inner: T) -> Self
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
{
|
{
|
||||||
TimeoutService { inner, timeout }
|
TimeoutService { inner, timeout }
|
||||||
}
|
}
|
||||||
@ -118,19 +119,20 @@ impl<T: Clone> Clone for TimeoutService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, Request> Service<Request> for TimeoutService<T>
|
impl<T> Service for TimeoutService<T>
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
{
|
{
|
||||||
|
type Request = T::Request;
|
||||||
type Response = T::Response;
|
type Response = T::Response;
|
||||||
type Error = TimeoutError<T::Error>;
|
type Error = TimeoutError<T::Error>;
|
||||||
type Future = TimeoutServiceResponse<T, Request>;
|
type Future = TimeoutServiceResponse<T>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
self.inner.poll_ready().map_err(TimeoutError::Service)
|
self.inner.poll_ready().map_err(TimeoutError::Service)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, request: Request) -> Self::Future {
|
fn call(&mut self, request: T::Request) -> Self::Future {
|
||||||
TimeoutServiceResponse {
|
TimeoutServiceResponse {
|
||||||
fut: self.inner.call(request),
|
fut: self.inner.call(request),
|
||||||
sleep: Delay::new(clock::now() + self.timeout),
|
sleep: Delay::new(clock::now() + self.timeout),
|
||||||
@ -140,14 +142,14 @@ where
|
|||||||
|
|
||||||
/// `TimeoutService` response future
|
/// `TimeoutService` response future
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TimeoutServiceResponse<T: Service<Request>, Request> {
|
pub struct TimeoutServiceResponse<T: Service> {
|
||||||
fut: T::Future,
|
fut: T::Future,
|
||||||
sleep: Delay,
|
sleep: Delay,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, Request> Future for TimeoutServiceResponse<T, Request>
|
impl<T> Future for TimeoutServiceResponse<T>
|
||||||
where
|
where
|
||||||
T: Service<Request>,
|
T: Service,
|
||||||
{
|
{
|
||||||
type Item = T::Response;
|
type Item = T::Response;
|
||||||
type Error = TimeoutError<T::Error>;
|
type Error = TimeoutError<T::Error>;
|
||||||
|
Loading…
Reference in New Issue
Block a user