mirror of
https://github.com/actix/actix-web.git
synced 2025-01-03 05:48:45 +00:00
replace partial with more generic Apply
This commit is contained in:
parent
42b0d53c16
commit
e0513e4085
4 changed files with 188 additions and 196 deletions
|
@ -91,7 +91,7 @@ impl<T: HostAware> Connector<T> {
|
|||
InitError = E,
|
||||
> + Clone {
|
||||
move || -> FutureResult<Connector<T>, E> {
|
||||
ok(Connector::new(cfg.clone(), opts.clone()))
|
||||
ok(Connector::new(cfg.clone(), opts))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
171
src/service/apply.rs
Normal file
171
src/service/apply.rs
Normal file
|
@ -0,0 +1,171 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use futures::{Async, Future, Poll};
|
||||
use {NewService, Service};
|
||||
|
||||
/// `Apply` service combinator
|
||||
pub struct Apply<T, F, R, Req, Resp, Err> {
|
||||
service: T,
|
||||
f: F,
|
||||
r: PhantomData<R>,
|
||||
r1: PhantomData<Req>,
|
||||
r2: PhantomData<Resp>,
|
||||
e: PhantomData<Err>,
|
||||
}
|
||||
|
||||
impl<T, F, R, Req, Resp, Err> Apply<T, F, R, Req, Resp, Err>
|
||||
where
|
||||
T: Service,
|
||||
F: Fn(Req, &mut T) -> R,
|
||||
R: Future<Item = Resp, Error = Err>,
|
||||
{
|
||||
/// Create new `Apply` combinator
|
||||
pub fn new(f: F, service: T) -> Self {
|
||||
Self {
|
||||
service,
|
||||
f,
|
||||
r: PhantomData,
|
||||
r1: PhantomData,
|
||||
r2: PhantomData,
|
||||
e: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, F, R, Req, Resp, Err> Service for Apply<T, F, R, Req, Resp, Err>
|
||||
where
|
||||
T: Service,
|
||||
T::Error: Into<Err>,
|
||||
F: Fn(Req, &mut T) -> R,
|
||||
R: Future<Item = Resp, Error = Err>,
|
||||
{
|
||||
type Request = Req;
|
||||
type Response = Resp;
|
||||
type Error = Err;
|
||||
type Future = R;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.service.poll_ready().map_err(|e| e.into())
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
(self.f)(req, &mut self.service)
|
||||
}
|
||||
}
|
||||
|
||||
/// `ApplyNewService` new service combinator
|
||||
pub struct ApplyNewService<T, F, R, Req, Resp, Err> {
|
||||
service: T,
|
||||
f: F,
|
||||
r: PhantomData<R>,
|
||||
r1: PhantomData<Req>,
|
||||
r2: PhantomData<Resp>,
|
||||
e: PhantomData<Err>,
|
||||
}
|
||||
|
||||
impl<T, F, R, Req, Resp, Err> ApplyNewService<T, F, R, Req, Resp, Err>
|
||||
where
|
||||
T: NewService,
|
||||
F: Fn(Req, &mut T::Service) -> R,
|
||||
R: Future<Item = Resp, Error = Err>,
|
||||
{
|
||||
/// Create new `Partial` new service instance
|
||||
pub fn new(f: F, service: T) -> Self {
|
||||
Self {
|
||||
service,
|
||||
f,
|
||||
r: PhantomData,
|
||||
r1: PhantomData,
|
||||
r2: PhantomData,
|
||||
e: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, F, R, Req, Resp, Err> Clone for ApplyNewService<T, F, R, Req, Resp, Err>
|
||||
where
|
||||
T: NewService + Clone,
|
||||
F: Fn(Req, &mut T::Service) -> R + Clone,
|
||||
R: Future<Item = Resp, Error = Err>,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
service: self.service.clone(),
|
||||
f: self.f.clone(),
|
||||
r: PhantomData,
|
||||
r1: PhantomData,
|
||||
r2: PhantomData,
|
||||
e: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, F, R, Req, Resp, Err> NewService for ApplyNewService<T, F, R, Req, Resp, Err>
|
||||
where
|
||||
T: NewService,
|
||||
T::Error: Into<Err>,
|
||||
F: Fn(Req, &mut T::Service) -> R + Clone,
|
||||
R: Future<Item = Resp, Error = Err>,
|
||||
{
|
||||
type Request = Req;
|
||||
type Response = Resp;
|
||||
type Error = Err;
|
||||
type Service = Apply<T::Service, F, R, Req, Resp, Err>;
|
||||
|
||||
type InitError = T::InitError;
|
||||
type Future = ApplyNewServiceFuture<T, F, R, Req, Resp, Err>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
ApplyNewServiceFuture::new(self.service.new_service(), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ApplyNewServiceFuture<T, F, R, Req, Resp, Err>
|
||||
where
|
||||
T: NewService,
|
||||
F: Fn(Req, &mut T::Service) -> R,
|
||||
R: Future<Item = Resp, Error = Err>,
|
||||
{
|
||||
fut: T::Future,
|
||||
f: Option<F>,
|
||||
r: PhantomData<R>,
|
||||
r1: PhantomData<Req>,
|
||||
r2: PhantomData<Resp>,
|
||||
e: PhantomData<Err>,
|
||||
}
|
||||
|
||||
impl<T, F, R, Req, Resp, Err> ApplyNewServiceFuture<T, F, R, Req, Resp, Err>
|
||||
where
|
||||
T: NewService,
|
||||
F: Fn(Req, &mut T::Service) -> R,
|
||||
R: Future<Item = Resp, Error = Err>,
|
||||
{
|
||||
fn new(fut: T::Future, f: F) -> Self {
|
||||
ApplyNewServiceFuture {
|
||||
f: Some(f),
|
||||
fut,
|
||||
r: PhantomData,
|
||||
r1: PhantomData,
|
||||
r2: PhantomData,
|
||||
e: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, F, R, Req, Resp, Err> Future for ApplyNewServiceFuture<T, F, R, Req, Resp, Err>
|
||||
where
|
||||
T: NewService,
|
||||
F: Fn(Req, &mut T::Service) -> R,
|
||||
R: Future<Item = Resp, Error = Err>,
|
||||
{
|
||||
type Item = Apply<T::Service, F, R, Req, Resp, Err>;
|
||||
type Error = T::InitError;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if let Async::Ready(service) = self.fut.poll()? {
|
||||
Ok(Async::Ready(Apply::new(self.f.take().unwrap(), service)))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,23 +1,23 @@
|
|||
use futures::IntoFuture;
|
||||
use tower_service::{NewService, Service};
|
||||
use futures::{Future, IntoFuture};
|
||||
|
||||
mod and_then;
|
||||
mod apply;
|
||||
mod fn_service;
|
||||
mod fn_state_service;
|
||||
mod map;
|
||||
mod map_err;
|
||||
mod map_init_err;
|
||||
mod map_request;
|
||||
mod partial;
|
||||
|
||||
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::map::{Map, MapNewService};
|
||||
pub use self::map_err::{MapErr, MapErrNewService};
|
||||
pub use self::map_init_err::MapInitErr;
|
||||
pub use self::map_request::{MapReq, MapReqNewService};
|
||||
pub use self::partial::{Partial, PartialNewService};
|
||||
use {NewService, Service};
|
||||
|
||||
pub trait ServiceExt: Service {
|
||||
fn and_then<F, B>(self, new_service: F) -> AndThen<Self, B>
|
||||
|
@ -47,6 +47,18 @@ pub trait ServiceExt: Service {
|
|||
}
|
||||
|
||||
pub trait NewServiceExt: NewService {
|
||||
fn apply<T, F, R, Req, Resp, Err, F2>(
|
||||
f: F, service: F2,
|
||||
) -> ApplyNewService<T, F, R, Req, Resp, Err>
|
||||
where
|
||||
T: NewService,
|
||||
F: Fn(Req, &mut T::Service) -> R + Clone,
|
||||
R: Future<Item = Resp, Error = Err>,
|
||||
F2: IntoNewService<T>,
|
||||
{
|
||||
ApplyNewService::new(f, service.into_new_service())
|
||||
}
|
||||
|
||||
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
|
||||
where
|
||||
Self: Sized,
|
||||
|
@ -91,14 +103,6 @@ pub trait NewServiceExt: NewService {
|
|||
{
|
||||
MapInitErr::new(self, f)
|
||||
}
|
||||
|
||||
fn partial<F, Req, Resp>(self, f: F) -> PartialNewService<Self, F, Req, Resp>
|
||||
where
|
||||
Self: Sized,
|
||||
F: Fn(Req) -> (Self::Request, Resp),
|
||||
{
|
||||
PartialNewService::new(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Service> ServiceExt for T {}
|
||||
|
|
|
@ -1,183 +0,0 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use futures::{Async, Future, Poll};
|
||||
use {NewService, Service};
|
||||
|
||||
/// `Partial` service combinator
|
||||
pub struct Partial<A, F, Req, Resp> {
|
||||
a: A,
|
||||
f: F,
|
||||
r1: PhantomData<Req>,
|
||||
r2: PhantomData<Resp>,
|
||||
}
|
||||
|
||||
impl<A, F, Req, Resp> Partial<A, F, Req, Resp>
|
||||
where
|
||||
A: Service,
|
||||
F: Fn(Req) -> (A::Request, Resp),
|
||||
{
|
||||
/// Create new `Partial` combinator
|
||||
pub fn new(a: A, f: F) -> Self {
|
||||
Self {
|
||||
a,
|
||||
f,
|
||||
r1: PhantomData,
|
||||
r2: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, Req, Resp> Service for Partial<A, F, Req, Resp>
|
||||
where
|
||||
A: Service,
|
||||
F: Fn(Req) -> (A::Request, Resp),
|
||||
{
|
||||
type Request = Req;
|
||||
type Response = (A::Response, Resp);
|
||||
type Error = A::Error;
|
||||
type Future = PartialFuture<A, Resp>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.a.poll_ready()
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
let (req, resp) = (self.f)(req);
|
||||
PartialFuture::new(self.a.call(req), resp)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PartialFuture<A, Resp>
|
||||
where
|
||||
A: Service,
|
||||
{
|
||||
fut: A::Future,
|
||||
resp: Option<Resp>,
|
||||
}
|
||||
|
||||
impl<A, Resp> PartialFuture<A, Resp>
|
||||
where
|
||||
A: Service,
|
||||
{
|
||||
fn new(fut: A::Future, resp: Resp) -> Self {
|
||||
PartialFuture {
|
||||
fut,
|
||||
resp: Some(resp),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, Resp> Future for PartialFuture<A, Resp>
|
||||
where
|
||||
A: Service,
|
||||
{
|
||||
type Item = (A::Response, Resp);
|
||||
type Error = A::Error;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
match self.fut.poll()? {
|
||||
Async::Ready(resp) => Ok(Async::Ready((resp, self.resp.take().unwrap()))),
|
||||
Async::NotReady => Ok(Async::NotReady),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `PartialNewService` new service combinator
|
||||
pub struct PartialNewService<A, F, Req, Resp> {
|
||||
a: A,
|
||||
f: F,
|
||||
r1: PhantomData<Req>,
|
||||
r2: PhantomData<Resp>,
|
||||
}
|
||||
|
||||
impl<A, F, Req, Resp> PartialNewService<A, F, Req, Resp>
|
||||
where
|
||||
A: NewService,
|
||||
F: Fn(Req) -> (A::Request, Resp),
|
||||
{
|
||||
/// Create new `Partial` new service instance
|
||||
pub fn new(a: A, f: F) -> Self {
|
||||
Self {
|
||||
a,
|
||||
f,
|
||||
r1: PhantomData,
|
||||
r2: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, Req, Resp> Clone for PartialNewService<A, F, Req, Resp>
|
||||
where
|
||||
A: NewService + Clone,
|
||||
F: Fn(Req) -> (A::Request, Resp) + Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
a: self.a.clone(),
|
||||
f: self.f.clone(),
|
||||
r1: PhantomData,
|
||||
r2: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, Req, Resp> NewService for PartialNewService<A, F, Req, Resp>
|
||||
where
|
||||
A: NewService,
|
||||
F: Fn(Req) -> (A::Request, Resp) + Clone,
|
||||
{
|
||||
type Request = Req;
|
||||
type Response = (A::Response, Resp);
|
||||
type Error = A::Error;
|
||||
type Service = Partial<A::Service, F, Req, Resp>;
|
||||
|
||||
type InitError = A::InitError;
|
||||
type Future = PartialNewServiceFuture<A, F, Req, Resp>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
PartialNewServiceFuture::new(self.a.new_service(), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PartialNewServiceFuture<A, F, Req, Resp>
|
||||
where
|
||||
A: NewService,
|
||||
F: Fn(Req) -> (A::Request, Resp),
|
||||
{
|
||||
fut: A::Future,
|
||||
f: Option<F>,
|
||||
r1: PhantomData<Req>,
|
||||
r2: PhantomData<Resp>,
|
||||
}
|
||||
|
||||
impl<A, F, Req, Resp> PartialNewServiceFuture<A, F, Req, Resp>
|
||||
where
|
||||
A: NewService,
|
||||
F: Fn(Req) -> (A::Request, Resp),
|
||||
{
|
||||
fn new(fut: A::Future, f: F) -> Self {
|
||||
PartialNewServiceFuture {
|
||||
f: Some(f),
|
||||
fut,
|
||||
r1: PhantomData,
|
||||
r2: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, Req, Resp> Future for PartialNewServiceFuture<A, F, Req, Resp>
|
||||
where
|
||||
A: NewService,
|
||||
F: Fn(Req) -> (A::Request, Resp),
|
||||
{
|
||||
type Item = Partial<A::Service, F, Req, Resp>;
|
||||
type Error = A::InitError;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if let Async::Ready(service) = self.fut.poll()? {
|
||||
Ok(Async::Ready(Partial::new(service, self.f.take().unwrap())))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue