mirror of
https://github.com/fafhrd91/actix-net
synced 2025-08-13 04:58:21 +02:00
Compare commits
15 Commits
utils-0.3.
...
utils-v0.3
Author | SHA1 | Date | |
---|---|---|---|
|
15dafeff3d | ||
|
ed14e6b8ea | ||
|
700abc997e | ||
|
8c48bf4de7 | ||
|
9bc492cf6c | ||
|
d2a223e69e | ||
|
e9657a399a | ||
|
9f25fdf929 | ||
|
82930de8e7 | ||
|
04a3e59bd5 | ||
|
0ff0daa795 | ||
|
fb43940824 | ||
|
0410f59cf5 | ||
|
400023a07b | ||
|
2e4c84dbb6 |
@@ -1,8 +1,15 @@
|
||||
# Changes
|
||||
|
||||
## [0.3.0] - 2019-03-02
|
||||
|
||||
### Changed
|
||||
|
||||
* Migrate to actix-service 0.3
|
||||
|
||||
|
||||
## [0.2.0] - 2019-02-01
|
||||
|
||||
### Changes
|
||||
### Changed
|
||||
|
||||
* Migrate to actix-service 0.2
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-connector"
|
||||
version = "0.2.0"
|
||||
version = "0.3.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix Connector - tcp connector service"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
@@ -11,7 +11,7 @@ categories = ["network-programming", "asynchronous"]
|
||||
license = "MIT/Apache-2.0"
|
||||
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
||||
edition = "2018"
|
||||
workspace = "../"
|
||||
workspace = ".."
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = ["ssl"]
|
||||
@@ -27,8 +27,7 @@ default = []
|
||||
ssl = ["openssl", "tokio-openssl"]
|
||||
|
||||
[dependencies]
|
||||
#actix-service = "0.2.0"
|
||||
actix-service = { path="../actix-service" }
|
||||
actix-service = "0.3.0"
|
||||
actix-codec = "0.1.0"
|
||||
futures = "0.1"
|
||||
tokio-tcp = "0.1"
|
||||
|
@@ -1,15 +1,38 @@
|
||||
# Changes
|
||||
|
||||
## [0.3.1] - 2019-03-xx
|
||||
|
||||
### Added
|
||||
|
||||
* Add `ServerBuilder::maxconnrate` sets the maximum per-worker number of concurrent connections
|
||||
|
||||
* Add helper ssl error `SslError`
|
||||
|
||||
|
||||
### Changed
|
||||
|
||||
* Rename `StreamServiceFactory` to `ServiceFactory`
|
||||
|
||||
* Deprecate `StreamServiceFactory`
|
||||
|
||||
|
||||
## [0.3.0] - 2019-03-02
|
||||
|
||||
### Changed
|
||||
|
||||
* Use new `NewService` trait
|
||||
|
||||
|
||||
## [0.2.1] - 2019-02-09
|
||||
|
||||
### Changes
|
||||
### Changed
|
||||
|
||||
* Drop service response
|
||||
|
||||
|
||||
## [0.2.0] - 2019-02-01
|
||||
|
||||
### Changes
|
||||
### Changed
|
||||
|
||||
* Migrate to actix-service 0.2
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-server"
|
||||
version = "0.2.1"
|
||||
version = "0.3.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix server - General purpose tcp server"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
@@ -11,7 +11,7 @@ categories = ["network-programming", "asynchronous"]
|
||||
license = "MIT/Apache-2.0"
|
||||
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
||||
edition = "2018"
|
||||
workspace = "../"
|
||||
workspace = ".."
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = ["ssl", "tls", "rust-tls"]
|
||||
@@ -33,17 +33,14 @@ ssl = ["openssl", "tokio-openssl"]
|
||||
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
||||
|
||||
[dependencies]
|
||||
#actix-service = "0.2.1"
|
||||
actix-service = { path="../actix-service" }
|
||||
actix-service = "0.3.0"
|
||||
actix-rt = "0.1.0"
|
||||
|
||||
log = "0.4"
|
||||
num_cpus = "1.0"
|
||||
|
||||
# io
|
||||
mio = "^0.6.13"
|
||||
net2 = "0.2"
|
||||
bytes = "0.4"
|
||||
futures = "0.1"
|
||||
slab = "0.4"
|
||||
tokio-io = "0.1"
|
||||
|
@@ -14,10 +14,10 @@ use tokio_timer::sleep;
|
||||
use crate::accept::{AcceptLoop, AcceptNotify, Command};
|
||||
use crate::config::{ConfiguredService, ServiceConfig};
|
||||
use crate::server::{Server, ServerCommand};
|
||||
use crate::services::{InternalServiceFactory, StreamNewService, StreamServiceFactory};
|
||||
use crate::services::{InternalServiceFactory, ServiceFactory, StreamNewService};
|
||||
use crate::signals::{Signal, Signals};
|
||||
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};
|
||||
use crate::Token;
|
||||
use crate::{ssl, Token};
|
||||
|
||||
/// Server builder
|
||||
pub struct ServerBuilder {
|
||||
@@ -81,9 +81,18 @@ impl ServerBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
/// Stop actix system.
|
||||
/// Sets the maximum per-worker concurrent connection establish process.
|
||||
///
|
||||
/// `SystemExit` message stops currently running system.
|
||||
/// All listeners will stop accepting connections when this limit is reached. It
|
||||
/// can be used to limit the global SSL CPU usage.
|
||||
///
|
||||
/// By default max connections is set to a 256.
|
||||
pub fn maxconnrate(self, num: usize) -> Self {
|
||||
ssl::max_concurrent_ssl_connect(num);
|
||||
self
|
||||
}
|
||||
|
||||
/// Stop actix system.
|
||||
pub fn system_exit(mut self) -> Self {
|
||||
self.exit = true;
|
||||
self
|
||||
@@ -137,7 +146,7 @@ impl ServerBuilder {
|
||||
/// Add new service to the server.
|
||||
pub fn bind<F, U, N: AsRef<str>>(mut self, name: N, addr: U, factory: F) -> io::Result<Self>
|
||||
where
|
||||
F: StreamServiceFactory,
|
||||
F: ServiceFactory,
|
||||
U: net::ToSocketAddrs,
|
||||
{
|
||||
let sockets = bind_addr(addr)?;
|
||||
@@ -163,7 +172,7 @@ impl ServerBuilder {
|
||||
factory: F,
|
||||
) -> Self
|
||||
where
|
||||
F: StreamServiceFactory,
|
||||
F: ServiceFactory,
|
||||
{
|
||||
let token = self.token.next();
|
||||
self.services.push(StreamNewService::create(
|
||||
@@ -183,9 +192,6 @@ impl ServerBuilder {
|
||||
/// This methods panics if no socket addresses get bound.
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// # extern crate futures;
|
||||
/// # extern crate actix_web;
|
||||
/// # use futures::Future;
|
||||
/// use actix_web::*;
|
||||
///
|
||||
/// fn main() {
|
||||
|
@@ -13,7 +13,10 @@ mod worker;
|
||||
pub use self::builder::ServerBuilder;
|
||||
pub use self::config::{ServiceConfig, ServiceRuntime};
|
||||
pub use self::server::Server;
|
||||
pub use self::services::StreamServiceFactory;
|
||||
pub use self::services::ServiceFactory;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub use self::services::ServiceFactory as StreamServiceFactory;
|
||||
|
||||
/// Socket id token
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
@@ -28,6 +31,6 @@ impl Token {
|
||||
}
|
||||
|
||||
/// Start server building process
|
||||
pub fn build() -> ServerBuilder {
|
||||
pub fn new() -> ServerBuilder {
|
||||
ServerBuilder::default()
|
||||
}
|
||||
|
@@ -22,7 +22,7 @@ pub(crate) enum ServerMessage {
|
||||
ForceShutdown,
|
||||
}
|
||||
|
||||
pub trait StreamServiceFactory: Send + Clone + 'static {
|
||||
pub trait ServiceFactory: Send + Clone + 'static {
|
||||
type NewService: NewService<Request = TcpStream>;
|
||||
|
||||
fn create(&self) -> Self::NewService;
|
||||
@@ -92,7 +92,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct StreamNewService<F: StreamServiceFactory> {
|
||||
pub(crate) struct StreamNewService<F: ServiceFactory> {
|
||||
name: String,
|
||||
inner: F,
|
||||
token: Token,
|
||||
@@ -100,7 +100,7 @@ pub(crate) struct StreamNewService<F: StreamServiceFactory> {
|
||||
|
||||
impl<F> StreamNewService<F>
|
||||
where
|
||||
F: StreamServiceFactory,
|
||||
F: ServiceFactory,
|
||||
{
|
||||
pub(crate) fn create(name: String, token: Token, inner: F) -> Box<InternalServiceFactory> {
|
||||
Box::new(Self { name, token, inner })
|
||||
@@ -109,7 +109,7 @@ where
|
||||
|
||||
impl<F> InternalServiceFactory for StreamNewService<F>
|
||||
where
|
||||
F: StreamServiceFactory,
|
||||
F: ServiceFactory,
|
||||
{
|
||||
fn name(&self, _: Token) -> &str {
|
||||
&self.name
|
||||
@@ -152,7 +152,7 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T> StreamServiceFactory for F
|
||||
impl<F, T> ServiceFactory for F
|
||||
where
|
||||
F: Fn() -> T + Send + Clone + 'static,
|
||||
T: NewService<Request = TcpStream>,
|
||||
|
@@ -33,3 +33,9 @@ pub(crate) static MAX_CONN: AtomicUsize = AtomicUsize::new(256);
|
||||
thread_local! {
|
||||
static MAX_CONN_COUNTER: Counter = Counter::new(MAX_CONN.load(Ordering::Relaxed));
|
||||
}
|
||||
|
||||
/// Ssl error combinded with service error.
|
||||
pub enum SslError<E1, E2> {
|
||||
Ssl(E1),
|
||||
Service(E2),
|
||||
}
|
||||
|
@@ -1,5 +1,21 @@
|
||||
# Changes
|
||||
|
||||
## [0.3.2] - 2019-03-04
|
||||
|
||||
### Changed
|
||||
|
||||
* Change `NewService::Future` and `Transform::Future` to the `IntoFuture` trait.
|
||||
|
||||
* Export `AndThenTransform` type
|
||||
|
||||
|
||||
## [0.3.1] - 2019-03-04
|
||||
|
||||
### Changed
|
||||
|
||||
* Simplify Transform trait
|
||||
|
||||
|
||||
## [0.3.0] - 2019-03-02
|
||||
|
||||
## Added
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-service"
|
||||
version = "0.3.0"
|
||||
version = "0.3.2"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix Service"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::{try_ready, Async, Future, Poll};
|
||||
use futures::{try_ready, Async, Future, IntoFuture, Poll};
|
||||
|
||||
use super::{IntoNewService, NewService, Service};
|
||||
use crate::cell::Cell;
|
||||
@@ -142,7 +142,10 @@ where
|
||||
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))
|
||||
AndThenNewServiceFuture::new(
|
||||
self.a.new_service(cfg).into_future(),
|
||||
self.b.new_service(cfg).into_future(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,8 +168,8 @@ where
|
||||
A: NewService<C>,
|
||||
B: NewService<C, Request = A::Response>,
|
||||
{
|
||||
fut_b: B::Future,
|
||||
fut_a: A::Future,
|
||||
fut_b: <B::Future as IntoFuture>::Future,
|
||||
fut_a: <A::Future as IntoFuture>::Future,
|
||||
a: Option<A::Service>,
|
||||
b: Option<B::Service>,
|
||||
}
|
||||
@@ -176,7 +179,10 @@ where
|
||||
A: NewService<C>,
|
||||
B: NewService<C, Request = A::Response>,
|
||||
{
|
||||
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
||||
fn new(
|
||||
fut_a: <A::Future as IntoFuture>::Future,
|
||||
fut_b: <B::Future as IntoFuture>::Future,
|
||||
) -> Self {
|
||||
AndThenNewServiceFuture {
|
||||
fut_a,
|
||||
fut_b,
|
||||
|
@@ -1,141 +1,24 @@
|
||||
use futures::{Async, Future, Poll};
|
||||
use std::rc::Rc;
|
||||
|
||||
use super::{NewService, NewTransform, Service, Transform};
|
||||
use crate::cell::Cell;
|
||||
use futures::{Async, Future, IntoFuture, Poll};
|
||||
|
||||
/// `Apply` service combinator
|
||||
pub struct AndThenTransform<T, A, B>
|
||||
where
|
||||
A: Service,
|
||||
B: Service<Error = A::Error>,
|
||||
T: Transform<B, Request = A::Response>,
|
||||
T::Error: From<A::Error>,
|
||||
{
|
||||
a: A,
|
||||
b: Cell<B>,
|
||||
t: Cell<T>,
|
||||
}
|
||||
|
||||
impl<T, A, B> AndThenTransform<T, A, B>
|
||||
where
|
||||
A: Service,
|
||||
B: Service<Error = A::Error>,
|
||||
T: Transform<B, Request = A::Response>,
|
||||
T::Error: From<A::Error>,
|
||||
{
|
||||
/// Create new `Apply` combinator
|
||||
pub fn new(t: T, a: A, b: B) -> Self {
|
||||
Self {
|
||||
a,
|
||||
b: Cell::new(b),
|
||||
t: Cell::new(t),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, A, B> Clone for AndThenTransform<T, A, B>
|
||||
where
|
||||
A: Service + Clone,
|
||||
B: Service<Error = A::Error>,
|
||||
T: Transform<B, Request = A::Response>,
|
||||
T::Error: From<A::Error>,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
AndThenTransform {
|
||||
a: self.a.clone(),
|
||||
b: self.b.clone(),
|
||||
t: self.t.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, A, B> Service for AndThenTransform<T, A, B>
|
||||
where
|
||||
A: Service,
|
||||
B: Service<Error = A::Error>,
|
||||
T: Transform<B, Request = A::Response>,
|
||||
T::Error: From<A::Error>,
|
||||
{
|
||||
type Request = A::Request;
|
||||
type Response = T::Response;
|
||||
type Error = T::Error;
|
||||
type Future = AndThenTransformFuture<T, A, B>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
let notready = Async::NotReady == self.a.poll_ready()?;
|
||||
let notready = Async::NotReady == self.b.get_mut().poll_ready()? || notready;
|
||||
let notready = Async::NotReady == self.t.get_mut().poll_ready()? || notready;
|
||||
|
||||
if notready {
|
||||
Ok(Async::NotReady)
|
||||
} else {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
}
|
||||
|
||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||
AndThenTransformFuture {
|
||||
b: self.b.clone(),
|
||||
t: self.t.clone(),
|
||||
fut_t: None,
|
||||
fut_a: Some(self.a.call(req)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AndThenTransformFuture<T, A, B>
|
||||
where
|
||||
A: Service,
|
||||
B: Service<Error = A::Error>,
|
||||
T: Transform<B, Request = A::Response>,
|
||||
T::Error: From<A::Error>,
|
||||
{
|
||||
b: Cell<B>,
|
||||
t: Cell<T>,
|
||||
fut_a: Option<A::Future>,
|
||||
fut_t: Option<T::Future>,
|
||||
}
|
||||
|
||||
impl<T, A, B> Future for AndThenTransformFuture<T, A, B>
|
||||
where
|
||||
A: Service,
|
||||
B: Service<Error = A::Error>,
|
||||
T: Transform<B, Request = A::Response>,
|
||||
T::Error: From<A::Error>,
|
||||
{
|
||||
type Item = T::Response;
|
||||
type Error = T::Error;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if let Some(ref mut fut) = self.fut_t {
|
||||
return fut.poll();
|
||||
}
|
||||
|
||||
match self.fut_a.as_mut().expect("Bug in actix-service").poll() {
|
||||
Ok(Async::Ready(resp)) => {
|
||||
let _ = self.fut_a.take();
|
||||
self.fut_t = Some(self.t.get_mut().call(resp, self.b.get_mut()));
|
||||
self.poll()
|
||||
}
|
||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
||||
Err(err) => Err(err.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::and_then::AndThen;
|
||||
use crate::from_err::FromErr;
|
||||
use crate::{NewService, Transform};
|
||||
|
||||
/// `Apply` new service combinator
|
||||
pub struct AndThenTransformNewService<T, A, B, C> {
|
||||
pub struct AndThenTransform<T, A, B, C> {
|
||||
a: A,
|
||||
b: B,
|
||||
t: T,
|
||||
t: Rc<T>,
|
||||
_t: std::marker::PhantomData<C>,
|
||||
}
|
||||
|
||||
impl<T, A, B, C> AndThenTransformNewService<T, A, B, C>
|
||||
impl<T, A, B, C> AndThenTransform<T, A, B, C>
|
||||
where
|
||||
A: NewService<C>,
|
||||
B: NewService<C, Error = A::Error, InitError = A::InitError>,
|
||||
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
|
||||
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
|
||||
@@ -143,17 +26,16 @@ where
|
||||
Self {
|
||||
a,
|
||||
b,
|
||||
t,
|
||||
t: Rc::new(t),
|
||||
_t: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, A, B, C> Clone for AndThenTransformNewService<T, A, B, C>
|
||||
impl<T, A, B, C> Clone for AndThenTransform<T, A, B, C>
|
||||
where
|
||||
A: Clone,
|
||||
B: Clone,
|
||||
T: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
@@ -165,11 +47,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, A, B, C> NewService<C> for AndThenTransformNewService<T, A, B, C>
|
||||
impl<T, A, B, C> NewService<C> for AndThenTransform<T, A, B, C>
|
||||
where
|
||||
A: NewService<C>,
|
||||
B: NewService<C, Error = A::Error, InitError = A::InitError>,
|
||||
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
|
||||
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;
|
||||
@@ -177,50 +59,50 @@ where
|
||||
type Error = T::Error;
|
||||
|
||||
type InitError = T::InitError;
|
||||
type Service = AndThenTransform<T::Transform, A::Service, B::Service>;
|
||||
type Future = AndThenTransformNewServiceFuture<T, A, B, C>;
|
||||
type Service = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
|
||||
type Future = AndThenTransformFuture<T, A, B, C>;
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
AndThenTransformNewServiceFuture {
|
||||
AndThenTransformFuture {
|
||||
a: None,
|
||||
b: None,
|
||||
t: None,
|
||||
fut_a: self.a.new_service(cfg),
|
||||
fut_b: self.b.new_service(cfg),
|
||||
fut_t: self.t.new_transform(cfg),
|
||||
t_cell: self.t.clone(),
|
||||
fut_a: self.a.new_service(cfg).into_future(),
|
||||
fut_b: self.b.new_service(cfg).into_future(),
|
||||
fut_t: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AndThenTransformNewServiceFuture<T, A, B, C>
|
||||
pub struct AndThenTransformFuture<T, A, B, C>
|
||||
where
|
||||
A: NewService<C>,
|
||||
B: NewService<C, Error = A::Error, InitError = A::InitError>,
|
||||
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
|
||||
B: NewService<C, InitError = A::InitError>,
|
||||
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
|
||||
T::Error: From<A::Error>,
|
||||
{
|
||||
fut_b: B::Future,
|
||||
fut_a: A::Future,
|
||||
fut_t: T::Future,
|
||||
fut_a: <A::Future as IntoFuture>::Future,
|
||||
fut_b: <B::Future as IntoFuture>::Future,
|
||||
fut_t: Option<<T::Future as IntoFuture>::Future>,
|
||||
a: Option<A::Service>,
|
||||
b: Option<B::Service>,
|
||||
t: Option<T::Transform>,
|
||||
t_cell: Rc<T>,
|
||||
}
|
||||
|
||||
impl<T, A, B, C> Future for AndThenTransformNewServiceFuture<T, A, B, C>
|
||||
impl<T, A, B, C> Future for AndThenTransformFuture<T, A, B, C>
|
||||
where
|
||||
A: NewService<C>,
|
||||
B: NewService<C, Error = A::Error, InitError = A::InitError>,
|
||||
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
|
||||
B: NewService<C, InitError = A::InitError>,
|
||||
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
|
||||
T::Error: From<A::Error>,
|
||||
{
|
||||
type Item = AndThenTransform<T::Transform, A::Service, B::Service>;
|
||||
type Item = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
|
||||
type Error = T::InitError;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if self.t.is_none() {
|
||||
if let Async::Ready(transform) = self.fut_t.poll()? {
|
||||
self.t = Some(transform);
|
||||
if self.fut_t.is_none() {
|
||||
if let Async::Ready(service) = self.fut_b.poll()? {
|
||||
self.fut_t = Some(self.t_cell.new_transform(service).into_future());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,18 +112,17 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
if self.b.is_none() {
|
||||
if let Async::Ready(service) = self.fut_b.poll()? {
|
||||
self.b = Some(service);
|
||||
if let Some(ref mut fut) = self.fut_t {
|
||||
if let Async::Ready(transform) = fut.poll()? {
|
||||
self.t = Some(transform);
|
||||
}
|
||||
}
|
||||
|
||||
if self.a.is_some() && self.b.is_some() && self.t.is_some() {
|
||||
Ok(Async::Ready(AndThenTransform {
|
||||
a: self.a.take().unwrap(),
|
||||
t: Cell::new(self.t.take().unwrap()),
|
||||
b: Cell::new(self.b.take().unwrap()),
|
||||
}))
|
||||
if self.a.is_some() && self.t.is_some() {
|
||||
Ok(Async::Ready(AndThen::new(
|
||||
FromErr::new(self.a.take().unwrap()),
|
||||
self.t.take().unwrap(),
|
||||
)))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
@@ -276,10 +157,11 @@ mod tests {
|
||||
fn test_apply() {
|
||||
let blank = |req| Ok(req);
|
||||
|
||||
let mut srv = blank.into_service().apply(
|
||||
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|
||||
Srv,
|
||||
);
|
||||
let mut srv = blank
|
||||
.into_service()
|
||||
.apply_fn(Srv, |req: &'static str, srv: &mut Srv| {
|
||||
srv.call(()).map(move |res| (req, res))
|
||||
});
|
||||
assert!(srv.poll_ready().is_ok());
|
||||
let res = srv.call("srv").poll();
|
||||
assert!(res.is_ok());
|
||||
|
@@ -195,8 +195,8 @@ where
|
||||
a: None,
|
||||
b: None,
|
||||
f: self.f.clone(),
|
||||
fut_a: self.a.new_service(cfg),
|
||||
fut_b: self.b.new_service(cfg),
|
||||
fut_a: self.a.new_service(cfg).into_future(),
|
||||
fut_b: self.b.new_service(cfg).into_future(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -209,8 +209,8 @@ where
|
||||
Out: IntoFuture,
|
||||
Out::Error: Into<A::Error>,
|
||||
{
|
||||
fut_b: B::Future,
|
||||
fut_a: A::Future,
|
||||
fut_b: <B::Future as IntoFuture>::Future,
|
||||
fut_a: <A::Future as IntoFuture>::Future,
|
||||
f: Cell<F>,
|
||||
a: Option<A::Service>,
|
||||
b: Option<B::Service>,
|
||||
|
@@ -1,218 +1,172 @@
|
||||
use futures::{try_ready, Async, Future, IntoFuture, Poll};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use super::{FnNewTransform, FnTransform};
|
||||
use super::{
|
||||
IntoNewService, IntoNewTransform, IntoService, IntoTransform, NewService, NewTransform,
|
||||
Service, Transform,
|
||||
};
|
||||
use futures::{Async, Future, IntoFuture, Poll};
|
||||
|
||||
use super::{IntoNewService, IntoService, NewService, Service};
|
||||
|
||||
/// `Apply` service combinator
|
||||
pub struct Apply<T, S>
|
||||
pub struct Apply<T, F, In, Out>
|
||||
where
|
||||
T: Transform<S>,
|
||||
T::Error: From<S::Error>,
|
||||
S: Service,
|
||||
T: Service,
|
||||
{
|
||||
transform: T,
|
||||
service: S,
|
||||
service: T,
|
||||
f: F,
|
||||
r: PhantomData<(In, Out)>,
|
||||
}
|
||||
|
||||
impl<T, S> Apply<T, S>
|
||||
impl<T, F, In, Out> Apply<T, F, In, Out>
|
||||
where
|
||||
T: Transform<S>,
|
||||
T::Error: From<S::Error>,
|
||||
S: Service,
|
||||
{
|
||||
/// Create new `Apply` combinator
|
||||
pub fn new<T1: IntoTransform<T, S>, S1: IntoService<S>>(
|
||||
transform: T1,
|
||||
service: S1,
|
||||
) -> Self {
|
||||
Self {
|
||||
transform: transform.into_transform(),
|
||||
service: service.into_service(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, S, Req, Out> Apply<FnTransform<F, S, Req, Out>, S>
|
||||
where
|
||||
F: FnMut(Req, &mut S) -> Out,
|
||||
T: Service,
|
||||
F: FnMut(In, &mut T) -> Out,
|
||||
Out: IntoFuture,
|
||||
Out::Error: From<S::Error>,
|
||||
S: Service,
|
||||
Out::Error: From<T::Error>,
|
||||
{
|
||||
/// Create new `Apply` combinator
|
||||
pub fn new_fn<S1: IntoService<S>>(service: S1, transform: F) -> Self {
|
||||
pub fn new<I: IntoService<T>>(service: I, f: F) -> Self {
|
||||
Self {
|
||||
service: service.into_service(),
|
||||
transform: transform.into_transform(),
|
||||
f,
|
||||
r: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Clone for Apply<T, S>
|
||||
impl<T, F, In, Out> Clone for Apply<T, F, In, Out>
|
||||
where
|
||||
S: Service + Clone,
|
||||
T::Error: From<S::Error>,
|
||||
T: Transform<S> + Clone,
|
||||
T: Service + Clone,
|
||||
F: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Apply {
|
||||
service: self.service.clone(),
|
||||
transform: self.transform.clone(),
|
||||
f: self.f.clone(),
|
||||
r: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Service for Apply<T, S>
|
||||
impl<T, F, In, Out> Service for Apply<T, F, In, Out>
|
||||
where
|
||||
T: Transform<S>,
|
||||
T::Error: From<S::Error>,
|
||||
S: Service,
|
||||
T: Service,
|
||||
F: FnMut(In, &mut T) -> Out,
|
||||
Out: IntoFuture,
|
||||
Out::Error: From<T::Error>,
|
||||
{
|
||||
type Request = T::Request;
|
||||
type Response = T::Response;
|
||||
type Error = T::Error;
|
||||
type Future = T::Future;
|
||||
type Request = In;
|
||||
type Response = Out::Item;
|
||||
type Error = Out::Error;
|
||||
type Future = Out::Future;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
try_ready!(self.service.poll_ready());
|
||||
self.transform.poll_ready()
|
||||
self.service.poll_ready().map_err(|e| e.into())
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
self.transform.call(req, &mut self.service).into_future()
|
||||
fn call(&mut self, req: In) -> Self::Future {
|
||||
(self.f)(req, &mut self.service).into_future()
|
||||
}
|
||||
}
|
||||
|
||||
/// `ApplyNewService` new service combinator
|
||||
pub struct ApplyNewService<T, S, C>
|
||||
pub struct ApplyNewService<T, F, In, Out, Cfg>
|
||||
where
|
||||
T: NewTransform<S::Service, C, InitError = S::InitError>,
|
||||
T::Error: From<S::Error>,
|
||||
S: NewService<C>,
|
||||
T: NewService<Cfg>,
|
||||
{
|
||||
transform: T,
|
||||
service: S,
|
||||
_t: std::marker::PhantomData<C>,
|
||||
service: T,
|
||||
f: F,
|
||||
r: PhantomData<(In, Out, Cfg)>,
|
||||
}
|
||||
|
||||
impl<T, S, C> ApplyNewService<T, S, C>
|
||||
impl<T, F, In, Out, Cfg> ApplyNewService<T, F, In, Out, Cfg>
|
||||
where
|
||||
T: NewTransform<S::Service, C, InitError = S::InitError>,
|
||||
T::Error: From<S::Error>,
|
||||
S: NewService<C>,
|
||||
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<T1: IntoNewTransform<T, S::Service, C>, S1: IntoNewService<S, C>>(
|
||||
transform: T1,
|
||||
service: S1,
|
||||
) -> Self {
|
||||
pub fn new<F1: IntoNewService<T, Cfg>>(service: F1, f: F) -> Self {
|
||||
Self {
|
||||
transform: transform.into_new_transform(),
|
||||
f,
|
||||
service: service.into_new_service(),
|
||||
_t: std::marker::PhantomData,
|
||||
r: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, S, In, Out, Cfg>
|
||||
ApplyNewService<FnNewTransform<F, S::Service, In, Out, S::InitError, Cfg>, S, Cfg>
|
||||
impl<T, F, In, Out, Cfg> Clone for ApplyNewService<T, F, In, Out, Cfg>
|
||||
where
|
||||
F: FnMut(In, &mut S::Service) -> Out + Clone,
|
||||
T: NewService<Cfg> + Clone,
|
||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||
Out: IntoFuture,
|
||||
Out::Error: From<S::Error>,
|
||||
S: NewService<Cfg>,
|
||||
{
|
||||
/// Create new `Apply` combinator factory
|
||||
pub fn new_fn<S1: IntoNewService<S, Cfg>>(service: S1, transform: F) -> Self {
|
||||
Self {
|
||||
service: service.into_new_service(),
|
||||
transform: FnNewTransform::new(transform),
|
||||
_t: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, C> Clone for ApplyNewService<T, S, C>
|
||||
where
|
||||
T: NewTransform<S::Service, C, InitError = S::InitError> + Clone,
|
||||
T::Error: From<S::Error>,
|
||||
S: NewService<C> + Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
service: self.service.clone(),
|
||||
transform: self.transform.clone(),
|
||||
_t: std::marker::PhantomData,
|
||||
f: self.f.clone(),
|
||||
r: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, C> NewService<C> for ApplyNewService<T, S, C>
|
||||
impl<T, F, In, Out, Cfg> NewService<Cfg> for ApplyNewService<T, F, In, Out, Cfg>
|
||||
where
|
||||
T: NewTransform<S::Service, C, InitError = S::InitError>,
|
||||
T::Error: From<S::Error>,
|
||||
S: NewService<C>,
|
||||
T: NewService<Cfg>,
|
||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||
Out: IntoFuture,
|
||||
Out::Error: From<T::Error>,
|
||||
{
|
||||
type Request = T::Request;
|
||||
type Response = T::Response;
|
||||
type Error = T::Error;
|
||||
type Service = Apply<T::Transform, S::Service>;
|
||||
type Request = In;
|
||||
type Response = Out::Item;
|
||||
type Error = Out::Error;
|
||||
type Service = Apply<T::Service, F, In, Out>;
|
||||
|
||||
type InitError = T::InitError;
|
||||
type Future = ApplyNewServiceFuture<T, S, C>;
|
||||
type Future = ApplyNewServiceFuture<T, F, In, Out, Cfg>;
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
fn new_service(&self, cfg: &Cfg) -> Self::Future {
|
||||
ApplyNewServiceFuture::new(self.service.new_service(cfg).into_future(), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ApplyNewServiceFuture<T, F, In, Out, Cfg>
|
||||
where
|
||||
T: NewService<Cfg>,
|
||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||
Out: IntoFuture,
|
||||
{
|
||||
fut: <T::Future as IntoFuture>::Future,
|
||||
f: Option<F>,
|
||||
r: PhantomData<(In, Out)>,
|
||||
}
|
||||
|
||||
impl<T, F, In, Out, Cfg> ApplyNewServiceFuture<T, F, In, Out, Cfg>
|
||||
where
|
||||
T: NewService<Cfg>,
|
||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||
Out: IntoFuture,
|
||||
{
|
||||
fn new(fut: <T::Future as IntoFuture>::Future, f: F) -> Self {
|
||||
ApplyNewServiceFuture {
|
||||
fut_t: self.transform.new_transform(cfg),
|
||||
fut_s: self.service.new_service(cfg),
|
||||
service: None,
|
||||
transform: None,
|
||||
f: Some(f),
|
||||
fut,
|
||||
r: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ApplyNewServiceFuture<T, S, C>
|
||||
impl<T, F, In, Out, Cfg> Future for ApplyNewServiceFuture<T, F, In, Out, Cfg>
|
||||
where
|
||||
T: NewTransform<S::Service, C, InitError = S::InitError>,
|
||||
T::Error: From<S::Error>,
|
||||
S: NewService<C>,
|
||||
T: NewService<Cfg>,
|
||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||
Out: IntoFuture,
|
||||
Out::Error: From<T::Error>,
|
||||
{
|
||||
fut_s: S::Future,
|
||||
fut_t: T::Future,
|
||||
service: Option<S::Service>,
|
||||
transform: Option<T::Transform>,
|
||||
}
|
||||
|
||||
impl<T, S, C> Future for ApplyNewServiceFuture<T, S, C>
|
||||
where
|
||||
T: NewTransform<S::Service, C, InitError = S::InitError>,
|
||||
T::Error: From<S::Error>,
|
||||
S: NewService<C>,
|
||||
{
|
||||
type Item = Apply<T::Transform, S::Service>;
|
||||
type Item = Apply<T::Service, F, In, Out>;
|
||||
type Error = T::InitError;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if self.transform.is_none() {
|
||||
if let Async::Ready(transform) = self.fut_t.poll()? {
|
||||
self.transform = Some(transform);
|
||||
}
|
||||
}
|
||||
|
||||
if self.service.is_none() {
|
||||
if let Async::Ready(service) = self.fut_s.poll()? {
|
||||
self.service = Some(service);
|
||||
}
|
||||
}
|
||||
|
||||
if self.transform.is_some() && self.service.is_some() {
|
||||
Ok(Async::Ready(Apply {
|
||||
service: self.service.take().unwrap(),
|
||||
transform: self.transform.take().unwrap(),
|
||||
}))
|
||||
if let Async::Ready(service) = self.fut.poll()? {
|
||||
Ok(Async::Ready(Apply::new(service, self.f.take().unwrap())))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
@@ -225,7 +179,7 @@ mod tests {
|
||||
use futures::{Async, Future, Poll};
|
||||
|
||||
use super::*;
|
||||
use crate::{NewService, Service};
|
||||
use crate::{IntoService, NewService, Service, ServiceExt};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Srv;
|
||||
@@ -245,10 +199,14 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply() {
|
||||
let mut srv = Apply::new_fn(Srv, |req: &'static str, srv| {
|
||||
srv.call(()).map(move |res| (req, res))
|
||||
});
|
||||
fn test_call() {
|
||||
let blank = |req| Ok(req);
|
||||
|
||||
let mut srv = blank
|
||||
.into_service()
|
||||
.apply_fn(Srv, |req: &'static str, srv| {
|
||||
srv.call(()).map(move |res| (req, res))
|
||||
});
|
||||
assert!(srv.poll_ready().is_ok());
|
||||
let res = srv.call("srv").poll();
|
||||
assert!(res.is_ok());
|
||||
@@ -258,22 +216,6 @@ mod tests {
|
||||
#[test]
|
||||
fn test_new_service() {
|
||||
let new_srv = ApplyNewService::new(
|
||||
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|
||||
|| Ok::<_, ()>(Srv),
|
||||
);
|
||||
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
|
||||
assert!(srv.poll_ready().is_ok());
|
||||
let res = srv.call("srv").poll();
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
|
||||
} else {
|
||||
panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_service_fn() {
|
||||
let new_srv = ApplyNewService::new_fn(
|
||||
|| Ok::<_, ()>(Srv),
|
||||
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
|
||||
);
|
||||
|
@@ -1,5 +1,5 @@
|
||||
use crate::{NewService, Service};
|
||||
use futures::{Future, Poll};
|
||||
use futures::{Future, IntoFuture, Poll};
|
||||
|
||||
pub type BoxedService<Req, Res, Err> = Box<
|
||||
Service<
|
||||
@@ -99,7 +99,8 @@ where
|
||||
Box::new(
|
||||
self.service
|
||||
.new_service(cfg)
|
||||
.map(|service| ServiceWrapper::boxed(service)),
|
||||
.into_future()
|
||||
.map(ServiceWrapper::boxed),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@@ -1,122 +1,64 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::future::{ok, FutureResult};
|
||||
use futures::{Async, IntoFuture, Poll};
|
||||
use futures::IntoFuture;
|
||||
|
||||
use crate::{IntoNewTransform, IntoTransform, NewTransform, Transform};
|
||||
use crate::{Apply, IntoTransform, Service, Transform};
|
||||
|
||||
pub struct FnTransform<F, S, Req, Res>
|
||||
pub struct FnTransform<F, S, In, Out, Err>
|
||||
where
|
||||
F: FnMut(Req, &mut S) -> Res,
|
||||
Res: IntoFuture,
|
||||
F: FnMut(In, &mut S) -> Out + Clone,
|
||||
Out: IntoFuture,
|
||||
{
|
||||
f: F,
|
||||
_t: PhantomData<(S, Req, Res)>,
|
||||
_t: PhantomData<(S, In, Out, Err)>,
|
||||
}
|
||||
|
||||
impl<F, S, Req, Res> FnTransform<F, S, Req, Res>
|
||||
impl<F, S, In, Out, Err> FnTransform<F, S, In, Out, Err>
|
||||
where
|
||||
F: FnMut(Req, &mut S) -> Res,
|
||||
Res: IntoFuture,
|
||||
F: FnMut(In, &mut S) -> Out + Clone,
|
||||
Out: IntoFuture,
|
||||
{
|
||||
pub fn new(f: F) -> Self {
|
||||
FnTransform { f, _t: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, S, Req, Res> Clone for FnTransform<F, S, Req, Res>
|
||||
impl<F, S, In, Out, Err> Transform<S> for FnTransform<F, S, In, Out, Err>
|
||||
where
|
||||
F: FnMut(Req, &mut S) -> Res + Clone,
|
||||
Res: IntoFuture,
|
||||
S: Service,
|
||||
F: FnMut(In, &mut S) -> Out + Clone,
|
||||
Out: IntoFuture,
|
||||
Out::Error: From<S::Error>,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
FnTransform {
|
||||
f: self.f.clone(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
type Request = In;
|
||||
type Response = Out::Item;
|
||||
type Error = Out::Error;
|
||||
type Transform = Apply<S, F, In, Out>;
|
||||
type InitError = Err;
|
||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ok(Apply::new(service, self.f.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, S, Req, Res> Transform<S> for FnTransform<F, S, Req, Res>
|
||||
impl<F, S, In, Out, Err> IntoTransform<FnTransform<F, S, In, Out, Err>, S> for F
|
||||
where
|
||||
F: FnMut(Req, &mut S) -> Res,
|
||||
Res: IntoFuture,
|
||||
S: Service,
|
||||
F: FnMut(In, &mut S) -> Out + Clone,
|
||||
Out: IntoFuture,
|
||||
Out::Error: From<S::Error>,
|
||||
{
|
||||
type Request = Req;
|
||||
type Response = Res::Item;
|
||||
type Error = Res::Error;
|
||||
type Future = Res::Future;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, request: Req, service: &mut S) -> Self::Future {
|
||||
(self.f)(request, service).into_future()
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, S, Req, Res> IntoTransform<FnTransform<F, S, Req, Res>, S> for F
|
||||
where
|
||||
F: FnMut(Req, &mut S) -> Res,
|
||||
Res: IntoFuture,
|
||||
{
|
||||
fn into_transform(self) -> FnTransform<F, S, Req, Res> {
|
||||
fn into_transform(self) -> FnTransform<F, S, In, Out, Err> {
|
||||
FnTransform::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FnNewTransform<F, S, Req, Out, Err, Cfg>
|
||||
impl<F, S, In, Out, Err> Clone for FnTransform<F, S, In, Out, Err>
|
||||
where
|
||||
F: FnMut(Req, &mut S) -> Out + Clone,
|
||||
F: FnMut(In, &mut S) -> Out + Clone,
|
||||
Out: IntoFuture,
|
||||
{
|
||||
f: F,
|
||||
_t: PhantomData<(S, Req, Out, Err, Cfg)>,
|
||||
}
|
||||
|
||||
impl<F, S, Req, Res, Err, Cfg> FnNewTransform<F, S, Req, Res, Err, Cfg>
|
||||
where
|
||||
F: FnMut(Req, &mut S) -> Res + Clone,
|
||||
Res: IntoFuture,
|
||||
{
|
||||
pub fn new(f: F) -> Self {
|
||||
FnNewTransform { f, _t: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, S, Req, Res, Err, Cfg> NewTransform<S, Cfg> for FnNewTransform<F, S, Req, Res, Err, Cfg>
|
||||
where
|
||||
F: FnMut(Req, &mut S) -> Res + Clone,
|
||||
Res: IntoFuture,
|
||||
{
|
||||
type Request = Req;
|
||||
type Response = Res::Item;
|
||||
type Error = Res::Error;
|
||||
type Transform = FnTransform<F, S, Req, Res>;
|
||||
type InitError = Err;
|
||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||
|
||||
fn new_transform(&self, _: &Cfg) -> Self::Future {
|
||||
ok(FnTransform::new(self.f.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, S, Req, Res, Err, Cfg>
|
||||
IntoNewTransform<FnNewTransform<F, S, Req, Res, Err, Cfg>, S, Cfg> for F
|
||||
where
|
||||
F: FnMut(Req, &mut S) -> Res + Clone,
|
||||
Res: IntoFuture,
|
||||
{
|
||||
fn into_new_transform(self) -> FnNewTransform<F, S, Req, Res, Err, Cfg> {
|
||||
FnNewTransform::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, S, Req, Res, Err, Cfg> Clone for FnNewTransform<F, S, Req, Res, Err, Cfg>
|
||||
where
|
||||
F: FnMut(Req, &mut S) -> Res + Clone,
|
||||
Res: IntoFuture,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self::new(self.f.clone())
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::{Async, Future, Poll};
|
||||
use futures::{Async, Future, IntoFuture, Poll};
|
||||
|
||||
use super::{NewService, Service};
|
||||
|
||||
@@ -124,7 +124,7 @@ where
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
FromErrNewServiceFuture {
|
||||
fut: self.a.new_service(cfg),
|
||||
fut: self.a.new_service(cfg).into_future(),
|
||||
e: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -135,7 +135,7 @@ where
|
||||
A: NewService<C>,
|
||||
E: From<A::Error>,
|
||||
{
|
||||
fut: A::Future,
|
||||
fut: <A::Future as IntoFuture>::Future,
|
||||
e: PhantomData<E>,
|
||||
}
|
||||
|
||||
|
@@ -20,21 +20,20 @@ mod map_err;
|
||||
mod map_init_err;
|
||||
mod then;
|
||||
mod transform;
|
||||
mod transform_map_err;
|
||||
mod transform_map_init_err;
|
||||
|
||||
pub use self::and_then::{AndThen, AndThenNewService};
|
||||
use self::and_then_apply::{AndThenTransform, AndThenTransformNewService};
|
||||
pub use self::and_then_apply::AndThenTransform;
|
||||
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
|
||||
pub use self::apply::{Apply, ApplyNewService};
|
||||
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
|
||||
pub use self::fn_transform::{FnNewTransform, FnTransform};
|
||||
pub use self::fn_transform::FnTransform;
|
||||
pub use self::from_err::{FromErr, FromErrNewService};
|
||||
pub use self::map::{Map, MapNewService};
|
||||
pub use self::map_err::{MapErr, MapErrNewService};
|
||||
pub use self::map_init_err::MapInitErr;
|
||||
pub use self::then::{Then, ThenNewService};
|
||||
pub use self::transform::{IntoNewTransform, IntoTransform, NewTransform, Transform};
|
||||
pub use self::transform::{IntoTransform, Transform};
|
||||
|
||||
/// An asynchronous function from `Request` to a `Response`.
|
||||
pub trait Service {
|
||||
@@ -76,20 +75,6 @@ pub trait Service {
|
||||
/// An extension trait for `Service`s that provides a variety of convenient
|
||||
/// adapters
|
||||
pub trait ServiceExt: Service {
|
||||
/// Apply tranformation to specified service and use it as a next service in
|
||||
/// chain.
|
||||
fn apply<T, T1, B, B1>(self, transform: T1, service: B1) -> AndThenTransform<T, Self, B>
|
||||
where
|
||||
Self: Sized,
|
||||
T: Transform<B, Request = Self::Response>,
|
||||
T::Error: From<Self::Error>,
|
||||
T1: IntoTransform<T, B>,
|
||||
B: Service<Error = Self::Error>,
|
||||
B1: IntoService<B>,
|
||||
{
|
||||
AndThenTransform::new(transform.into_transform(), self, service.into_service())
|
||||
}
|
||||
|
||||
/// Apply function to specified service and use it as a next service in
|
||||
/// chain.
|
||||
fn apply_fn<F, B, B1, Out>(self, service: B1, f: F) -> AndThenApply<Self, B, F, Out>
|
||||
@@ -214,7 +199,7 @@ pub trait NewService<Config = ()> {
|
||||
type InitError;
|
||||
|
||||
/// The future of the `Service` instance.
|
||||
type Future: Future<Item = Self::Service, Error = Self::InitError>;
|
||||
type Future: IntoFuture<Item = Self::Service, Error = Self::InitError>;
|
||||
|
||||
/// Create and return a new service value asynchronously.
|
||||
fn new_service(&self, cfg: &Config) -> Self::Future;
|
||||
@@ -225,20 +210,16 @@ pub trait NewService<Config = ()> {
|
||||
self,
|
||||
transform: T1,
|
||||
service: B1,
|
||||
) -> AndThenTransformNewService<T, Self, B, Config>
|
||||
) -> AndThenTransform<T, Self, B, Config>
|
||||
where
|
||||
Self: Sized,
|
||||
T: NewTransform<B::Service, Request = Self::Response, InitError = Self::InitError>,
|
||||
T: Transform<B::Service, Request = Self::Response, InitError = Self::InitError>,
|
||||
T::Error: From<Self::Error>,
|
||||
T1: IntoNewTransform<T, B::Service>,
|
||||
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
|
||||
T1: IntoTransform<T, B::Service>,
|
||||
B: NewService<Config, InitError = Self::InitError>,
|
||||
B1: IntoNewService<B, Config>,
|
||||
{
|
||||
AndThenTransformNewService::new(
|
||||
transform.into_new_transform(),
|
||||
self,
|
||||
service.into_new_service(),
|
||||
)
|
||||
AndThenTransform::new(transform.into_transform(), self, service.into_new_service())
|
||||
}
|
||||
|
||||
/// Apply function to specified service and use it as a next service in
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::{Async, Future, Poll};
|
||||
use futures::{Async, Future, IntoFuture, Poll};
|
||||
|
||||
use super::{NewService, Service};
|
||||
|
||||
@@ -146,7 +146,7 @@ where
|
||||
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())
|
||||
MapNewServiceFuture::new(self.a.new_service(cfg).into_future(), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ where
|
||||
A: NewService<Cfg>,
|
||||
F: FnMut(A::Response) -> Res,
|
||||
{
|
||||
fut: A::Future,
|
||||
fut: <A::Future as IntoFuture>::Future,
|
||||
f: Option<F>,
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@ where
|
||||
A: NewService<Cfg>,
|
||||
F: FnMut(A::Response) -> Res,
|
||||
{
|
||||
fn new(fut: A::Future, f: F) -> Self {
|
||||
fn new(fut: <A::Future as IntoFuture>::Future, f: F) -> Self {
|
||||
MapNewServiceFuture { f: Some(f), fut }
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::{Async, Future, Poll};
|
||||
use futures::{Async, Future, IntoFuture, Poll};
|
||||
|
||||
use super::{NewService, Service};
|
||||
|
||||
@@ -147,7 +147,7 @@ where
|
||||
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())
|
||||
MapErrNewServiceFuture::new(self.a.new_service(cfg).into_future(), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ where
|
||||
A: NewService<C>,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
fut: A::Future,
|
||||
fut: <A::Future as IntoFuture>::Future,
|
||||
f: F,
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ where
|
||||
A: NewService<C>,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
fn new(fut: A::Future, f: F) -> Self {
|
||||
fn new(fut: <A::Future as IntoFuture>::Future, f: F) -> Self {
|
||||
MapErrNewServiceFuture { f, fut }
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::{Future, Poll};
|
||||
use futures::{Future, IntoFuture, Poll};
|
||||
|
||||
use super::NewService;
|
||||
|
||||
@@ -54,7 +54,7 @@ where
|
||||
type Future = MapInitErrFuture<A, F, E, C>;
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
|
||||
MapInitErrFuture::new(self.a.new_service(cfg).into_future(), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ where
|
||||
F: Fn(A::InitError) -> E,
|
||||
{
|
||||
f: F,
|
||||
fut: A::Future,
|
||||
fut: <A::Future as IntoFuture>::Future,
|
||||
}
|
||||
|
||||
impl<A, F, E, C> MapInitErrFuture<A, F, E, C>
|
||||
@@ -72,7 +72,7 @@ where
|
||||
A: NewService<C>,
|
||||
F: Fn(A::InitError) -> E,
|
||||
{
|
||||
fn new(fut: A::Future, f: F) -> Self {
|
||||
fn new(fut: <A::Future as IntoFuture>::Future, f: F) -> Self {
|
||||
MapInitErrFuture { f, fut }
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::{try_ready, Async, Future, Poll};
|
||||
use futures::{try_ready, Async, Future, IntoFuture, Poll};
|
||||
|
||||
use super::{IntoNewService, NewService, Service};
|
||||
use crate::cell::Cell;
|
||||
@@ -157,7 +157,10 @@ where
|
||||
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))
|
||||
ThenNewServiceFuture::new(
|
||||
self.a.new_service(cfg).into_future(),
|
||||
self.b.new_service(cfg).into_future(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,8 +188,8 @@ where
|
||||
InitError = A::InitError,
|
||||
>,
|
||||
{
|
||||
fut_b: B::Future,
|
||||
fut_a: A::Future,
|
||||
fut_b: <B::Future as IntoFuture>::Future,
|
||||
fut_a: <A::Future as IntoFuture>::Future,
|
||||
a: Option<A::Service>,
|
||||
b: Option<B::Service>,
|
||||
}
|
||||
@@ -201,7 +204,10 @@ where
|
||||
InitError = A::InitError,
|
||||
>,
|
||||
{
|
||||
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
||||
fn new(
|
||||
fut_a: <A::Future as IntoFuture>::Future,
|
||||
fut_b: <B::Future as IntoFuture>::Future,
|
||||
) -> Self {
|
||||
ThenNewServiceFuture {
|
||||
fut_a,
|
||||
fut_b,
|
||||
|
@@ -1,56 +1,16 @@
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::{Future, Poll};
|
||||
use futures::IntoFuture;
|
||||
|
||||
use crate::transform_map_err::{TransformMapErr, TransformMapErrNewTransform};
|
||||
use crate::transform_map_init_err::TransformMapInitErr;
|
||||
use crate::Service;
|
||||
|
||||
/// An asynchronous function for transforming service call result.
|
||||
pub trait Transform<Service> {
|
||||
/// Requests handled by the service.
|
||||
type Request;
|
||||
|
||||
/// Responses given by the service.
|
||||
type Response;
|
||||
|
||||
/// Errors produced by the service.
|
||||
type Error;
|
||||
|
||||
/// The future response value.
|
||||
type Future: Future<Item = Self::Response, Error = Self::Error>;
|
||||
|
||||
/// Returns `Ready` when the service is able to process requests.
|
||||
///
|
||||
/// This method is similar to `Service::poll_ready` method.
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error>;
|
||||
|
||||
/// Process the request and apply it to provided service,
|
||||
/// return the response asynchronously.
|
||||
fn call(&mut self, request: Self::Request, service: &mut Service) -> Self::Future;
|
||||
|
||||
/// Map this transform's error to a different error, returning a new transform.
|
||||
///
|
||||
/// This function is similar to the `Result::map_err` where it will change
|
||||
/// the error type of the underlying transform. This is useful for example to
|
||||
/// ensure that services and transforms have the same error type.
|
||||
///
|
||||
/// Note that this function consumes the receiving transform and returns a
|
||||
/// wrapped version of it.
|
||||
fn map_err<F, E>(self, f: F) -> TransformMapErr<Self, Service, F, E>
|
||||
where
|
||||
Self: Sized,
|
||||
F: Fn(Self::Error) -> E,
|
||||
{
|
||||
TransformMapErr::new(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
/// `Transform` service factory
|
||||
/// `Transform` service factory.
|
||||
///
|
||||
/// Transform factory creates service that wraps other services.
|
||||
/// `Config` is a service factory configuration type.
|
||||
pub trait NewTransform<Service, Config = ()> {
|
||||
pub trait Transform<S> {
|
||||
/// Requests handled by the service.
|
||||
type Request;
|
||||
|
||||
@@ -61,8 +21,7 @@ pub trait NewTransform<Service, Config = ()> {
|
||||
type Error;
|
||||
|
||||
/// The `TransformService` value created by this factory
|
||||
type Transform: Transform<
|
||||
Service,
|
||||
type Transform: Service<
|
||||
Request = Self::Request,
|
||||
Response = Self::Response,
|
||||
Error = Self::Error,
|
||||
@@ -72,24 +31,14 @@ pub trait NewTransform<Service, Config = ()> {
|
||||
type InitError;
|
||||
|
||||
/// The future response value.
|
||||
type Future: Future<Item = Self::Transform, Error = Self::InitError>;
|
||||
type Future: IntoFuture<Item = Self::Transform, Error = Self::InitError>;
|
||||
|
||||
/// Create and return a new service value asynchronously.
|
||||
fn new_transform(&self, cfg: &Config) -> Self::Future;
|
||||
|
||||
/// Map this transforms's output to a different type, returning a new transform
|
||||
/// of the resulting type.
|
||||
fn map_err<F, E>(self, f: F) -> TransformMapErrNewTransform<Self, Service, Config, F, E>
|
||||
where
|
||||
Self: Sized,
|
||||
F: Fn(Self::Error) -> E,
|
||||
{
|
||||
TransformMapErrNewTransform::new(self, f)
|
||||
}
|
||||
fn new_transform(&self, service: S) -> Self::Future;
|
||||
|
||||
/// Map this service's factory init error to a different error,
|
||||
/// returning a new transform service factory.
|
||||
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, Service, Config, F, E>
|
||||
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
|
||||
where
|
||||
Self: Sized,
|
||||
F: Fn(Self::InitError) -> E,
|
||||
@@ -98,77 +47,39 @@ pub trait NewTransform<Service, Config = ()> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> Transform<S> for &'a mut T
|
||||
impl<T, S> Transform<S> for Rc<T>
|
||||
where
|
||||
T: Transform<S> + 'a,
|
||||
S: Service<Error = T::Error>,
|
||||
T: Transform<S>,
|
||||
{
|
||||
type Request = T::Request;
|
||||
type Response = T::Response;
|
||||
type Error = T::Error;
|
||||
type Future = T::Future;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), T::Error> {
|
||||
(**self).poll_ready()
|
||||
}
|
||||
|
||||
fn call(&mut self, request: Self::Request, service: &mut S) -> T::Future {
|
||||
(**self).call(request, service)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Transform<S> for Box<T>
|
||||
where
|
||||
T: Transform<S> + ?Sized,
|
||||
S: Service<Error = T::Error>,
|
||||
{
|
||||
type Request = T::Request;
|
||||
type Response = T::Response;
|
||||
type Error = T::Error;
|
||||
type Future = T::Future;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), S::Error> {
|
||||
(**self).poll_ready()
|
||||
}
|
||||
|
||||
fn call(&mut self, request: Self::Request, service: &mut S) -> T::Future {
|
||||
(**self).call(request, service)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, C, T> NewTransform<S, C> for Rc<T>
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
{
|
||||
type Request = T::Request;
|
||||
type Response = T::Response;
|
||||
type Error = T::Error;
|
||||
type Transform = T::Transform;
|
||||
type InitError = T::InitError;
|
||||
type Transform = T::Transform;
|
||||
type Future = T::Future;
|
||||
|
||||
fn new_transform(&self, cfg: &C) -> T::Future {
|
||||
self.as_ref().new_transform(cfg)
|
||||
fn new_transform(&self, service: S) -> T::Future {
|
||||
self.as_ref().new_transform(service)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, C, T> NewTransform<S, C> for Arc<T>
|
||||
impl<T, S> Transform<S> for Arc<T>
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
T: Transform<S>,
|
||||
{
|
||||
type Request = T::Request;
|
||||
type Response = T::Response;
|
||||
type Error = T::Error;
|
||||
type Transform = T::Transform;
|
||||
type InitError = T::InitError;
|
||||
type Transform = T::Transform;
|
||||
type Future = T::Future;
|
||||
|
||||
fn new_transform(&self, cfg: &C) -> T::Future {
|
||||
self.as_ref().new_transform(cfg)
|
||||
fn new_transform(&self, service: S) -> T::Future {
|
||||
self.as_ref().new_transform(service)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types that can be converted to a `TransformService`
|
||||
/// Trait for types that can be converted to a *transform service*
|
||||
pub trait IntoTransform<T, S>
|
||||
where
|
||||
T: Transform<S>,
|
||||
@@ -177,15 +88,6 @@ where
|
||||
fn into_transform(self) -> T;
|
||||
}
|
||||
|
||||
/// Trait for types that can be converted to a TransfromNewService
|
||||
pub trait IntoNewTransform<T, S, C = ()>
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
{
|
||||
/// Convert to an `TranformNewService`
|
||||
fn into_new_transform(self) -> T;
|
||||
}
|
||||
|
||||
impl<T, S> IntoTransform<T, S> for T
|
||||
where
|
||||
T: Transform<S>,
|
||||
@@ -194,12 +96,3 @@ where
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, C> IntoNewTransform<T, S, C> for T
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
{
|
||||
fn into_new_transform(self) -> T {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
@@ -1,188 +0,0 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::{Async, Future, Poll};
|
||||
|
||||
use super::{NewTransform, Transform};
|
||||
|
||||
/// Service for the `map_err` combinator, changing the type of a transform's
|
||||
/// error.
|
||||
///
|
||||
/// This is created by the `Transform::map_err` method.
|
||||
pub struct TransformMapErr<T, S, F, E> {
|
||||
transform: T,
|
||||
f: F,
|
||||
_t: PhantomData<(S, E)>,
|
||||
}
|
||||
|
||||
impl<T, S, F, E> TransformMapErr<T, S, F, E> {
|
||||
/// Create new `MapErr` combinator
|
||||
pub fn new(transform: T, f: F) -> Self
|
||||
where
|
||||
T: Transform<S>,
|
||||
F: Fn(T::Error) -> E,
|
||||
{
|
||||
Self {
|
||||
transform,
|
||||
f,
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, F, E> Clone for TransformMapErr<T, S, F, E>
|
||||
where
|
||||
T: Clone,
|
||||
F: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
TransformMapErr {
|
||||
transform: self.transform.clone(),
|
||||
f: self.f.clone(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, F, E> Transform<S> for TransformMapErr<T, S, F, E>
|
||||
where
|
||||
T: Transform<S>,
|
||||
F: Fn(T::Error) -> E + Clone,
|
||||
{
|
||||
type Request = T::Request;
|
||||
type Response = T::Response;
|
||||
type Error = E;
|
||||
type Future = TransformMapErrFuture<T, S, F, E>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.transform.poll_ready().map_err(&self.f)
|
||||
}
|
||||
|
||||
fn call(&mut self, req: T::Request, service: &mut S) -> Self::Future {
|
||||
TransformMapErrFuture::new(self.transform.call(req, service), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TransformMapErrFuture<T, S, F, E>
|
||||
where
|
||||
T: Transform<S>,
|
||||
F: Fn(T::Error) -> E,
|
||||
{
|
||||
f: F,
|
||||
fut: T::Future,
|
||||
}
|
||||
|
||||
impl<T, S, F, E> TransformMapErrFuture<T, S, F, E>
|
||||
where
|
||||
T: Transform<S>,
|
||||
F: Fn(T::Error) -> E,
|
||||
{
|
||||
fn new(fut: T::Future, f: F) -> Self {
|
||||
TransformMapErrFuture { f, fut }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, F, E> Future for TransformMapErrFuture<T, S, F, E>
|
||||
where
|
||||
T: Transform<S>,
|
||||
F: Fn(T::Error) -> E,
|
||||
{
|
||||
type Item = T::Response;
|
||||
type Error = E;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
self.fut.poll().map_err(&self.f)
|
||||
}
|
||||
}
|
||||
|
||||
/// NewTransform for the `map_err` combinator, changing the type of a new
|
||||
/// transform's error.
|
||||
///
|
||||
/// This is created by the `NewTransform::map_err` method.
|
||||
pub struct TransformMapErrNewTransform<T, S, C, F, E> {
|
||||
t: T,
|
||||
f: F,
|
||||
e: PhantomData<(S, C, E)>,
|
||||
}
|
||||
|
||||
impl<T, S, C, F, E> TransformMapErrNewTransform<T, S, C, F, E> {
|
||||
/// Create new `MapErr` new service instance
|
||||
pub fn new(t: T, f: F) -> Self
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
F: Fn(T::Error) -> E,
|
||||
{
|
||||
Self {
|
||||
t,
|
||||
f,
|
||||
e: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, C, F, E> Clone for TransformMapErrNewTransform<T, S, C, F, E>
|
||||
where
|
||||
T: Clone,
|
||||
F: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
t: self.t.clone(),
|
||||
f: self.f.clone(),
|
||||
e: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, C, F, E> NewTransform<S, C> for TransformMapErrNewTransform<T, S, C, F, E>
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
F: Fn(T::Error) -> E + Clone,
|
||||
{
|
||||
type Request = T::Request;
|
||||
type Response = T::Response;
|
||||
type Error = E;
|
||||
type Transform = TransformMapErr<T::Transform, S, F, E>;
|
||||
|
||||
type InitError = T::InitError;
|
||||
type Future = TransformMapErrNewTransformFuture<T, S, C, F, E>;
|
||||
|
||||
fn new_transform(&self, cfg: &C) -> Self::Future {
|
||||
TransformMapErrNewTransformFuture::new(self.t.new_transform(cfg), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TransformMapErrNewTransformFuture<T, S, C, F, E>
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
F: Fn(T::Error) -> E,
|
||||
{
|
||||
fut: T::Future,
|
||||
f: F,
|
||||
}
|
||||
|
||||
impl<T, S, C, F, E> TransformMapErrNewTransformFuture<T, S, C, F, E>
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
F: Fn(T::Error) -> E,
|
||||
{
|
||||
fn new(fut: T::Future, f: F) -> Self {
|
||||
TransformMapErrNewTransformFuture { f, fut }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, C, F, E> Future for TransformMapErrNewTransformFuture<T, S, C, F, E>
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
F: Fn(T::Error) -> E + Clone,
|
||||
{
|
||||
type Item = TransformMapErr<T::Transform, S, F, E>;
|
||||
type Error = T::InitError;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if let Async::Ready(tr) = self.fut.poll()? {
|
||||
Ok(Async::Ready(TransformMapErr::new(tr, self.f.clone())))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,24 +1,24 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use futures::{Future, Poll};
|
||||
use futures::{Future, IntoFuture, Poll};
|
||||
|
||||
use super::NewTransform;
|
||||
use super::Transform;
|
||||
|
||||
/// NewTransform for the `map_init_err` combinator, changing the type of a new
|
||||
/// transform's error.
|
||||
///
|
||||
/// This is created by the `NewTransform::map_init_err` method.
|
||||
pub struct TransformMapInitErr<T, S, C, F, E> {
|
||||
pub struct TransformMapInitErr<T, S, F, E> {
|
||||
t: T,
|
||||
f: F,
|
||||
e: PhantomData<(S, C, E)>,
|
||||
e: PhantomData<(S, E)>,
|
||||
}
|
||||
|
||||
impl<T, S, C, F, E> TransformMapInitErr<T, S, C, F, E> {
|
||||
impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
|
||||
/// Create new `MapInitErr` new transform instance
|
||||
pub fn new(t: T, f: F) -> Self
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
T: Transform<S>,
|
||||
F: Fn(T::InitError) -> E,
|
||||
{
|
||||
Self {
|
||||
@@ -29,7 +29,7 @@ impl<T, S, C, F, E> TransformMapInitErr<T, S, C, F, E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, C, F, E> Clone for TransformMapInitErr<T, S, C, F, E>
|
||||
impl<T, S, F, E> Clone for TransformMapInitErr<T, S, F, E>
|
||||
where
|
||||
T: Clone,
|
||||
F: Clone,
|
||||
@@ -43,9 +43,9 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, C, F, E> NewTransform<S, C> for TransformMapInitErr<T, S, C, F, E>
|
||||
impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
T: Transform<S>,
|
||||
F: Fn(T::InitError) -> E + Clone,
|
||||
{
|
||||
type Request = T::Request;
|
||||
@@ -54,41 +54,44 @@ where
|
||||
type Transform = T::Transform;
|
||||
|
||||
type InitError = E;
|
||||
type Future = TransformMapInitErrFuture<T, S, C, F, E>;
|
||||
type Future = TransformMapInitErrFuture<T, S, F, E>;
|
||||
|
||||
fn new_transform(&self, cfg: &C) -> Self::Future {
|
||||
TransformMapInitErrFuture::new(self.t.new_transform(cfg), self.f.clone())
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
TransformMapInitErrFuture::new(
|
||||
self.t.new_transform(service).into_future(),
|
||||
self.f.clone(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TransformMapInitErrFuture<T, S, C, F, E>
|
||||
pub struct TransformMapInitErrFuture<T, S, F, E>
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
T: Transform<S>,
|
||||
F: Fn(T::InitError) -> E,
|
||||
{
|
||||
fut: T::Future,
|
||||
fut: <T::Future as IntoFuture>::Future,
|
||||
f: F,
|
||||
}
|
||||
|
||||
impl<T, S, C, F, E> TransformMapInitErrFuture<T, S, C, F, E>
|
||||
impl<T, S, F, E> TransformMapInitErrFuture<T, S, F, E>
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
T: Transform<S>,
|
||||
F: Fn(T::InitError) -> E,
|
||||
{
|
||||
fn new(fut: T::Future, f: F) -> Self {
|
||||
fn new(fut: <T::Future as IntoFuture>::Future, f: F) -> Self {
|
||||
TransformMapInitErrFuture { f, fut }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, C, F, E> Future for TransformMapInitErrFuture<T, S, C, F, E>
|
||||
impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
|
||||
where
|
||||
T: NewTransform<S, C>,
|
||||
T: Transform<S>,
|
||||
F: Fn(T::InitError) -> E + Clone,
|
||||
{
|
||||
type Item = T::Transform;
|
||||
type Error = E;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
self.fut.poll().map_err(|e| (self.f)(e))
|
||||
self.fut.poll().map_err(&self.f)
|
||||
}
|
||||
}
|
||||
|
@@ -34,12 +34,9 @@ rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
||||
|
||||
[dependencies]
|
||||
actix-rt = "0.1.0"
|
||||
#actix-server = "0.2.0"
|
||||
actix-server = { path="../actix-server" }
|
||||
actix-server = "0.3.0"
|
||||
|
||||
log = "0.4"
|
||||
|
||||
# io
|
||||
net2 = "0.2"
|
||||
futures = "0.1"
|
||||
tokio-tcp = "0.1"
|
||||
|
@@ -1,5 +1,19 @@
|
||||
# Changes
|
||||
|
||||
## [0.3.2] - 2019-03-04
|
||||
|
||||
### Changed
|
||||
|
||||
* Use IntoFuture for new services
|
||||
|
||||
|
||||
## [0.3.1] - 2019-03-04
|
||||
|
||||
### Changed
|
||||
|
||||
* Use new type of transform trait
|
||||
|
||||
|
||||
## [0.3.0] - 2019-03-02
|
||||
|
||||
### Changed
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-utils"
|
||||
version = "0.3.0"
|
||||
version = "0.3.2"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix utils - various actix net related services"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
@@ -18,7 +18,7 @@ name = "actix_utils"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-service = "0.3.0"
|
||||
actix-service = "0.3.2"
|
||||
actix-codec = "0.1.0"
|
||||
bytes = "0.4"
|
||||
futures = "0.1.24"
|
||||
|
@@ -1,6 +1,6 @@
|
||||
//! Contains `Either` service and related types and functions.
|
||||
use actix_service::{NewService, Service};
|
||||
use futures::{future, try_ready, Async, Future, Poll};
|
||||
use futures::{future, try_ready, Async, Future, IntoFuture, Poll};
|
||||
|
||||
/// Combine two different service types into a single type.
|
||||
///
|
||||
@@ -102,8 +102,8 @@ where
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
match self {
|
||||
Either::A(ref inner) => EitherNewService::A(inner.new_service(cfg)),
|
||||
Either::B(ref inner) => EitherNewService::B(inner.new_service(cfg)),
|
||||
Either::A(ref inner) => EitherNewService::A(inner.new_service(cfg).into_future()),
|
||||
Either::B(ref inner) => EitherNewService::B(inner.new_service(cfg).into_future()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,8 +119,8 @@ impl<A: Clone, B: Clone> Clone for Either<A, B> {
|
||||
|
||||
#[doc(hidden)]
|
||||
pub enum EitherNewService<A: NewService<C>, B: NewService<C>, C> {
|
||||
A(A::Future),
|
||||
B(B::Future),
|
||||
A(<A::Future as IntoFuture>::Future),
|
||||
B(<B::Future as IntoFuture>::Future),
|
||||
}
|
||||
|
||||
impl<A, B, C> Future for EitherNewService<A, B, C>
|
||||
|
@@ -7,7 +7,7 @@ use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
||||
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
||||
use futures::future::{ok, FutureResult};
|
||||
use futures::task::AtomicTask;
|
||||
use futures::{Async, Future, Poll, Sink, Stream};
|
||||
use futures::{Async, Future, IntoFuture, Poll, Sink, Stream};
|
||||
use log::debug;
|
||||
|
||||
use crate::cell::Cell;
|
||||
@@ -120,7 +120,7 @@ where
|
||||
|
||||
fn call(&mut self, req: Framed<T, U>) -> Self::Future {
|
||||
FramedServiceResponseFuture {
|
||||
fut: self.factory.new_service(&self.config),
|
||||
fut: self.factory.new_service(&self.config).into_future(),
|
||||
framed: Some(req),
|
||||
}
|
||||
}
|
||||
@@ -137,7 +137,7 @@ where
|
||||
<U as Encoder>::Item: 'static,
|
||||
<U as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
fut: S::Future,
|
||||
fut: <S::Future as IntoFuture>::Future,
|
||||
framed: Option<Framed<T, U>>,
|
||||
}
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
use actix_service::{NewTransform, Service, Transform, Void};
|
||||
use actix_service::{Service, Transform, Void};
|
||||
use futures::future::{ok, FutureResult};
|
||||
use futures::{Async, Future, Poll};
|
||||
|
||||
@@ -24,32 +24,34 @@ impl Default for InFlight {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Service, C> NewTransform<T, C> for InFlight {
|
||||
type Request = T::Request;
|
||||
type Response = T::Response;
|
||||
type Error = T::Error;
|
||||
impl<S: Service> Transform<S> for InFlight {
|
||||
type Request = S::Request;
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type InitError = Void;
|
||||
type Transform = InFlightService;
|
||||
type Transform = InFlightService<S>;
|
||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||
|
||||
fn new_transform(&self, _: &C) -> Self::Future {
|
||||
ok(InFlightService::new(self.max_inflight))
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ok(InFlightService::new(self.max_inflight, service))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct InFlightService {
|
||||
pub struct InFlightService<S> {
|
||||
count: Counter,
|
||||
service: S,
|
||||
}
|
||||
|
||||
impl InFlightService {
|
||||
pub fn new(max: usize) -> Self {
|
||||
impl<S> InFlightService<S> {
|
||||
pub fn new(max: usize, service: S) -> Self {
|
||||
Self {
|
||||
service,
|
||||
count: Counter::new(max),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Transform<T> for InFlightService
|
||||
impl<T> Service for InFlightService<T>
|
||||
where
|
||||
T: Service,
|
||||
{
|
||||
@@ -59,6 +61,8 @@ where
|
||||
type Future = InFlightServiceResponse<T>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.service.poll_ready()?;
|
||||
|
||||
if !self.count.available() {
|
||||
log::trace!("InFlight limit exceeded");
|
||||
Ok(Async::NotReady)
|
||||
@@ -67,9 +71,9 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn call(&mut self, req: T::Request, service: &mut T) -> Self::Future {
|
||||
fn call(&mut self, req: T::Request) -> Self::Future {
|
||||
InFlightServiceResponse {
|
||||
fut: service.call(req),
|
||||
fut: self.service.call(req),
|
||||
_guard: self.count.get(),
|
||||
}
|
||||
}
|
||||
@@ -122,7 +126,8 @@ mod tests {
|
||||
fn test_transform() {
|
||||
let wait_time = Duration::from_millis(50);
|
||||
let _ = actix_rt::System::new("test").block_on(lazy(|| {
|
||||
let mut srv = Blank::new().apply(InFlightService::new(1), SleepService(wait_time));
|
||||
let mut srv =
|
||||
Blank::new().and_then(InFlightService::new(1, SleepService(wait_time)));
|
||||
assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
|
||||
|
||||
let mut res = srv.call(());
|
||||
|
@@ -3,7 +3,7 @@ use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::rc::Rc;
|
||||
|
||||
use actix_service::{NewTransform, Service, Transform, Void};
|
||||
use actix_service::{Service, Transform, Void};
|
||||
use futures::future::{ok, FutureResult};
|
||||
use futures::task::AtomicTask;
|
||||
use futures::unsync::oneshot;
|
||||
@@ -63,13 +63,8 @@ where
|
||||
Self { _t: PhantomData }
|
||||
}
|
||||
|
||||
pub fn service() -> impl Transform<
|
||||
S,
|
||||
Request = S::Request,
|
||||
Response = S::Response,
|
||||
Error = InOrderError<S::Error>,
|
||||
> {
|
||||
InOrderService::new()
|
||||
pub fn service(service: S) -> InOrderService<S> {
|
||||
InOrderService::new(service)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +80,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, C> NewTransform<S, C> for InOrder<S>
|
||||
impl<S> Transform<S> for InOrder<S>
|
||||
where
|
||||
S: Service,
|
||||
S::Response: 'static,
|
||||
@@ -99,12 +94,13 @@ where
|
||||
type Transform = InOrderService<S>;
|
||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||
|
||||
fn new_transform(&self, _: &C) -> Self::Future {
|
||||
ok(InOrderService::new())
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ok(InOrderService::new(service))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct InOrderService<S: Service> {
|
||||
service: S,
|
||||
task: Rc<AtomicTask>,
|
||||
acks: VecDeque<Record<S::Response, S::Error>>,
|
||||
}
|
||||
@@ -116,27 +112,16 @@ where
|
||||
S::Future: 'static,
|
||||
S::Error: 'static,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
pub fn new(service: S) -> Self {
|
||||
Self {
|
||||
service,
|
||||
acks: VecDeque::new(),
|
||||
task: Rc::new(AtomicTask::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Default for InOrderService<S>
|
||||
where
|
||||
S: Service,
|
||||
S::Response: 'static,
|
||||
S::Future: 'static,
|
||||
S::Error: 'static,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Transform<S> for InOrderService<S>
|
||||
impl<S> Service for InOrderService<S>
|
||||
where
|
||||
S: Service,
|
||||
S::Response: 'static,
|
||||
@@ -149,8 +134,12 @@ where
|
||||
type Future = InOrderServiceResponse<S>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
// poll_ready could be called from different task
|
||||
self.task.register();
|
||||
|
||||
// check nested service
|
||||
self.service.poll_ready().map_err(InOrderError::Service)?;
|
||||
|
||||
// check acks
|
||||
while !self.acks.is_empty() {
|
||||
let rec = self.acks.front_mut().unwrap();
|
||||
@@ -167,13 +156,13 @@ where
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, request: S::Request, service: &mut S) -> 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 });
|
||||
|
||||
let task = self.task.clone();
|
||||
tokio_current_thread::spawn(service.call(request).then(move |res| {
|
||||
tokio_current_thread::spawn(self.service.call(request).then(move |res| {
|
||||
task.notify();
|
||||
let _ = tx1.send(res);
|
||||
Ok(())
|
||||
@@ -257,7 +246,7 @@ mod tests {
|
||||
let rx3 = rx3;
|
||||
let tx_stop = tx_stop;
|
||||
let _ = actix_rt::System::new("test").block_on(lazy(move || {
|
||||
let mut srv = Blank::new().apply(InOrderService::new(), Srv);
|
||||
let mut srv = Blank::new().and_then(InOrderService::new(Srv));
|
||||
|
||||
let res1 = srv.call(rx1);
|
||||
let res2 = srv.call(rx2);
|
||||
|
@@ -4,7 +4,7 @@ use std::rc::Rc;
|
||||
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
||||
use futures::future::{ok, Future, FutureResult};
|
||||
use futures::unsync::mpsc;
|
||||
use futures::{Async, Poll, Stream};
|
||||
use futures::{Async, IntoFuture, Poll, Stream};
|
||||
|
||||
type Request<T> = Result<<T as IntoStream>::Item, <T as IntoStream>::Error>;
|
||||
|
||||
@@ -113,6 +113,7 @@ where
|
||||
Box::new(
|
||||
self.factory
|
||||
.new_service(&self.config)
|
||||
.into_future()
|
||||
.and_then(move |srv| StreamDispatcher::new(req, srv)),
|
||||
)
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@ use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::time::Duration;
|
||||
|
||||
use actix_service::{NewTransform, Service, Transform};
|
||||
use actix_service::{Service, Transform};
|
||||
use futures::future::{ok, FutureResult};
|
||||
use futures::{Async, Future, Poll};
|
||||
use tokio_timer::{clock, Delay};
|
||||
@@ -80,7 +80,7 @@ impl<E> Clone for Timeout<E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, C, E> NewTransform<S, C> for Timeout<E>
|
||||
impl<S, E> Transform<S> for Timeout<E>
|
||||
where
|
||||
S: Service,
|
||||
{
|
||||
@@ -88,11 +88,12 @@ where
|
||||
type Response = S::Response;
|
||||
type Error = TimeoutError<S::Error>;
|
||||
type InitError = E;
|
||||
type Transform = TimeoutService;
|
||||
type Transform = TimeoutService<S>;
|
||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||
|
||||
fn new_transform(&self, _: &C) -> Self::Future {
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ok(TimeoutService {
|
||||
service,
|
||||
timeout: self.timeout,
|
||||
})
|
||||
}
|
||||
@@ -100,17 +101,18 @@ where
|
||||
|
||||
/// Applies a timeout to requests.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TimeoutService {
|
||||
pub struct TimeoutService<S> {
|
||||
service: S,
|
||||
timeout: Duration,
|
||||
}
|
||||
|
||||
impl TimeoutService {
|
||||
pub fn new(timeout: Duration) -> Self {
|
||||
TimeoutService { timeout }
|
||||
impl<S> TimeoutService<S> {
|
||||
pub fn new(timeout: Duration, service: S) -> Self {
|
||||
TimeoutService { service, timeout }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Transform<S> for TimeoutService
|
||||
impl<S> Service for TimeoutService<S>
|
||||
where
|
||||
S: Service,
|
||||
{
|
||||
@@ -120,12 +122,12 @@ where
|
||||
type Future = TimeoutServiceResponse<S>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
self.service.poll_ready().map_err(TimeoutError::Service)
|
||||
}
|
||||
|
||||
fn call(&mut self, request: S::Request, service: &mut S) -> Self::Future {
|
||||
fn call(&mut self, request: S::Request) -> Self::Future {
|
||||
TimeoutServiceResponse {
|
||||
fut: service.call(request),
|
||||
fut: self.service.call(request),
|
||||
sleep: Delay::new(clock::now() + self.timeout),
|
||||
}
|
||||
}
|
||||
@@ -197,7 +199,7 @@ mod tests {
|
||||
|
||||
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
||||
let mut timeout = Blank::default()
|
||||
.apply(TimeoutService::new(resolution), SleepService(wait_time));
|
||||
.and_then(TimeoutService::new(resolution, SleepService(wait_time)));
|
||||
timeout.call(())
|
||||
}));
|
||||
assert_eq!(res, Ok(()));
|
||||
@@ -210,7 +212,7 @@ mod tests {
|
||||
|
||||
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
||||
let mut timeout = Blank::default()
|
||||
.apply(TimeoutService::new(resolution), SleepService(wait_time));
|
||||
.and_then(TimeoutService::new(resolution, SleepService(wait_time)));
|
||||
timeout.call(())
|
||||
}));
|
||||
assert_eq!(res, Err(TimeoutError::Timeout));
|
||||
@@ -222,7 +224,7 @@ mod tests {
|
||||
let wait_time = Duration::from_millis(150);
|
||||
|
||||
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
||||
let timeout = BlankNewService::<_, _, ()>::default()
|
||||
let timeout = BlankNewService::<(), (), ()>::default()
|
||||
.apply(Timeout::new(resolution), || Ok(SleepService(wait_time)));
|
||||
if let Async::Ready(mut to) = timeout.new_service(&()).poll().unwrap() {
|
||||
to.call(())
|
||||
|
@@ -9,6 +9,10 @@ pub use self::path::Path;
|
||||
pub use self::resource::ResourceDef;
|
||||
pub use self::router::{ResourceInfo, Router, RouterBuilder};
|
||||
|
||||
pub trait Resource<T: ResourcePath> {
|
||||
fn resource_path(&mut self) -> &mut Path<T>;
|
||||
}
|
||||
|
||||
pub trait ResourcePath {
|
||||
fn path(&self) -> &str;
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ use std::rc::Rc;
|
||||
use serde::de;
|
||||
|
||||
use crate::de::PathDeserializer;
|
||||
use crate::ResourcePath;
|
||||
use crate::{Resource, ResourcePath};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub(crate) enum PathItem {
|
||||
@@ -202,3 +202,9 @@ impl<T: ResourcePath> Index<usize> for Path<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ResourcePath> Resource<T> for Path<T> {
|
||||
fn resource_path(&mut self) -> &mut Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,8 @@
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::path::Path;
|
||||
use crate::resource::ResourceDef;
|
||||
use crate::ResourcePath;
|
||||
use crate::{Resource, ResourcePath};
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||
pub(crate) enum ResourceId {
|
||||
@@ -26,14 +25,14 @@ pub(crate) struct ResourceMap {
|
||||
}
|
||||
|
||||
/// Resource router.
|
||||
pub struct Router<T> {
|
||||
pub struct Router<T, U = ()> {
|
||||
rmap: Rc<ResourceMap>,
|
||||
named: HashMap<String, ResourceDef>,
|
||||
resources: Vec<T>,
|
||||
resources: Vec<(T, Option<U>)>,
|
||||
}
|
||||
|
||||
impl<T> Router<T> {
|
||||
pub fn build() -> RouterBuilder<T> {
|
||||
impl<T, U> Router<T, U> {
|
||||
pub fn build() -> RouterBuilder<T, U> {
|
||||
RouterBuilder {
|
||||
rmap: ResourceMap::default(),
|
||||
named: HashMap::new(),
|
||||
@@ -41,52 +40,71 @@ impl<T> Router<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn recognize<U: ResourcePath>(&self, path: &mut Path<U>) -> Option<(&T, ResourceInfo)> {
|
||||
if !path.path().is_empty() {
|
||||
for (idx, resource) in self.rmap.patterns.iter().enumerate() {
|
||||
if resource.match_path(path) {
|
||||
let info = ResourceInfo {
|
||||
rmap: self.rmap.clone(),
|
||||
resource: ResourceId::Normal(idx as u16),
|
||||
};
|
||||
return Some((&self.resources[idx], info));
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn recognize_mut<U: ResourcePath>(
|
||||
pub fn recognize_mut<R: Resource<P>, P: ResourcePath>(
|
||||
&mut self,
|
||||
path: &mut Path<U>,
|
||||
res: &mut R,
|
||||
) -> Option<(&mut T, ResourceInfo)> {
|
||||
if !path.path().is_empty() {
|
||||
for (idx, resource) in self.rmap.patterns.iter().enumerate() {
|
||||
if resource.match_path(path) {
|
||||
let info = ResourceInfo {
|
||||
rmap: self.rmap.clone(),
|
||||
resource: ResourceId::Normal(idx as u16),
|
||||
};
|
||||
return Some((&mut self.resources[idx], info));
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn recognize_mut_checked<R: Resource<P>, P: ResourcePath, F>(
|
||||
&mut self,
|
||||
res: &mut R,
|
||||
check: F,
|
||||
) -> Option<(&mut T, ResourceInfo)>
|
||||
where
|
||||
F: Fn(&R, &Option<U>) -> bool,
|
||||
{
|
||||
for (idx, resource) in self.rmap.patterns.iter().enumerate() {
|
||||
if resource.match_path(res.resource_path()) && check(res, &self.resources[idx].1) {
|
||||
let info = ResourceInfo {
|
||||
rmap: self.rmap.clone(),
|
||||
resource: ResourceId::Normal(idx as u16),
|
||||
};
|
||||
return Some((&mut self.resources[idx].0, info));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> IntoIterator for &'a Router<T> {
|
||||
type Item = &'a T;
|
||||
type IntoIter = std::slice::Iter<'a, T>;
|
||||
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> IntoIterator for &'a mut Router<T> {
|
||||
type Item = &'a mut T;
|
||||
type IntoIter = std::slice::IterMut<'a, T>;
|
||||
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()
|
||||
@@ -108,33 +126,40 @@ impl ResourceMap {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RouterBuilder<T> {
|
||||
pub struct RouterBuilder<T, U = ()> {
|
||||
rmap: ResourceMap,
|
||||
named: HashMap<String, ResourceDef>,
|
||||
resources: Vec<T>,
|
||||
resources: Vec<(T, Option<U>)>,
|
||||
}
|
||||
|
||||
impl<T> RouterBuilder<T> {
|
||||
impl<T, U> RouterBuilder<T, U> {
|
||||
/// Register resource for specified path.
|
||||
pub fn path(&mut self, path: &str, resource: T) {
|
||||
self.rmap.register(ResourceDef::new(path));
|
||||
self.resources.push(resource);
|
||||
self.resources.push((resource, None));
|
||||
}
|
||||
|
||||
/// 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);
|
||||
self.resources.push((resource, None));
|
||||
}
|
||||
|
||||
/// Register resource for ResourceDef
|
||||
pub fn rdef(&mut self, rdef: ResourceDef, resource: T) {
|
||||
self.rmap.register(rdef);
|
||||
self.resources.push(resource);
|
||||
self.resources.push((resource, None));
|
||||
}
|
||||
|
||||
/// Method attachs user data to lastly added resource.
|
||||
///
|
||||
/// This panics if no resources were added.
|
||||
pub fn set_user_data(&mut self, userdata: Option<U>) {
|
||||
self.resources.last_mut().unwrap().1 = userdata;
|
||||
}
|
||||
|
||||
/// Finish configuration and create router instance.
|
||||
pub fn finish(self) -> Router<T> {
|
||||
pub fn finish(self) -> Router<T, U> {
|
||||
Router {
|
||||
rmap: Rc::new(self.rmap),
|
||||
named: self.named,
|
||||
|
Reference in New Issue
Block a user