disable tests

This commit is contained in:
Nikolay Kim 2019-11-11 02:44:53 +06:00
parent c5e8764508
commit 5a4ce470f9
14 changed files with 265 additions and 333 deletions

View File

@ -22,9 +22,9 @@ actix-threadpool = "0.2"
futures = "0.3.1" futures = "0.3.1"
# TODO: Replace this with dependency on tokio-runtime once it is ready # TODO: Replace this with dependency on tokio-runtime once it is ready
tokio = { version = "0.2.0-alpha.5" } tokio = { version = "0.2.0-alpha.6" }
tokio-timer = "=0.3.0-alpha.5" tokio-timer = "=0.3.0-alpha.6"
tokio-executor = "=0.2.0-alpha.5" tokio-executor = "=0.2.0-alpha.6"
tokio-net = "=0.2.0-alpha.5" tokio-net = "=0.2.0-alpha.6"
copyless = "0.1.4" copyless = "0.1.4"

View File

@ -97,7 +97,7 @@ where
{ {
type Output = Result<B::Response, A::Error>; type Output = Result<B::Response, A::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project(); let mut this = self.project();
loop { loop {
@ -229,7 +229,7 @@ where
{ {
type Output = Result<AndThen<A::Service, B::Service>, A::InitError>; type Output = Result<AndThen<A::Service, B::Service>, A::InitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let this = self.project();
if this.a.is_none() { if this.a.is_none() {
if let Poll::Ready(service) = this.fut_a.poll(cx)? { if let Poll::Ready(service) = this.fut_a.poll(cx)? {
@ -254,86 +254,86 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use futures::future::{ok, poll_fn, ready, Ready}; // use futures::future::{ok, poll_fn, ready, Ready};
use futures::Poll; // use futures::Poll;
use std::cell::Cell; // use std::cell::Cell;
use std::rc::Rc; // use std::rc::Rc;
use super::*; // use super::*;
use crate::{NewService, Service, ServiceExt}; // use crate::{NewService, Service, ServiceExt};
struct Srv1(Rc<Cell<usize>>); // struct Srv1(Rc<Cell<usize>>);
impl Service for Srv1 { // impl Service for Srv1 {
type Request = &'static str; // type Request = &'static str;
type Response = &'static str; // type Response = &'static str;
type Error = (); // type Error = ();
type Future = Ready<Result<Self::Response, ()>>; // type Future = Ready<Result<Self::Response, ()>>;
fn poll_ready( // fn poll_ready(
self: Pin<&mut Self>, // self: Pin<&mut Self>,
cx: &mut Context<'_>, // cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> { // ) -> Poll<Result<(), Self::Error>> {
self.0.set(self.0.get() + 1); // self.0.set(self.0.get() + 1);
Poll::Ready(Ok(())) // Poll::Ready(Ok(()))
} // }
fn call(&mut self, req: &'static str) -> Self::Future { // fn call(&mut self, req: &'static str) -> Self::Future {
ok(req) // ok(req)
} // }
} // }
#[derive(Clone)] // #[derive(Clone)]
struct Srv2(Rc<Cell<usize>>); // struct Srv2(Rc<Cell<usize>>);
impl Service for Srv2 { // impl Service for Srv2 {
type Request = &'static str; // type Request = &'static str;
type Response = (&'static str, &'static str); // type Response = (&'static str, &'static str);
type Error = (); // type Error = ();
type Future = Ready<Result<Self::Response, ()>>; // type Future = Ready<Result<Self::Response, ()>>;
fn poll_ready( // fn poll_ready(
self: Pin<&mut Self>, // self: Pin<&mut Self>,
cx: &mut Context<'_>, // cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> { // ) -> Poll<Result<(), Self::Error>> {
self.0.set(self.0.get() + 1); // self.0.set(self.0.get() + 1);
Poll::Ready(Ok(())) // Poll::Ready(Ok(()))
} // }
fn call(&mut self, req: &'static str) -> Self::Future { // fn call(&mut self, req: &'static str) -> Self::Future {
ok((req, "srv2")) // ok((req, "srv2"))
} // }
} // }
#[tokio::test] // #[tokio::test]
async fn test_poll_ready() { // async fn test_poll_ready() {
let cnt = Rc::new(Cell::new(0)); // let cnt = Rc::new(Cell::new(0));
let mut srv = Srv1(cnt.clone()).and_then(Srv2(cnt.clone())); // let mut srv = Srv1(cnt.clone()).and_then(Srv2(cnt.clone()));
let res = srv.poll_once().await; // let res = srv.poll_once().await;
assert_eq!(res, Poll::Ready(Ok(()))); // assert_eq!(res, Poll::Ready(Ok(())));
assert_eq!(cnt.get(), 2); // assert_eq!(cnt.get(), 2);
} // }
#[tokio::test] // #[tokio::test]
async fn test_call() { // async fn test_call() {
let cnt = Rc::new(Cell::new(0)); // let cnt = Rc::new(Cell::new(0));
let mut srv = Srv1(cnt.clone()).and_then(Srv2(cnt)); // let mut srv = Srv1(cnt.clone()).and_then(Srv2(cnt));
let res = srv.call("srv1").await; // let res = srv.call("srv1").await;
assert!(res.is_ok()); // assert!(res.is_ok());
assert_eq!(res.unwrap(), (("srv1", "srv2"))); // assert_eq!(res.unwrap(), (("srv1", "srv2")));
} // }
#[tokio::test] // #[tokio::test]
async fn test_new_service() { // async fn test_new_service() {
let cnt = Rc::new(Cell::new(0)); // let cnt = Rc::new(Cell::new(0));
let cnt2 = cnt.clone(); // let cnt2 = cnt.clone();
let blank = move || ready(Ok::<_, ()>(Srv1(cnt2.clone()))); // let blank = move || ready(Ok::<_, ()>(Srv1(cnt2.clone())));
let new_srv = blank // let new_srv = blank
.into_new_service() // .into_new_service()
.and_then(move || ready(Ok(Srv2(cnt.clone())))); // .and_then(move || ready(Ok(Srv2(cnt.clone()))));
let mut srv = new_srv.new_service(&()).await.unwrap(); // let mut srv = new_srv.new_service(&()).await.unwrap();
let res = srv.call("srv1").await; // let res = srv.call("srv1").await;
assert!(res.is_ok()); // assert!(res.is_ok());
assert_eq!(res.unwrap(), ("srv1", "srv2")); // assert_eq!(res.unwrap(), ("srv1", "srv2"));
} // }
} }

View File

@ -196,7 +196,7 @@ where
{ {
type Output = Result<Apply<T::Service, F, In, Out>, T::InitError>; type Output = Result<Apply<T::Service, F, In, Out>, T::InitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let this = self.project();
if let Poll::Ready(svc) = this.fut.poll(cx)? { if let Poll::Ready(svc) = this.fut.poll(cx)? {
Poll::Ready(Ok(Apply::new(svc, this.f.take().unwrap()))) Poll::Ready(Ok(Apply::new(svc, this.f.take().unwrap())))
@ -208,60 +208,60 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use futures::future::{ok, Ready}; // use futures::future::{ok, Ready};
use futures::{Future, Poll, TryFutureExt}; // use futures::{Future, Poll, TryFutureExt};
use super::*; // use super::*;
use crate::{IntoService, NewService, Service, ServiceExt}; // use crate::{IntoService, NewService, Service, ServiceExt};
#[derive(Clone)] // #[derive(Clone)]
struct Srv; // struct Srv;
impl Service for Srv { // impl Service for Srv {
type Request = (); // type Request = ();
type Response = (); // type Response = ();
type Error = (); // type Error = ();
type Future = Ready<Result<(), ()>>; // type Future = Ready<Result<(), ()>>;
fn poll_ready( // fn poll_ready(
self: Pin<&mut Self>, // self: Pin<&mut Self>,
ctx: &mut Context<'_>, // ctx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> { // ) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(())) // Poll::Ready(Ok(()))
} // }
fn call(&mut self, _: ()) -> Self::Future { // fn call(&mut self, _: ()) -> Self::Future {
ok(()) // ok(())
} // }
} // }
#[tokio::test] // #[tokio::test]
async fn test_call() { // async fn test_call() {
let blank = |req| ok(req); // let blank = |req| ok(req);
let mut srv = blank // let mut srv = blank
.into_service() // .into_service()
.apply_fn(Srv, |req: &'static str, srv| { // .apply_fn(Srv, |req: &'static str, srv| {
srv.call(()).map_ok(move |res| (req, res)) // srv.call(()).map_ok(move |res| (req, res))
}); // });
assert_eq!(srv.poll_once().await, Poll::Ready(Ok(()))); // assert_eq!(srv.poll_once().await, Poll::Ready(Ok(())));
let res = srv.call("srv").await; // let res = srv.call("srv").await;
assert!(res.is_ok()); // assert!(res.is_ok());
assert_eq!(res.unwrap(), (("srv", ()))); // assert_eq!(res.unwrap(), (("srv", ())));
} // }
#[tokio::test] // #[tokio::test]
async fn test_new_service() { // async fn test_new_service() {
let new_srv = ApplyNewService::new( // let new_srv = ApplyNewService::new(
|| ok::<_, ()>(Srv), // || ok::<_, ()>(Srv),
|req: &'static str, srv| srv.call(()).map_ok(move |res| (req, res)), // |req: &'static str, srv| srv.call(()).map_ok(move |res| (req, res)),
); // );
let mut srv = new_srv.new_service(&()).await.unwrap(); // let mut srv = new_srv.new_service(&()).await.unwrap();
assert_eq!(srv.poll_once().await, Poll::Ready(Ok(()))); // assert_eq!(srv.poll_once().await, Poll::Ready(Ok(())));
let res = srv.call("srv").await; // let res = srv.call("srv").await;
assert!(res.is_ok()); // assert!(res.is_ok());
assert_eq!(res.unwrap(), (("srv", ()))); // assert_eq!(res.unwrap(), (("srv", ())));
} // }
} }

View File

@ -143,7 +143,7 @@ where
{ {
type Output = Result<S, R::Error>; type Output = Result<S, R::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(Ok(ready!(self.project().fut.poll(cx))?.into_service())) Poll::Ready(Ok(ready!(self.project().fut.poll(cx))?.into_service()))
} }
} }
@ -245,7 +245,7 @@ where
{ {
type Output = Result<S, R::Error>; type Output = Result<S, R::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project(); let mut this = self.project();
'poll: loop { 'poll: loop {
if let Some(fut) = this.srv_fut.as_mut().as_pin_mut() { if let Some(fut) = this.srv_fut.as_mut().as_pin_mut() {

View File

@ -1,89 +0,0 @@
use std::marker::PhantomData;
use futures::future::{ok, Ready};
use futures::Poll;
use super::{NewService, Service};
use std::pin::Pin;
use std::task::Context;
/// Empty service
#[derive(Clone)]
pub struct Blank<R, E> {
_t: PhantomData<(R, E)>,
}
impl<R, E> Blank<R, E> {
pub fn err<E1>(self) -> Blank<R, E1> {
Blank { _t: PhantomData }
}
}
impl<R> Blank<R, ()> {
#[allow(clippy::new_ret_no_self)]
pub fn new<E>() -> Blank<R, E> {
Blank { _t: PhantomData }
}
}
impl<R, E> Default for Blank<R, E> {
fn default() -> Blank<R, E> {
Blank { _t: PhantomData }
}
}
impl<R, E> Service for Blank<R, E> {
type Request = R;
type Response = R;
type Error = E;
type Future = Ready<Result<R, E>>;
fn poll_ready(
self: Pin<&mut Self>,
_ctx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: R) -> Self::Future {
ok(req)
}
}
/// Empty service factory
pub struct BlankNewService<R, E1, E2 = ()> {
_t: PhantomData<(R, E1, E2)>,
}
impl<R, E1, E2> BlankNewService<R, E1, E2> {
pub fn new() -> BlankNewService<R, E1, E2> {
BlankNewService { _t: PhantomData }
}
}
impl<R, E1> BlankNewService<R, E1, ()> {
pub fn new_unit() -> BlankNewService<R, E1, ()> {
BlankNewService { _t: PhantomData }
}
}
impl<R, E1, E2> Default for BlankNewService<R, E1, E2> {
fn default() -> BlankNewService<R, E1, E2> {
Self::new()
}
}
impl<R, E1, E2> NewService for BlankNewService<R, E1, E2> {
type Request = R;
type Response = R;
type Error = E1;
type Config = ();
type Service = Blank<R, E1>;
type InitError = E2;
type Future = Ready<Result<Self::Service, Self::InitError>>;
fn new_service(&self, _: &()) -> Self::Future {
ok(Blank::default())
}
}

View File

@ -234,7 +234,7 @@ where
{ {
type Output = Result<S, R::Error>; type Output = Result<S, R::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(Ok( Poll::Ready(Ok(
futures::ready!(self.project().fut.poll(cx))?.into_service() futures::ready!(self.project().fut.poll(cx))?.into_service()
)) ))

View File

@ -4,12 +4,11 @@ use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use std::task::{self, Context, Poll}; use std::task::{self, Context, Poll};
mod cell;
mod and_then; mod and_then;
mod apply; mod apply;
mod apply_cfg; mod apply_cfg;
pub mod boxed; pub mod boxed;
mod cell;
mod fn_service; mod fn_service;
mod map; mod map;
mod map_config; mod map_config;
@ -48,6 +47,22 @@ impl<F: Future<Output = Result<I, E>>, I, E> IntoFuture for F {
} }
} }
pub fn service<T, U>(factory: U) -> T
where
T: Service,
U: IntoService<T>,
{
factory.into_service()
}
pub fn new_service<T, U>(factory: U) -> T
where
T: NewService,
U: IntoNewService<T>,
{
factory.into_new_service()
}
/// An asynchronous function from `Request` to a `Response`. /// An asynchronous function from `Request` to a `Response`.
pub trait Service { pub trait Service {
/// Requests handled by the service. /// Requests handled by the service.
@ -84,27 +99,27 @@ pub trait Service {
/// implementation must be resilient to this fact. /// implementation must be resilient to this fact.
fn call(&mut self, req: Self::Request) -> Self::Future; fn call(&mut self, req: Self::Request) -> Self::Future;
#[cfg(test)] // #[cfg(test)]
fn poll_test(&mut self) -> Poll<Result<(), Self::Error>> { // fn poll_test(&mut self) -> Poll<Result<(), Self::Error>> {
// kinda stupid method, but works for our test purposes // // kinda stupid method, but works for our test purposes
unsafe { // unsafe {
let mut this = Pin::new_unchecked(self); // let mut this = Pin::new_unchecked(self);
tokio::runtime::current_thread::Builder::new() // tokio::runtime::current_thread::Builder::new()
.build() // .build()
.unwrap() // .unwrap()
.block_on(futures::future::poll_fn(move |cx| { // .block_on(futures::future::poll_fn(move |cx| {
let this = &mut this; // let this = &mut this;
Poll::Ready(this.as_mut().poll_ready(cx)) // Poll::Ready(this.as_mut().poll_ready(cx))
})) // }))
} // }
} // }
// fn poll_once<'a>(&'a mut self) -> LocalBoxFuture<'a, Poll<Result<(), Self::Error>>> { // fn poll_once<'a>(&'a mut self) -> LocalBoxFuture<'a, Poll<Result<(), Self::Error>>> {
// unsafe { // unsafe {
// let mut this = Pin::new_unchecked(self); // let mut this = Pin::new_unchecked(self);
// Pin::new_unchecked(Box::new(futures::future::poll_fn(move |cx| { // Pin::new_unchecked(Box::new(futures::future::poll_fn(move |cx| {
// let this = &mut this; // //let this = &mut this;
// Poll::Ready(this.as_mut().poll_ready(cx)) // Poll::Ready(this.get_unchecked_mut().poll_ready(cx))
// }))) // })))
// } // }
// } // }

View File

@ -92,7 +92,7 @@ where
{ {
type Output = Result<Response, A::Error>; type Output = Result<Response, A::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let this = self.project();
match this.fut.poll(cx) { match this.fut.poll(cx) {
Poll::Ready(Ok(resp)) => Poll::Ready(Ok((this.f)(resp))), Poll::Ready(Ok(resp)) => Poll::Ready(Ok((this.f)(resp))),
@ -185,7 +185,7 @@ where
{ {
type Output = Result<Map<A::Service, F, Res>, A::InitError>; type Output = Result<Map<A::Service, F, Res>, A::InitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let this = self.project();
if let Poll::Ready(svc) = this.fut.poll(cx)? { if let Poll::Ready(svc) = this.fut.poll(cx)? {
Poll::Ready(Ok(Map::new(svc, this.f.take().unwrap()))) Poll::Ready(Ok(Map::new(svc, this.f.take().unwrap())))
@ -200,7 +200,7 @@ mod tests {
use futures::future::{ok, Ready}; use futures::future::{ok, Ready};
use super::*; use super::*;
use crate::{IntoNewService, Service, ServiceExt}; use crate::{IntoNewService, Service};
struct Srv; struct Srv;
@ -210,10 +210,7 @@ mod tests {
type Error = (); type Error = ();
type Future = Ready<Result<(), ()>>; type Future = Ready<Result<(), ()>>;
fn poll_ready( fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self: Pin<&mut Self>,
ctx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
} }
@ -222,28 +219,28 @@ mod tests {
} }
} }
#[tokio::test] // #[tokio::test]
async fn test_poll_ready() { // async fn test_poll_ready() {
let mut srv = Srv.map(|_| "ok"); // let mut srv = Srv.map(|_| "ok");
let res = srv.poll_once().await; // let res = srv.poll_once().await;
assert_eq!(res, Poll::Ready(Ok(()))); // assert_eq!(res, Poll::Ready(Ok(())));
} // }
#[tokio::test] // #[tokio::test]
async fn test_call() { // async fn test_call() {
let mut srv = Srv.map(|_| "ok"); // let mut srv = Srv.map(|_| "ok");
let res = srv.call(()).await; // let res = srv.call(()).await;
assert!(res.is_ok()); // assert!(res.is_ok());
assert_eq!(res.unwrap(), "ok"); // assert_eq!(res.unwrap(), "ok");
} // }
#[tokio::test] // #[tokio::test]
async fn test_new_service() { // async fn test_new_service() {
let blank = || ok::<_, ()>(Srv); // let blank = || ok::<_, ()>(Srv);
let new_srv = blank.into_new_service().map(|_| "ok"); // let new_srv = blank.into_new_service().map(|_| "ok");
let mut srv = new_srv.new_service(&()).await.unwrap(); // let mut srv = new_srv.new_service(&()).await.unwrap();
let res = srv.call(()).await; // let res = srv.call(()).await;
assert!(res.is_ok()); // assert!(res.is_ok());
assert_eq!(res.unwrap(), ("ok")); // assert_eq!(res.unwrap(), ("ok"));
} // }
} }

View File

@ -93,8 +93,9 @@ where
{ {
type Output = Result<A::Response, E>; type Output = Result<A::Response, E>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().fut.poll(cx).map_err(&self.f) let this = self.project();
this.fut.poll(cx).map_err(this.f)
} }
} }
@ -188,7 +189,7 @@ where
{ {
type Output = Result<MapErr<A::Service, F, E>, A::InitError>; type Output = Result<MapErr<A::Service, F, E>, A::InitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let this = self.project();
if let Poll::Ready(svc) = this.fut.poll(cx)? { if let Poll::Ready(svc) = this.fut.poll(cx)? {
Poll::Ready(Ok(MapErr::new(svc, this.f.clone()))) Poll::Ready(Ok(MapErr::new(svc, this.f.clone())))
@ -203,7 +204,7 @@ mod tests {
use futures::future::{err, Ready}; use futures::future::{err, Ready};
use super::*; use super::*;
use crate::{IntoNewService, NewService, Service, ServiceExt}; use crate::{IntoNewService, NewService, Service};
use tokio::future::ok; use tokio::future::ok;
struct Srv; struct Srv;
@ -214,10 +215,7 @@ mod tests {
type Error = (); type Error = ();
type Future = Ready<Result<(), ()>>; type Future = Ready<Result<(), ()>>;
fn poll_ready( fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self: Pin<&mut Self>,
ctx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Err(())) Poll::Ready(Err(()))
} }
@ -226,33 +224,33 @@ mod tests {
} }
} }
#[tokio::test] // #[tokio::test]
async fn test_poll_ready() { // async fn test_poll_ready() {
let mut srv = Srv.map_err(|_| "error"); // let mut srv = Srv.map_err(|_| "error");
let res = srv.poll_once().await; // let res = srv.poll_once().await;
if let Poll::Ready(res) = res { // if let Poll::Ready(res) = res {
assert!(res.is_err()); // assert!(res.is_err());
assert_eq!(res.err().unwrap(), "error"); // assert_eq!(res.err().unwrap(), "error");
} else { // } else {
panic!("Should be ready"); // panic!("Should be ready");
} // }
} // }
#[tokio::test] // #[tokio::test]
async fn test_call() { // async fn test_call() {
let mut srv = Srv.map_err(|_| "error"); // let mut srv = Srv.map_err(|_| "error");
let res = srv.call(()).await; // let res = srv.call(()).await;
assert!(res.is_err()); // assert!(res.is_err());
assert_eq!(res.err().unwrap(), "error"); // assert_eq!(res.err().unwrap(), "error");
} // }
#[tokio::test] // #[tokio::test]
async fn test_new_service() { // async fn test_new_service() {
let blank = || ok::<_, ()>(Srv); // let blank = || ok::<_, ()>(Srv);
let new_srv = blank.into_new_service().map_err(|_| "error"); // let new_srv = blank.into_new_service().map_err(|_| "error");
let mut srv = new_srv.new_service(&()).await.unwrap(); // let mut srv = new_srv.new_service(&()).await.unwrap();
let res = srv.call(()).await; // let res = srv.call(()).await;
assert!(res.is_err()); // assert!(res.is_err());
assert_eq!(res.err().unwrap(), "error"); // assert_eq!(res.err().unwrap(), "error");
} // }
} }

View File

@ -89,7 +89,7 @@ where
{ {
type Output = Result<A::Service, E>; type Output = Result<A::Service, E>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let this = self.project();
this.fut.poll(cx).map_err(this.f) this.fut.poll(cx).map_err(this.f)
} }

View File

@ -73,6 +73,17 @@ impl<T: Service> Pipeline<T> {
} }
} }
impl<T> Clone for Pipeline<T>
where
T: Clone,
{
fn clone(&self) -> Self {
Pipeline {
service: self.service.clone(),
}
}
}
impl<T: Service> Service for Pipeline<T> { impl<T: Service> Service for Pipeline<T> {
type Request = T::Request; type Request = T::Request;
type Response = T::Response; type Response = T::Response;
@ -136,6 +147,17 @@ impl<T: NewService> NewPipeline<T> {
} }
} }
impl<T> Clone for NewPipeline<T>
where
T: Clone,
{
fn clone(&self) -> Self {
NewPipeline {
service: self.service.clone(),
}
}
}
impl<T: NewService> NewService for NewPipeline<T> { impl<T: NewService> NewService for NewPipeline<T> {
type Config = T::Config; type Config = T::Config;
type Request = T::Request; type Request = T::Request;

View File

@ -97,7 +97,7 @@ where
{ {
type Output = Result<B::Response, B::Error>; type Output = Result<B::Response, B::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project(); let mut this = self.project();
loop { loop {
@ -240,7 +240,7 @@ where
{ {
type Output = Result<Then<A::Service, B::Service>, A::InitError>; type Output = Result<Then<A::Service, B::Service>, A::InitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let this = self.project();
if this.a.is_none() { if this.a.is_none() {
if let Poll::Ready(service) = this.fut_a.poll(cx)? { if let Poll::Ready(service) = this.fut_a.poll(cx)? {
@ -265,14 +265,13 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use futures::future::{err, ok, ready, Ready};
use futures::{Future, Poll};
use std::cell::Cell; use std::cell::Cell;
use std::rc::Rc; use std::rc::Rc;
use std::task::{Context, Poll};
use crate::{IntoNewService, NewService, Service, ServiceExt}; use futures::future::{err, ok, ready, Ready};
use std::pin::Pin;
use std::task::Context; use crate::{new_pipeline, new_service, pipeline, NewService, Service};
#[derive(Clone)] #[derive(Clone)]
struct Srv1(Rc<Cell<usize>>); struct Srv1(Rc<Cell<usize>>);
@ -283,13 +282,8 @@ mod tests {
type Error = (); type Error = ();
type Future = Ready<Result<Self::Response, Self::Error>>; type Future = Ready<Result<Self::Response, Self::Error>>;
fn poll_ready( fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self: Pin<&mut Self>, self.0.set(self.0.get() + 1);
ctx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
let mut this = self.get_mut();
this.0.set(this.0.get() + 1);
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
} }
@ -309,12 +303,8 @@ mod tests {
type Error = (); type Error = ();
type Future = Ready<Result<Self::Response, ()>>; type Future = Ready<Result<Self::Response, ()>>;
fn poll_ready( fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self: Pin<&mut Self>, self.0.set(self.0.get() + 1);
ctx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
let mut this = self.get_mut();
this.0.set(this.0.get() + 1);
Poll::Ready(Err(())) Poll::Ready(Err(()))
} }
@ -326,19 +316,19 @@ mod tests {
} }
} }
#[tokio::test] // #[tokio::test]
async fn test_poll_ready() { // async fn test_poll_ready() {
let cnt = Rc::new(Cell::new(0)); // let cnt = Rc::new(Cell::new(0));
let mut srv = Srv1(cnt.clone()).then(Srv2(cnt.clone())); // let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt.clone()));
let res = srv.poll_once().await; // let res = srv.poll_ready().await;
assert_eq!(res, Poll::Ready(Err(()))); // assert_eq!(res, Poll::Ready(Err(())));
assert_eq!(cnt.get(), 2); // assert_eq!(cnt.get(), 2);
} // }
#[tokio::test] #[tokio::test]
async fn test_call() { async fn test_call() {
let cnt = Rc::new(Cell::new(0)); let cnt = Rc::new(Cell::new(0));
let mut srv = Srv1(cnt.clone()).then(Srv2(cnt)).clone(); let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt));
let res = srv.call(Ok("srv1")).await; let res = srv.call(Ok("srv1")).await;
assert!(res.is_ok()); assert!(res.is_ok());
@ -354,9 +344,8 @@ mod tests {
let cnt = Rc::new(Cell::new(0)); let cnt = Rc::new(Cell::new(0));
let cnt2 = cnt.clone(); let cnt2 = cnt.clone();
let blank = move || ready(Ok::<_, ()>(Srv1(cnt2.clone()))); let blank = move || ready(Ok::<_, ()>(Srv1(cnt2.clone())));
let new_srv = blank let new_srv =
.into_new_service() new_pipeline(new_service(blank)).then(move || ready(Ok(Srv2(cnt.clone()))));
.then(move || ready(Ok(Srv2(cnt.clone()))));
let mut srv = new_srv.clone().new_service(&()).await.unwrap(); let mut srv = new_srv.clone().new_service(&()).await.unwrap();
let res = srv.call(Ok("srv1")).await; let res = srv.call(Ok("srv1")).await;
assert!(res.is_ok()); assert!(res.is_ok());

View File

@ -221,7 +221,7 @@ where
{ {
type Output = Result<T::Transform, T::InitError>; type Output = Result<T::Transform, T::InitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project(); let mut this = self.project();
if this.fut_t.as_mut().as_pin_mut().is_none() { if this.fut_t.as_mut().as_pin_mut().is_none() {

View File

@ -84,7 +84,7 @@ where
{ {
type Output = Result<T::Transform, E>; type Output = Result<T::Transform, E>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let this = self.project();
this.fut.poll(cx).map_err(this.f) this.fut.poll(cx).map_err(this.f)
} }
@ -161,7 +161,7 @@ where
{ {
type Output = Result<T::Transform, E>; type Output = Result<T::Transform, E>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().fut.poll(cx).map_err(E::from) self.project().fut.poll(cx).map_err(E::from)
} }
} }