Skip to content

Commit

Permalink
upgrade pin-project
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Jun 15, 2020
1 parent f3ab3a7 commit c5f9697
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 63 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.3] - 2020-04-15

* Upgrade pin-project

## [0.1.2] - 2020-04-27

* Check ready state for map_config_service
Expand Down
5 changes: 3 additions & 2 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.2"
version = "0.1.3"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "ntex service"
keywords = ["network", "framework", "async", "futures"]
Expand All @@ -17,7 +17,8 @@ path = "src/lib.rs"

[dependencies]
futures-util = "0.3.4"
pin-project = "0.4.8"
pin-project = "0.4.20"
pin-project-lite = "0.1.5"

[dev-dependencies]
ntex-rt = "0.1"
16 changes: 8 additions & 8 deletions ntex-service/src/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ where
}
}

#[pin_project::pin_project]
pin_project_lite::pin_project! {
pub(crate) struct AndThenServiceResponse<A, B>
where
A: Service,
Expand All @@ -77,8 +77,9 @@ where
#[pin]
state: State<A, B>,
}
}

#[pin_project::pin_project]
#[pin_project::pin_project(project = StateProject)]
enum State<A, B>
where
A: Service,
Expand All @@ -96,13 +97,11 @@ where
{
type Output = Result<B::Response, A::Error>;

#[pin_project::project]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();

#[project]
match this.state.as_mut().project() {
State::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
Expand All @@ -112,11 +111,11 @@ where
}
Poll::Pending => Poll::Pending,
},
State::B(fut) => fut.poll(cx).map(|r| {
StateProject::B(fut) => fut.poll(cx).map(|r| {
this.state.set(State::Empty);
r
}),
State::Empty => {
StateProject::Empty => {
panic!("future must not be polled after it returned `Poll::Ready`")
}
}
Expand Down Expand Up @@ -204,7 +203,7 @@ where
}
}

#[pin_project::pin_project]
pin_project_lite::pin_project! {
pub(crate) struct AndThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
Expand All @@ -218,6 +217,7 @@ where
a: Option<A::Service>,
b: Option<B::Service>,
}
}

impl<A, B> AndThenServiceFactoryResponse<A, B>
where
Expand Down
18 changes: 9 additions & 9 deletions ntex-service/src/and_then_apply_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ where
}
}

#[pin_project::pin_project]
pin_project_lite::pin_project! {
pub(crate) struct AndThenApplyFnFuture<A, B, F, Fut, Res, Err>
where
A: Service,
Expand All @@ -108,8 +108,9 @@ where
#[pin]
state: State<A, B, F, Fut, Res, Err>,
}
}

#[pin_project::pin_project]
#[pin_project::pin_project(project = StateProject)]
enum State<A, B, F, Fut, Res, Err>
where
A: Service,
Expand All @@ -134,13 +135,11 @@ where
{
type Output = Result<Res, Err>;

#[pin_project::project]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();

#[project]
match this.state.as_mut().project() {
State::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);
Expand All @@ -151,11 +150,11 @@ where
}
Poll::Pending => Poll::Pending,
},
State::B(fut) => fut.poll(cx).map(|r| {
StateProject::B(fut) => fut.poll(cx).map(|r| {
this.state.set(State::Empty);
r
}),
State::Empty => {
StateProject::Empty => {
panic!("future must not be polled after it returned `Poll::Ready`")
}
}
Expand Down Expand Up @@ -224,12 +223,12 @@ where
}
}

#[pin_project::pin_project]
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 + Clone,
F: Fn(A::Response, &B::Service) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
Err: From<A::Error>,
Err: From<B::Error>,
Expand All @@ -242,6 +241,7 @@ where
a: Option<A::Service>,
b: Option<B::Service>,
}
}

impl<A, B, F, Fut, Res, Err> Future
for AndThenApplyFnFactoryResponse<A, B, F, Fut, Res, Err>
Expand Down
3 changes: 2 additions & 1 deletion ntex-service/src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ where
}
}

#[pin_project::pin_project]
pin_project_lite::pin_project! {
pub struct ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
Expand All @@ -176,6 +176,7 @@ where
f: Option<F>,
r: PhantomData<(In, Out)>,
}
}

impl<T, F, R, In, Out, Err> ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
where
Expand Down
13 changes: 6 additions & 7 deletions ntex-service/src/apply_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ where
}
}

#[pin_project::pin_project]
pin_project_lite::pin_project! {
struct ApplyConfigServiceFactoryResponse<F, C, T, R, S>
where
F: Fn(C, &T::Service) -> R,
Expand All @@ -174,8 +174,9 @@ where
#[pin]
state: State<T, R, S>,
}
}

#[pin_project::pin_project]
#[pin_project::pin_project(project = StateProject)]
enum State<T, R, S>
where
T: ServiceFactory<Config = ()>,
Expand All @@ -198,28 +199,26 @@ where
{
type Output = Result<S, T::InitError>;

#[pin_project::project]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();

#[project]
match this.state.as_mut().project() {
State::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));
self.poll(cx)
}
},
State::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));
self.poll(cx)
}
Poll::Pending => Poll::Pending,
},
State::C(fut) => fut.poll(cx),
StateProject::C(fut) => fut.poll(cx),
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions ntex-service/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ where
}
}

#[pin_project::pin_project]
pin_project_lite::pin_project! {
pub struct MapFuture<A, F, Response>
where
A: Service,
Expand All @@ -80,6 +80,7 @@ where
#[pin]
fut: A::Future,
}
}

impl<A, F, Response> MapFuture<A, F, Response>
where
Expand Down Expand Up @@ -166,15 +167,16 @@ 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>,
pin_project_lite::pin_project! {
pub struct MapServiceFuture<A, F, Res>
where
A: ServiceFactory,
F: FnMut(A::Response) -> Res,
{
#[pin]
fut: A::Future,
f: Option<F>,
}
}

impl<A, F, Res> MapServiceFuture<A, F, Res>
Expand Down
15 changes: 7 additions & 8 deletions ntex-service/src/map_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ where
}
}

#[pin_project::pin_project]
pin_project_lite::pin_project! {
pub struct MapConfigServiceResponse<A, M: ServiceFactory, C>
where
A: ServiceFactory,
Expand All @@ -241,8 +241,9 @@ where
#[pin]
state: ResponseState<A, M>,
}
}

#[pin_project::pin_project]
#[pin_project::pin_project(project = ResponseStateProject)]
enum ResponseState<A: ServiceFactory, M: ServiceFactory> {
CreateMapper(#[pin] M::Future),
MapReady,
Expand All @@ -263,33 +264,31 @@ where
{
type Output = Result<A::Service, A::InitError>;

#[pin_project::project]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();

#[project]
match this.state.as_mut().project() {
ResponseState::CreateMapper(fut) => {
ResponseStateProject::CreateMapper(fut) => {
let mapper = ready!(fut.poll(cx))?;
*this.inner.mapper.borrow_mut() = Some(mapper);
this.state.set(ResponseState::MapReady);
self.poll(cx)
}
ResponseState::MapReady => {
ResponseStateProject::MapReady => {
let mapper = this.inner.mapper.borrow();
ready!(mapper.as_ref().unwrap().poll_ready(cx))?;
let fut = mapper.as_ref().unwrap().call(this.config.take().unwrap());
this.state.set(ResponseState::MapConfig(fut));
drop(mapper);
self.poll(cx)
}
ResponseState::MapConfig(fut) => {
ResponseStateProject::MapConfig(fut) => {
let config = ready!(fut.poll(cx))?;
let fut = this.inner.a.new_service(config);
this.state.set(ResponseState::CreateService(fut));
self.poll(cx)
}
ResponseState::CreateService(fut) => fut.poll(cx),
ResponseStateProject::CreateService(fut) => fut.poll(cx),
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions ntex-service/src/map_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
}
}

#[pin_project::pin_project]
pin_project_lite::pin_project! {
pub struct MapErrFuture<A, F, E>
where
A: Service,
Expand All @@ -81,6 +81,7 @@ where
#[pin]
fut: A::Future,
}
}

impl<A, F, E> MapErrFuture<A, F, E>
where
Expand Down Expand Up @@ -168,7 +169,7 @@ where
}
}

#[pin_project::pin_project]
pin_project_lite::pin_project! {
pub struct MapErrServiceFuture<A, F, E>
where
A: ServiceFactory,
Expand All @@ -178,6 +179,7 @@ where
fut: A::Future,
f: F,
}
}

impl<A, F, E> MapErrServiceFuture<A, F, E>
where
Expand Down
3 changes: 2 additions & 1 deletion ntex-service/src/map_init_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where
}
}

#[pin_project::pin_project]
pin_project_lite::pin_project! {
pub struct MapInitErrFuture<A, F, E>
where
A: ServiceFactory,
Expand All @@ -70,6 +70,7 @@ where
#[pin]
fut: A::Future,
}
}

impl<A, F, E> MapInitErrFuture<A, F, E>
where
Expand Down
Loading

0 comments on commit c5f9697

Please sign in to comment.