mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-27 18:02:58 +01:00
remove fn state service
This commit is contained in:
parent
601c8a4ee6
commit
4c422983ba
15
README.md
15
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();
|
||||
|
@ -32,22 +32,6 @@ fn logger<T: AsyncRead + AsyncWrite + fmt::Debug>(
|
||||
future::ok(stream)
|
||||
}
|
||||
|
||||
/// Stateful service, counts number of connections, `ServiceState` is a state
|
||||
/// for the service
|
||||
#[derive(Debug)]
|
||||
struct ServiceState {
|
||||
num: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
/// Service function for our stateful service
|
||||
fn service<T: AsyncRead + AsyncWrite>(
|
||||
st: &mut ServiceState, _stream: T,
|
||||
) -> impl Future<Item = (), Error = ()> {
|
||||
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();
|
||||
|
@ -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();
|
||||
|
||||
|
@ -1,145 +0,0 @@
|
||||
use std::marker;
|
||||
|
||||
use futures::{Async, Future, IntoFuture, Poll};
|
||||
|
||||
use super::{IntoNewService, NewService, Service};
|
||||
|
||||
pub struct FnStateService<S, F, Req, Resp, Err, Fut>
|
||||
where
|
||||
F: Fn(&mut S, Req) -> Fut,
|
||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||
{
|
||||
f: F,
|
||||
state: S,
|
||||
_t: marker::PhantomData<(Req, Resp, Err)>,
|
||||
}
|
||||
|
||||
impl<S, F, Req, Resp, Err, Fut> FnStateService<S, F, Req, Resp, Err, Fut>
|
||||
where
|
||||
F: Fn(&mut S, Req) -> Fut,
|
||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||
{
|
||||
pub fn new(state: S, f: F) -> Self {
|
||||
FnStateService {
|
||||
f,
|
||||
state,
|
||||
_t: marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, F, Req, Resp, Err, Fut> Clone for FnStateService<S, F, Req, Resp, Err, Fut>
|
||||
where
|
||||
S: Clone,
|
||||
F: Fn(&mut S, Req) -> Fut + Clone,
|
||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
FnStateService {
|
||||
f: self.f.clone(),
|
||||
state: self.state.clone(),
|
||||
_t: marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, F, Req, Resp, Err, Fut> Service for FnStateService<S, F, Req, Resp, Err, Fut>
|
||||
where
|
||||
F: Fn(&mut S, Req) -> Fut,
|
||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||
{
|
||||
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<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2> {
|
||||
f: F1,
|
||||
state: F2,
|
||||
_t: marker::PhantomData<(S, Req, Resp, Err1, Err2, Fut1, Fut2)>,
|
||||
}
|
||||
|
||||
impl<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2>
|
||||
FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2>
|
||||
{
|
||||
fn new(f: F1, state: F2) -> Self {
|
||||
FnStateNewService {
|
||||
f,
|
||||
state,
|
||||
_t: marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2> NewService
|
||||
for FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2>
|
||||
where
|
||||
S: 'static,
|
||||
F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static,
|
||||
F2: Fn() -> Fut2,
|
||||
Fut1: IntoFuture<Item = Resp, Error = Err1> + 'static,
|
||||
Fut2: IntoFuture<Item = S, Error = Err2> + 'static,
|
||||
Req: 'static,
|
||||
Resp: 'static,
|
||||
Err1: 'static,
|
||||
Err2: 'static,
|
||||
{
|
||||
type Request = Req;
|
||||
type Response = Resp;
|
||||
type Error = Err1;
|
||||
type Service = FnStateService<S, F1, Req, Resp, Err1, Fut1>;
|
||||
type InitError = Err2;
|
||||
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
||||
|
||||
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<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2>
|
||||
IntoNewService<FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2>> for (F1, F2)
|
||||
where
|
||||
S: 'static,
|
||||
F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static,
|
||||
F2: Fn() -> Fut2,
|
||||
Fut1: IntoFuture<Item = Resp, Error = Err1> + 'static,
|
||||
Fut2: IntoFuture<Item = S, Error = Err2> + 'static,
|
||||
Req: 'static,
|
||||
Resp: 'static,
|
||||
Err1: 'static,
|
||||
Err2: 'static,
|
||||
{
|
||||
fn into_new_service(
|
||||
self,
|
||||
) -> FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2> {
|
||||
FnStateNewService::new(self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2> Clone
|
||||
for FnStateNewService<S, F1, F2, Req, Resp, Err1, Err2, Fut1, Fut2>
|
||||
where
|
||||
F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static,
|
||||
F2: Fn() -> Fut2 + Clone,
|
||||
Fut1: IntoFuture<Item = Resp, Error = Err1>,
|
||||
Fut2: IntoFuture<Item = S, Error = Err2>,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self::new(self.f.clone(), self.state.clone())
|
||||
}
|
||||
}
|
@ -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};
|
||||
|
Loading…
Reference in New Issue
Block a user