mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-30 16:34:36 +01:00
use owned value for service factory config
This commit is contained in:
parent
3385682e09
commit
9ed35cca7a
@ -49,7 +49,7 @@ impl<T: Address> ServiceFactory for TcpConnectorFactory<T> {
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
fn new_service(&self, _: ()) -> Self::Future {
|
||||
ok(self.service())
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ impl<T: Address> ServiceFactory for ResolverFactory<T> {
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
fn new_service(&self, _: ()) -> Self::Future {
|
||||
ok(self.service())
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ impl<T: Address> ServiceFactory for ConnectServiceFactory<T> {
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
fn new_service(&self, _: ()) -> Self::Future {
|
||||
ok(self.service())
|
||||
}
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ where
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
fn new_service(&self, _: ()) -> Self::Future {
|
||||
ok(OpensslConnectorService {
|
||||
connector: self.connector.clone(),
|
||||
_t: PhantomData,
|
||||
@ -201,7 +201,7 @@ impl<T: Address + 'static> ServiceFactory for OpensslConnectServiceFactory<T> {
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
fn new_service(&self, _: ()) -> Self::Future {
|
||||
ok(self.service())
|
||||
}
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ async fn test_new_service() {
|
||||
|
||||
let factory = actix_connect::new_connector_factory(resolver);
|
||||
|
||||
let mut conn = factory.new_service(&()).await.unwrap();
|
||||
let mut conn = factory.new_service(()).await.unwrap();
|
||||
let con = conn.call(Connect::with("10", srv.addr())).await.unwrap();
|
||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ use crate::cell::Cell;
|
||||
use crate::error::ServiceError;
|
||||
use crate::item::Item;
|
||||
use crate::sink::Sink;
|
||||
use crate::state::State;
|
||||
|
||||
type Request<S, U> = Item<S, U>;
|
||||
type Response<U> = <U as Encoder>::Item;
|
||||
@ -33,6 +32,7 @@ pub(crate) enum FramedMessage<T> {
|
||||
#[pin_project::pin_project]
|
||||
pub(crate) struct FramedDispatcher<St, S, T, U>
|
||||
where
|
||||
St: Clone,
|
||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||
S::Error: 'static,
|
||||
S::Future: 'static,
|
||||
@ -43,7 +43,7 @@ where
|
||||
{
|
||||
service: S,
|
||||
sink: Sink<<U as Encoder>::Item>,
|
||||
state: State<St>,
|
||||
state: St,
|
||||
dispatch_state: FramedState<S, U>,
|
||||
framed: Framed<T, U>,
|
||||
rx: Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>,
|
||||
@ -53,6 +53,7 @@ where
|
||||
|
||||
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
|
||||
where
|
||||
St: Clone,
|
||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||
S::Error: 'static,
|
||||
S::Future: 'static,
|
||||
@ -63,7 +64,7 @@ where
|
||||
{
|
||||
pub(crate) fn new<F: IntoService<S>>(
|
||||
framed: Framed<T, U>,
|
||||
state: State<St>,
|
||||
state: St,
|
||||
service: F,
|
||||
rx: mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>,
|
||||
sink: Sink<<U as Encoder>::Item>,
|
||||
@ -124,6 +125,7 @@ struct FramedDispatcherInner<I, E> {
|
||||
|
||||
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
|
||||
where
|
||||
St: Clone,
|
||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||
S::Error: 'static,
|
||||
S::Future: 'static,
|
||||
@ -156,7 +158,7 @@ where
|
||||
fn poll<St, S, T, U>(
|
||||
cx: &mut Context,
|
||||
srv: &mut S,
|
||||
state: &mut State<St>,
|
||||
state: &mut St,
|
||||
sink: &mut Sink<<U as Encoder>::Item>,
|
||||
framed: &mut Framed<T, U>,
|
||||
dispatch_state: &mut FramedState<S, U>,
|
||||
@ -165,6 +167,7 @@ fn poll<St, S, T, U>(
|
||||
disconnect: &mut Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||
) -> Poll<Result<(), ServiceError<S::Error, U>>>
|
||||
where
|
||||
St: Clone,
|
||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||
S::Error: 'static,
|
||||
S::Future: 'static,
|
||||
@ -199,7 +202,7 @@ where
|
||||
|| framed.is_write_buf_empty())
|
||||
{
|
||||
if let Some(ref disconnect) = disconnect {
|
||||
(&*disconnect)(&mut *state.get_mut(), true);
|
||||
(&*disconnect)(&mut *state, true);
|
||||
}
|
||||
Poll::Ready(Err(err))
|
||||
} else {
|
||||
@ -224,19 +227,19 @@ where
|
||||
let _ = tx.send(());
|
||||
}
|
||||
if let Some(ref disconnect) = disconnect {
|
||||
(&*disconnect)(&mut *state.get_mut(), false);
|
||||
(&*disconnect)(&mut *state, false);
|
||||
}
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
FramedState::FramedError(err) => {
|
||||
if let Some(ref disconnect) = disconnect {
|
||||
(&*disconnect)(&mut *state.get_mut(), true);
|
||||
(&*disconnect)(&mut *state, true);
|
||||
}
|
||||
Poll::Ready(Err(err))
|
||||
}
|
||||
FramedState::Stopping => {
|
||||
if let Some(ref disconnect) = disconnect {
|
||||
(&*disconnect)(&mut *state.get_mut(), false);
|
||||
(&*disconnect)(&mut *state, false);
|
||||
}
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
@ -246,13 +249,14 @@ where
|
||||
fn poll_read<St, S, T, U>(
|
||||
cx: &mut Context,
|
||||
srv: &mut S,
|
||||
state: &mut State<St>,
|
||||
state: &mut St,
|
||||
sink: &mut Sink<<U as Encoder>::Item>,
|
||||
framed: &mut Framed<T, U>,
|
||||
dispatch_state: &mut FramedState<S, U>,
|
||||
inner: &mut Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
||||
) -> bool
|
||||
where
|
||||
St: Clone,
|
||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||
S::Error: 'static,
|
||||
S::Future: 'static,
|
||||
|
@ -1,14 +1,12 @@
|
||||
use std::cell::{Ref, RefMut};
|
||||
use std::fmt;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use actix_codec::{Decoder, Encoder};
|
||||
|
||||
use crate::sink::Sink;
|
||||
use crate::state::State;
|
||||
|
||||
pub struct Item<St, Codec: Encoder + Decoder> {
|
||||
state: State<St>,
|
||||
state: St,
|
||||
sink: Sink<<Codec as Encoder>::Item>,
|
||||
item: <Codec as Decoder>::Item,
|
||||
}
|
||||
@ -18,7 +16,7 @@ where
|
||||
Codec: Encoder + Decoder,
|
||||
{
|
||||
pub(crate) fn new(
|
||||
state: State<St>,
|
||||
state: St,
|
||||
sink: Sink<<Codec as Encoder>::Item>,
|
||||
item: <Codec as Decoder>::Item,
|
||||
) -> Self {
|
||||
@ -26,13 +24,13 @@ where
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn state(&self) -> Ref<St> {
|
||||
self.state.get_ref()
|
||||
pub fn state(&self) -> &St {
|
||||
&self.state
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn state_mut(&mut self) -> RefMut<St> {
|
||||
self.state.get_mut()
|
||||
pub fn state_mut(&mut self) -> &mut St {
|
||||
&mut self.state
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -46,13 +44,7 @@ where
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn into_parts(
|
||||
self,
|
||||
) -> (
|
||||
State<St>,
|
||||
Sink<<Codec as Encoder>::Item>,
|
||||
<Codec as Decoder>::Item,
|
||||
) {
|
||||
pub fn into_parts(self) -> (St, Sink<<Codec as Encoder>::Item>, <Codec as Decoder>::Item) {
|
||||
(self.state, self.sink, self.item)
|
||||
}
|
||||
}
|
||||
|
@ -5,11 +5,9 @@ mod error;
|
||||
mod item;
|
||||
mod service;
|
||||
mod sink;
|
||||
mod state;
|
||||
|
||||
pub use self::connect::{Connect, ConnectResult};
|
||||
pub use self::error::ServiceError;
|
||||
pub use self::item::Item;
|
||||
pub use self::service::{Builder, NewServiceBuilder, ServiceBuilder};
|
||||
pub use self::sink::Sink;
|
||||
pub use self::state::State;
|
||||
|
@ -14,7 +14,6 @@ use crate::connect::{Connect, ConnectResult};
|
||||
use crate::dispatcher::FramedDispatcher;
|
||||
use crate::error::ServiceError;
|
||||
use crate::item::Item;
|
||||
use crate::state::State;
|
||||
|
||||
type RequestItem<S, U> = Item<S, U>;
|
||||
type ResponseItem<U> = Option<<U as Encoder>::Item>;
|
||||
@ -23,7 +22,7 @@ type ResponseItem<U> = Option<<U as Encoder>::Item>;
|
||||
/// for building instances for framed services.
|
||||
pub struct Builder<St, Codec>(PhantomData<(St, Codec)>);
|
||||
|
||||
impl<St, Codec> Builder<St, Codec> {
|
||||
impl<St: Clone, Codec> Builder<St, Codec> {
|
||||
pub fn new() -> Builder<St, Codec> {
|
||||
Builder(PhantomData)
|
||||
}
|
||||
@ -73,7 +72,7 @@ pub struct ServiceBuilder<St, C, Io, Codec> {
|
||||
|
||||
impl<St, C, Io, Codec> ServiceBuilder<St, C, Io, Codec>
|
||||
where
|
||||
St: 'static,
|
||||
St: Clone,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
@ -121,7 +120,7 @@ pub struct NewServiceBuilder<St, C, Io, Codec> {
|
||||
|
||||
impl<St, C, Io, Codec> NewServiceBuilder<St, C, Io, Codec>
|
||||
where
|
||||
St: 'static,
|
||||
St: Clone,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: ServiceFactory<
|
||||
Config = (),
|
||||
@ -174,7 +173,7 @@ pub struct FramedService<St, C, T, Io, Codec, Cfg> {
|
||||
|
||||
impl<St, C, T, Io, Codec, Cfg> ServiceFactory for FramedService<St, C, T, Io, Codec, Cfg>
|
||||
where
|
||||
St: 'static,
|
||||
St: Clone + 'static,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: ServiceFactory<
|
||||
Config = (),
|
||||
@ -203,13 +202,13 @@ where
|
||||
type Service = FramedServiceImpl<St, C::Service, T, Io, Codec>;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
|
||||
|
||||
fn new_service(&self, _: &Cfg) -> Self::Future {
|
||||
fn new_service(&self, _: Cfg) -> Self::Future {
|
||||
let handler = self.handler.clone();
|
||||
let disconnect = self.disconnect.clone();
|
||||
|
||||
// create connect service and then create service impl
|
||||
self.connect
|
||||
.new_service(&())
|
||||
.new_service(())
|
||||
.map(move |result| {
|
||||
result.map(move |connect| FramedServiceImpl {
|
||||
connect,
|
||||
@ -231,6 +230,7 @@ pub struct FramedServiceImpl<St, C, T, Io, Codec> {
|
||||
|
||||
impl<St, C, T, Io, Codec> Service for FramedServiceImpl<St, C, T, Io, Codec>
|
||||
where
|
||||
St: Clone,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
@ -269,6 +269,7 @@ where
|
||||
#[pin_project::pin_project]
|
||||
pub struct FramedServiceImplResponse<St, Io, Codec, C, T>
|
||||
where
|
||||
St: Clone,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
T: ServiceFactory<
|
||||
@ -290,6 +291,7 @@ where
|
||||
|
||||
impl<St, Io, Codec, C, T> Future for FramedServiceImplResponse<St, Io, Codec, C, T>
|
||||
where
|
||||
St: Clone,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
T: ServiceFactory<
|
||||
@ -325,6 +327,7 @@ where
|
||||
#[pin_project::pin_project]
|
||||
enum FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
||||
where
|
||||
St: Clone,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
T: ServiceFactory<
|
||||
@ -351,6 +354,7 @@ where
|
||||
|
||||
impl<St, Io, Codec, C, T> FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
||||
where
|
||||
St: Clone,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
T: ServiceFactory<
|
||||
@ -380,7 +384,7 @@ where
|
||||
match fut.poll(cx) {
|
||||
Poll::Ready(Ok(res)) => {
|
||||
Either::Left(FramedServiceImplResponseInner::Handler(
|
||||
handler.new_service(&res.state),
|
||||
handler.new_service(res.state.clone()),
|
||||
Some(res),
|
||||
disconnect.take(),
|
||||
))
|
||||
@ -396,7 +400,7 @@ where
|
||||
Either::Left(FramedServiceImplResponseInner::Dispatcher(
|
||||
FramedDispatcher::new(
|
||||
res.framed,
|
||||
State::new(res.state),
|
||||
res.state,
|
||||
handler,
|
||||
res.rx,
|
||||
res.sink,
|
||||
|
@ -1,30 +0,0 @@
|
||||
use std::cell::{Ref, RefCell, RefMut};
|
||||
use std::rc::Rc;
|
||||
|
||||
/// Connection state
|
||||
///
|
||||
/// Connection state is an arbitrary data attached to the each incoming message.
|
||||
#[derive(Debug)]
|
||||
pub struct State<T>(Rc<RefCell<T>>);
|
||||
|
||||
impl<T> State<T> {
|
||||
pub(crate) fn new(st: T) -> Self {
|
||||
State(Rc::new(RefCell::new(st)))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_ref(&self) -> Ref<T> {
|
||||
self.0.borrow()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_mut(&mut self) -> RefMut<T> {
|
||||
self.0.borrow_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for State<T> {
|
||||
fn clone(&self) -> Self {
|
||||
State(self.0.clone())
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ use futures::future::ok;
|
||||
|
||||
use actix_ioframe::{Builder, Connect};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct State;
|
||||
|
||||
#[actix_rt::test]
|
||||
|
@ -121,7 +121,7 @@ impl InternalServiceFactory for ConfiguredService {
|
||||
}
|
||||
let mut res = vec![];
|
||||
for (token, ns) in services.into_iter() {
|
||||
let newserv = ns.new_service(&());
|
||||
let newserv = ns.new_service(());
|
||||
match newserv.await {
|
||||
Ok(serv) => {
|
||||
res.push((token, serv));
|
||||
@ -250,8 +250,8 @@ where
|
||||
type Service = BoxedServerService;
|
||||
type Future = LocalBoxFuture<'static, Result<BoxedServerService, ()>>;
|
||||
|
||||
fn new_service(&self, cfg: &()) -> Self::Future {
|
||||
let fut = self.inner.new_service(cfg);
|
||||
fn new_service(&self, _: ()) -> Self::Future {
|
||||
let fut = self.inner.new_service(());
|
||||
async move {
|
||||
return match fut.await {
|
||||
Ok(s) => Ok(Box::new(StreamService::new(s)) as BoxedServerService),
|
||||
|
@ -150,7 +150,7 @@ where
|
||||
let token = self.token;
|
||||
self.inner
|
||||
.create()
|
||||
.new_service(&())
|
||||
.new_service(())
|
||||
.map_err(|_| ())
|
||||
.map_ok(move |inner| {
|
||||
let service: BoxedServerService = Box::new(StreamService::new(inner));
|
||||
|
@ -134,6 +134,7 @@ where
|
||||
impl<A, B> AndThenServiceFactory<A, B>
|
||||
where
|
||||
A: ServiceFactory,
|
||||
A::Config: Clone,
|
||||
B: ServiceFactory<
|
||||
Config = A::Config,
|
||||
Request = A::Response,
|
||||
@ -142,7 +143,7 @@ where
|
||||
>,
|
||||
{
|
||||
/// Create new `AndThenFactory` combinator
|
||||
pub fn new(a: A, b: B) -> Self {
|
||||
pub(crate) fn new(a: A, b: B) -> Self {
|
||||
Self { a, b }
|
||||
}
|
||||
}
|
||||
@ -150,6 +151,7 @@ where
|
||||
impl<A, B> ServiceFactory for AndThenServiceFactory<A, B>
|
||||
where
|
||||
A: ServiceFactory,
|
||||
A::Config: Clone,
|
||||
B: ServiceFactory<
|
||||
Config = A::Config,
|
||||
Request = A::Response,
|
||||
@ -166,8 +168,11 @@ where
|
||||
type InitError = A::InitError;
|
||||
type Future = AndThenServiceFactoryResponse<A, B>;
|
||||
|
||||
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
||||
AndThenServiceFactoryResponse::new(self.a.new_service(cfg), self.b.new_service(cfg))
|
||||
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
||||
AndThenServiceFactoryResponse::new(
|
||||
self.a.new_service(cfg.clone()),
|
||||
self.b.new_service(cfg),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -318,7 +323,7 @@ mod tests {
|
||||
pipeline_factory(factory_fn(move || ready(Ok::<_, ()>(Srv1(cnt2.clone())))))
|
||||
.and_then(move || ready(Ok(Srv2(cnt.clone()))));
|
||||
|
||||
let mut srv = new_srv.new_service(&()).await.unwrap();
|
||||
let mut srv = new_srv.new_service(()).await.unwrap();
|
||||
let res = srv.call("srv1").await;
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), ("srv1", "srv2"));
|
||||
|
@ -117,7 +117,7 @@ where
|
||||
type InitError = T::InitError;
|
||||
type Future = ApplyServiceFactoryResponse<T, F, R, In, Out, Err>;
|
||||
|
||||
fn new_service(&self, cfg: &T::Config) -> Self::Future {
|
||||
fn new_service(&self, cfg: T::Config) -> Self::Future {
|
||||
ApplyServiceFactoryResponse::new(self.service.new_service(cfg), self.f.clone())
|
||||
}
|
||||
}
|
||||
@ -226,7 +226,7 @@ mod tests {
|
||||
},
|
||||
));
|
||||
|
||||
let mut srv = new_srv.new_service(&()).await.unwrap();
|
||||
let mut srv = new_srv.new_service(()).await.unwrap();
|
||||
|
||||
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||
|
||||
|
@ -9,7 +9,7 @@ use crate::{Service, ServiceFactory};
|
||||
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
||||
pub fn apply_cfg<F, C, T, R, S, E>(srv: T, f: F) -> ApplyConfigService<F, C, T, R, S, E>
|
||||
where
|
||||
F: FnMut(&C, &mut T) -> R,
|
||||
F: FnMut(C, &mut T) -> R,
|
||||
T: Service,
|
||||
R: Future<Output = Result<S, E>>,
|
||||
S: Service,
|
||||
@ -28,8 +28,7 @@ pub fn apply_cfg_factory<F, C, T, R, S>(
|
||||
f: F,
|
||||
) -> ApplyConfigServiceFactory<F, C, T, R, S>
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
T: ServiceFactory<Config = ()>,
|
||||
T::InitError: From<T::Error>,
|
||||
R: Future<Output = Result<S, T::InitError>>,
|
||||
@ -45,7 +44,7 @@ where
|
||||
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService\
|
||||
pub struct ApplyConfigService<F, C, T, R, S, E>
|
||||
where
|
||||
F: FnMut(&C, &mut T) -> R,
|
||||
F: FnMut(C, &mut T) -> R,
|
||||
T: Service,
|
||||
R: Future<Output = Result<S, E>>,
|
||||
S: Service,
|
||||
@ -57,7 +56,7 @@ where
|
||||
|
||||
impl<F, C, T, R, S, E> Clone for ApplyConfigService<F, C, T, R, S, E>
|
||||
where
|
||||
F: FnMut(&C, &mut T) -> R,
|
||||
F: FnMut(C, &mut T) -> R,
|
||||
T: Service,
|
||||
R: Future<Output = Result<S, E>>,
|
||||
S: Service,
|
||||
@ -73,7 +72,7 @@ where
|
||||
|
||||
impl<F, C, T, R, S, E> ServiceFactory for ApplyConfigService<F, C, T, R, S, E>
|
||||
where
|
||||
F: FnMut(&C, &mut T) -> R,
|
||||
F: FnMut(C, &mut T) -> R,
|
||||
T: Service,
|
||||
R: Future<Output = Result<S, E>>,
|
||||
S: Service,
|
||||
@ -87,7 +86,7 @@ where
|
||||
type InitError = E;
|
||||
type Future = R;
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
fn new_service(&self, cfg: C) -> Self::Future {
|
||||
unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) }
|
||||
}
|
||||
}
|
||||
@ -95,8 +94,7 @@ where
|
||||
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
||||
pub struct ApplyConfigServiceFactory<F, C, T, R, S>
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
T: ServiceFactory<Config = ()>,
|
||||
R: Future<Output = Result<S, T::InitError>>,
|
||||
S: Service,
|
||||
@ -108,8 +106,7 @@ where
|
||||
|
||||
impl<F, C, T, R, S> Clone for ApplyConfigServiceFactory<F, C, T, R, S>
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
T: ServiceFactory<Config = ()>,
|
||||
R: Future<Output = Result<S, T::InitError>>,
|
||||
S: Service,
|
||||
@ -125,8 +122,7 @@ where
|
||||
|
||||
impl<F, C, T, R, S> ServiceFactory for ApplyConfigServiceFactory<F, C, T, R, S>
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
T: ServiceFactory<Config = ()>,
|
||||
T::InitError: From<T::Error>,
|
||||
R: Future<Output = Result<S, T::InitError>>,
|
||||
@ -141,13 +137,13 @@ where
|
||||
type InitError = T::InitError;
|
||||
type Future = ApplyConfigServiceFactoryResponse<F, C, T, R, S>;
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
fn new_service(&self, cfg: C) -> Self::Future {
|
||||
ApplyConfigServiceFactoryResponse {
|
||||
f: self.f.clone(),
|
||||
cfg: cfg.clone(),
|
||||
cfg: Some(cfg),
|
||||
fut: None,
|
||||
srv: None,
|
||||
srv_fut: Some(self.srv.get_ref().new_service(&())),
|
||||
srv_fut: Some(self.srv.get_ref().new_service(())),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
@ -156,14 +152,13 @@ where
|
||||
#[pin_project::pin_project]
|
||||
pub struct ApplyConfigServiceFactoryResponse<F, C, T, R, S>
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
T: ServiceFactory<Config = ()>,
|
||||
T::InitError: From<T::Error>,
|
||||
R: Future<Output = Result<S, T::InitError>>,
|
||||
S: Service,
|
||||
{
|
||||
cfg: C,
|
||||
cfg: Option<C>,
|
||||
f: Cell<F>,
|
||||
srv: Option<T::Service>,
|
||||
#[pin]
|
||||
@ -175,8 +170,7 @@ where
|
||||
|
||||
impl<F, C, T, R, S> Future for ApplyConfigServiceFactoryResponse<F, C, T, R, S>
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
T: ServiceFactory<Config = ()>,
|
||||
T::InitError: From<T::Error>,
|
||||
R: Future<Output = Result<S, T::InitError>>,
|
||||
@ -205,7 +199,7 @@ where
|
||||
} else if let Some(srv) = this.srv {
|
||||
match srv.poll_ready(cx)? {
|
||||
Poll::Ready(_) => {
|
||||
let fut = this.f.get_mut()(&this.cfg, srv);
|
||||
let fut = this.f.get_mut()(this.cfg.take().unwrap(), srv);
|
||||
this = self.as_mut().project();
|
||||
this.fut.set(Some(fut));
|
||||
continue;
|
||||
|
@ -69,7 +69,7 @@ where
|
||||
|
||||
type Future = BoxFuture<Self::Service, InitErr>;
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
fn new_service(&self, cfg: C) -> Self::Future {
|
||||
self.0.new_service(cfg)
|
||||
}
|
||||
}
|
||||
@ -104,7 +104,7 @@ where
|
||||
type Service = BoxService<Req, Res, Err>;
|
||||
type Future = BoxFuture<Self::Service, Self::InitError>;
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
fn new_service(&self, cfg: C) -> Self::Future {
|
||||
Box::pin(
|
||||
self.factory
|
||||
.new_service(cfg)
|
||||
|
@ -38,7 +38,7 @@ where
|
||||
/// Create `ServiceFactory` for function that can produce services with configuration
|
||||
pub fn factory_fn_cfg<F, Fut, Cfg, Srv, Err>(f: F) -> FnServiceConfig<F, Fut, Cfg, Srv, Err>
|
||||
where
|
||||
F: Fn(&Cfg) -> Fut,
|
||||
F: Fn(Cfg) -> Fut,
|
||||
Fut: Future<Output = Result<Srv, Err>>,
|
||||
Srv: Service,
|
||||
{
|
||||
@ -146,7 +146,7 @@ where
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||
|
||||
fn new_service(&self, _: &Cfg) -> Self::Future {
|
||||
fn new_service(&self, _: Cfg) -> Self::Future {
|
||||
ok(FnService::new(self.f.clone()))
|
||||
}
|
||||
}
|
||||
@ -165,7 +165,7 @@ where
|
||||
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
||||
pub struct FnServiceConfig<F, Fut, Cfg, Srv, Err>
|
||||
where
|
||||
F: Fn(&Cfg) -> Fut,
|
||||
F: Fn(Cfg) -> Fut,
|
||||
Fut: Future<Output = Result<Srv, Err>>,
|
||||
Srv: Service,
|
||||
{
|
||||
@ -175,7 +175,7 @@ where
|
||||
|
||||
impl<F, Fut, Cfg, Srv, Err> FnServiceConfig<F, Fut, Cfg, Srv, Err>
|
||||
where
|
||||
F: Fn(&Cfg) -> Fut,
|
||||
F: Fn(Cfg) -> Fut,
|
||||
Fut: Future<Output = Result<Srv, Err>>,
|
||||
Srv: Service,
|
||||
{
|
||||
@ -186,7 +186,7 @@ where
|
||||
|
||||
impl<F, Fut, Cfg, Srv, Err> ServiceFactory for FnServiceConfig<F, Fut, Cfg, Srv, Err>
|
||||
where
|
||||
F: Fn(&Cfg) -> Fut,
|
||||
F: Fn(Cfg) -> Fut,
|
||||
Fut: Future<Output = Result<Srv, Err>>,
|
||||
Srv: Service,
|
||||
{
|
||||
@ -199,7 +199,7 @@ where
|
||||
type InitError = Err;
|
||||
type Future = Fut;
|
||||
|
||||
fn new_service(&self, cfg: &Cfg) -> Self::Future {
|
||||
fn new_service(&self, cfg: Cfg) -> Self::Future {
|
||||
(self.f)(cfg)
|
||||
}
|
||||
}
|
||||
@ -240,7 +240,7 @@ where
|
||||
type InitError = E;
|
||||
type Future = R;
|
||||
|
||||
fn new_service(&self, _: &C) -> Self::Future {
|
||||
fn new_service(&self, _: C) -> Self::Future {
|
||||
(self.f)()
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ mod transform;
|
||||
pub use self::apply::{apply_fn, apply_fn_factory};
|
||||
pub use self::apply_cfg::{apply_cfg, apply_cfg_factory};
|
||||
pub use self::fn_service::{factory_fn, factory_fn_cfg, service_fn, service_fn2};
|
||||
pub use self::map_config::{map_config, unit_config, MappedConfig};
|
||||
pub use self::map_config::{map_config, unit_config};
|
||||
pub use self::pipeline::{pipeline, pipeline_factory, Pipeline, PipelineFactory};
|
||||
pub use self::transform::{apply, Transform};
|
||||
|
||||
@ -131,7 +131,7 @@ pub trait ServiceFactory {
|
||||
type Future: Future<Output = Result<Self::Service, Self::InitError>>;
|
||||
|
||||
/// Create and return a new service value asynchronously.
|
||||
fn new_service(&self, cfg: &Self::Config) -> Self::Future;
|
||||
fn new_service(&self, cfg: Self::Config) -> Self::Future;
|
||||
|
||||
/// Map this service's output to a different type, returning a new service
|
||||
/// of the resulting type.
|
||||
@ -246,7 +246,7 @@ where
|
||||
type InitError = S::InitError;
|
||||
type Future = S::Future;
|
||||
|
||||
fn new_service(&self, cfg: &S::Config) -> S::Future {
|
||||
fn new_service(&self, cfg: S::Config) -> S::Future {
|
||||
self.as_ref().new_service(cfg)
|
||||
}
|
||||
}
|
||||
@ -263,7 +263,7 @@ where
|
||||
type InitError = S::InitError;
|
||||
type Future = S::Future;
|
||||
|
||||
fn new_service(&self, cfg: &S::Config) -> S::Future {
|
||||
fn new_service(&self, cfg: S::Config) -> S::Future {
|
||||
self.as_ref().new_service(cfg)
|
||||
}
|
||||
}
|
||||
|
@ -151,7 +151,7 @@ where
|
||||
type InitError = A::InitError;
|
||||
type Future = MapServiceFuture<A, F, Res>;
|
||||
|
||||
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
||||
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
||||
MapServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
@ -2,16 +2,11 @@ use std::marker::PhantomData;
|
||||
|
||||
use super::ServiceFactory;
|
||||
|
||||
pub enum MappedConfig<'a, T> {
|
||||
Ref(&'a T),
|
||||
Owned(T),
|
||||
}
|
||||
|
||||
/// Adapt external config to a config for provided new service
|
||||
pub fn map_config<T, F, C>(factory: T, f: F) -> MapConfig<T, F, C>
|
||||
where
|
||||
T: ServiceFactory,
|
||||
F: Fn(&C) -> MappedConfig<T::Config>,
|
||||
F: Fn(C) -> T::Config,
|
||||
{
|
||||
MapConfig::new(factory, f)
|
||||
}
|
||||
@ -36,7 +31,7 @@ impl<A, F, C> MapConfig<A, F, C> {
|
||||
pub(crate) fn new(a: A, f: F) -> Self
|
||||
where
|
||||
A: ServiceFactory,
|
||||
F: Fn(&C) -> MappedConfig<A::Config>,
|
||||
F: Fn(C) -> A::Config,
|
||||
{
|
||||
Self {
|
||||
a,
|
||||
@ -63,7 +58,7 @@ where
|
||||
impl<A, F, C> ServiceFactory for MapConfig<A, F, C>
|
||||
where
|
||||
A: ServiceFactory,
|
||||
F: Fn(&C) -> MappedConfig<A::Config>,
|
||||
F: Fn(C) -> A::Config,
|
||||
{
|
||||
type Request = A::Request;
|
||||
type Response = A::Response;
|
||||
@ -74,11 +69,8 @@ where
|
||||
type InitError = A::InitError;
|
||||
type Future = A::Future;
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
match (self.f)(cfg) {
|
||||
MappedConfig::Ref(cfg) => self.a.new_service(cfg),
|
||||
MappedConfig::Owned(cfg) => self.a.new_service(&cfg),
|
||||
}
|
||||
fn new_service(&self, cfg: C) -> Self::Future {
|
||||
self.a.new_service((self.f)(cfg))
|
||||
}
|
||||
}
|
||||
|
||||
@ -123,7 +115,7 @@ where
|
||||
type InitError = A::InitError;
|
||||
type Future = A::Future;
|
||||
|
||||
fn new_service(&self, _: &C) -> Self::Future {
|
||||
self.a.new_service(&())
|
||||
fn new_service(&self, _: C) -> Self::Future {
|
||||
self.a.new_service(())
|
||||
}
|
||||
}
|
||||
|
@ -154,7 +154,7 @@ where
|
||||
type InitError = A::InitError;
|
||||
type Future = MapErrServiceFuture<A, F, E>;
|
||||
|
||||
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
||||
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
||||
MapErrServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ where
|
||||
type InitError = E;
|
||||
type Future = MapInitErrFuture<A, F, E>;
|
||||
|
||||
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
||||
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
||||
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
@ -145,6 +145,7 @@ impl<T: ServiceFactory> PipelineFactory<T> {
|
||||
pub fn and_then<F, U>(self, factory: F) -> PipelineFactory<AndThenServiceFactory<T, U>>
|
||||
where
|
||||
Self: Sized,
|
||||
T::Config: Clone,
|
||||
F: IntoServiceFactory<U>,
|
||||
U: ServiceFactory<
|
||||
Config = T::Config,
|
||||
@ -167,6 +168,7 @@ impl<T: ServiceFactory> PipelineFactory<T> {
|
||||
pub fn then<F, U>(self, factory: F) -> PipelineFactory<ThenServiceFactory<T, U>>
|
||||
where
|
||||
Self: Sized,
|
||||
T::Config: Clone,
|
||||
F: IntoServiceFactory<U>,
|
||||
U: ServiceFactory<
|
||||
Config = T::Config,
|
||||
@ -236,7 +238,7 @@ impl<T: ServiceFactory> ServiceFactory for PipelineFactory<T> {
|
||||
type Future = T::Future;
|
||||
|
||||
#[inline]
|
||||
fn new_service(&self, cfg: &T::Config) -> Self::Future {
|
||||
fn new_service(&self, cfg: T::Config) -> Self::Future {
|
||||
self.factory.new_service(cfg)
|
||||
}
|
||||
}
|
||||
|
@ -129,6 +129,7 @@ pub struct ThenServiceFactory<A, B> {
|
||||
impl<A, B> ThenServiceFactory<A, B>
|
||||
where
|
||||
A: ServiceFactory,
|
||||
A::Config: Clone,
|
||||
B: ServiceFactory<
|
||||
Config = A::Config,
|
||||
Request = Result<A::Response, A::Error>,
|
||||
@ -145,6 +146,7 @@ where
|
||||
impl<A, B> ServiceFactory for ThenServiceFactory<A, B>
|
||||
where
|
||||
A: ServiceFactory,
|
||||
A::Config: Clone,
|
||||
B: ServiceFactory<
|
||||
Config = A::Config,
|
||||
Request = Result<A::Response, A::Error>,
|
||||
@ -161,8 +163,11 @@ where
|
||||
type InitError = A::InitError;
|
||||
type Future = ThenServiceFactoryResponse<A, B>;
|
||||
|
||||
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
||||
ThenServiceFactoryResponse::new(self.a.new_service(cfg), self.b.new_service(cfg))
|
||||
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
||||
ThenServiceFactoryResponse::new(
|
||||
self.a.new_service(cfg.clone()),
|
||||
self.b.new_service(cfg),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -125,7 +125,7 @@ where
|
||||
type InitError = T::InitError;
|
||||
type Future = ApplyTransformFuture<T, S>;
|
||||
|
||||
fn new_service(&self, cfg: &S::Config) -> Self::Future {
|
||||
fn new_service(&self, cfg: S::Config) -> Self::Future {
|
||||
ApplyTransformFuture {
|
||||
t_cell: self.t.clone(),
|
||||
fut_a: self.s.new_service(cfg),
|
||||
|
@ -49,7 +49,7 @@ impl<T: AsyncRead + AsyncWrite + Unpin + 'static> ServiceFactory for Acceptor<T>
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
fn new_service(&self, _: ()) -> Self::Future {
|
||||
MAX_CONN_COUNTER.with(|conns| {
|
||||
ok(AcceptorService {
|
||||
acceptor: self.acceptor.clone(),
|
||||
|
@ -63,6 +63,7 @@ impl<A, B> Either<A, B> {
|
||||
pub fn new(left: A, right: B) -> Either<A, B>
|
||||
where
|
||||
A: ServiceFactory,
|
||||
A::Config: Clone,
|
||||
B: ServiceFactory<
|
||||
Config = A::Config,
|
||||
Response = A::Response,
|
||||
@ -77,6 +78,7 @@ impl<A, B> Either<A, B> {
|
||||
impl<A, B> ServiceFactory for Either<A, B>
|
||||
where
|
||||
A: ServiceFactory,
|
||||
A::Config: Clone,
|
||||
B: ServiceFactory<
|
||||
Config = A::Config,
|
||||
Response = A::Response,
|
||||
@ -92,11 +94,11 @@ where
|
||||
type Service = EitherService<A::Service, B::Service>;
|
||||
type Future = EitherNewService<A, B>;
|
||||
|
||||
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
||||
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
||||
EitherNewService {
|
||||
left: None,
|
||||
right: None,
|
||||
left_fut: self.left.new_service(cfg),
|
||||
left_fut: self.left.new_service(cfg.clone()),
|
||||
right_fut: self.right.new_service(cfg),
|
||||
}
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ where
|
||||
type Service = KeepAliveService<R, E, F>;
|
||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
fn new_service(&self, _: ()) -> Self::Future {
|
||||
ok(KeepAliveService::new(
|
||||
self.ka,
|
||||
self.time.timer(),
|
||||
|
@ -51,7 +51,7 @@ impl ServiceFactory for LowResTime {
|
||||
type Service = LowResTimeService;
|
||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
fn new_service(&self, _: ()) -> Self::Future {
|
||||
ok(self.timer())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user