Skip to content

Commit

Permalink
service trait takes shared self reference (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeshadow authored Jan 23, 2021
1 parent 874e5f2 commit 636cef8
Show file tree
Hide file tree
Showing 27 changed files with 225 additions and 219 deletions.
3 changes: 3 additions & 0 deletions actix-router/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changes

## Unreleased - 2021-xx-xx
* Add `Router::recognize_checked` [#247]

[#247]: https://github.com/actix/actix-net/pull/247


## 0.2.6 - 2021-01-09
Expand Down
18 changes: 18 additions & 0 deletions actix-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ impl<T, U> Router<T, U> {
None
}

pub fn recognize_checked<R, P, F>(
&self,
resource: &mut R,
check: F,
) -> Option<(&T, ResourceId)>
where
F: Fn(&R, &Option<U>) -> bool,
R: Resource<P>,
P: ResourcePath,
{
for item in self.0.iter() {
if item.0.match_path_checked(resource, &check, &item.2) {
return Some((&item.1, ResourceId(item.0.id())));
}
}
None
}

pub fn recognize_mut_checked<R, P, F>(
&mut self,
resource: &mut R,
Expand Down
1 change: 1 addition & 0 deletions actix-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ slab = "0.4"
tokio = { version = "1", features = ["sync"] }

[dev-dependencies]
actix-rt = "2.0.0-beta.2"
bytes = "1"
env_logger = "0.8"
futures-util = { version = "0.3.7", default-features = false, features = ["sink"] }
Expand Down
4 changes: 2 additions & 2 deletions actix-server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ where
type Error = ();
type Future = Ready<Result<(), ()>>;

fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(ctx).map_err(|_| ())
}

fn call(&mut self, (guard, req): (Option<CounterGuard>, MioStream)) -> Self::Future {
fn call(&self, (guard, req): (Option<CounterGuard>, MioStream)) -> Self::Future {
ready(match FromStream::from_mio(req) {
Ok(stream) => {
let f = self.service.call(stream);
Expand Down
6 changes: 6 additions & 0 deletions actix-service/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changes

## Unreleased - 2021-xx-xx
* `Service::poll_ready` and `Service::call` take `&self`. [#247]
* `apply_fn` and `apply_fn_factory` would take `Fn(Req, &Service)` function type [#247]
* `apply_cfg` and `apply_cfg_factory` would take `Fn(Req, &Service)` function type [#247]
* `fn_service` module would take `Fn(Req)` function type. [#247]

[#247]: https://github.com/actix/actix-net/pull/247


## 2.0.0-beta.3 - 2021-01-09
Expand Down
60 changes: 25 additions & 35 deletions actix-service/src/and_then.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use alloc::rc::Rc;
use core::{
cell::RefCell,
future::Future,
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
};

use alloc::rc::Rc;
use futures_core::ready;
use pin_project_lite::pin_project;

use super::{Service, ServiceFactory};
Expand All @@ -15,7 +15,7 @@ use super::{Service, ServiceFactory};
/// of another service which completes successfully.
///
/// This is created by the `Pipeline::and_then` method.
pub(crate) struct AndThenService<A, B, Req>(Rc<RefCell<(A, B)>>, PhantomData<Req>);
pub(crate) struct AndThenService<A, B, Req>(Rc<(A, B)>, PhantomData<Req>);

impl<A, B, Req> AndThenService<A, B, Req> {
/// Create new `AndThen` combinator
Expand All @@ -24,7 +24,7 @@ impl<A, B, Req> AndThenService<A, B, Req> {
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
Self(Rc::new(RefCell::new((a, b))), PhantomData)
Self(Rc::new((a, b)), PhantomData)
}
}

Expand All @@ -43,20 +43,20 @@ where
type Error = A::Error;
type Future = AndThenServiceResponse<A, B, Req>;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let mut srv = self.0.borrow_mut();
let not_ready = !srv.0.poll_ready(cx)?.is_ready();
if !srv.1.poll_ready(cx)?.is_ready() || not_ready {
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let (a, b) = &*self.0;
let not_ready = !a.poll_ready(cx)?.is_ready();
if !b.poll_ready(cx)?.is_ready() || not_ready {
Poll::Pending
} else {
Poll::Ready(Ok(()))
}
}

fn call(&mut self, req: Req) -> Self::Future {
fn call(&self, req: Req) -> Self::Future {
AndThenServiceResponse {
state: State::A {
fut: self.0.borrow_mut().0.call(req),
fut: self.0 .0.call(req),
b: Some(self.0.clone()),
},
}
Expand Down Expand Up @@ -84,13 +84,12 @@ pin_project! {
A {
#[pin]
fut: A::Future,
b: Option<Rc<RefCell<(A, B)>>>,
b: Option<Rc<(A, B)>>,
},
B {
#[pin]
fut: B::Future,
},
Empty,
}
}

Expand All @@ -105,23 +104,14 @@ where
let mut this = self.as_mut().project();

match this.state.as_mut().project() {
StateProj::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.borrow_mut().1.call(res);
this.state.set(State::B { fut });
self.poll(cx)
}
Poll::Pending => Poll::Pending,
},
StateProj::B { fut } => fut.poll(cx).map(|r| {
this.state.set(State::Empty);
r
}),
StateProj::Empty => {
panic!("future must not be polled after it returned `Poll::Ready`")
StateProj::A { fut, b } => {
let res = ready!(fut.poll(cx))?;
let b = b.take().unwrap();
let fut = b.1.call(res);
this.state.set(State::B { fut });
self.poll(cx)
}
StateProj::B { fut } => fut.poll(cx),
}
}
}
Expand Down Expand Up @@ -292,12 +282,12 @@ mod tests {
type Error = ();
type Future = Ready<Result<Self::Response, ()>>;

fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.0.set(self.0.get() + 1);
Poll::Ready(Ok(()))
}

fn call(&mut self, req: &'static str) -> Self::Future {
fn call(&self, req: &'static str) -> Self::Future {
ok(req)
}
}
Expand All @@ -310,20 +300,20 @@ mod tests {
type Error = ();
type Future = Ready<Result<Self::Response, ()>>;

fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.0.set(self.0.get() + 1);
Poll::Ready(Ok(()))
}

fn call(&mut self, req: &'static str) -> Self::Future {
fn call(&self, req: &'static str) -> Self::Future {
ok((req, "srv2"))
}
}

#[actix_rt::test]
async fn test_poll_ready() {
let cnt = Rc::new(Cell::new(0));
let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt.clone()));
let srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt.clone()));
let res = lazy(|cx| srv.poll_ready(cx)).await;
assert_eq!(res, Poll::Ready(Ok(())));
assert_eq!(cnt.get(), 2);
Expand All @@ -332,7 +322,7 @@ mod tests {
#[actix_rt::test]
async fn test_call() {
let cnt = Rc::new(Cell::new(0));
let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt));
let srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt));
let res = srv.call("srv1").await;
assert!(res.is_ok());
assert_eq!(res.unwrap(), ("srv1", "srv2"));
Expand All @@ -346,7 +336,7 @@ mod tests {
pipeline_factory(fn_factory(move || ready(Ok::<_, ()>(Srv1(cnt2.clone())))))
.and_then(move || ready(Ok(Srv2(cnt.clone()))));

let mut srv = new_srv.new_service(()).await.unwrap();
let srv = new_srv.new_service(()).await.unwrap();
let res = srv.call("srv1").await;
assert!(res.is_ok());
assert_eq!(res.unwrap(), ("srv1", "srv2"));
Expand Down
32 changes: 16 additions & 16 deletions actix-service/src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn apply_fn<I, S, F, Fut, Req, In, Res, Err>(
where
I: IntoService<S, In>,
S: Service<In, Error = Err>,
F: FnMut(Req, &mut S) -> Fut,
F: Fn(Req, &S) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
Apply::new(service.into_service(), wrap_fn)
Expand All @@ -36,7 +36,7 @@ pub fn apply_fn_factory<I, SF, F, Fut, Req, In, Res, Err>(
where
I: IntoServiceFactory<SF, In>,
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
F: Fn(Req, &SF::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
ApplyFactory::new(service.into_factory(), f)
Expand All @@ -57,7 +57,7 @@ where
impl<S, F, Fut, Req, In, Res, Err> Apply<S, F, Req, In, Res, Err>
where
S: Service<In, Error = Err>,
F: FnMut(Req, &mut S) -> Fut,
F: Fn(Req, &S) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
/// Create new `Apply` combinator
Expand All @@ -73,7 +73,7 @@ where
impl<S, F, Fut, Req, In, Res, Err> Clone for Apply<S, F, Req, In, Res, Err>
where
S: Service<In, Error = Err> + Clone,
F: FnMut(Req, &mut S) -> Fut + Clone,
F: Fn(Req, &S) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
fn clone(&self) -> Self {
Expand All @@ -88,7 +88,7 @@ where
impl<S, F, Fut, Req, In, Res, Err> Service<Req> for Apply<S, F, Req, In, Res, Err>
where
S: Service<In, Error = Err>,
F: FnMut(Req, &mut S) -> Fut,
F: Fn(Req, &S) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
type Response = Res;
Expand All @@ -97,8 +97,8 @@ where

crate::forward_ready!(service);

fn call(&mut self, req: Req) -> Self::Future {
(self.wrap_fn)(req, &mut self.service)
fn call(&self, req: Req) -> Self::Future {
(self.wrap_fn)(req, &self.service)
}
}

Expand All @@ -112,7 +112,7 @@ pub struct ApplyFactory<SF, F, Req, In, Res, Err> {
impl<SF, F, Fut, Req, In, Res, Err> ApplyFactory<SF, F, Req, In, Res, Err>
where
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
F: Fn(Req, &SF::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
/// Create new `ApplyFactory` new service instance
Expand All @@ -128,7 +128,7 @@ where
impl<SF, F, Fut, Req, In, Res, Err> Clone for ApplyFactory<SF, F, Req, In, Res, Err>
where
SF: ServiceFactory<In, Error = Err> + Clone,
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
F: Fn(Req, &SF::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
fn clone(&self) -> Self {
Expand All @@ -144,7 +144,7 @@ impl<SF, F, Fut, Req, In, Res, Err> ServiceFactory<Req>
for ApplyFactory<SF, F, Req, In, Res, Err>
where
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
F: Fn(Req, &SF::Service) -> Fut + Clone,
Fut: Future<Output = Result<Res, Err>>,
{
type Response = Res;
Expand All @@ -165,7 +165,7 @@ pin_project! {
pub struct ApplyServiceFactoryResponse<SF, F, Fut, Req, In, Res, Err>
where
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut,
F: Fn(Req, &SF::Service) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
#[pin]
Expand All @@ -178,7 +178,7 @@ pin_project! {
impl<SF, F, Fut, Req, In, Res, Err> ApplyServiceFactoryResponse<SF, F, Fut, Req, In, Res, Err>
where
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut,
F: Fn(Req, &SF::Service) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
fn new(fut: SF::Future, wrap_fn: F) -> Self {
Expand All @@ -194,7 +194,7 @@ impl<SF, F, Fut, Req, In, Res, Err> Future
for ApplyServiceFactoryResponse<SF, F, Fut, Req, In, Res, Err>
where
SF: ServiceFactory<In, Error = Err>,
F: FnMut(Req, &mut SF::Service) -> Fut,
F: Fn(Req, &SF::Service) -> Fut,
Fut: Future<Output = Result<Res, Err>>,
{
type Output = Result<Apply<SF::Service, F, Req, In, Res, Err>, SF::InitError>;
Expand Down Expand Up @@ -226,14 +226,14 @@ mod tests {

crate::always_ready!();

fn call(&mut self, _: ()) -> Self::Future {
fn call(&self, _: ()) -> Self::Future {
ok(())
}
}

#[actix_rt::test]
async fn test_call() {
let mut srv = pipeline(apply_fn(Srv, |req: &'static str, srv| {
let srv = pipeline(apply_fn(Srv, |req: &'static str, srv| {
let fut = srv.call(());
async move {
fut.await.unwrap();
Expand Down Expand Up @@ -261,7 +261,7 @@ mod tests {
},
));

let mut srv = new_srv.new_service(()).await.unwrap();
let srv = new_srv.new_service(()).await.unwrap();

assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));

Expand Down
Loading

0 comments on commit 636cef8

Please sign in to comment.