mirror of
https://github.com/fafhrd91/actix-net
synced 2025-06-26 19:47:43 +02:00
add Config argument to NewService
This commit is contained in:
@ -18,7 +18,8 @@ name = "actix_utils"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-service = "0.2.2"
|
||||
# actix-service = "0.2.2"
|
||||
actix-service = { path = "../actix-service" }
|
||||
actix-codec = "0.1.0"
|
||||
bytes = "0.4"
|
||||
futures = "0.1.24"
|
||||
|
@ -11,14 +11,23 @@ pub type BoxedService<Req, Res, Err> = Box<
|
||||
>;
|
||||
|
||||
/// Create boxed new service
|
||||
pub fn new_service<T>(
|
||||
pub fn new_service<T, C>(
|
||||
service: T,
|
||||
) -> BoxedNewService<T::Request, T::Response, T::Error, T::InitError>
|
||||
) -> BoxedNewService<C, T::Request, T::Response, T::Error, T::InitError>
|
||||
where
|
||||
T: NewService + 'static,
|
||||
C: 'static,
|
||||
T: NewService<C> + 'static,
|
||||
T::Request: 'static,
|
||||
T::Response: 'static,
|
||||
T::Service: 'static,
|
||||
T::Future: 'static,
|
||||
T::Error: 'static,
|
||||
T::InitError: 'static,
|
||||
{
|
||||
BoxedNewService(Box::new(NewServiceWrapper(service)))
|
||||
BoxedNewService(Box::new(NewServiceWrapper {
|
||||
service,
|
||||
_t: std::marker::PhantomData,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Create boxed service
|
||||
@ -30,8 +39,9 @@ where
|
||||
Box::new(ServiceWrapper(service))
|
||||
}
|
||||
|
||||
type Inner<Req, Res, Err, InitErr> = Box<
|
||||
type Inner<C, Req, Res, Err, InitErr> = Box<
|
||||
NewService<
|
||||
C,
|
||||
Request = Req,
|
||||
Response = Res,
|
||||
Error = Err,
|
||||
@ -41,9 +51,9 @@ type Inner<Req, Res, Err, InitErr> = Box<
|
||||
>,
|
||||
>;
|
||||
|
||||
pub struct BoxedNewService<Req, Res, Err, InitErr>(Inner<Req, Res, Err, InitErr>);
|
||||
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
|
||||
|
||||
impl<Req, Res, Err, InitErr> NewService for BoxedNewService<Req, Res, Err, InitErr>
|
||||
impl<C, Req, Res, Err, InitErr> NewService<C> for BoxedNewService<C, Req, Res, Err, InitErr>
|
||||
where
|
||||
Req: 'static,
|
||||
Res: 'static,
|
||||
@ -57,20 +67,23 @@ where
|
||||
type Service = BoxedService<Req, Res, Err>;
|
||||
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
self.0.new_service()
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
self.0.new_service(cfg)
|
||||
}
|
||||
}
|
||||
|
||||
struct NewServiceWrapper<T: NewService>(T);
|
||||
struct NewServiceWrapper<C, T: NewService<C>> {
|
||||
service: T,
|
||||
_t: std::marker::PhantomData<C>,
|
||||
}
|
||||
|
||||
impl<T, Req, Res, Err, InitErr> NewService for NewServiceWrapper<T>
|
||||
impl<C, T, Req, Res, Err, InitErr> NewService<C> for NewServiceWrapper<C, T>
|
||||
where
|
||||
Req: 'static,
|
||||
Res: 'static,
|
||||
Err: 'static,
|
||||
InitErr: 'static,
|
||||
T: NewService<Request = Req, Response = Res, Error = Err, InitError = InitErr>,
|
||||
T: NewService<C, Request = Req, Response = Res, Error = Err, InitError = InitErr>,
|
||||
T::Future: 'static,
|
||||
T::Service: 'static,
|
||||
<T::Service as Service>::Future: 'static,
|
||||
@ -82,10 +95,10 @@ where
|
||||
type Service = BoxedService<Req, Res, Err>;
|
||||
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
Box::new(
|
||||
self.0
|
||||
.new_service()
|
||||
self.service
|
||||
.new_service(cfg)
|
||||
.map(|service| ServiceWrapper::boxed(service)),
|
||||
)
|
||||
}
|
||||
|
@ -53,10 +53,11 @@ pub enum Either<A, B> {
|
||||
}
|
||||
|
||||
impl<A, B> Either<A, B> {
|
||||
pub fn new_a(srv: A) -> Self
|
||||
pub fn new_a<C>(srv: A) -> Self
|
||||
where
|
||||
A: NewService,
|
||||
A: NewService<C>,
|
||||
B: NewService<
|
||||
C,
|
||||
Request = A::Request,
|
||||
Response = A::Response,
|
||||
Error = A::Error,
|
||||
@ -66,10 +67,11 @@ impl<A, B> Either<A, B> {
|
||||
Either::A(srv)
|
||||
}
|
||||
|
||||
pub fn new_b(srv: B) -> Self
|
||||
pub fn new_b<C>(srv: B) -> Self
|
||||
where
|
||||
A: NewService,
|
||||
A: NewService<C>,
|
||||
B: NewService<
|
||||
C,
|
||||
Request = A::Request,
|
||||
Response = A::Response,
|
||||
Error = A::Error,
|
||||
@ -80,10 +82,11 @@ impl<A, B> Either<A, B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, B> NewService for Either<A, B>
|
||||
impl<A, B, C> NewService<C> for Either<A, B>
|
||||
where
|
||||
A: NewService,
|
||||
A: NewService<C>,
|
||||
B: NewService<
|
||||
C,
|
||||
Request = A::Request,
|
||||
Response = A::Response,
|
||||
Error = A::Error,
|
||||
@ -95,12 +98,12 @@ where
|
||||
type Error = A::Error;
|
||||
type InitError = A::InitError;
|
||||
type Service = EitherService<A::Service, B::Service>;
|
||||
type Future = EitherNewService<A, B>;
|
||||
type Future = EitherNewService<A, B, C>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
match self {
|
||||
Either::A(ref inner) => EitherNewService::A(inner.new_service()),
|
||||
Either::B(ref inner) => EitherNewService::B(inner.new_service()),
|
||||
Either::A(ref inner) => EitherNewService::A(inner.new_service(cfg)),
|
||||
Either::B(ref inner) => EitherNewService::B(inner.new_service(cfg)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -115,15 +118,16 @@ impl<A: Clone, B: Clone> Clone for Either<A, B> {
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub enum EitherNewService<A: NewService, B: NewService> {
|
||||
pub enum EitherNewService<A: NewService<C>, B: NewService<C>, C> {
|
||||
A(A::Future),
|
||||
B(B::Future),
|
||||
}
|
||||
|
||||
impl<A, B> Future for EitherNewService<A, B>
|
||||
impl<A, B, C> Future for EitherNewService<A, B, C>
|
||||
where
|
||||
A: NewService,
|
||||
A: NewService<C>,
|
||||
B: NewService<
|
||||
C,
|
||||
Request = A::Request,
|
||||
Response = A::Response,
|
||||
Error = A::Error,
|
||||
|
@ -15,14 +15,15 @@ use crate::cell::Cell;
|
||||
type Request<U> = <U as Decoder>::Item;
|
||||
type Response<U> = <U as Encoder>::Item;
|
||||
|
||||
pub struct FramedNewService<S, T, U> {
|
||||
pub struct FramedNewService<S, T, U, C> {
|
||||
factory: S,
|
||||
_t: PhantomData<(T, U)>,
|
||||
_t: PhantomData<(T, U, C)>,
|
||||
}
|
||||
|
||||
impl<S, T, U> FramedNewService<S, T, U>
|
||||
impl<S, T, U, C> FramedNewService<S, T, U, C>
|
||||
where
|
||||
S: NewService<Request = Request<U>, Response = Response<U>>,
|
||||
C: Clone,
|
||||
S: NewService<C, Request = Request<U>, Response = Response<U>>,
|
||||
S::Error: 'static,
|
||||
<S::Service as Service>::Future: 'static,
|
||||
T: AsyncRead + AsyncWrite,
|
||||
@ -30,7 +31,7 @@ where
|
||||
<U as Encoder>::Item: 'static,
|
||||
<U as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
pub fn new<F1: IntoNewService<S>>(factory: F1) -> Self {
|
||||
pub fn new<F1: IntoNewService<S, C>>(factory: F1) -> Self {
|
||||
Self {
|
||||
factory: factory.into_new_service(),
|
||||
_t: PhantomData,
|
||||
@ -38,7 +39,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, T, U> Clone for FramedNewService<S, T, U>
|
||||
impl<S, T, U, C> Clone for FramedNewService<S, T, U, C>
|
||||
where
|
||||
S: Clone,
|
||||
{
|
||||
@ -50,9 +51,10 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, T, U> NewService for FramedNewService<S, T, U>
|
||||
impl<S, T, U, C> NewService<C> for FramedNewService<S, T, U, C>
|
||||
where
|
||||
S: NewService<Request = Request<U>, Response = Response<U>> + Clone,
|
||||
C: Clone,
|
||||
S: NewService<C, Request = Request<U>, Response = Response<U>> + Clone,
|
||||
S::Error: 'static,
|
||||
<S::Service as Service>::Future: 'static,
|
||||
T: AsyncRead + AsyncWrite,
|
||||
@ -64,48 +66,53 @@ where
|
||||
type Response = FramedTransport<S::Service, T, U>;
|
||||
type Error = S::InitError;
|
||||
type InitError = S::InitError;
|
||||
type Service = FramedService<S, T, U>;
|
||||
type Service = FramedService<S, T, U, C>;
|
||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
ok(FramedService {
|
||||
factory: self.factory.clone(),
|
||||
config: cfg.clone(),
|
||||
_t: PhantomData,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FramedService<S, T, U> {
|
||||
pub struct FramedService<S, T, U, C> {
|
||||
factory: S,
|
||||
config: C,
|
||||
_t: PhantomData<(T, U)>,
|
||||
}
|
||||
|
||||
impl<S, T, U> Clone for FramedService<S, T, U>
|
||||
impl<S, T, U, C> Clone for FramedService<S, T, U, C>
|
||||
where
|
||||
S: Clone,
|
||||
C: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
factory: self.factory.clone(),
|
||||
config: self.config.clone(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, T, U> Service for FramedService<S, T, U>
|
||||
impl<S, T, U, C> Service for FramedService<S, T, U, C>
|
||||
where
|
||||
S: NewService<Request = Request<U>, Response = Response<U>>,
|
||||
S: NewService<C, Request = Request<U>, Response = Response<U>>,
|
||||
S::Error: 'static,
|
||||
<S::Service as Service>::Future: 'static,
|
||||
T: AsyncRead + AsyncWrite,
|
||||
U: Decoder + Encoder,
|
||||
<U as Encoder>::Item: 'static,
|
||||
<U as Encoder>::Error: std::fmt::Debug,
|
||||
C: Clone,
|
||||
{
|
||||
type Request = Framed<T, U>;
|
||||
type Response = FramedTransport<S::Service, T, U>;
|
||||
type Error = S::InitError;
|
||||
type Future = FramedServiceResponseFuture<S, T, U>;
|
||||
type Future = FramedServiceResponseFuture<S, T, U, C>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
@ -113,17 +120,16 @@ where
|
||||
|
||||
fn call(&mut self, req: Framed<T, U>) -> Self::Future {
|
||||
FramedServiceResponseFuture {
|
||||
fut: self.factory.new_service(),
|
||||
|
||||
fut: self.factory.new_service(&self.config),
|
||||
framed: Some(req),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct FramedServiceResponseFuture<S, T, U>
|
||||
pub struct FramedServiceResponseFuture<S, T, U, C>
|
||||
where
|
||||
S: NewService<Request = Request<U>, Response = Response<U>>,
|
||||
S: NewService<C, Request = Request<U>, Response = Response<U>>,
|
||||
S::Error: 'static,
|
||||
<S::Service as Service>::Future: 'static,
|
||||
T: AsyncRead + AsyncWrite,
|
||||
@ -135,9 +141,9 @@ where
|
||||
framed: Option<Framed<T, U>>,
|
||||
}
|
||||
|
||||
impl<S, T, U> Future for FramedServiceResponseFuture<S, T, U>
|
||||
impl<S, T, U, C> Future for FramedServiceResponseFuture<S, T, U, C>
|
||||
where
|
||||
S: NewService<Request = Request<U>, Response = Response<U>>,
|
||||
S: NewService<C, Request = Request<U>, Response = Response<U>>,
|
||||
S::Error: 'static,
|
||||
<S::Service as Service>::Future: 'static,
|
||||
T: AsyncRead + AsyncWrite,
|
||||
@ -402,7 +408,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U, F> NewService for IntoFramed<T, U, F>
|
||||
impl<T, U, F> NewService<()> for IntoFramed<T, U, F>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite,
|
||||
F: Fn() -> U + Send + Clone + 'static,
|
||||
@ -415,7 +421,7 @@ where
|
||||
type Service = IntoFramedService<T, U, F>;
|
||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
ok(IntoFramedService {
|
||||
factory: self.factory.clone(),
|
||||
_t: PhantomData,
|
||||
|
@ -44,7 +44,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<R, E, F> NewService for KeepAlive<R, E, F>
|
||||
impl<R, E, F> NewService<()> for KeepAlive<R, E, F>
|
||||
where
|
||||
F: Fn() -> E + Clone,
|
||||
{
|
||||
@ -55,7 +55,7 @@ where
|
||||
type Service = KeepAliveService<R, E, F>;
|
||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
ok(KeepAliveService::new(
|
||||
self.ka,
|
||||
self.time.timer(),
|
||||
|
@ -29,20 +29,21 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StreamNewService<S, T, E> {
|
||||
pub struct StreamNewService<S, T, E, C> {
|
||||
factory: Rc<T>,
|
||||
_t: PhantomData<(S, E)>,
|
||||
_t: PhantomData<(S, E, C)>,
|
||||
}
|
||||
|
||||
impl<S, T, E> StreamNewService<S, T, E>
|
||||
impl<S, T, E, C> StreamNewService<S, T, E, C>
|
||||
where
|
||||
C: Clone,
|
||||
S: IntoStream,
|
||||
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>,
|
||||
T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>,
|
||||
T::Future: 'static,
|
||||
T::Service: 'static,
|
||||
<T::Service as Service>::Future: 'static,
|
||||
{
|
||||
pub fn new<F: IntoNewService<T>>(factory: F) -> Self {
|
||||
pub fn new<F: IntoNewService<T, C>>(factory: F) -> Self {
|
||||
Self {
|
||||
factory: Rc::new(factory.into_new_service()),
|
||||
_t: PhantomData,
|
||||
@ -50,7 +51,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, T, E> Clone for StreamNewService<S, T, E> {
|
||||
impl<S, T, E, C> Clone for StreamNewService<S, T, E, C> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
factory: self.factory.clone(),
|
||||
@ -59,10 +60,11 @@ impl<S, T, E> Clone for StreamNewService<S, T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, T, E> NewService for StreamNewService<S, T, E>
|
||||
impl<S, T, E, C> NewService<C> for StreamNewService<S, T, E, C>
|
||||
where
|
||||
C: Clone,
|
||||
S: IntoStream + 'static,
|
||||
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>,
|
||||
T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>,
|
||||
T::Future: 'static,
|
||||
T::Service: 'static,
|
||||
<T::Service as Service>::Future: 'static,
|
||||
@ -71,29 +73,32 @@ where
|
||||
type Response = ();
|
||||
type Error = E;
|
||||
type InitError = E;
|
||||
type Service = StreamService<S, T, E>;
|
||||
type Service = StreamService<S, T, E, C>;
|
||||
type Future = FutureResult<Self::Service, E>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
ok(StreamService {
|
||||
factory: self.factory.clone(),
|
||||
config: cfg.clone(),
|
||||
_t: PhantomData,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StreamService<S, T, E> {
|
||||
pub struct StreamService<S, T, E, C = ()> {
|
||||
factory: Rc<T>,
|
||||
config: C,
|
||||
_t: PhantomData<(S, E)>,
|
||||
}
|
||||
|
||||
impl<S, T, E> Service for StreamService<S, T, E>
|
||||
impl<S, T, E, C> Service for StreamService<S, T, E, C>
|
||||
where
|
||||
S: IntoStream + 'static,
|
||||
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>,
|
||||
T: NewService<C, Request = Request<S>, Response = (), Error = E, InitError = E>,
|
||||
T::Future: 'static,
|
||||
T::Service: 'static,
|
||||
<T::Service as Service>::Future: 'static,
|
||||
C: Clone,
|
||||
{
|
||||
type Request = S;
|
||||
type Response = ();
|
||||
@ -107,7 +112,7 @@ where
|
||||
fn call(&mut self, req: S) -> Self::Future {
|
||||
Box::new(
|
||||
self.factory
|
||||
.new_service()
|
||||
.new_service(&self.config)
|
||||
.and_then(move |srv| StreamDispatcher::new(req, srv)),
|
||||
)
|
||||
}
|
||||
@ -227,7 +232,7 @@ impl<T> Clone for TakeItem<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Stream> NewService for TakeItem<T> {
|
||||
impl<T: Stream> NewService<()> for TakeItem<T> {
|
||||
type Request = T;
|
||||
type Response = (Option<T::Item>, T);
|
||||
type Error = T::Error;
|
||||
@ -235,7 +240,7 @@ impl<T: Stream> NewService for TakeItem<T> {
|
||||
type Service = TakeItemService<T>;
|
||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
ok(TakeItemService { _t: PhantomData })
|
||||
}
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ impl Default for LowResTime {
|
||||
}
|
||||
}
|
||||
|
||||
impl NewService for LowResTime {
|
||||
impl NewService<()> for LowResTime {
|
||||
type Request = ();
|
||||
type Response = Instant;
|
||||
type Error = Never;
|
||||
@ -50,7 +50,7 @@ impl NewService for LowResTime {
|
||||
type Service = LowResTimeService;
|
||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
ok(self.timer())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user