1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-29 04:54:59 +02:00

service trait takes request type parameter (#232)

This commit is contained in:
Rob Ede
2020-12-27 04:28:00 +00:00
committed by GitHub
parent 518bf3f6a6
commit 3ab8c3eb69
28 changed files with 1142 additions and 1136 deletions

View File

@ -5,11 +5,14 @@ use actix_service::Service;
use criterion::{criterion_main, Criterion};
use futures_util::future::join_all;
use futures_util::future::TryFutureExt;
use std::cell::{RefCell, UnsafeCell};
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll};
use std::{
cell::{RefCell, UnsafeCell},
marker::PhantomData,
};
/*
* Test services A,B for AndThen service implementations
@ -28,71 +31,72 @@ async fn svc2(req: usize) -> Result<usize, ()> {
* Cut down version of actix_service::AndThenService based on actix-service::Cell
*/
struct AndThenUC<A, B>(Rc<UnsafeCell<(A, B)>>);
struct AndThenUC<A, Req, B>(Rc<UnsafeCell<(A, B)>>, PhantomData<Req>);
impl<A, B> AndThenUC<A, B> {
impl<A, Req, B> AndThenUC<A, Req, B> {
fn new(a: A, b: B) -> Self
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
Self(Rc::new(UnsafeCell::new((a, b))))
Self(Rc::new(UnsafeCell::new((a, b))), PhantomData)
}
}
impl<A, B> Clone for AndThenUC<A, B> {
impl<A, Req, B> Clone for AndThenUC<A, Req, B> {
fn clone(&self) -> Self {
Self(self.0.clone())
Self(self.0.clone(), PhantomData)
}
}
impl<A, B> Service for AndThenUC<A, B>
impl<A, Req, B> Service<Req> for AndThenUC<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Future = AndThenServiceResponse<A, B>;
type Future = AndThenServiceResponse<A, Req, B>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: A::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
let fut = unsafe { &mut *(*self.0).get() }.0.call(req);
AndThenServiceResponse {
state: State::A(fut, Some(self.0.clone())),
_phantom: PhantomData,
}
}
}
#[pin_project::pin_project]
pub(crate) struct AndThenServiceResponse<A, B>
pub(crate) struct AndThenServiceResponse<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
#[pin]
state: State<A, B>,
state: State<A, Req, B>,
_phantom: PhantomData<Req>,
}
#[pin_project::pin_project(project = StateProj)]
enum State<A, B>
enum State<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
A(#[pin] A::Future, Option<Rc<UnsafeCell<(A, B)>>>),
B(#[pin] B::Future),
Empty,
Empty(PhantomData<Req>),
}
impl<A, B> Future for AndThenServiceResponse<A, B>
impl<A, Req, B> Future for AndThenServiceResponse<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
type Output = Result<B::Response, A::Error>;
@ -103,7 +107,7 @@ where
StateProj::A(fut, b) => match fut.poll(cx)? {
Poll::Ready(res) => {
let b = b.take().unwrap();
this.state.set(State::Empty); // drop fut A
this.state.set(State::Empty(PhantomData)); // drop fut A
let fut = unsafe { &mut (*b.get()).1 }.call(res);
this.state.set(State::B(fut));
self.poll(cx)
@ -111,10 +115,10 @@ where
Poll::Pending => Poll::Pending,
},
StateProj::B(fut) => fut.poll(cx).map(|r| {
this.state.set(State::Empty);
this.state.set(State::Empty(PhantomData));
r
}),
StateProj::Empty => {
StateProj::Empty(_) => {
panic!("future must not be polled after it returned `Poll::Ready`")
}
}
@ -125,39 +129,38 @@ where
* AndThenRC - AndThen service based on RefCell
*/
struct AndThenRC<A, B>(Rc<RefCell<(A, B)>>);
struct AndThenRC<A, Req, B>(Rc<RefCell<(A, B)>>, PhantomData<Req>);
impl<A, B> AndThenRC<A, B> {
impl<A, Req, B> AndThenRC<A, Req, B> {
fn new(a: A, b: B) -> Self
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
Self(Rc::new(RefCell::new((a, b))))
Self(Rc::new(RefCell::new((a, b))), PhantomData)
}
}
impl<A, B> Clone for AndThenRC<A, B> {
impl<A, Req, B> Clone for AndThenRC<A, Req, B> {
fn clone(&self) -> Self {
Self(self.0.clone())
Self(self.0.clone(), PhantomData)
}
}
impl<A, B> Service for AndThenRC<A, B>
impl<A, Req, B> Service<Req> for AndThenRC<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Future = AndThenServiceResponseRC<A, B>;
type Future = AndThenServiceResponseRC<A, Req, B>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: A::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
let fut = self.0.borrow_mut().0.call(req);
AndThenServiceResponseRC {
state: StateRC::A(fut, Some(self.0.clone())),
@ -166,30 +169,30 @@ where
}
#[pin_project::pin_project]
pub(crate) struct AndThenServiceResponseRC<A, B>
pub(crate) struct AndThenServiceResponseRC<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
#[pin]
state: StateRC<A, B>,
state: StateRC<A, Req, B>,
}
#[pin_project::pin_project(project = StateRCProj)]
enum StateRC<A, B>
enum StateRC<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
A(#[pin] A::Future, Option<Rc<RefCell<(A, B)>>>),
B(#[pin] B::Future),
Empty,
Empty(PhantomData<Req>),
}
impl<A, B> Future for AndThenServiceResponseRC<A, B>
impl<A, Req, B> Future for AndThenServiceResponseRC<A, Req, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
type Output = Result<B::Response, A::Error>;
@ -200,7 +203,7 @@ where
StateRCProj::A(fut, b) => match fut.poll(cx)? {
Poll::Ready(res) => {
let b = b.take().unwrap();
this.state.set(StateRC::Empty); // drop fut A
this.state.set(StateRC::Empty(PhantomData)); // drop fut A
let fut = b.borrow_mut().1.call(res);
this.state.set(StateRC::B(fut));
self.poll(cx)
@ -208,10 +211,10 @@ where
Poll::Pending => Poll::Pending,
},
StateRCProj::B(fut) => fut.poll(cx).map(|r| {
this.state.set(StateRC::Empty);
this.state.set(StateRC::Empty(PhantomData));
r
}),
StateRCProj::Empty => {
StateRCProj::Empty(_) => {
panic!("future must not be polled after it returned `Poll::Ready`")
}
}
@ -223,32 +226,31 @@ where
* and standard futures::future::and_then combinator in a Box
*/
struct AndThenRCFuture<A, B>(Rc<RefCell<(A, B)>>);
struct AndThenRCFuture<A, Req, B>(Rc<RefCell<(A, B)>>, PhantomData<Req>);
impl<A, B> AndThenRCFuture<A, B> {
impl<A, Req, B> AndThenRCFuture<A, Req, B> {
fn new(a: A, b: B) -> Self
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A: Service<Req>,
B: Service<A::Response, Error = A::Error>,
{
Self(Rc::new(RefCell::new((a, b))))
Self(Rc::new(RefCell::new((a, b))), PhantomData)
}
}
impl<A, B> Clone for AndThenRCFuture<A, B> {
impl<A, Req, B> Clone for AndThenRCFuture<A, Req, B> {
fn clone(&self) -> Self {
Self(self.0.clone())
Self(self.0.clone(), PhantomData)
}
}
impl<A, B> Service for AndThenRCFuture<A, B>
impl<A, Req, B> Service<Req> for AndThenRCFuture<A, Req, B>
where
A: Service + 'static,
A: Service<Req> + 'static,
A::Future: 'static,
B: Service<Request = A::Response, Error = A::Error> + 'static,
B: Service<A::Response, Error = A::Error> + 'static,
B::Future: 'static,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Future = BoxFuture<Self::Response, Self::Error>;
@ -257,7 +259,7 @@ where
Poll::Ready(Ok(()))
}
fn call(&mut self, req: A::Request) -> Self::Future {
fn call(&mut self, req: Req) -> Self::Future {
let fut = self.0.borrow_mut().0.call(req);
let core = self.0.clone();
let fut2 = move |res| (*core).borrow_mut().1.call(res);
@ -281,7 +283,7 @@ where
/// async_service_direct time: [1.0908 us 1.1656 us 1.2613 us]
pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str)
where
S: Service<Request = (), Response = usize, Error = ()> + Clone + 'static,
S: Service<(), Response = usize, Error = ()> + Clone + 'static,
{
let mut rt = actix_rt::System::new("test");

View File

@ -20,8 +20,7 @@ impl Clone for SrvUC {
}
}
impl Service for SrvUC {
type Request = ();
impl Service<()> for SrvUC {
type Response = usize;
type Error = ();
type Future = Ready<Result<Self::Response, ()>>;
@ -50,8 +49,7 @@ impl Clone for SrvRC {
}
}
impl Service for SrvRC {
type Request = ();
impl Service<()> for SrvRC {
type Response = usize;
type Error = ();
type Future = Ready<Result<Self::Response, ()>>;
@ -83,7 +81,7 @@ impl Service for SrvRC {
/// async_service_direct time: [1.0908 us 1.1656 us 1.2613 us]
pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str)
where
S: Service<Request = (), Response = usize, Error = ()> + Clone + 'static,
S: Service<(), Response = usize, Error = ()> + Clone + 'static,
{
let mut rt = actix_rt::System::new("test");