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

Compare commits

...

21 Commits

Author SHA1 Message Date
Nikolay Kim
86f57e5a4a prepare actix-service release 2019-03-09 14:10:02 -08:00
Nikolay Kim
43ad18ccb1 remove get_decoded 2019-03-09 13:58:07 -08:00
Nikolay Kim
34995a8ccf revert re-quoting change 2019-03-09 13:56:09 -08:00
Nikolay Kim
0ff300c40f export ApplyConfig 2019-03-09 09:05:51 -08:00
Nikolay Kim
629ef23371 add .apply_cfg new service combinator 2019-03-09 09:02:23 -08:00
Nikolay Kim
d2b96ff877 add ServerConfig to server services 2019-03-09 07:31:22 -08:00
Nikolay Kim
ac62e2dbf9 revert generic request in actix-utils 2019-03-09 07:27:35 -08:00
Nikolay Kim
6bbbdba921 revert generic Request change 2019-03-09 06:36:23 -08:00
Nikolay Kim
2099629fe3 cleanup ssl services 2019-03-08 22:41:30 -08:00
Nikolay Kim
a4d4770462 remove server config 2019-03-08 22:38:39 -08:00
Nikolay Kim
70ead175b9 IntoService for fn_cfg_factory 2019-03-08 20:51:50 -08:00
Nikolay Kim
49867b5e9d fix IntoService 2019-03-08 20:50:29 -08:00
Nikolay Kim
0f064c43e9 remove uneeded code 2019-03-08 20:10:47 -08:00
Nikolay Kim
7db29544f9 add ServerConfig param for server service 2019-03-08 19:43:13 -08:00
Nikolay Kim
4850cf41ff rename module 2019-03-08 16:26:30 -08:00
Nikolay Kim
046142ffbc added is_prefix_match and resource_path 2019-03-08 15:34:40 -08:00
Nikolay Kim
49e6dbcda2 remove unused ResourceMap 2019-03-08 12:33:44 -08:00
Nikolay Kim
877614a494 add params decoding 2019-03-08 04:43:51 -08:00
Nikolay Kim
ac0e8b9e53 re-enable examples 2019-03-06 23:33:35 -08:00
Nikolay Kim
b407c65f4c add FramedParts::with_read_buf method 2019-03-06 22:53:55 -08:00
Nikolay Kim
51bd7d2721 update actix-rt 2019-03-06 10:39:53 -08:00
51 changed files with 1434 additions and 1002 deletions

View File

@@ -1,3 +1,18 @@
[package]
name = "actix-net"
version = "0.3.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix net - framework for the compisible network services for Rust"
readme = "README.md"
keywords = ["network", "framework", "async", "futures"]
homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-net.git"
documentation = "https://docs.rs/actix-net/"
categories = ["network-programming", "asynchronous"]
license = "MIT/Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
edition = "2018"
[workspace]
members = [
"actix-codec",
@@ -5,7 +20,19 @@ members = [
"actix-rt",
"actix-service",
"actix-server",
"actix-server-config",
"actix-test-server",
"actix-utils",
"router",
]
[dev-dependencies]
actix-service = { path="actix-service" }
actix-codec = "0.1.1"
actix-rt = "0.2.0"
actix-server = { path="actix-server", features=["ssl"] }
env_logger = "0.6"
futures = "0.1.25"
openssl = "0.10"
tokio-tcp = "0.1"
tokio-openssl = "0.3"

View File

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

View File

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

View File

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

View File

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

View File

@@ -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 Error = ResolveError;
type Future = ResolverFuture<T>;

View File

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

View File

@@ -0,0 +1,18 @@
[package]
name = "actix-server-config"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix server config utils"
homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-net.git"
license = "MIT/Apache-2.0"
edition = "2018"
workspace = ".."
[lib]
name = "actix_server_config"
path = "src/lib.rs"
[dependencies]
actix-service = { path="../actix-service" }
futures = "0.1.25"

View File

@@ -0,0 +1,33 @@
use std::cell::Cell;
use std::net::SocketAddr;
use std::rc::Rc;
#[derive(Debug, Clone)]
pub struct ServerConfig {
addr: SocketAddr,
secure: Rc<Cell<bool>>,
}
impl ServerConfig {
pub fn new(addr: SocketAddr) -> Self {
ServerConfig {
addr,
secure: Rc::new(Cell::new(false)),
}
}
/// Returns the address of the local half of this TCP server socket
pub fn local_addr(&self) -> SocketAddr {
self.addr
}
/// Returns true if connection is secure (tls enabled)
pub fn secure(&self) -> bool {
self.secure.as_ref().get()
}
/// Set secure flag
pub fn set_secure(&self) {
self.secure.as_ref().set(true)
}
}

View File

@@ -33,9 +33,10 @@ ssl = ["openssl", "tokio-openssl"]
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
[dependencies]
actix-rt = "0.2.0"
#actix-service = "0.3.2"
actix-service = { path="../actix-service" }
actix-rt = "0.1.0"
actix-server-config = { path="../actix-server-config" }
log = "0.4"
num_cpus = "1.0"

View File

@@ -12,8 +12,8 @@ use num_cpus;
use tokio_timer::sleep;
use crate::accept::{AcceptLoop, AcceptNotify, Command};
use crate::config::{ConfiguredService, ServiceConfig};
use crate::server::{Server, ServerCommand};
use crate::service_config::{ConfiguredService, ServiceConfig};
use crate::services::{InternalServiceFactory, ServiceFactory, StreamNewService};
use crate::signals::{Signal, Signals};
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};
@@ -151,14 +151,14 @@ impl ServerBuilder {
{
let sockets = bind_addr(addr)?;
let token = self.token.next();
self.services.push(StreamNewService::create(
name.as_ref().to_string(),
token,
factory,
));
for lst in sockets {
let token = self.token.next();
self.services.push(StreamNewService::create(
name.as_ref().to_string(),
token,
factory.clone(),
lst.local_addr()?,
));
self.sockets.push((token, lst));
}
Ok(self)
@@ -170,7 +170,7 @@ impl ServerBuilder {
name: N,
lst: net::TcpListener,
factory: F,
) -> Self
) -> io::Result<Self>
where
F: ServiceFactory,
{
@@ -179,9 +179,10 @@ impl ServerBuilder {
name.as_ref().to_string(),
token,
factory,
lst.local_addr()?,
));
self.sockets.push((token, lst));
self
Ok(self)
}
/// Spawn new thread and start listening for incoming connections.
@@ -194,19 +195,18 @@ impl ServerBuilder {
/// ```rust,ignore
/// use actix_web::*;
///
/// fn main() {
/// fn main() -> std::io::Result<()> {
/// Server::new().
/// .service(
/// HttpServer::new(|| App::new().resource("/", |r| r.h(|_| HttpResponse::Ok())))
/// HttpServer::new(|| App::new().service(web::service("/").to(|| HttpResponse::Ok())))
/// .bind("127.0.0.1:0")
/// .expect("Can not bind to 127.0.0.1:0"))
/// .run();
/// .run()
/// }
/// ```
pub fn run(self) {
pub fn run(self) -> io::Result<()> {
let sys = System::new("http-server");
self.start();
sys.run();
sys.run()
}
/// Starts processing incoming connections and return server controller.

View File

@@ -2,17 +2,19 @@
mod accept;
mod builder;
mod config;
mod counter;
mod server;
mod service_config;
mod services;
mod signals;
pub mod ssl;
mod worker;
pub use actix_server_config::ServerConfig;
pub use self::builder::ServerBuilder;
pub use self::config::{ServiceConfig, ServiceRuntime};
pub use self::server::Server;
pub use self::service_config::{ServiceConfig, ServiceRuntime};
pub use self::services::ServiceFactory;
#[doc(hidden)]

View File

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

View File

@@ -1,13 +1,14 @@
use std::net;
use std::net::{SocketAddr, TcpStream};
use std::time::Duration;
use actix_rt::spawn;
use actix_server_config::ServerConfig;
use actix_service::{NewService, Service};
use futures::future::{err, ok, FutureResult};
use futures::{Future, Poll};
use log::error;
use tokio_reactor::Handle;
use tokio_tcp::TcpStream;
use tokio_tcp::TcpStream as TokioTcpStream;
use super::Token;
use crate::counter::CounterGuard;
@@ -15,7 +16,7 @@ use crate::counter::CounterGuard;
/// Server message
pub(crate) enum ServerMessage {
/// New stream
Connect(net::TcpStream),
Connect(TcpStream),
/// Gracefull shutdown
Shutdown(Duration),
/// Force shutdown
@@ -23,7 +24,7 @@ pub(crate) enum ServerMessage {
}
pub trait ServiceFactory: Send + Clone + 'static {
type NewService: NewService<TcpStream>;
type NewService: NewService<ServerConfig, Request = TokioTcpStream>;
fn create(&self) -> Self::NewService;
}
@@ -38,7 +39,7 @@ pub(crate) trait InternalServiceFactory: Send {
pub(crate) type BoxedServerService = Box<
Service<
(Option<CounterGuard>, ServerMessage),
Request = (Option<CounterGuard>, ServerMessage),
Response = (),
Error = (),
Future = FutureResult<(), ()>,
@@ -55,12 +56,13 @@ impl<T> StreamService<T> {
}
}
impl<T> Service<(Option<CounterGuard>, ServerMessage)> for StreamService<T>
impl<T> Service for StreamService<T>
where
T: Service<TcpStream>,
T: Service<Request = TokioTcpStream>,
T::Future: 'static,
T::Error: 'static,
{
type Request = (Option<CounterGuard>, ServerMessage);
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;
@@ -72,9 +74,10 @@ where
fn call(&mut self, (guard, req): (Option<CounterGuard>, ServerMessage)) -> Self::Future {
match req {
ServerMessage::Connect(stream) => {
let stream = TcpStream::from_std(stream, &Handle::default()).map_err(|e| {
error!("Can not convert to an async tcp stream: {}", e);
});
let stream =
TokioTcpStream::from_std(stream, &Handle::default()).map_err(|e| {
error!("Can not convert to an async tcp stream: {}", e);
});
if let Ok(stream) = stream {
spawn(self.service.call(stream).then(move |res| {
@@ -95,14 +98,25 @@ pub(crate) struct StreamNewService<F: ServiceFactory> {
name: String,
inner: F,
token: Token,
addr: SocketAddr,
}
impl<F> StreamNewService<F>
where
F: ServiceFactory,
{
pub(crate) fn create(name: String, token: Token, inner: F) -> Box<InternalServiceFactory> {
Box::new(Self { name, token, inner })
pub(crate) fn create(
name: String,
token: Token,
inner: F,
addr: SocketAddr,
) -> Box<InternalServiceFactory> {
Box::new(Self {
name,
token,
inner,
addr,
})
}
}
@@ -119,15 +133,17 @@ where
name: self.name.clone(),
inner: self.inner.clone(),
token: self.token,
addr: self.addr,
})
}
fn create(&self) -> Box<Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>> {
let token = self.token;
let config = ServerConfig::new(self.addr);
Box::new(
self.inner
.create()
.new_service(&())
.new_service(&config)
.map_err(|_| ())
.map(move |inner| {
let service: BoxedServerService = Box::new(StreamService::new(inner));
@@ -154,7 +170,7 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
impl<F, T> ServiceFactory for F
where
F: Fn() -> T + Send + Clone + 'static,
T: NewService<TcpStream>,
T: NewService<ServerConfig, Request = TokioTcpStream>,
{
type NewService = T;

View File

@@ -1,13 +1,14 @@
use std::io;
use std::marker::PhantomData;
use actix_server_config::ServerConfig;
use actix_service::{NewService, Service};
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
use native_tls::{self, Error, HandshakeError, TlsAcceptor};
use tokio_io::{AsyncRead, AsyncWrite};
use super::MAX_CONN_COUNTER;
use crate::counter::{Counter, CounterGuard};
use crate::ssl::MAX_CONN_COUNTER;
/// Support `SSL` connections via native-tls package
///
@@ -36,14 +37,17 @@ impl<T: AsyncRead + AsyncWrite> Clone for NativeTlsAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> NewService<T> for NativeTlsAcceptor<T> {
impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for NativeTlsAcceptor<T> {
type Request = T;
type Response = TlsStream<T>;
type Error = Error;
type Service = NativeTlsAcceptorService<T>;
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &()) -> Self::Future {
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
cfg.set_secure();
MAX_CONN_COUNTER.with(|conns| {
ok(NativeTlsAcceptorService {
acceptor: self.acceptor.clone(),
@@ -60,7 +64,8 @@ pub struct NativeTlsAcceptorService<T> {
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 Error = Error;
type Future = Accept<T>;

View File

@@ -6,8 +6,9 @@ use openssl::ssl::{HandshakeError, SslAcceptor};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_openssl::{AcceptAsync, SslAcceptorExt, SslStream};
use super::MAX_CONN_COUNTER;
use crate::counter::{Counter, CounterGuard};
use crate::ssl::MAX_CONN_COUNTER;
use crate::ServerConfig;
/// Support `SSL` connections via openssl package
///
@@ -36,14 +37,17 @@ impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> NewService<T> for OpensslAcceptor<T> {
impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for OpensslAcceptor<T> {
type Request = T;
type Response = SslStream<T>;
type Error = HandshakeError<T>;
type Service = OpensslAcceptorService<T>;
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &()) -> Self::Future {
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
cfg.set_secure();
MAX_CONN_COUNTER.with(|conns| {
ok(OpensslAcceptorService {
acceptor: self.acceptor.clone(),
@@ -60,7 +64,8 @@ pub struct OpensslAcceptorService<T> {
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 Error = HandshakeError<T>;
type Future = OpensslAcceptorServiceFut<T>;

View File

@@ -8,8 +8,9 @@ use rustls::{ServerConfig, ServerSession};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_rustls::{Accept, TlsAcceptor, TlsStream};
use super::MAX_CONN_COUNTER;
use crate::counter::{Counter, CounterGuard};
use crate::ssl::MAX_CONN_COUNTER;
use crate::ServerConfig as SrvConfig;
/// Support `SSL` connections via rustls package
///
@@ -38,14 +39,17 @@ impl<T> Clone for RustlsAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> NewService<T> for RustlsAcceptor<T> {
impl<T: AsyncRead + AsyncWrite> NewService<SrvConfig> for RustlsAcceptor<T> {
type Request = T;
type Response = TlsStream<T, ServerSession>;
type Error = io::Error;
type Service = RustlsAcceptorService<T>;
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &()) -> Self::Future {
fn new_service(&self, cfg: &SrvConfig) -> Self::Future {
cfg.set_secure();
MAX_CONN_COUNTER.with(|conns| {
ok(RustlsAcceptorService {
acceptor: self.config.clone().into(),
@@ -62,7 +66,8 @@ pub struct RustlsAcceptorService<T> {
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 Error = io::Error;
type Future = RustlsAcceptorServiceFut<T>;

View File

@@ -0,0 +1,70 @@
use std::{net, thread, time};
use actix_server::{Server, ServerConfig};
use actix_service::{fn_cfg_factory, fn_service, IntoService};
use net2::TcpBuilder;
fn unused_addr() -> net::SocketAddr {
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
let socket = TcpBuilder::new_v4().unwrap();
socket.bind(&addr).unwrap();
socket.reuse_address(true).unwrap();
let tcp = socket.to_tcp_listener().unwrap();
tcp.local_addr().unwrap()
}
#[test]
fn test_bind() {
let addr = unused_addr();
thread::spawn(move || {
Server::build()
.bind("test", addr, move || {
fn_cfg_factory(move |cfg: &ServerConfig| {
assert_eq!(cfg.local_addr(), addr);
Ok::<_, ()>((|_| Ok::<_, ()>(())).into_service())
})
})
.unwrap()
.run()
});
thread::sleep(time::Duration::from_millis(500));
assert!(net::TcpStream::connect(addr).is_ok());
}
#[test]
fn test_bind_no_config() {
let addr = unused_addr();
thread::spawn(move || {
Server::build()
.bind("test", addr, move || fn_service(|_| Ok::<_, ()>(())))
.unwrap()
.run()
});
thread::sleep(time::Duration::from_millis(500));
assert!(net::TcpStream::connect(addr).is_ok());
}
#[test]
fn test_listen() {
let addr = unused_addr();
thread::spawn(move || {
let lst = net::TcpListener::bind(addr).unwrap();
Server::build()
.listen("test", lst, move || {
fn_cfg_factory(move |cfg: &ServerConfig| {
assert_eq!(cfg.local_addr(), addr);
Ok::<_, ()>((|_| Ok::<_, ()>(())).into_service())
})
})
.unwrap()
.run()
});
thread::sleep(time::Duration::from_millis(500));
assert!(net::TcpStream::connect(addr).is_ok());
}

View File

@@ -1,11 +1,14 @@
# Changes
## [0.3.2] - 2019-03-xx
## [0.3.3] - 2019-03-09
### Added
* Add `ApplyTransform` new service for transform and new service.
* Add `NewService::apply_cfg()` combinator, allows to use
nested `NewService` with different config parameter.
### Changed
* Revert IntoFuture change

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-service"
version = "0.3.2"
version = "0.3.3"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix Service"
keywords = ["network", "framework", "async", "futures"]

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,163 @@
use std::marker::PhantomData;
use futures::{Async, Future, Poll};
use crate::and_then::AndThen;
use crate::{IntoNewService, NewService};
/// `ApplyNewService` new service combinator
pub struct ApplyConfig<F, A, B, C1, C2> {
a: A,
b: B,
f: F,
r: PhantomData<(C1, C2)>,
}
impl<F, A, B, C1, C2> ApplyConfig<F, A, B, C1, C2>
where
A: NewService<C1>,
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
F: Fn(&C1) -> C2,
{
/// Create new `ApplyNewService` new service instance
pub fn new<A1: IntoNewService<A, C1>, B1: IntoNewService<B, C2>>(
a: A1,
b: B1,
f: F,
) -> Self {
Self {
f,
a: a.into_new_service(),
b: b.into_new_service(),
r: PhantomData,
}
}
}
impl<F, A, B, C1, C2> Clone for ApplyConfig<F, A, B, C1, C2>
where
A: Clone,
B: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
a: self.a.clone(),
b: self.b.clone(),
f: self.f.clone(),
r: PhantomData,
}
}
}
impl<F, A, B, C1, C2> NewService<C1> for ApplyConfig<F, A, B, C1, C2>
where
A: NewService<C1>,
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
F: Fn(&C1) -> C2,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Service = AndThen<A::Service, B::Service>;
type InitError = A::InitError;
type Future = ApplyConfigResponse<A, B, C1, C2>;
fn new_service(&self, cfg: &C1) -> Self::Future {
let cfg2 = (self.f)(cfg);
ApplyConfigResponse {
a: None,
b: None,
fut_a: self.a.new_service(cfg),
fut_b: self.b.new_service(&cfg2),
}
}
}
pub struct ApplyConfigResponse<A, B, C1, C2>
where
A: NewService<C1>,
B: NewService<C2>,
{
fut_b: B::Future,
fut_a: A::Future,
a: Option<A::Service>,
b: Option<B::Service>,
}
impl<A, B, C1, C2> Future for ApplyConfigResponse<A, B, C1, C2>
where
A: NewService<C1>,
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{
type Item = AndThen<A::Service, B::Service>;
type Error = A::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if self.a.is_none() {
if let Async::Ready(service) = self.fut_a.poll()? {
self.a = Some(service);
}
}
if self.b.is_none() {
if let Async::Ready(service) = self.fut_b.poll()? {
self.b = Some(service);
}
}
if self.a.is_some() && self.b.is_some() {
Ok(Async::Ready(AndThen::new(
self.a.take().unwrap(),
self.b.take().unwrap(),
)))
} else {
Ok(Async::NotReady)
}
}
}
#[cfg(test)]
mod tests {
use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll};
use crate::{fn_cfg_factory, NewService, Service};
#[derive(Clone)]
struct Srv;
impl Service for Srv {
type Request = ();
type Response = ();
type Error = ();
type Future = FutureResult<(), ()>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, _: ()) -> Self::Future {
ok(())
}
}
#[test]
fn test_new_service() {
let new_srv = fn_cfg_factory(|_: &usize| Ok::<_, ()>(Srv)).apply_cfg(
fn_cfg_factory(|s: &String| {
assert_eq!(s, "test");
Ok::<_, ()>(Srv)
}),
|cfg: &usize| {
assert_eq!(*cfg, 1);
"test".to_string()
},
);
if let Async::Ready(mut srv) = new_srv.new_service(&1).poll().unwrap() {
assert!(srv.poll_ready().is_ok());
}
}
}

View File

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

View File

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

View File

@@ -1,7 +1,7 @@
use std::marker::PhantomData;
use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll};
use futures::future::{ok, Future, FutureResult};
use futures::{try_ready, Async, IntoFuture, Poll};
use crate::{IntoConfigurableNewService, IntoNewService, IntoService, NewService, Service};
@@ -15,21 +15,22 @@ where
}
/// Create `NewService` for function that can produce services
pub fn fn_factory<F, R, S, E, Req>(f: F) -> FnNewServiceNoConfig<F, R, S, E, Req>
pub fn fn_factory<F, R, S, E>(f: F) -> FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
S: Service,
{
FnNewServiceNoConfig::new(f)
}
/// Create `NewService` for function that can produce services with configuration
pub fn fn_cfg_factory<F, C, R, S, E, Req>(f: F) -> FnNewServiceConfig<F, C, R, S, E, Req>
pub fn fn_cfg_factory<F, C, R, S, E>(f: F) -> FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
FnNewServiceConfig::new(f)
}
@@ -66,11 +67,12 @@ where
}
}
impl<F, Req, Out> Service<Req> for FnService<F, Req, Out>
impl<F, Req, Out> Service for FnService<F, Req, Out>
where
F: FnMut(Req) -> Out,
Out: IntoFuture,
{
type Request = Req;
type Response = Out::Item;
type Error = Out::Error;
type Future = Out::Future;
@@ -84,7 +86,7 @@ where
}
}
impl<F, Req, Out> IntoService<FnService<F, Req, Out>, Req> for F
impl<F, Req, Out> IntoService<FnService<F, Req, Out>> for F
where
F: FnMut(Req) -> Out + 'static,
Out: IntoFuture,
@@ -113,11 +115,12 @@ where
}
}
impl<F, Req, Out, Cfg> NewService<Req, Cfg> for FnNewService<F, Req, Out, Cfg>
impl<F, Req, Out, Cfg> NewService<Cfg> for FnNewService<F, Req, Out, Cfg>
where
F: FnMut(Req) -> Out + Clone,
Out: IntoFuture,
{
type Request = Req;
type Response = Out::Item;
type Error = Out::Error;
type Service = FnService<F, Req, Out>;
@@ -140,7 +143,7 @@ where
}
}
impl<F, Req, Out, Cfg> IntoNewService<FnNewService<F, Req, Out, Cfg>, Req, Cfg> for F
impl<F, Req, Out, Cfg> IntoNewService<FnNewService<F, Req, Out, Cfg>, Cfg> for F
where
F: Fn(Req) -> Out + Clone,
Out: IntoFuture,
@@ -151,33 +154,33 @@ where
}
/// Converter for `Fn() -> Future<Service>` fn
pub struct FnNewServiceNoConfig<F, R, S, E, Req>
pub struct FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
S: Service,
{
f: F,
_t: PhantomData<Req>,
}
impl<F, R, S, E, Req> FnNewServiceNoConfig<F, R, S, E, Req>
impl<F, R, S, E> FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
S: Service,
{
pub fn new(f: F) -> Self {
FnNewServiceNoConfig { f, _t: PhantomData }
FnNewServiceNoConfig { f }
}
}
impl<F, R, S, E, Req> NewService<Req, ()> for FnNewServiceNoConfig<F, R, S, E, Req>
impl<F, R, S, E> NewService<()> for FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
S: Service,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S;
@@ -190,104 +193,119 @@ where
}
}
impl<F, R, S, E, Req> Clone for FnNewServiceNoConfig<F, R, S, E, Req>
impl<F, R, S, E> Clone for FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
S: Service,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())
}
}
impl<F, R, S, E, Req> IntoNewService<FnNewServiceNoConfig<F, R, S, E, Req>, Req, ()> for F
impl<F, R, S, E> IntoNewService<FnNewServiceNoConfig<F, R, S, E>, ()> for F
where
F: Fn() -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
S: Service,
{
fn into_new_service(self) -> FnNewServiceNoConfig<F, R, S, E, Req> {
fn into_new_service(self) -> FnNewServiceNoConfig<F, R, S, E> {
FnNewServiceNoConfig::new(self)
}
}
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
pub struct FnNewServiceConfig<F, C, R, S, E, Req>
pub struct FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
f: F,
_t: PhantomData<(C, R, S, E, Req)>,
_t: PhantomData<(C, R, S, E)>,
}
impl<F, C, R, S, E, Req> FnNewServiceConfig<F, C, R, S, E, Req>
impl<F, C, R, S, E> FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
pub fn new(f: F) -> Self {
FnNewServiceConfig { f, _t: PhantomData }
}
}
impl<F, C, R, S, E, Req> NewService<Req, C> for FnNewServiceConfig<F, C, R, S, E, Req>
impl<F, C, R, S, E> NewService<C> for FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S;
type InitError = E;
type Future = R::Future;
type Future = FnNewServiceConfigFut<R, S, E>;
fn new_service(&self, cfg: &C) -> Self::Future {
(self.f)(cfg).into_future()
FnNewServiceConfigFut {
fut: (self.f)(cfg).into_future(),
_t: PhantomData,
}
}
}
impl<F, C, R, S, E, Req> Clone for FnNewServiceConfig<F, C, R, S, E, Req>
pub struct FnNewServiceConfigFut<R, S, E>
where
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
fut: R::Future,
_t: PhantomData<(S,)>,
}
impl<R, S, E> Future for FnNewServiceConfigFut<R, S, E>
where
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
type Item = S;
type Error = R::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
Ok(Async::Ready(try_ready!(self.fut.poll()).into_service()))
}
}
impl<F, C, R, S, E> Clone for FnNewServiceConfig<F, C, R, S, E>
where
F: Fn(&C) -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())
}
}
impl<F, C, R, S, E, Req>
IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E, Req>, Req, C> for F
impl<F, C, R, S, E> IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E>, C> for F
where
F: Fn(&C) -> R,
R: IntoFuture<Item = S, Error = E>,
S: Service<Req>,
R: IntoFuture<Error = E>,
R::Item: IntoService<S>,
S: Service,
{
fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E, Req> {
fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E> {
FnNewServiceConfig::new(self)
}
}
#[cfg(test)]
mod tests {
use crate::{IntoService, Service, ServiceExt};
#[test]
fn test_fn_service() {
let mut rt = actix_rt::Runtime::new().unwrap();
let srv = (|_t: &str| -> Result<usize, ()> { Ok(1) }).into_service();
let mut srv = srv.and_then(|test: usize| Ok(test));
let s = "HELLO".to_owned();
let res = rt.block_on(srv.call(&s)).unwrap();
assert_eq!(res, 1);
}
}

View File

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

View File

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

View File

@@ -9,6 +9,7 @@ mod and_then;
mod and_then_apply;
mod and_then_apply_fn;
mod apply;
mod apply_cfg;
pub mod blank;
pub mod boxed;
mod cell;
@@ -26,6 +27,7 @@ pub use self::and_then::{AndThen, AndThenNewService};
use self::and_then_apply::AndThenTransform;
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
pub use self::apply::{Apply, ApplyNewService};
pub use self::apply_cfg::ApplyConfig;
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
pub use self::fn_transform::FnTransform;
pub use self::from_err::{FromErr, FromErrNewService};
@@ -36,9 +38,10 @@ pub use self::then::{Then, ThenNewService};
pub use self::transform::{ApplyTransform, IntoTransform, Transform};
/// An asynchronous function from `Request` to a `Response`.
///
/// `Request` - requests handled by the service.
pub trait Service<Request> {
pub trait Service {
/// Requests handled by the service.
type Request;
/// Responses given by the service.
type Response;
@@ -68,26 +71,22 @@ pub trait Service<Request> {
///
/// Calling `call` without calling `poll_ready` is permitted. The
/// 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
/// adapters
pub trait ServiceExt<Request>: Service<Request> {
pub trait ServiceExt: Service {
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply_fn<F, B, B1, Out, Req>(
self,
service: B1,
f: F,
) -> AndThenApply<Self, B, F, Out, Request, Req>
fn apply_fn<F, B, B1, Out>(self, service: B1, f: F) -> AndThenApply<Self, B, F, Out>
where
Self: Sized,
F: FnMut(Self::Response, &mut B) -> Out,
Out: IntoFuture,
Out::Error: Into<Self::Error>,
B: Service<Req, Error = Self::Error>,
B1: IntoService<B, Req>,
B: Service<Error = Self::Error>,
B1: IntoService<B>,
{
AndThenApply::new(self, service, f)
}
@@ -104,8 +103,8 @@ pub trait ServiceExt<Request>: Service<Request> {
fn and_then<F, B>(self, service: F) -> AndThen<Self, B>
where
Self: Sized,
F: IntoService<B, Self::Response>,
B: Service<Self::Response, Error = Self::Error>,
F: IntoService<B>,
B: Service<Request = Self::Response, Error = Self::Error>,
{
AndThen::new(self, service.into_service())
}
@@ -131,7 +130,7 @@ pub trait ServiceExt<Request>: Service<Request> {
fn then<B>(self, service: B) -> Then<Self, B>
where
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)
}
@@ -170,7 +169,7 @@ pub trait ServiceExt<Request>: Service<Request> {
}
}
impl<T: ?Sized, R> ServiceExt<R> for T where T: Service<R> {}
impl<T: ?Sized> ServiceExt for T where T: Service {}
/// Creates new `Service` values.
///
@@ -180,9 +179,11 @@ impl<T: ?Sized, R> ServiceExt<R> for T where T: Service<R> {}
/// `NewService` trait, and uses that new `Service` value to process inbound
/// requests on that new TCP stream.
///
/// * `Request` - requests handled by the service.
/// * `Config` - is a service factory configuration type.
pub trait NewService<Request, Config = ()> {
/// `Config` is a service factory configuration type.
pub trait NewService<Config = ()> {
/// Requests handled by the service.
type Request;
/// Responses given by the service
type Response;
@@ -190,7 +191,11 @@ pub trait NewService<Request, Config = ()> {
type Error;
/// 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.
type InitError;
@@ -201,35 +206,35 @@ pub trait NewService<Request, Config = ()> {
/// Create and return a new service value asynchronously.
fn new_service(&self, cfg: &Config) -> Self::Future;
/// Apply function to specified service and use it as a next service in
/// Apply transform service to specified service and use it as a next service in
/// chain.
fn apply<T, T1, B, B1, Req>(
fn apply<T, T1, B, B1>(
self,
transform: T1,
service: B1,
) -> AndThenTransform<T, Self, B, Req>
) -> AndThenTransform<T, Self, B, Config>
where
Self: Sized,
T: Transform<B::Service, Self::Response, InitError = Self::InitError>,
T: Transform<B::Service, Request = Self::Response, InitError = Self::InitError>,
T::Error: From<Self::Error>,
T1: IntoTransform<T, B::Service, Self::Response>,
B: NewService<Req, Config, InitError = Self::InitError>,
B1: IntoNewService<B, Req, Config>,
T1: IntoTransform<T, B::Service>,
B: NewService<Config, InitError = Self::InitError>,
B1: IntoNewService<B, Config>,
{
AndThenTransform::new(transform.into_transform(), self, service.into_new_service())
}
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply_fn<B, I, F, Out, Req>(
fn apply_fn<B, I, F, Out>(
self,
service: I,
f: F,
) -> AndThenApplyNewService<Self, B, F, Out, Request, Req, Config>
) -> AndThenApplyNewService<Self, B, F, Out, Config>
where
Self: Sized,
B: NewService<Req, Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Req, Config>,
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
I: IntoNewService<B, Config>,
F: FnMut(Self::Response, &mut B::Service) -> Out,
Out: IntoFuture,
Out::Error: Into<Self::Error>,
@@ -237,12 +242,34 @@ pub trait NewService<Request, Config = ()> {
AndThenApplyNewService::new(self, service, f)
}
/// Call another service after call to this one has resolved successfully.
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
/// Map this service's config type to a different config,
/// and use for nested service
fn apply_cfg<F, C, B, B1>(self, service: B1, f: F) -> ApplyConfig<F, Self, B, Config, C>
where
Self: Sized,
F: IntoNewService<B, Self::Response, Config>,
B: NewService<Self::Response, Config, Error = Self::Error, InitError = Self::InitError>,
F: Fn(&Config) -> C,
B1: IntoNewService<B, C>,
B: NewService<
C,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
>,
{
ApplyConfig::new(self, service, f)
}
/// Call another service after call to this one has resolved successfully.
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B, Config>
where
Self: Sized,
F: IntoNewService<B, Config>,
B: NewService<
Config,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
>,
{
AndThenNewService::new(self, new_service)
}
@@ -270,15 +297,15 @@ pub trait NewService<Request, Config = ()> {
fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B, Config>
where
Self: Sized,
F: IntoNewService<B, Result<Self::Response, Self::Error>, Config>,
F: IntoNewService<B, Config>,
B: NewService<
Result<Self::Response, Self::Error>,
Config,
Request = Result<Self::Response, Self::Error>,
Error = Self::Error,
InitError = Self::InitError,
>,
{
ThenNewService::new(self, new_service.into_new_service())
ThenNewService::new(self, new_service)
}
/// Map this service's output to a different type, returning a new service
@@ -310,10 +337,11 @@ pub trait NewService<Request, Config = ()> {
}
}
impl<'a, S, R> Service<R> for &'a mut S
impl<'a, S> Service for &'a mut S
where
S: Service<R> + 'a,
S: Service + 'a,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@@ -322,15 +350,16 @@ where
(**self).poll_ready()
}
fn call(&mut self, request: R) -> S::Future {
fn call(&mut self, request: Self::Request) -> S::Future {
(**self).call(request)
}
}
impl<S, R> Service<R> for Box<S>
impl<S> Service for Box<S>
where
S: Service<R> + ?Sized,
S: Service + ?Sized,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
@@ -339,15 +368,16 @@ where
(**self).poll_ready()
}
fn call(&mut self, request: R) -> S::Future {
fn call(&mut self, request: Self::Request) -> S::Future {
(**self).call(request)
}
}
impl<S, R, C> NewService<R, C> for Rc<S>
impl<S, C> NewService<C> for Rc<S>
where
S: NewService<R, C>,
S: NewService<C>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S::Service;
@@ -359,10 +389,11 @@ where
}
}
impl<S, R, C> NewService<R, C> for Arc<S>
impl<S, C> NewService<C> for Arc<S>
where
S: NewService<R, C>,
S: NewService<C>,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S::Service;
@@ -375,35 +406,35 @@ where
}
/// Trait for types that can be converted to a `Service`
pub trait IntoService<T, R>
pub trait IntoService<T>
where
T: Service<R>,
T: Service,
{
/// Convert to a `Service`
fn into_service(self) -> T;
}
/// Trait for types that can be converted to a `NewService`
pub trait IntoNewService<T, R, C = ()>
pub trait IntoNewService<T, C = ()>
where
T: NewService<R, C>,
T: NewService<C>,
{
/// Convert to an `NewService`
fn into_new_service(self) -> T;
}
impl<T, R> IntoService<T, R> for T
impl<T> IntoService<T> for T
where
T: Service<R>,
T: Service,
{
fn into_service(self) -> T {
self
}
}
impl<T, R, C> IntoNewService<T, R, C> for T
impl<T, C> IntoNewService<T, C> for T
where
T: NewService<R, C>,
T: NewService<C>,
{
fn into_new_service(self) -> T {
self
@@ -411,17 +442,17 @@ where
}
/// Trait for types that can be converted to a configurable `NewService`
pub trait IntoConfigurableNewService<T, R, C>
pub trait IntoConfigurableNewService<T, C>
where
T: NewService<R, C>,
T: NewService<C>,
{
/// Convert to an `NewService`
fn into_new_service(self) -> T;
}
impl<T, R, C> IntoConfigurableNewService<T, R, C> for T
impl<T, C> IntoConfigurableNewService<T, C> for T
where
T: NewService<R, C>,
T: NewService<C>,
{
fn into_new_service(self) -> T {
self

View File

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

View File

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

View File

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

View File

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

View File

@@ -9,10 +9,11 @@ use crate::{NewService, Service};
/// `Transform` service factory.
///
/// Transform factory creates service that wraps other services.
///
/// * `S` is a wrapped service.
/// * `R` requests handled by this transform service.
pub trait Transform<S, R> {
/// `Config` is a service factory configuration type.
pub trait Transform<S> {
/// Requests handled by the service.
type Request;
/// Responses given by the service.
type Response;
@@ -20,7 +21,11 @@ pub trait Transform<S, R> {
type Error;
/// The `TransformService` value created by this factory
type Transform: Service<R, Response = Self::Response, Error = Self::Error>;
type Transform: Service<
Request = Self::Request,
Response = Self::Response,
Error = Self::Error,
>;
/// Errors produced while building a service.
type InitError;
@@ -42,10 +47,11 @@ pub trait Transform<S, R> {
}
}
impl<T, S, R> Transform<S, R> for Rc<T>
impl<T, S> Transform<S> for Rc<T>
where
T: Transform<S, R>,
T: Transform<S>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
@@ -57,10 +63,11 @@ where
}
}
impl<T, S, R> Transform<S, R> for Arc<T>
impl<T, S> Transform<S> for Arc<T>
where
T: Transform<S, R>,
T: Transform<S>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type InitError = T::InitError;
@@ -73,17 +80,17 @@ where
}
/// Trait for types that can be converted to a *transform service*
pub trait IntoTransform<T, S, R>
pub trait IntoTransform<T, S>
where
T: Transform<S, R>,
T: Transform<S>,
{
/// Convert to a `TransformService`
fn into_transform(self) -> T;
}
impl<T, S, R> IntoTransform<T, S, R> for T
impl<T, S> IntoTransform<T, S> for T
where
T: Transform<S, R>,
T: Transform<S>,
{
fn into_transform(self) -> T {
self
@@ -92,19 +99,19 @@ where
/// `Apply` transform new service
#[derive(Clone)]
pub struct ApplyTransform<T, S, R, Req, Cfg> {
a: S,
pub struct ApplyTransform<T, A, C> {
a: A,
t: Rc<T>,
_t: std::marker::PhantomData<(R, Req, Cfg)>,
_t: std::marker::PhantomData<C>,
}
impl<T, S, R, Req, Cfg> ApplyTransform<T, S, R, Req, Cfg>
impl<T, A, C> ApplyTransform<T, A, C>
where
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
/// Create new `ApplyNewService` new service instance
pub fn new<F: IntoTransform<T, S::Service, R>>(t: F, a: S) -> Self {
pub fn new<F: IntoTransform<T, A::Service>>(t: F, a: A) -> Self {
Self {
a,
t: Rc::new(t.into_transform()),
@@ -113,19 +120,20 @@ where
}
}
impl<T, S, R, Req, Cfg> NewService<R, Cfg> for ApplyTransform<T, S, R, Req, Cfg>
impl<T, A, C> NewService<C> for ApplyTransform<T, A, C>
where
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Service = T::Transform;
type InitError = T::InitError;
type Future = ApplyTransformFuture<T, S, R, Req, Cfg>;
type Future = ApplyTransformFuture<T, A, C>;
fn new_service(&self, cfg: &Cfg) -> Self::Future {
fn new_service(&self, cfg: &C) -> Self::Future {
ApplyTransformFuture {
t_cell: self.t.clone(),
fut_a: self.a.new_service(cfg).into_future(),
@@ -134,20 +142,20 @@ where
}
}
pub struct ApplyTransformFuture<T, S, R, Req, Cfg>
pub struct ApplyTransformFuture<T, A, C>
where
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
fut_a: S::Future,
fut_t: Option<T::Future>,
fut_a: A::Future,
fut_t: Option<<T::Future as IntoFuture>::Future>,
t_cell: Rc<T>,
}
impl<T, S, R, Req, Cfg> Future for ApplyTransformFuture<T, S, R, Req, Cfg>
impl<T, A, C> Future for ApplyTransformFuture<T, A, C>
where
S: NewService<Req, Cfg>,
T: Transform<S::Service, R, Error = S::Error, InitError = S::InitError>,
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
{
type Item = T::Transform;
type Error = T::InitError;

View File

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

View File

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

View File

@@ -21,11 +21,12 @@ impl<A: Clone, B: Clone> Clone for EitherService<A, B> {
}
}
impl<A, B, R> Service<R> for EitherService<A, B>
impl<A, B> Service for EitherService<A, B>
where
A: Service<R>,
B: Service<R, Response = A::Response, Error = A::Error>,
A: Service,
B: Service<Request = A::Request, Response = A::Response, Error = A::Error>,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type Future = future::Either<A::Future, B::Future>;
@@ -37,7 +38,7 @@ where
}
}
fn call(&mut self, req: R) -> Self::Future {
fn call(&mut self, req: A::Request) -> Self::Future {
match self {
EitherService::A(ref mut inner) => future::Either::A(inner.call(req)),
EitherService::B(ref mut inner) => future::Either::B(inner.call(req)),
@@ -52,33 +53,52 @@ pub enum Either<A, B> {
}
impl<A, B> Either<A, B> {
pub fn new_a<R, C>(srv: A) -> Self
pub fn new_a<C>(srv: A) -> Self
where
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
B: NewService<
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{
Either::A(srv)
}
pub fn new_b<R, C>(srv: B) -> Self
pub fn new_b<C>(srv: B) -> Self
where
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
B: NewService<
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{
Either::B(srv)
}
}
impl<A, B, R, C> NewService<R, C> for Either<A, B>
impl<A, B, C> NewService<C> for Either<A, B>
where
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
B: NewService<
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{
type Request = A::Request;
type Response = A::Response;
type Error = A::Error;
type InitError = A::InitError;
type Service = EitherService<A::Service, B::Service>;
type Future = EitherNewService<A, B, R, C>;
type Future = EitherNewService<A, B, C>;
fn new_service(&self, cfg: &C) -> Self::Future {
match self {
@@ -98,15 +118,21 @@ impl<A: Clone, B: Clone> Clone for Either<A, B> {
}
#[doc(hidden)]
pub enum EitherNewService<A: NewService<R, C>, B: NewService<R, C>, R, C> {
pub enum EitherNewService<A: NewService<C>, B: NewService<C>, C> {
A(<A::Future as IntoFuture>::Future),
B(<B::Future as IntoFuture>::Future),
}
impl<A, B, R, C> Future for EitherNewService<A, B, R, C>
impl<A, B, C> Future for EitherNewService<A, B, C>
where
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
A: NewService<C>,
B: NewService<
C,
Request = A::Request,
Response = A::Response,
Error = A::Error,
InitError = A::InitError,
>,
{
type Item = EitherService<A::Service, B::Service>;
type Error = A::InitError;

View File

@@ -23,15 +23,15 @@ pub struct FramedNewService<S, T, U, C> {
impl<S, T, U, C> FramedNewService<S, T, U, C>
where
C: Clone,
S: NewService<Request<U>, C, Response = Response<U>>,
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
pub fn new<F1: IntoNewService<S, Request<U>, C>>(factory: F1) -> Self {
pub fn new<F1: IntoNewService<S, C>>(factory: F1) -> Self {
Self {
factory: factory.into_new_service(),
_t: PhantomData,
@@ -51,17 +51,18 @@ where
}
}
impl<S, T, U, C> NewService<Framed<T, U>, C> for FramedNewService<S, T, U, C>
impl<S, T, U, C> NewService<C> for FramedNewService<S, T, U, C>
where
C: Clone,
S: NewService<Request<U>, C, Response = Response<U>> + Clone,
S: NewService<C, Request = Request<U>, Response = Response<U>> + Clone,
S::Error: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
type Request = Framed<T, U>;
type Response = FramedTransport<S::Service, T, U>;
type Error = S::InitError;
type InitError = S::InitError;
@@ -97,17 +98,18 @@ where
}
}
impl<S, T, U, C> Service<Framed<T, U>> for FramedService<S, T, U, C>
impl<S, T, U, C> Service for FramedService<S, T, U, C>
where
S: NewService<Request<U>, C, Response = Response<U>>,
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
C: Clone,
{
type Request = Framed<T, U>;
type Response = FramedTransport<S::Service, T, U>;
type Error = S::InitError;
type Future = FramedServiceResponseFuture<S, T, U, C>;
@@ -127,9 +129,9 @@ where
#[doc(hidden)]
pub struct FramedServiceResponseFuture<S, T, U, C>
where
S: NewService<Request<U>, C, Response = Response<U>>,
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
@@ -141,9 +143,9 @@ where
impl<S, T, U, C> Future for FramedServiceResponseFuture<S, T, U, C>
where
S: NewService<Request<U>, C, Response = Response<U>>,
S: NewService<C, Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
<S::Service as Service<Request<U>>>::Future: 'static,
<S::Service as Service>::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
@@ -180,7 +182,7 @@ impl<E, U: Encoder + Decoder> From<E> for FramedTransportError<E, U> {
/// and pass then to the service.
pub struct FramedTransport<S, T, U>
where
S: Service<Request<U>, Response = Response<U>>,
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@@ -194,7 +196,7 @@ where
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,
Error(FramedTransportError<S::Error, U>),
FramedError(FramedTransportError<S::Error, U>),
@@ -208,7 +210,7 @@ struct FramedTransportInner<I, E> {
impl<S, T, U> FramedTransport<S, T, U>
where
S: Service<Request<U>, Response = Response<U>>,
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@@ -300,7 +302,7 @@ where
impl<S, T, U> FramedTransport<S, T, U>
where
S: Service<Request<U>, Response = Response<U>>,
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@@ -308,7 +310,7 @@ where
<U as Encoder>::Item: 'static,
<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 {
framed,
service: service.into_service(),
@@ -346,7 +348,7 @@ where
impl<S, T, U> Future for FramedTransport<S, T, U>
where
S: Service<Request<U>, Response = Response<U>>,
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@@ -406,19 +408,20 @@ where
}
}
impl<T, U, F> NewService<T, ()> for IntoFramed<T, U, F>
impl<T, C, U, F> NewService<C> for IntoFramed<T, U, F>
where
T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static,
U: Encoder + Decoder,
{
type Request = T;
type Response = Framed<T, U>;
type Error = ();
type InitError = ();
type Service = IntoFramedService<T, U, F>;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &()) -> Self::Future {
fn new_service(&self, _: &C) -> Self::Future {
ok(IntoFramedService {
factory: self.factory.clone(),
_t: PhantomData,
@@ -436,12 +439,13 @@ where
_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
T: AsyncRead + AsyncWrite,
F: Fn() -> U + Send + Clone + 'static,
U: Encoder + Decoder,
{
type Request = T;
type Response = Framed<T, U>;
type Error = ();
type Future = FutureResult<Self::Response, Self::Error>;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -5,12 +5,12 @@ use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
use std::{env, fmt};
use std::{env, fmt, io};
use actix_codec::{AsyncRead, AsyncWrite};
use actix_rt::System;
use actix_server::Server;
use actix_service::{IntoNewService, NewService};
use actix_service::{fn_service, NewService};
use futures::{future, Future};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
use tokio_openssl::SslAcceptorExt;
@@ -23,7 +23,7 @@ fn logger<T: AsyncRead + AsyncWrite + fmt::Debug>(
future::ok(stream)
}
fn main() {
fn main() -> io::Result<()> {
env::set_var("RUST_LOG", "actix_net=trace");
env_logger::init();
@@ -54,16 +54,14 @@ fn main() {
let acceptor = acceptor.clone();
// service for converting incoming TcpStream to a SslStream<TcpStream>
(move |stream| {
fn_service(move |stream: tokio_tcp::TcpStream| {
SslAcceptorExt::accept_async(&acceptor, stream)
.map_err(|e| println!("Openssl error: {}", e))
})
// convert closure to a `NewService`
.into_new_service()
// .and_then() combinator uses other service to convert incoming `Request` to a
// `Response` and then uses that response as an input for next
// service. in this case, on success we use `logger` service
.and_then(logger)
.and_then(fn_service(logger))
// Next service counts number of connections
.and_then(move |_| {
let num = num.fetch_add(1, Ordering::Relaxed);
@@ -75,5 +73,5 @@ fn main() {
.unwrap()
.start();
sys.run();
sys.run()
}

View File

@@ -1,13 +1,13 @@
use std::io;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
use actix_codec::{AsyncRead, AsyncWrite};
use actix_rt::System;
use actix_server::{ssl, Server};
use actix_service::NewService;
use futures::{future, Future};
use futures::future;
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
#[derive(Debug)]
@@ -15,16 +15,7 @@ struct ServiceState {
num: Arc<AtomicUsize>,
}
fn service<T: AsyncRead + AsyncWrite>(
st: &mut ServiceState,
_: T,
) -> impl Future<Item = (), Error = ()> {
let num = st.num.fetch_add(1, Ordering::Relaxed);
println!("got ssl connection {:?}", num);
future::ok(())
}
fn main() {
fn main() -> io::Result<()> {
let sys = System::new("test");
// load ssl keys
@@ -53,9 +44,8 @@ fn main() {
println!("got ssl connection {:?}", num);
future::ok(())
})
})
.unwrap()
})?
.start();
sys.run();
sys.run()
}

View File

@@ -208,3 +208,32 @@ impl<T: ResourcePath> Resource<T> for Path<T> {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "http")]
#[test]
fn test_get_param_by_name() {
use crate::Url;
use http::{HttpTryFrom, Uri};
let mut params = Path::new(Url::new(Uri::try_from("/").unwrap()));
params.add_static("item1", "path");
params.add_static("item2", "http%3A%2F%2Flocalhost%3A80%2Ffoo");
assert_eq!(params.get("item0"), None);
assert_eq!(params.get_decoded("item0"), None);
assert_eq!(params.get("item1"), Some("path"));
assert_eq!(params.get_decoded("item1").unwrap().to_owned(), "path");
assert_eq!(
params.get("item2"),
Some("http%3A%2F%2Flocalhost%3A80%2Ffoo")
);
assert_eq!(
params.get_decoded("item2").unwrap().to_owned(),
"http://localhost:80/foo"
);
}
}

View File

@@ -14,26 +14,13 @@ const MAX_DYNAMIC_SEGMENTS: usize = 16;
/// Resource definition can contain only 16 dynamic segments
#[derive(Clone, Debug)]
pub struct ResourceDef {
id: u16,
tp: PatternType,
rtp: ResourceType,
name: String,
pattern: String,
elements: Vec<PatternElement>,
}
#[derive(Debug, Copy, Clone, PartialEq)]
/// Resource type
pub enum ResourceType {
/// Normal resource
Normal,
/// Resource for application default handler
Default,
/// External resource
External,
/// Unknown resource type
Unset,
}
#[derive(Debug, Clone, PartialEq)]
enum PatternElement {
Str(String),
@@ -75,13 +62,14 @@ impl ResourceDef {
ResourceDef::with_prefix(&insert_slash(path), true)
}
/// Construct external resource def
///
/// Panics if path pattern is malformed.
pub fn external(path: &str) -> Self {
let mut resource = ResourceDef::with_prefix(path, false);
resource.rtp = ResourceType::External;
resource
/// Resource id
pub fn id(&self) -> u16 {
self.id
}
/// Set resource id
pub fn set_id(&mut self, id: u16) {
self.id = id;
}
/// Parse path pattern and create new `Pattern` instance with custom prefix
@@ -109,12 +97,22 @@ impl ResourceDef {
ResourceDef {
tp,
elements,
id: 0,
name: String::new(),
rtp: ResourceType::Normal,
pattern: path.to_owned(),
}
}
/// Resource pattern name
pub fn name(&self) -> &str {
&self.name
}
/// Mutable reference to a name of a resource definition.
pub fn name_mut(&mut self) -> &mut String {
&mut self.name
}
/// Path pattern of the resource
pub fn pattern(&self) -> &str {
&self.pattern
@@ -129,6 +127,57 @@ impl ResourceDef {
}
}
/// Is prefix path a match against this resource?
pub fn is_prefix_match(&self, path: &str) -> Option<usize> {
let plen = path.len();
let path = if path.is_empty() { "/" } else { path };
match self.tp {
PatternType::Static(ref s) => {
if s == path {
Some(plen)
} else {
None
}
}
PatternType::Dynamic(ref re, _, len) => {
if let Some(captures) = re.captures(path) {
let mut pos = 0;
let mut passed = false;
for capture in captures.iter() {
if let Some(ref m) = capture {
if !passed {
passed = true;
continue;
}
pos = m.end();
}
}
Some(pos + len)
} else {
None
}
}
PatternType::Prefix(ref s) => {
let len = if path == s {
s.len()
} else if path.starts_with(s)
&& (s.ends_with('/') || path.split_at(s.len()).1.starts_with('/'))
{
if s.ends_with('/') {
s.len() - 1
} else {
s.len()
}
} else {
return None;
};
Some(min(plen, len))
}
}
}
/// Is the given path and parameters a match against this pattern?
pub fn match_path<T: ResourcePath>(&self, path: &mut Path<T>) -> bool {
match self.tp {
@@ -191,34 +240,32 @@ impl ResourceDef {
}
}
// /// Build resource path.
// pub fn resource_path<U, I>(
// &self, path: &mut String, elements: &mut U,
// ) -> Result<(), UrlGenerationError>
// where
// U: Iterator<Item = I>,
// I: AsRef<str>,
// {
// match self.tp {
// PatternType::Prefix(ref p) => path.push_str(p),
// PatternType::Static(ref p) => path.push_str(p),
// PatternType::Dynamic(..) => {
// for el in &self.elements {
// match *el {
// PatternElement::Str(ref s) => path.push_str(s),
// PatternElement::Var(_) => {
// if let Some(val) = elements.next() {
// path.push_str(val.as_ref())
// } else {
// return Err(UrlGenerationError::NotEnoughElements);
// }
// }
// }
// }
// }
// };
// Ok(())
// }
/// Build resource path from elements. Returns `true` on success.
pub fn resource_path<U, I>(&self, path: &mut String, elements: &mut U) -> bool
where
U: Iterator<Item = I>,
I: AsRef<str>,
{
match self.tp {
PatternType::Prefix(ref p) => path.push_str(p),
PatternType::Static(ref p) => path.push_str(p),
PatternType::Dynamic(..) => {
for el in &self.elements {
match *el {
PatternElement::Str(ref s) => path.push_str(s),
PatternElement::Var(_) => {
if let Some(val) = elements.next() {
path.push_str(val.as_ref())
} else {
return false;
}
}
}
}
}
};
true
}
fn parse_param(pattern: &str) -> (PatternElement, String, &str) {
const DEFAULT_PATTERN: &str = "[^/]+";
@@ -349,6 +396,11 @@ mod tests {
assert!(!re.is_match("/name/"));
assert!(!re.is_match("/name~"));
assert_eq!(re.is_prefix_match("/name"), Some(5));
assert_eq!(re.is_prefix_match("/name1"), None);
assert_eq!(re.is_prefix_match("/name/"), None);
assert_eq!(re.is_prefix_match("/name~"), None);
let re = ResourceDef::new("/name/");
assert!(re.is_match("/name/"));
assert!(!re.is_match("/name"));
@@ -423,6 +475,12 @@ mod tests {
assert!(re.is_match("/name1"));
assert!(re.is_match("/name~"));
assert_eq!(re.is_prefix_match("/name"), Some(5));
assert_eq!(re.is_prefix_match("/name/"), Some(5));
assert_eq!(re.is_prefix_match("/name/test/test"), Some(5));
assert_eq!(re.is_prefix_match("/name1"), None);
assert_eq!(re.is_prefix_match("/name~"), None);
let re = ResourceDef::prefix("/name/");
assert!(re.is_match("/name/"));
assert!(re.is_match("/name/gs"));
@@ -441,6 +499,10 @@ mod tests {
assert!(re.is_match("/name/gs"));
assert!(!re.is_match("/name"));
assert_eq!(re.is_prefix_match("/name/"), Some(6));
assert_eq!(re.is_prefix_match("/name/gs"), Some(6));
assert_eq!(re.is_prefix_match("/name"), None);
let mut path = Path::new("/test2/");
assert!(re.match_path(&mut path));
assert_eq!(&path["name"], "test2");

View File

@@ -1,170 +1,97 @@
use std::collections::HashMap;
use std::rc::Rc;
use crate::resource::ResourceDef;
use crate::{Resource, ResourcePath};
use crate::{Resource, ResourceDef, ResourcePath};
#[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) enum ResourceId {
Default,
Normal(u16),
}
pub struct ResourceId(pub u16);
/// Information about current resource
#[derive(Clone, Debug)]
pub struct ResourceInfo {
rmap: Rc<ResourceMap>,
resource: ResourceId,
}
#[derive(Default, Debug)]
pub(crate) struct ResourceMap {
root: Option<ResourceDef>,
named: HashMap<String, ResourceDef>,
patterns: Vec<ResourceDef>,
}
/// Resource router.
pub struct Router<T, U = ()> {
rmap: Rc<ResourceMap>,
named: HashMap<String, ResourceDef>,
resources: Vec<(T, Option<U>)>,
}
pub struct Router<T, U = ()>(Vec<(ResourceDef, T, Option<U>)>);
impl<T, U> Router<T, U> {
pub fn build() -> RouterBuilder<T, U> {
RouterBuilder {
rmap: ResourceMap::default(),
named: HashMap::new(),
resources: Vec::new(),
}
}
pub fn recognize<R: Resource<P>, P: ResourcePath>(
&self,
res: &mut R,
) -> Option<(&T, ResourceInfo)> {
for (idx, resource) in self.rmap.patterns.iter().enumerate() {
if resource.match_path(res.resource_path()) {
let info = ResourceInfo {
rmap: self.rmap.clone(),
resource: ResourceId::Normal(idx as u16),
};
return Some((&self.resources[idx].0, info));
pub fn recognize<R, P>(&self, path: &mut R) -> Option<(&T, ResourceId)>
where
R: Resource<P>,
P: ResourcePath,
{
for item in self.0.iter() {
if item.0.match_path(path.resource_path()) {
return Some((&item.1, ResourceId(item.0.id())));
}
}
None
}
pub fn recognize_mut<R: Resource<P>, P: ResourcePath>(
&mut self,
res: &mut R,
) -> Option<(&mut T, ResourceInfo)> {
for (idx, resource) in self.rmap.patterns.iter().enumerate() {
if resource.match_path(res.resource_path()) {
let info = ResourceInfo {
rmap: self.rmap.clone(),
resource: ResourceId::Normal(idx as u16),
};
return Some((&mut self.resources[idx].0, info));
pub fn recognize_mut<R, P>(&mut self, res: &mut R) -> Option<(&mut T, ResourceId)>
where
R: Resource<P>,
P: ResourcePath,
{
for item in self.0.iter_mut() {
if item.0.match_path(res.resource_path()) {
return Some((&mut item.1, ResourceId(item.0.id())));
}
}
None
}
pub fn recognize_mut_checked<R: Resource<P>, P: ResourcePath, F>(
pub fn recognize_mut_checked<R, P, F>(
&mut self,
res: &mut R,
check: F,
) -> Option<(&mut T, ResourceInfo)>
) -> Option<(&mut T, ResourceId)>
where
F: Fn(&R, &Option<U>) -> bool,
R: Resource<P>,
P: ResourcePath,
{
for (idx, resource) in self.rmap.patterns.iter().enumerate() {
if resource.match_path(res.resource_path()) && check(res, &self.resources[idx].1) {
let info = ResourceInfo {
rmap: self.rmap.clone(),
resource: ResourceId::Normal(idx as u16),
};
return Some((&mut self.resources[idx].0, info));
for item in self.0.iter_mut() {
if item.0.match_path(res.resource_path()) && check(res, &item.2) {
return Some((&mut item.1, ResourceId(item.0.id())));
}
}
None
}
}
impl<'a, T, U> IntoIterator for &'a Router<T, U> {
type Item = &'a (T, Option<U>);
type IntoIter = std::slice::Iter<'a, (T, Option<U>)>;
fn into_iter(self) -> Self::IntoIter {
self.resources.iter()
}
}
impl<'a, T, U> IntoIterator for &'a mut Router<T, U> {
type Item = &'a mut (T, Option<U>);
type IntoIter = std::slice::IterMut<'a, (T, Option<U>)>;
fn into_iter(self) -> Self::IntoIter {
self.resources.iter_mut()
}
}
impl ResourceMap {
fn register(&mut self, pattern: ResourceDef) {
self.patterns.push(pattern);
}
fn register_named(&mut self, name: String, pattern: ResourceDef) {
self.patterns.push(pattern.clone());
self.named.insert(name, pattern);
}
fn has_resource(&self, path: &str) -> bool {
unimplemented!()
}
}
pub struct RouterBuilder<T, U = ()> {
rmap: ResourceMap,
named: HashMap<String, ResourceDef>,
resources: Vec<(T, Option<U>)>,
resources: Vec<(ResourceDef, T, Option<U>)>,
}
impl<T, U> RouterBuilder<T, U> {
/// Register resource for specified path.
pub fn path(&mut self, path: &str, resource: T) {
self.rmap.register(ResourceDef::new(path));
self.resources.push((resource, None));
pub fn path(&mut self, path: &str, resource: T) -> &mut (ResourceDef, T, Option<U>) {
self.resources
.push((ResourceDef::new(path), resource, None));
self.resources.last_mut().unwrap()
}
/// Register resource for specified path prefix.
pub fn prefix(&mut self, prefix: &str, resource: T) {
self.rmap.register(ResourceDef::prefix(prefix));
self.resources.push((resource, None));
pub fn prefix(&mut self, prefix: &str, resource: T) -> &mut (ResourceDef, T, Option<U>) {
self.resources
.push((ResourceDef::prefix(prefix), resource, None));
self.resources.last_mut().unwrap()
}
/// Register resource for ResourceDef
pub fn rdef(&mut self, rdef: ResourceDef, resource: T) {
self.rmap.register(rdef);
self.resources.push((resource, None));
}
/// Method attachs user data to lastly added resource.
///
/// This panics if no resources were added.
pub fn set_user_data(&mut self, userdata: Option<U>) {
self.resources.last_mut().unwrap().1 = userdata;
pub fn rdef(&mut self, rdef: ResourceDef, resource: T) -> &mut (ResourceDef, T, Option<U>) {
self.resources.push((rdef, resource, None));
self.resources.last_mut().unwrap()
}
/// Finish configuration and create router instance.
pub fn finish(self) -> Router<T, U> {
Router {
rmap: Rc::new(self.rmap),
named: self.named,
resources: self.resources,
}
Router(self.resources)
}
}
@@ -176,14 +103,14 @@ mod tests {
#[test]
fn test_recognizer_1() {
let mut router = Router::<usize>::build();
router.path("/name", 10);
router.path("/name/{val}", 11);
router.path("/name/{val}/index.html", 12);
router.path("/file/{file}.{ext}", 13);
router.path("/v{val}/{val2}/index.html", 14);
router.path("/v/{tail:.*}", 15);
router.path("/test2/{test}.html", 16);
router.path("/{test}/index.html", 17);
router.path("/name", 10).0.set_id(0);
router.path("/name/{val}", 11).0.set_id(1);
router.path("/name/{val}/index.html", 12).0.set_id(2);
router.path("/file/{file}.{ext}", 13).0.set_id(3);
router.path("/v{val}/{val2}/index.html", 14).0.set_id(4);
router.path("/v/{tail:.*}", 15).0.set_id(5);
router.path("/test2/{test}.html", 16).0.set_id(6);
router.path("/{test}/index.html", 17).0.set_id(7);
let mut router = router.finish();
let mut path = Path::new("/unknown");
@@ -192,52 +119,52 @@ mod tests {
let mut path = Path::new("/name");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 10);
assert_eq!(info.resource, ResourceId::Normal(0));
assert_eq!(info, ResourceId(0));
assert!(path.is_empty());
let mut path = Path::new("/name/value");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 11);
assert_eq!(info.resource, ResourceId::Normal(1));
assert_eq!(info, ResourceId(1));
assert_eq!(path.get("val").unwrap(), "value");
assert_eq!(&path["val"], "value");
let mut path = Path::new("/name/value2/index.html");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 12);
assert_eq!(info.resource, ResourceId::Normal(2));
assert_eq!(info, ResourceId(2));
assert_eq!(path.get("val").unwrap(), "value2");
let mut path = Path::new("/file/file.gz");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 13);
assert_eq!(info.resource, ResourceId::Normal(3));
assert_eq!(info, ResourceId(3));
assert_eq!(path.get("file").unwrap(), "file");
assert_eq!(path.get("ext").unwrap(), "gz");
let mut path = Path::new("/vtest/ttt/index.html");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 14);
assert_eq!(info.resource, ResourceId::Normal(4));
assert_eq!(info, ResourceId(4));
assert_eq!(path.get("val").unwrap(), "test");
assert_eq!(path.get("val2").unwrap(), "ttt");
let mut path = Path::new("/v/blah-blah/index.html");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 15);
assert_eq!(info.resource, ResourceId::Normal(5));
assert_eq!(info, ResourceId(5));
assert_eq!(path.get("tail").unwrap(), "blah-blah/index.html");
let mut path = Path::new("/test2/index.html");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 16);
assert_eq!(info.resource, ResourceId::Normal(6));
assert_eq!(info, ResourceId(6));
assert_eq!(path.get("test").unwrap(), "index");
let mut path = Path::new("/bbb/index.html");
let (h, info) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 17);
assert_eq!(info.resource, ResourceId::Normal(7));
assert_eq!(info, ResourceId(7));
assert_eq!(path.get("test").unwrap(), "bbb");
}
@@ -260,8 +187,8 @@ mod tests {
#[test]
fn test_recognizer_with_prefix() {
let mut router = Router::<usize>::build();
router.path("/name", 10);
router.path("/name/{val}", 11);
router.path("/name", 10).0.set_id(0);
router.path("/name/{val}", 11).0.set_id(1);
let mut router = router.finish();
let mut path = Path::new("/name");
@@ -275,9 +202,9 @@ mod tests {
let mut path = Path::new("/test/name/value");
path.skip(5);
let (h, info) = router.recognize_mut(&mut path).unwrap();
let (h, id) = router.recognize_mut(&mut path).unwrap();
assert_eq!(*h, 11);
assert_eq!(info.resource, ResourceId::Normal(1));
assert_eq!(id, ResourceId(1));
assert_eq!(path.get("val").unwrap(), "value");
assert_eq!(&path["val"], "value");
@@ -306,134 +233,4 @@ mod tests {
assert_eq!(*h, 11);
assert_eq!(&path["val"], "ttt");
}
// #[test]
// fn test_request_resource() {
// let mut router = Router::<()>::default();
// let mut resource = Resource::new(ResourcePattern::new("/index.json"));
// resource.name("r1");
// router.register_resource(resource);
// let mut resource = Resource::new(ResourcePattern::new("/test.json"));
// resource.name("r2");
// router.register_resource(resource);
// let req = TestRequest::with_uri("/index.json").finish();
// let info = router.recognize(&req, &(), 0);
// assert_eq!(info.resource, ResourceId::Normal(0));
// assert_eq!(info.name(), "r1");
// let req = TestRequest::with_uri("/test.json").finish();
// let info = router.recognize(&req, &(), 0);
// assert_eq!(info.resource, ResourceId::Normal(1));
// assert_eq!(info.name(), "r2");
// }
// #[test]
// fn test_has_resource() {
// let mut router = Router::<()>::default();
// let scope = Scope::new("/test").resource("/name", |_| "done");
// router.register_scope(scope);
// {
// let info = router.default_route_info();
// assert!(!info.has_resource("/test"));
// assert!(info.has_resource("/test/name"));
// }
// let scope = Scope::new("/test2").nested("/test10", |s| s.resource("/name", |_| "done"));
// router.register_scope(scope);
// let info = router.default_route_info();
// assert!(info.has_resource("/test2/test10/name"));
// }
// #[test]
// fn test_url_for() {
// let mut router = Router::<()>::new(ResourcePattern::prefix(""));
// let mut resource = Resource::new(ResourcePattern::new("/tttt"));
// resource.name("r0");
// router.register_resource(resource);
// let scope = Scope::new("/test").resource("/name", |r| {
// r.name("r1");
// });
// router.register_scope(scope);
// let scope =
// Scope::new("/test2").nested("/test10", |s| s.resource("/name", |r| r.name("r2")));
// router.register_scope(scope);
// router.finish();
// let req = TestRequest::with_uri("/test").request();
// {
// let info = router.default_route_info();
// let res = info
// .url_for(&req, "r0", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/tttt");
// let res = info
// .url_for(&req, "r1", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/test/name");
// let res = info
// .url_for(&req, "r2", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/test2/test10/name");
// }
// let req = TestRequest::with_uri("/test/name").request();
// let info = router.recognize(&req, &(), 0);
// assert_eq!(info.resource, ResourceId::Normal(1));
// let res = info
// .url_for(&req, "r0", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/tttt");
// let res = info
// .url_for(&req, "r1", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/test/name");
// let res = info
// .url_for(&req, "r2", Vec::<&'static str>::new())
// .unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/test2/test10/name");
// }
// #[test]
// fn test_url_for_dynamic() {
// let mut router = Router::<()>::new(ResourcePattern::prefix(""));
// let mut resource = Resource::new(ResourcePattern::new("/{name}/test/index.{ext}"));
// resource.name("r0");
// router.register_resource(resource);
// let scope = Scope::new("/{name1}").nested("/{name2}", |s| {
// s.resource("/{name3}/test/index.{ext}", |r| r.name("r2"))
// });
// router.register_scope(scope);
// router.finish();
// let req = TestRequest::with_uri("/test").request();
// {
// let info = router.default_route_info();
// let res = info.url_for(&req, "r0", vec!["sec1", "html"]).unwrap();
// assert_eq!(res.as_str(), "http://localhost:8080/sec1/test/index.html");
// let res = info
// .url_for(&req, "r2", vec!["sec1", "sec2", "sec3", "html"])
// .unwrap();
// assert_eq!(
// res.as_str(),
// "http://localhost:8080/sec1/sec2/sec3/test/index.html"
// );
// }
// }
}