1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-26 22:07:42 +02:00

cleanup Unpin constraint; simplify Framed impl

This commit is contained in:
Nikolay Kim
2019-11-19 14:51:40 +06:00
parent 617e40a7e9
commit 3bf83c1d98
30 changed files with 493 additions and 1252 deletions

View File

@ -24,7 +24,8 @@ path = "src/lib.rs"
[dependencies]
futures = "0.3.1"
pin-project = "0.4.5"
[dev-dependencies]
tokio = "0.2.0-alpha.5"
tokio = "0.2.0-alpha.6"
actix-rt = "0.2"

View File

@ -41,8 +41,6 @@ impl<A, B> Service for AndThenService<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A::Future: Unpin,
B::Future: Unpin,
{
type Request = A::Request;
type Response = B::Response;
@ -63,13 +61,16 @@ where
}
}
#[pin_project::pin_project]
pub struct AndThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
b: Cell<B>,
#[pin]
fut_b: Option<B::Future>,
#[pin]
fut_a: Option<A::Future>,
}
@ -77,8 +78,6 @@ impl<A, B> AndThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A::Future: Unpin,
B::Future: Unpin,
{
fn new(a: A::Future, b: Cell<B>) -> Self {
AndThenServiceResponse {
@ -93,23 +92,27 @@ impl<A, B> Future for AndThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A::Future: Unpin,
B::Future: Unpin,
{
type Output = Result<B::Response, A::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.get_mut();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
loop {
if let Some(ref mut fut) = this.fut_b {
return Pin::new(fut).poll(cx);
if let Some(fut) = this.fut_b.as_pin_mut() {
return fut.poll(cx);
}
match Pin::new(&mut this.fut_a.as_mut().expect("Bug in actix-service")).poll(cx) {
match this
.fut_a
.as_pin_mut()
.expect("Bug in actix-service")
.poll(cx)
{
Poll::Ready(Ok(resp)) => {
let _ = this.fut_a.take();
this.fut_b = Some(this.b.get_mut().call(resp));
this = self.as_mut().project();
this.fut_a.set(None);
this.fut_b.set(Some(this.b.get_mut().call(resp)));
}
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
Poll::Pending => return Poll::Pending,
@ -153,10 +156,6 @@ where
Error = A::Error,
InitError = A::InitError,
>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
type Request = A::Request;
type Response = B::Response;
@ -185,12 +184,15 @@ where
}
}
#[pin_project::pin_project]
pub struct AndThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response>,
{
#[pin]
fut_b: B::Future,
#[pin]
fut_a: A::Future,
a: Option<A::Service>,
@ -201,10 +203,6 @@ impl<A, B> AndThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
AndThenServiceFactoryResponse {
@ -216,39 +214,24 @@ where
}
}
impl<A, B> Unpin for AndThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response, Error = A::Error, InitError = A::InitError>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
}
impl<A, B> Future for AndThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response, Error = A::Error, InitError = A::InitError>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
type Output = Result<AndThenService<A::Service, B::Service>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let this = self.project();
if this.a.is_none() {
if let Poll::Ready(service) = Pin::new(&mut this.fut_a).poll(cx)? {
this.a = Some(service);
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
*this.a = Some(service);
}
}
if this.b.is_none() {
if let Poll::Ready(service) = Pin::new(&mut this.fut_b).poll(cx)? {
this.b = Some(service);
if let Poll::Ready(service) = this.fut_b.poll(cx)? {
*this.b = Some(service);
}
}
if this.a.is_some() && this.b.is_some() {

View File

@ -23,8 +23,7 @@ pub fn apply_fn_factory<T, F, R, In, Out, Err, U>(
) -> ApplyServiceFactory<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
T::Future: Unpin,
F: FnMut(In, &mut T::Service) -> R + Unpin + Clone,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
U: IntoServiceFactory<T>,
{
@ -106,8 +105,7 @@ where
impl<T, F, R, In, Out, Err> ServiceFactory for ApplyServiceFactory<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
T::Future: Unpin,
F: FnMut(In, &mut T::Service) -> R + Unpin + Clone,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
{
type Request = In;
@ -124,12 +122,14 @@ where
}
}
#[pin_project::pin_project]
pub struct ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
{
#[pin]
fut: T::Future,
f: Option<F>,
r: PhantomData<(In, Out)>,
@ -150,28 +150,18 @@ where
}
}
impl<T, F, R, In, Out, Err> Unpin for ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
T::Future: Unpin,
F: FnMut(In, &mut T::Service) -> R + Unpin + Clone,
R: Future<Output = Result<Out, Err>>,
{
}
impl<T, F, R, In, Out, Err> Future for ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
T::Future: Unpin,
F: FnMut(In, &mut T::Service) -> R + Unpin + Clone,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
{
type Output = Result<Apply<T::Service, F, R, In, Out, Err>, T::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let this = self.project();
if let Poll::Ready(svc) = Pin::new(&mut this.fut).poll(cx)? {
if let Poll::Ready(svc) = this.fut.poll(cx)? {
Poll::Ready(Ok(Apply::new(svc, this.f.take().unwrap())))
} else {
Poll::Pending

View File

@ -11,7 +11,7 @@ pub fn apply_cfg<F, C, T, R, S, E>(srv: T, f: F) -> ApplyConfigService<F, C, T,
where
F: FnMut(&C, &mut T) -> R,
T: Service,
R: Future<Output = Result<S, E>> + Unpin,
R: Future<Output = Result<S, E>>,
S: Service,
{
ApplyConfigService {
@ -75,8 +75,7 @@ impl<F, C, T, R, S, E> ServiceFactory for ApplyConfigService<F, C, T, R, S, E>
where
F: FnMut(&C, &mut T) -> R,
T: Service,
T::Future: Unpin,
R: Future<Output = Result<S, E>> + Unpin,
R: Future<Output = Result<S, E>>,
S: Service,
{
type Config = C;
@ -86,41 +85,10 @@ where
type Service = S;
type InitError = E;
type Future = ApplyConfigServiceResponse<R, S, E>;
type Future = R;
fn new_service(&self, cfg: &C) -> Self::Future {
ApplyConfigServiceResponse {
fut: unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) },
_t: PhantomData,
}
}
}
pub struct ApplyConfigServiceResponse<R, S, E>
where
R: Future<Output = Result<S, E>>,
S: Service,
{
fut: R,
_t: PhantomData<(S,)>,
}
impl<R, S, E> Unpin for ApplyConfigServiceResponse<R, S, E>
where
R: Future<Output = Result<S, E>> + Unpin,
S: Service,
{
}
impl<R, S, E> Future for ApplyConfigServiceResponse<R, S, E>
where
R: Future<Output = Result<S, E>> + Unpin,
S: Service,
{
type Output = Result<S, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.get_mut().fut).poll(cx)
unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) }
}
}
@ -160,9 +128,8 @@ where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::Future: Unpin,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>> + Unpin,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
{
type Config = C;
@ -186,6 +153,7 @@ where
}
}
#[pin_project::pin_project]
pub struct ApplyConfigServiceFactoryResponse<F, C, T, R, S>
where
C: Clone,
@ -198,56 +166,48 @@ where
cfg: C,
f: Cell<F>,
srv: Option<T::Service>,
#[pin]
srv_fut: Option<T::Future>,
#[pin]
fut: Option<R>,
_t: PhantomData<(S,)>,
}
impl<F, C, T, R, S> Unpin for ApplyConfigServiceFactoryResponse<F, C, T, R, S>
where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::Future: Unpin,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>> + Unpin,
S: Service,
{
}
impl<F, C, T, R, S> Future for ApplyConfigServiceFactoryResponse<F, C, T, R, S>
where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::Future: Unpin,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>> + Unpin,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
{
type Output = Result<S, T::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
loop {
if let Some(ref mut fut) = this.srv_fut {
match Pin::new(fut).poll(cx)? {
if let Some(fut) = this.srv_fut.as_pin_mut() {
match fut.poll(cx)? {
Poll::Pending => return Poll::Pending,
Poll::Ready(srv) => {
let _ = this.srv_fut.take();
this.srv = Some(srv);
this = self.as_mut().project();
this.srv_fut.set(None);
*this.srv = Some(srv);
continue;
}
}
}
if let Some(ref mut fut) = this.fut {
return Pin::new(fut).poll(cx);
} else if let Some(ref mut srv) = this.srv {
if let Some(fut) = this.fut.as_pin_mut() {
return fut.poll(cx);
} else if let Some(srv) = this.srv {
match srv.poll_ready(cx)? {
Poll::Ready(_) => {
this.fut = Some(this.f.get_mut()(&this.cfg, srv));
let fut = this.f.get_mut()(&this.cfg, srv);
this = self.as_mut().project();
this.fut.set(Some(fut));
continue;
}
Poll::Pending => return Poll::Pending,

View File

@ -1,6 +1,5 @@
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures::future::{ok, Ready};
@ -188,7 +187,7 @@ where
impl<F, Fut, Cfg, Srv, Err> ServiceFactory for FnServiceConfig<F, Fut, Cfg, Srv, Err>
where
F: Fn(&Cfg) -> Fut,
Fut: Future<Output = Result<Srv, Err>> + Unpin,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service,
{
type Request = Srv::Request;
@ -198,41 +197,10 @@ where
type Config = Cfg;
type Service = Srv;
type InitError = Err;
type Future = NewServiceFnConfigFut<Fut, Srv, Err>;
type Future = Fut;
fn new_service(&self, cfg: &Cfg) -> Self::Future {
NewServiceFnConfigFut {
fut: (self.f)(cfg),
_t: PhantomData,
}
}
}
pub struct NewServiceFnConfigFut<R, S, E>
where
R: Future<Output = Result<S, E>> + Unpin,
S: Service,
{
fut: R,
_t: PhantomData<(S,)>,
}
impl<R, S, E> Unpin for NewServiceFnConfigFut<R, S, E>
where
R: Future<Output = Result<S, E>> + Unpin,
S: Service,
{
}
impl<R, S, E> Future for NewServiceFnConfigFut<R, S, E>
where
R: Future<Output = Result<S, E>> + Unpin,
S: Service,
{
type Output = Result<S, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.get_mut().fut).poll(cx)
(self.f)(cfg)
}
}

View File

@ -73,7 +73,7 @@ pub trait Service {
fn map<F, R>(self, f: F) -> crate::dev::Map<Self, F, R>
where
Self: Sized,
F: FnMut(Self::Response) -> R + Unpin,
F: FnMut(Self::Response) -> R,
{
crate::dev::Map::new(self, f)
}
@ -138,7 +138,7 @@ pub trait ServiceFactory {
fn map<F, R>(self, f: F) -> crate::map::MapServiceFactory<Self, F, R>
where
Self: Sized,
F: FnMut(Self::Response) -> R + Unpin + Clone,
F: FnMut(Self::Response) -> R + Clone,
{
crate::map::MapServiceFactory::new(self, f)
}
@ -147,7 +147,7 @@ pub trait ServiceFactory {
fn map_err<F, E>(self, f: F) -> crate::map_err::MapErrServiceFactory<Self, F, E>
where
Self: Sized,
F: Fn(Self::Error) -> E + Unpin + Clone,
F: Fn(Self::Error) -> E + Clone,
{
crate::map_err::MapErrServiceFactory::new(self, f)
}
@ -156,7 +156,7 @@ pub trait ServiceFactory {
fn map_init_err<F, E>(self, f: F) -> crate::map_init_err::MapInitErr<Self, F, E>
where
Self: Sized,
F: Fn(Self::InitError) -> E + Unpin + Clone,
F: Fn(Self::InitError) -> E + Clone,
{
crate::map_init_err::MapInitErr::new(self, f)
}

View File

@ -46,8 +46,7 @@ where
impl<A, F, Response> Service for Map<A, F, Response>
where
A: Service,
A::Future: Unpin,
F: FnMut(A::Response) -> Response + Unpin + Clone,
F: FnMut(A::Response) -> Response + Clone,
{
type Request = A::Request;
type Response = Response;
@ -63,12 +62,14 @@ where
}
}
#[pin_project::pin_project]
pub struct MapFuture<A, F, Response>
where
A: Service,
F: FnMut(A::Response) -> Response,
{
f: F,
#[pin]
fut: A::Future,
}
@ -85,15 +86,14 @@ where
impl<A, F, Response> Future for MapFuture<A, F, Response>
where
A: Service,
A::Future: Unpin,
F: FnMut(A::Response) -> Response + Unpin,
F: FnMut(A::Response) -> Response,
{
type Output = Result<Response, A::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let this = self.project();
match Pin::new(&mut this.fut).poll(cx) {
match this.fut.poll(cx) {
Poll::Ready(Ok(resp)) => Poll::Ready(Ok((this.f)(resp))),
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
Poll::Pending => Poll::Pending,
@ -113,7 +113,7 @@ impl<A, F, Res> MapServiceFactory<A, F, Res> {
pub(crate) fn new(a: A, f: F) -> Self
where
A: ServiceFactory,
F: FnMut(A::Response) -> Res + Unpin,
F: FnMut(A::Response) -> Res,
{
Self {
a,
@ -140,9 +140,7 @@ where
impl<A, F, Res> ServiceFactory for MapServiceFactory<A, F, Res>
where
A: ServiceFactory,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
F: FnMut(A::Response) -> Res + Unpin + Clone,
F: FnMut(A::Response) -> Res + Clone,
{
type Request = A::Request;
type Response = Res;
@ -158,11 +156,13 @@ where
}
}
#[pin_project::pin_project]
pub struct MapServiceFuture<A, F, Res>
where
A: ServiceFactory,
F: FnMut(A::Response) -> Res,
{
#[pin]
fut: A::Future,
f: Option<F>,
}
@ -170,7 +170,7 @@ where
impl<A, F, Res> MapServiceFuture<A, F, Res>
where
A: ServiceFactory,
F: FnMut(A::Response) -> Res + Unpin,
F: FnMut(A::Response) -> Res,
{
fn new(fut: A::Future, f: F) -> Self {
MapServiceFuture { f: Some(f), fut }
@ -180,15 +180,14 @@ where
impl<A, F, Res> Future for MapServiceFuture<A, F, Res>
where
A: ServiceFactory,
A::Future: Unpin,
F: FnMut(A::Response) -> Res + Unpin,
F: FnMut(A::Response) -> Res,
{
type Output = Result<Map<A::Service, F, Res>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let this = self.project();
if let Poll::Ready(svc) = Pin::new(&mut this.fut).poll(cx)? {
if let Poll::Ready(svc) = this.fut.poll(cx)? {
Poll::Ready(Ok(Map::new(svc, this.f.take().unwrap())))
} else {
Poll::Pending

View File

@ -47,8 +47,7 @@ where
impl<A, F, E> Service for MapErr<A, F, E>
where
A: Service,
A::Future: Unpin,
F: Fn(A::Error) -> E + Unpin + Clone,
F: Fn(A::Error) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
@ -64,21 +63,21 @@ where
}
}
#[pin_project::pin_project]
pub struct MapErrFuture<A, F, E>
where
A: Service,
A::Future: Unpin,
F: Fn(A::Error) -> E + Unpin,
F: Fn(A::Error) -> E,
{
f: F,
#[pin]
fut: A::Future,
}
impl<A, F, E> MapErrFuture<A, F, E>
where
A: Service,
A::Future: Unpin,
F: Fn(A::Error) -> E + Unpin,
F: Fn(A::Error) -> E,
{
fn new(fut: A::Future, f: F) -> Self {
MapErrFuture { f, fut }
@ -88,14 +87,13 @@ where
impl<A, F, E> Future for MapErrFuture<A, F, E>
where
A: Service,
A::Future: Unpin,
F: Fn(A::Error) -> E + Unpin,
F: Fn(A::Error) -> E,
{
type Output = Result<A::Response, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
Pin::new(&mut this.fut).poll(cx).map_err(&this.f)
let this = self.project();
this.fut.poll(cx).map_err(this.f)
}
}
@ -145,9 +143,7 @@ where
impl<A, F, E> ServiceFactory for MapErrServiceFactory<A, F, E>
where
A: ServiceFactory,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
F: Fn(A::Error) -> E + Unpin + Clone,
F: Fn(A::Error) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
@ -163,11 +159,13 @@ where
}
}
#[pin_project::pin_project]
pub struct MapErrServiceFuture<A, F, E>
where
A: ServiceFactory,
F: Fn(A::Error) -> E,
{
#[pin]
fut: A::Future,
f: F,
}
@ -185,14 +183,13 @@ where
impl<A, F, E> Future for MapErrServiceFuture<A, F, E>
where
A: ServiceFactory,
A::Future: Unpin,
F: Fn(A::Error) -> E + Unpin + Clone,
F: Fn(A::Error) -> E + Clone,
{
type Output = Result<MapErr<A::Service, F, E>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
if let Poll::Ready(svc) = Pin::new(&mut this.fut).poll(cx)? {
let this = self.project();
if let Poll::Ready(svc) = this.fut.poll(cx)? {
Poll::Ready(Ok(MapErr::new(svc, this.f.clone())))
} else {
Poll::Pending

View File

@ -44,8 +44,7 @@ where
impl<A, F, E> ServiceFactory for MapInitErr<A, F, E>
where
A: ServiceFactory,
A::Future: Unpin,
F: Fn(A::InitError) -> E + Unpin + Clone,
F: Fn(A::InitError) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
@ -61,12 +60,14 @@ where
}
}
#[pin_project::pin_project]
pub struct MapInitErrFuture<A, F, E>
where
A: ServiceFactory,
F: Fn(A::InitError) -> E,
{
f: F,
#[pin]
fut: A::Future,
}
@ -83,13 +84,12 @@ where
impl<A, F, E> Future for MapInitErrFuture<A, F, E>
where
A: ServiceFactory,
A::Future: Unpin,
F: Fn(A::InitError) -> E + Unpin,
F: Fn(A::InitError) -> E,
{
type Output = Result<A::Service, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
Pin::new(&mut this.fut).poll(cx).map_err(&this.f)
let this = self.project();
this.fut.poll(cx).map_err(this.f)
}
}

View File

@ -41,8 +41,6 @@ impl<A, B> Service for ThenService<A, B>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
A::Future: Unpin,
B::Future: Unpin,
{
type Request = A::Request;
type Response = B::Response;
@ -63,13 +61,16 @@ where
}
}
#[pin_project::pin_project]
pub struct ThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
{
b: Cell<B>,
#[pin]
fut_b: Option<B::Future>,
#[pin]
fut_a: Option<A::Future>,
}
@ -77,8 +78,6 @@ impl<A, B> ThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
A::Future: Unpin,
B::Future: Unpin,
{
fn new(a: A::Future, b: Cell<B>) -> Self {
ThenServiceResponse {
@ -93,22 +92,26 @@ impl<A, B> Future for ThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
A::Future: Unpin,
B::Future: Unpin,
{
type Output = Result<B::Response, B::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
loop {
if let Some(ref mut fut) = this.fut_b {
return Pin::new(fut).poll(cx);
if let Some(fut) = this.fut_b.as_pin_mut() {
return fut.poll(cx);
}
match Pin::new(this.fut_a.as_mut().expect("Bug in actix-service")).poll(cx) {
match this
.fut_a
.as_pin_mut()
.expect("Bug in actix-service")
.poll(cx)
{
Poll::Ready(r) => {
this.fut_b = Some(this.b.get_mut().call(r));
this = self.as_mut().project();
this.fut_b.set(Some(this.b.get_mut().call(r)));
}
Poll::Pending => return Poll::Pending,
@ -148,10 +151,6 @@ where
Error = A::Error,
InitError = A::InitError,
>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
type Request = A::Request;
type Response = B::Response;
@ -180,6 +179,7 @@ where
}
}
#[pin_project::pin_project]
pub struct ThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
@ -190,7 +190,9 @@ where
InitError = A::InitError,
>,
{
#[pin]
fut_b: B::Future,
#[pin]
fut_a: A::Future,
a: Option<A::Service>,
b: Option<B::Service>,
@ -205,10 +207,6 @@ where
Error = A::Error,
InitError = A::InitError,
>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
Self {
@ -220,22 +218,6 @@ where
}
}
impl<A, B> Unpin for ThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
}
impl<A, B> Future for ThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
@ -245,24 +227,20 @@ where
Error = A::Error,
InitError = A::InitError,
>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
type Output = Result<ThenService<A::Service, B::Service>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let this = self.project();
if this.a.is_none() {
if let Poll::Ready(service) = Pin::new(&mut this.fut_a).poll(cx)? {
this.a = Some(service);
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
*this.a = Some(service);
}
}
if this.b.is_none() {
if let Poll::Ready(service) = Pin::new(&mut this.fut_b).poll(cx)? {
this.b = Some(service);
if let Poll::Ready(service) = this.fut_b.poll(cx)? {
*this.b = Some(service);
}
}
if this.a.is_some() && this.b.is_some() {

View File

@ -76,9 +76,7 @@ where
pub fn apply<T, S, U>(t: T, service: U) -> ApplyTransform<T, S>
where
S: ServiceFactory,
S::Future: Unpin,
T: Transform<S::Service, InitError = S::InitError>,
T::Future: Unpin,
U: IntoServiceFactory<S>,
{
ApplyTransform::new(t, service.into_factory())
@ -116,9 +114,7 @@ impl<T, S> Clone for ApplyTransform<T, S> {
impl<T, S> ServiceFactory for ApplyTransform<T, S>
where
S: ServiceFactory,
S::Future: Unpin,
T: Transform<S::Service, InitError = S::InitError>,
T::Future: Unpin,
{
type Request = T::Request;
type Response = T::Response;
@ -138,12 +134,15 @@ where
}
}
#[pin_project::pin_project]
pub struct ApplyTransformFuture<T, S>
where
S: ServiceFactory,
T: Transform<S::Service, InitError = S::InitError>,
{
#[pin]
fut_a: S::Future,
#[pin]
fut_t: Option<T::Future>,
t_cell: Rc<T>,
}
@ -151,27 +150,24 @@ where
impl<T, S> Future for ApplyTransformFuture<T, S>
where
S: ServiceFactory,
S::Future: Unpin,
T: Transform<S::Service, InitError = S::InitError>,
T::Future: Unpin,
{
type Output = Result<T::Transform, T::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.get_mut();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
if this.fut_t.is_none() {
if let Poll::Ready(service) = Pin::new(&mut this.fut_a).poll(cx)? {
this.fut_t = Some(this.t_cell.new_transform(service));
} else {
return Poll::Pending;
}
if let Some(fut) = this.fut_t.as_pin_mut() {
return fut.poll(cx);
}
if let Some(ref mut fut) = this.fut_t {
Pin::new(fut).poll(cx)
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
let fut = this.t_cell.new_transform(service);
this = self.as_mut().project();
this.fut_t.set(Some(fut));
this.fut_t.as_pin_mut().unwrap().poll(cx)
} else {
Poll::Pending
return Poll::Pending;
}
}
}