diff --git a/README.md b/README.md index 851d8fa9..22e5d4ed 100644 --- a/README.md +++ b/README.md @@ -47,14 +47,13 @@ fn main() { // in this case, on success we use `logger` service .and_then(logger) - // next service uses two components, service state and service function - // actix-net generates `NewService` impl that creates `ServiceState` instance for each new service - // and use `service` function as `Service::call` - .and_then((service, move || { - Ok(ServiceState { num: num.clone() }) - })) - }, - ).unwrap() + // Next service counts number of connections + .and_then(move |req| { + let num = num.fetch_add(1, Ordering::Relaxed); + println!("processed {:?} connections", num); + future::ok(()) + }) + }).unwrap() .start(); sys.run(); diff --git a/examples/basic.rs b/examples/basic.rs index 34f6f30d..8162fcc6 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -32,22 +32,6 @@ fn logger( future::ok(stream) } -/// Stateful service, counts number of connections, `ServiceState` is a state -/// for the service -#[derive(Debug)] -struct ServiceState { - num: Arc, -} - -/// Service function for our stateful service -fn service( - st: &mut ServiceState, _stream: T, -) -> impl Future { - let num = st.num.fetch_add(1, Ordering::Relaxed); - println!("got ssl connection {:?}", num); - future::ok(()) -} - fn main() { env::set_var("RUST_LOG", "actix_net=trace"); env_logger::init(); @@ -89,11 +73,12 @@ fn main() { // `Response` and then uses that response as an input for next // service. in this case, on success we use `logger` service .and_then(logger) - // next service uses two components, service state and service function - // actix-net generates `NewService` impl that creates `ServiceState` instance - // for each new service and use `service` function as - // `Service::call` - .and_then((service, move || Ok(ServiceState { num: num.clone() }))) + // Next service counts number of connections + .and_then(move |_| { + let num = num.fetch_add(1, Ordering::Relaxed); + println!("got ssl connection {:?}", num); + future::ok(()) + }) }, ).unwrap() .start(); diff --git a/examples/ssl.rs b/examples/ssl.rs index 91631e76..752af4cb 100644 --- a/examples/ssl.rs +++ b/examples/ssl.rs @@ -55,7 +55,11 @@ fn main() { openssl .clone() .map_err(|e| println!("Openssl error: {}", e)) - .and_then((service, move || Ok(ServiceState { num: num.clone() }))) + .and_then(move |_| { + let num = num.fetch_add(1, Ordering::Relaxed); + println!("got ssl connection {:?}", num); + future::ok(()) + }) }).unwrap() .start(); diff --git a/src/service/fn_state_service.rs b/src/service/fn_state_service.rs deleted file mode 100644 index 70f8e205..00000000 --- a/src/service/fn_state_service.rs +++ /dev/null @@ -1,145 +0,0 @@ -use std::marker; - -use futures::{Async, Future, IntoFuture, Poll}; - -use super::{IntoNewService, NewService, Service}; - -pub struct FnStateService -where - F: Fn(&mut S, Req) -> Fut, - Fut: IntoFuture, -{ - f: F, - state: S, - _t: marker::PhantomData<(Req, Resp, Err)>, -} - -impl FnStateService -where - F: Fn(&mut S, Req) -> Fut, - Fut: IntoFuture, -{ - pub fn new(state: S, f: F) -> Self { - FnStateService { - f, - state, - _t: marker::PhantomData, - } - } -} - -impl Clone for FnStateService -where - S: Clone, - F: Fn(&mut S, Req) -> Fut + Clone, - Fut: IntoFuture, -{ - fn clone(&self) -> Self { - FnStateService { - f: self.f.clone(), - state: self.state.clone(), - _t: marker::PhantomData, - } - } -} - -impl Service for FnStateService -where - F: Fn(&mut S, Req) -> Fut, - Fut: IntoFuture, -{ - type Request = Req; - type Response = Resp; - type Error = Err; - type Future = Fut::Future; - - fn poll_ready(&mut self) -> Poll<(), Self::Error> { - Ok(Async::Ready(())) - } - - fn call(&mut self, req: Req) -> Self::Future { - (self.f)(&mut self.state, req).into_future() - } -} - -/// `NewService` for state and handler functions -pub struct FnStateNewService { - f: F1, - state: F2, - _t: marker::PhantomData<(S, Req, Resp, Err1, Err2, Fut1, Fut2)>, -} - -impl - FnStateNewService -{ - fn new(f: F1, state: F2) -> Self { - FnStateNewService { - f, - state, - _t: marker::PhantomData, - } - } -} - -impl NewService - for FnStateNewService -where - S: 'static, - F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static, - F2: Fn() -> Fut2, - Fut1: IntoFuture + 'static, - Fut2: IntoFuture + 'static, - Req: 'static, - Resp: 'static, - Err1: 'static, - Err2: 'static, -{ - type Request = Req; - type Response = Resp; - type Error = Err1; - type Service = FnStateService; - type InitError = Err2; - type Future = Box>; - - fn new_service(&self) -> Self::Future { - let f = self.f.clone(); - Box::new( - (self.state)() - .into_future() - .and_then(move |state| Ok(FnStateService::new(state, f))), - ) - } -} - -impl - IntoNewService> for (F1, F2) -where - S: 'static, - F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static, - F2: Fn() -> Fut2, - Fut1: IntoFuture + 'static, - Fut2: IntoFuture + 'static, - Req: 'static, - Resp: 'static, - Err1: 'static, - Err2: 'static, -{ - fn into_new_service( - self, - ) -> FnStateNewService { - FnStateNewService::new(self.0, self.1) - } -} - -impl Clone - for FnStateNewService -where - F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static, - F2: Fn() -> Fut2 + Clone, - Fut1: IntoFuture, - Fut2: IntoFuture, -{ - fn clone(&self) -> Self { - Self::new(self.f.clone(), self.state.clone()) - } -} diff --git a/src/service/mod.rs b/src/service/mod.rs index 41441cdb..852d87b8 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -6,7 +6,6 @@ pub use tower_service::{NewService, Service}; mod and_then; mod apply; mod fn_service; -mod fn_state_service; mod from_err; mod map; mod map_err; @@ -15,7 +14,6 @@ mod map_init_err; pub use self::and_then::{AndThen, AndThenNewService}; pub use self::apply::{Apply, ApplyNewService}; pub use self::fn_service::{FnNewService, FnService}; -pub use self::fn_state_service::{FnStateNewService, FnStateService}; pub use self::from_err::{FromErr, FromErrNewService}; pub use self::map::{Map, MapNewService}; pub use self::map_err::{MapErr, MapErrNewService};