Skip to content

Commit

Permalink
Use pin-project-lite instead of pin-project
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Jan 12, 2021
1 parent 3987fbe commit c0d0c2b
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 194 deletions.
4 changes: 4 additions & 0 deletions ntex-service/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [0.1.5] - 2021-01-13

* Use pin-project-lite instead of pin-project

## [0.1.4] - 2020-09-24

* Add `fn_transform` fn, allows to use function as transform service
Expand Down
7 changes: 3 additions & 4 deletions ntex-service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-service"
version = "0.1.4"
version = "0.1.5"
authors = ["ntex contributors <[email protected]>"]
description = "ntex service"
keywords = ["network", "framework", "async", "futures"]
Expand All @@ -16,9 +16,8 @@ name = "ntex_service"
path = "src/lib.rs"

[dependencies]
futures-util = "0.3.5"
pin-project = "0.4.20"
pin-project-lite = "0.1.5"
futures-util = "0.3.9"
pin-project-lite = "0.2.4"

[dev-dependencies]
ntex-rt = "0.1"
71 changes: 38 additions & 33 deletions ntex-service/src/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,36 @@ where
#[inline]
fn call(&self, req: A::Request) -> Self::Future {
AndThenServiceResponse {
state: State::A(self.0.as_ref().0.call(req), Some(self.0.clone())),
state: State::A {
fut: self.0.as_ref().0.call(req),
b: Some(self.0.clone()),
},
}
}
}

pin_project_lite::pin_project! {
pub(crate) struct AndThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
#[pin]
state: State<A, B>,
}
pub(crate) struct AndThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
#[pin]
state: State<A, B>,
}
}

#[pin_project::pin_project(project = StateProject)]
enum State<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
A(#[pin] A::Future, Option<Rc<(A, B)>>),
B(#[pin] B::Future),
Empty,
pin_project_lite::pin_project! {
#[project = StateProject]
enum State<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
A { #[pin] fut: A::Future, b: Option<Rc<(A, B)>> },
B { #[pin] fut: B::Future },
Empty,
}
}

impl<A, B> Future for AndThenServiceResponse<A, B>
Expand All @@ -101,17 +106,17 @@ where
let mut this = self.as_mut().project();

match this.state.as_mut().project() {
StateProject::A(fut, b) => match fut.poll(cx)? {
StateProject::A { fut, b } => match fut.poll(cx)? {
Poll::Ready(res) => {
let b = b.take().unwrap();
this.state.set(State::Empty); // drop fut A
let fut = b.as_ref().1.call(res);
this.state.set(State::B(fut));
this.state.set(State::B { fut });
self.poll(cx)
}
Poll::Pending => Poll::Pending,
},
StateProject::B(fut) => fut.poll(cx).map(|r| {
StateProject::B { fut } => fut.poll(cx).map(|r| {
this.state.set(State::Empty);
r
}),
Expand Down Expand Up @@ -204,19 +209,19 @@ where
}

pin_project_lite::pin_project! {
pub(crate) struct AndThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response>,
{
#[pin]
fut_a: A::Future,
#[pin]
fut_b: B::Future,
pub(crate) struct AndThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response>,
{
#[pin]
fut_a: A::Future,
#[pin]
fut_b: B::Future,

a: Option<A::Service>,
b: Option<B::Service>,
}
a: Option<A::Service>,
b: Option<B::Service>,
}
}

impl<A, B> AndThenServiceFactoryResponse<A, B>
Expand Down
97 changes: 51 additions & 46 deletions ntex-service/src/and_then_apply_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,39 +90,44 @@ where
fn call(&self, req: A::Request) -> Self::Future {
let fut = self.srv.as_ref().0.call(req);
AndThenApplyFnFuture {
state: State::A(fut, Some(self.srv.clone())),
state: State::A {
fut,
b: Some(self.srv.clone()),
},
}
}
}

pin_project_lite::pin_project! {
pub(crate) struct AndThenApplyFnFuture<A, B, F, Fut, Res, Err>
where
A: Service,
B: Service,
F: Fn(A::Response, &B) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error>,
Err: From<B::Error>,
{
#[pin]
state: State<A, B, F, Fut, Res, Err>,
}
pub(crate) struct AndThenApplyFnFuture<A, B, F, Fut, Res, Err>
where
A: Service,
B: Service,
F: Fn(A::Response, &B) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error>,
Err: From<B::Error>,
{
#[pin]
state: State<A, B, F, Fut, Res, Err>,
}
}

#[pin_project::pin_project(project = StateProject)]
enum State<A, B, F, Fut, Res, Err>
where
A: Service,
B: Service,
F: Fn(A::Response, &B) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error>,
Err: From<B::Error>,
{
A(#[pin] A::Future, Option<Rc<(A, B, F)>>),
B(#[pin] Fut),
Empty,
pin_project_lite::pin_project! {
#[project = StateProject]
enum State<A, B, F, Fut, Res, Err>
where
A: Service,
B: Service,
F: Fn(A::Response, &B) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error>,
Err: From<B::Error>,
{
A { #[pin] fut: A::Future, b: Option<Rc<(A, B, F)>> },
B { #[pin] fut: Fut },
Empty,
}
}

impl<A, B, F, Fut, Res, Err> Future for AndThenApplyFnFuture<A, B, F, Fut, Res, Err>
Expand All @@ -139,18 +144,18 @@ where
let mut this = self.as_mut().project();

match this.state.as_mut().project() {
StateProject::A(fut, b) => match fut.poll(cx)? {
StateProject::A { fut, b } => match fut.poll(cx)? {
Poll::Ready(res) => {
let b = b.take().unwrap();
this.state.set(State::Empty);
let b = b.as_ref();
let fut = (&b.2)(res, &b.1);
this.state.set(State::B(fut));
this.state.set(State::B { fut });
self.poll(cx)
}
Poll::Pending => Poll::Pending,
},
StateProject::B(fut) => fut.poll(cx).map(|r| {
StateProject::B { fut } => fut.poll(cx).map(|r| {
this.state.set(State::Empty);
r
}),
Expand Down Expand Up @@ -224,23 +229,23 @@ where
}

pin_project_lite::pin_project! {
pub(crate) struct AndThenApplyFnFactoryResponse<A, B, F, Fut, Res, Err>
where
A: ServiceFactory,
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
F: Fn(A::Response, &B::Service) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error>,
Err: From<B::Error>,
{
#[pin]
fut_b: B::Future,
#[pin]
fut_a: A::Future,
f: F,
a: Option<A::Service>,
b: Option<B::Service>,
}
pub(crate) struct AndThenApplyFnFactoryResponse<A, B, F, Fut, Res, Err>
where
A: ServiceFactory,
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
F: Fn(A::Response, &B::Service) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error>,
Err: From<B::Error>,
{
#[pin]
fut_b: B::Future,
#[pin]
fut_a: A::Future,
f: F,
a: Option<A::Service>,
b: Option<B::Service>,
}
}

impl<A, B, F, Fut, Res, Err> Future
Expand Down
64 changes: 34 additions & 30 deletions ntex-service/src/apply_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,38 +155,42 @@ where
ApplyConfigServiceFactoryResponse {
cfg: Some(cfg),
store: self.srv.clone(),
state: State::A(self.srv.as_ref().0.new_service(())),
state: State::A {
fut: self.srv.as_ref().0.new_service(()),
},
}
}
}

pin_project_lite::pin_project! {
struct ApplyConfigServiceFactoryResponse<F, C, T, R, S>
where
F: Fn(C, &T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
{
cfg: Option<C>,
store: Rc<(T, F)>,
#[pin]
state: State<T, R, S>,
}
struct ApplyConfigServiceFactoryResponse<F, C, T, R, S>
where
F: Fn(C, &T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
{
cfg: Option<C>,
store: Rc<(T, F)>,
#[pin]
state: State<T, R, S>,
}
}

#[pin_project::pin_project(project = StateProject)]
enum State<T, R, S>
where
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
{
A(#[pin] T::Future),
B(T::Service),
C(#[pin] R),
pin_project_lite::pin_project! {
#[project = StateProject]
enum State<T, R, S>
where
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
{
A { #[pin] fut: T::Future },
B { srv: T::Service },
C { #[pin] fut: R },
}
}

impl<F, C, T, R, S> Future for ApplyConfigServiceFactoryResponse<F, C, T, R, S>
Expand All @@ -203,22 +207,22 @@ where
let mut this = self.as_mut().project();

match this.state.as_mut().project() {
StateProject::A(fut) => match fut.poll(cx)? {
StateProject::A { fut } => match fut.poll(cx)? {
Poll::Pending => Poll::Pending,
Poll::Ready(srv) => {
this.state.set(State::B(srv));
this.state.set(State::B { srv });
self.poll(cx)
}
},
StateProject::B(srv) => match srv.poll_ready(cx)? {
StateProject::B { srv } => match srv.poll_ready(cx)? {
Poll::Ready(_) => {
let fut = (this.store.as_ref().1)(this.cfg.take().unwrap(), srv);
this.state.set(State::C(fut));
this.state.set(State::C { fut });
self.poll(cx)
}
Poll::Pending => Poll::Pending,
},
StateProject::C(fut) => fut.poll(cx),
StateProject::C { fut } => fut.poll(cx),
}
}
}
Expand Down
Loading

0 comments on commit c0d0c2b

Please sign in to comment.