mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-03 21:07:39 +03:00
refactor Service trait
This commit is contained in:
parent
a3abae8c16
commit
89cebe5534
58 changed files with 701 additions and 593 deletions
|
@ -4,13 +4,12 @@ use std::rc::Rc;
|
|||
use std::task::{Context, Poll};
|
||||
|
||||
use super::{Service, ServiceFactory};
|
||||
use crate::cell::Cell;
|
||||
|
||||
/// Service for the `and_then` combinator, chaining a computation onto the end
|
||||
/// of another service which completes successfully.
|
||||
///
|
||||
/// This is created by the `ServiceExt::and_then` method.
|
||||
pub(crate) struct AndThenService<A, B>(Cell<(A, B)>);
|
||||
pub(crate) struct AndThenService<A, B>(Rc<(A, B)>);
|
||||
|
||||
impl<A, B> AndThenService<A, B> {
|
||||
/// Create new `AndThen` combinator
|
||||
|
@ -19,7 +18,7 @@ impl<A, B> AndThenService<A, B> {
|
|||
A: Service,
|
||||
B: Service<Request = A::Response, Error = A::Error>,
|
||||
{
|
||||
Self(Cell::new((a, b)))
|
||||
Self(Rc::new((a, b)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,8 +38,8 @@ where
|
|||
type Error = A::Error;
|
||||
type Future = AndThenServiceResponse<A, B>;
|
||||
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
let srv = self.0.get_mut();
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
let srv = self.0.as_ref();
|
||||
let not_ready = !srv.0.poll_ready(cx)?.is_ready();
|
||||
if !srv.1.poll_ready(cx)?.is_ready() || not_ready {
|
||||
Poll::Pending
|
||||
|
@ -49,8 +48,8 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn poll_shutdown(&mut self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
let srv = self.0.get_mut();
|
||||
fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
let srv = self.0.as_ref();
|
||||
|
||||
if srv.0.poll_shutdown(cx, is_error).is_ready()
|
||||
&& srv.1.poll_shutdown(cx, is_error).is_ready()
|
||||
|
@ -62,9 +61,9 @@ where
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||
fn call(&self, req: A::Request) -> Self::Future {
|
||||
AndThenServiceResponse {
|
||||
state: State::A(self.0.get_mut().0.call(req), Some(self.0.clone())),
|
||||
state: State::A(self.0.as_ref().0.call(req), Some(self.0.clone())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -85,7 +84,7 @@ where
|
|||
A: Service,
|
||||
B: Service<Request = A::Response, Error = A::Error>,
|
||||
{
|
||||
A(#[pin] A::Future, Option<Cell<(A, B)>>),
|
||||
A(#[pin] A::Future, Option<Rc<(A, B)>>),
|
||||
B(#[pin] B::Future),
|
||||
Empty,
|
||||
}
|
||||
|
@ -105,9 +104,9 @@ where
|
|||
match this.state.as_mut().project() {
|
||||
State::A(fut, b) => match fut.poll(cx)? {
|
||||
Poll::Ready(res) => {
|
||||
let mut b = b.take().unwrap();
|
||||
let b = b.take().unwrap();
|
||||
this.state.set(State::Empty); // drop fut A
|
||||
let fut = b.get_mut().1.call(res);
|
||||
let fut = b.as_ref().1.call(res);
|
||||
this.state.set(State::B(fut));
|
||||
self.poll(cx)
|
||||
}
|
||||
|
@ -284,12 +283,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)
|
||||
}
|
||||
}
|
||||
|
@ -303,12 +302,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, "srv2"))
|
||||
}
|
||||
}
|
||||
|
@ -316,7 +315,7 @@ mod tests {
|
|||
#[ntex_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);
|
||||
|
@ -325,7 +324,7 @@ mod tests {
|
|||
#[ntex_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")));
|
||||
|
@ -339,7 +338,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"));
|
||||
|
|
|
@ -4,7 +4,6 @@ use std::pin::Pin;
|
|||
use std::rc::Rc;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use crate::cell::Cell;
|
||||
use crate::{Service, ServiceFactory};
|
||||
|
||||
/// `Apply` service combinator
|
||||
|
@ -12,11 +11,11 @@ pub(crate) struct AndThenApplyFn<A, B, F, Fut, Res, Err>
|
|||
where
|
||||
A: Service,
|
||||
B: Service,
|
||||
F: FnMut(A::Response, &mut B) -> Fut,
|
||||
F: Fn(A::Response, &B) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<A::Error> + From<B::Error>,
|
||||
{
|
||||
srv: Cell<(A, B, F)>,
|
||||
srv: Rc<(A, B, F)>,
|
||||
r: PhantomData<(Fut, Res, Err)>,
|
||||
}
|
||||
|
||||
|
@ -24,14 +23,14 @@ impl<A, B, F, Fut, Res, Err> AndThenApplyFn<A, B, F, Fut, Res, Err>
|
|||
where
|
||||
A: Service,
|
||||
B: Service,
|
||||
F: FnMut(A::Response, &mut B) -> Fut,
|
||||
F: Fn(A::Response, &B) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<A::Error> + From<B::Error>,
|
||||
{
|
||||
/// Create new `Apply` combinator
|
||||
pub(crate) fn new(a: A, b: B, f: F) -> Self {
|
||||
Self {
|
||||
srv: Cell::new((a, b, f)),
|
||||
srv: Rc::new((a, b, f)),
|
||||
r: PhantomData,
|
||||
}
|
||||
}
|
||||
|
@ -41,7 +40,7 @@ impl<A, B, F, Fut, Res, Err> Clone for AndThenApplyFn<A, B, F, Fut, Res, Err>
|
|||
where
|
||||
A: Service,
|
||||
B: Service,
|
||||
F: FnMut(A::Response, &mut B) -> Fut,
|
||||
F: Fn(A::Response, &B) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<A::Error> + From<B::Error>,
|
||||
{
|
||||
|
@ -57,7 +56,7 @@ impl<A, B, F, Fut, Res, Err> Service for AndThenApplyFn<A, B, F, Fut, Res, Err>
|
|||
where
|
||||
A: Service,
|
||||
B: Service,
|
||||
F: FnMut(A::Response, &mut B) -> Fut,
|
||||
F: Fn(A::Response, &B) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<A::Error> + From<B::Error>,
|
||||
{
|
||||
|
@ -66,8 +65,8 @@ where
|
|||
type Error = Err;
|
||||
type Future = AndThenApplyFnFuture<A, B, F, Fut, Res, Err>;
|
||||
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
let inner = self.srv.get_mut();
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
let inner = self.srv.as_ref();
|
||||
let not_ready = inner.0.poll_ready(cx)?.is_pending();
|
||||
if inner.1.poll_ready(cx)?.is_pending() || not_ready {
|
||||
Poll::Pending
|
||||
|
@ -76,8 +75,8 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn poll_shutdown(&mut self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
let srv = self.srv.get_mut();
|
||||
fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
let srv = self.srv.as_ref();
|
||||
|
||||
if srv.0.poll_shutdown(cx, is_error).is_ready()
|
||||
&& srv.1.poll_shutdown(cx, is_error).is_ready()
|
||||
|
@ -88,8 +87,8 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||
let fut = self.srv.get_mut().0.call(req);
|
||||
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())),
|
||||
}
|
||||
|
@ -101,7 +100,7 @@ pub(crate) struct AndThenApplyFnFuture<A, B, F, Fut, Res, Err>
|
|||
where
|
||||
A: Service,
|
||||
B: Service,
|
||||
F: FnMut(A::Response, &mut B) -> Fut,
|
||||
F: Fn(A::Response, &B) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<A::Error>,
|
||||
Err: From<B::Error>,
|
||||
|
@ -115,12 +114,12 @@ enum State<A, B, F, Fut, Res, Err>
|
|||
where
|
||||
A: Service,
|
||||
B: Service,
|
||||
F: FnMut(A::Response, &mut B) -> Fut,
|
||||
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<Cell<(A, B, F)>>),
|
||||
A(#[pin] A::Future, Option<Rc<(A, B, F)>>),
|
||||
B(#[pin] Fut),
|
||||
Empty,
|
||||
}
|
||||
|
@ -129,7 +128,7 @@ impl<A, B, F, Fut, Res, Err> Future for AndThenApplyFnFuture<A, B, F, Fut, Res,
|
|||
where
|
||||
A: Service,
|
||||
B: Service,
|
||||
F: FnMut(A::Response, &mut B) -> Fut,
|
||||
F: Fn(A::Response, &B) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<A::Error> + From<B::Error>,
|
||||
{
|
||||
|
@ -143,10 +142,10 @@ where
|
|||
match this.state.as_mut().project() {
|
||||
State::A(fut, b) => match fut.poll(cx)? {
|
||||
Poll::Ready(res) => {
|
||||
let mut b = b.take().unwrap();
|
||||
let b = b.take().unwrap();
|
||||
this.state.set(State::Empty);
|
||||
let b = b.get_mut();
|
||||
let fut = (&mut b.2)(res, &mut b.1);
|
||||
let b = b.as_ref();
|
||||
let fut = (&b.2)(res, &b.1);
|
||||
this.state.set(State::B(fut));
|
||||
self.poll(cx)
|
||||
}
|
||||
|
@ -173,7 +172,7 @@ impl<A, B, F, Fut, Res, Err> AndThenApplyFnFactory<A, B, F, Fut, Res, Err>
|
|||
where
|
||||
A: ServiceFactory,
|
||||
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
|
||||
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
|
||||
F: Fn(A::Response, &B::Service) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<A::Error> + From<B::Error>,
|
||||
{
|
||||
|
@ -201,7 +200,7 @@ where
|
|||
A: ServiceFactory,
|
||||
A::Config: Clone,
|
||||
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
|
||||
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
|
||||
F: Fn(A::Response, &B::Service) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<A::Error> + From<B::Error>,
|
||||
{
|
||||
|
@ -230,7 +229,7 @@ pub(crate) struct AndThenApplyFnFactoryResponse<A, B, F, Fut, Res, Err>
|
|||
where
|
||||
A: ServiceFactory,
|
||||
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
|
||||
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
|
||||
F: Fn(A::Response, &B::Service) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<A::Error>,
|
||||
Err: From<B::Error>,
|
||||
|
@ -249,7 +248,7 @@ impl<A, B, F, Fut, Res, Err> Future
|
|||
where
|
||||
A: ServiceFactory,
|
||||
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
|
||||
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
|
||||
F: Fn(A::Response, &B::Service) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<A::Error> + From<B::Error>,
|
||||
{
|
||||
|
@ -273,7 +272,7 @@ where
|
|||
|
||||
if this.a.is_some() && this.b.is_some() {
|
||||
Poll::Ready(Ok(AndThenApplyFn {
|
||||
srv: Cell::new((
|
||||
srv: Rc::new((
|
||||
this.a.take().unwrap(),
|
||||
this.b.take().unwrap(),
|
||||
this.f.clone(),
|
||||
|
@ -302,18 +301,18 @@ mod tests {
|
|||
type Error = ();
|
||||
type Future = Ready<Result<(), ()>>;
|
||||
|
||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
fn call(&self, req: Self::Request) -> Self::Future {
|
||||
ok(req)
|
||||
}
|
||||
}
|
||||
|
||||
#[ntex_rt::test]
|
||||
async fn test_service() {
|
||||
let mut srv = pipeline(|r: &'static str| ok(r))
|
||||
let srv = pipeline(|r: &'static str| ok(r))
|
||||
.and_then_apply_fn(Srv, |req: &'static str, s| {
|
||||
s.call(()).map_ok(move |res| (req, res))
|
||||
});
|
||||
|
@ -333,7 +332,7 @@ mod tests {
|
|||
|| ok(Srv),
|
||||
|req: &'static str, s| s.call(()).map_ok(move |res| (req, res)),
|
||||
);
|
||||
let mut srv = new_srv.new_service(()).await.unwrap();
|
||||
let srv = new_srv.new_service(()).await.unwrap();
|
||||
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
||||
assert_eq!(res, Poll::Ready(Ok(())));
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ pub fn apply_fn<T, F, R, In, Out, Err, U>(
|
|||
) -> Apply<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: Service<Error = Err>,
|
||||
F: FnMut(In, &mut T) -> R,
|
||||
F: Fn(In, &T) -> R,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
U: IntoService<T>,
|
||||
{
|
||||
|
@ -26,7 +26,7 @@ pub fn apply_fn_factory<T, F, R, In, Out, Err, U>(
|
|||
) -> ApplyServiceFactory<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: ServiceFactory<Error = Err>,
|
||||
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||
F: Fn(In, &T::Service) -> R + Clone,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
U: IntoServiceFactory<T>,
|
||||
{
|
||||
|
@ -46,7 +46,7 @@ where
|
|||
impl<T, F, R, In, Out, Err> Apply<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: Service<Error = Err>,
|
||||
F: FnMut(In, &mut T) -> R,
|
||||
F: Fn(In, &T) -> R,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
{
|
||||
/// Create new `Apply` combinator
|
||||
|
@ -62,7 +62,7 @@ where
|
|||
impl<T, F, R, In, Out, Err> Clone for Apply<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: Service<Error = Err> + Clone,
|
||||
F: FnMut(In, &mut T) -> R + Clone,
|
||||
F: Fn(In, &T) -> R + Clone,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
|
@ -77,7 +77,7 @@ where
|
|||
impl<T, F, R, In, Out, Err> Service for Apply<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: Service<Error = Err>,
|
||||
F: FnMut(In, &mut T) -> R,
|
||||
F: Fn(In, &T) -> R,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
{
|
||||
type Request = In;
|
||||
|
@ -86,18 +86,18 @@ where
|
|||
type Future = R;
|
||||
|
||||
#[inline]
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(futures_util::ready!(self.service.poll_ready(cx)))
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.service.poll_ready(cx)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_shutdown(&mut self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
self.service.poll_shutdown(cx, is_error)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn call(&mut self, req: In) -> Self::Future {
|
||||
(self.f)(req, &mut self.service)
|
||||
fn call(&self, req: In) -> Self::Future {
|
||||
(self.f)(req, &self.service)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,7 +105,7 @@ where
|
|||
pub struct ApplyServiceFactory<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: ServiceFactory<Error = Err>,
|
||||
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||
F: Fn(In, &T::Service) -> R + Clone,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
{
|
||||
service: T,
|
||||
|
@ -116,7 +116,7 @@ where
|
|||
impl<T, F, R, In, Out, Err> ApplyServiceFactory<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: ServiceFactory<Error = Err>,
|
||||
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||
F: Fn(In, &T::Service) -> R + Clone,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
{
|
||||
/// Create new `ApplyNewService` new service instance
|
||||
|
@ -132,7 +132,7 @@ where
|
|||
impl<T, F, R, In, Out, Err> Clone for ApplyServiceFactory<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: ServiceFactory<Error = Err> + Clone,
|
||||
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||
F: Fn(In, &T::Service) -> R + Clone,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
|
@ -147,7 +147,7 @@ where
|
|||
impl<T, F, R, In, Out, Err> ServiceFactory for ApplyServiceFactory<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: ServiceFactory<Error = Err>,
|
||||
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||
F: Fn(In, &T::Service) -> R + Clone,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
{
|
||||
type Request = In;
|
||||
|
@ -168,7 +168,7 @@ where
|
|||
pub struct ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: ServiceFactory<Error = Err>,
|
||||
F: FnMut(In, &mut T::Service) -> R,
|
||||
F: Fn(In, &T::Service) -> R,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
{
|
||||
#[pin]
|
||||
|
@ -180,7 +180,7 @@ where
|
|||
impl<T, F, R, In, Out, Err> ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: ServiceFactory<Error = Err>,
|
||||
F: FnMut(In, &mut T::Service) -> R,
|
||||
F: Fn(In, &T::Service) -> R,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
{
|
||||
fn new(fut: T::Future, f: F) -> Self {
|
||||
|
@ -195,7 +195,7 @@ where
|
|||
impl<T, F, R, In, Out, Err> Future for ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
|
||||
where
|
||||
T: ServiceFactory<Error = Err>,
|
||||
F: FnMut(In, &mut T::Service) -> R,
|
||||
F: Fn(In, &T::Service) -> R,
|
||||
R: Future<Output = Result<Out, Err>>,
|
||||
{
|
||||
type Output = Result<Apply<T::Service, F, R, In, Out, Err>, T::InitError>;
|
||||
|
@ -229,18 +229,18 @@ mod tests {
|
|||
type Error = ();
|
||||
type Future = Ready<Result<(), ()>>;
|
||||
|
||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, _: ()) -> Self::Future {
|
||||
fn call(&self, _: ()) -> Self::Future {
|
||||
ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[ntex_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 {
|
||||
let res = fut.await.unwrap();
|
||||
|
@ -268,7 +268,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(())));
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use std::future::Future;
|
||||
use std::marker::PhantomData;
|
||||
use std::pin::Pin;
|
||||
use std::rc::Rc;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use crate::cell::Cell;
|
||||
use crate::{Service, ServiceFactory};
|
||||
|
||||
/// Convert `Fn(Config, &mut Service1) -> Future<Service2>` fn to a service factory
|
||||
|
@ -20,18 +20,18 @@ pub fn apply_cfg<F, C, T, R, S, E>(
|
|||
Future = R,
|
||||
> + Clone
|
||||
where
|
||||
F: FnMut(C, &mut T) -> R,
|
||||
F: Fn(C, &T) -> R,
|
||||
T: Service,
|
||||
R: Future<Output = Result<S, E>>,
|
||||
S: Service,
|
||||
{
|
||||
ApplyConfigService {
|
||||
srv: Cell::new((srv, f)),
|
||||
srv: Rc::new((srv, f)),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert `Fn(Config, &mut Service1) -> Future<Service2>` fn to a service factory
|
||||
/// Convert `Fn(Config, &Service1) -> Future<Service2>` fn to a service factory
|
||||
///
|
||||
/// Service1 get constructed from `T` factory.
|
||||
pub fn apply_cfg_factory<F, C, T, R, S>(
|
||||
|
@ -46,33 +46,33 @@ pub fn apply_cfg_factory<F, C, T, R, S>(
|
|||
InitError = T::InitError,
|
||||
> + Clone
|
||||
where
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
F: Fn(C, &T::Service) -> R,
|
||||
T: ServiceFactory<Config = ()>,
|
||||
T::InitError: From<T::Error>,
|
||||
R: Future<Output = Result<S, T::InitError>>,
|
||||
S: Service,
|
||||
{
|
||||
ApplyConfigServiceFactory {
|
||||
srv: Cell::new((factory, f)),
|
||||
srv: Rc::new((factory, f)),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert `Fn(Config, &mut Server) -> Future<Service>` fn to NewService\
|
||||
/// Convert `Fn(Config, &Server) -> Future<Service>` fn to NewService\
|
||||
struct ApplyConfigService<F, C, T, R, S, E>
|
||||
where
|
||||
F: FnMut(C, &mut T) -> R,
|
||||
F: Fn(C, &T) -> R,
|
||||
T: Service,
|
||||
R: Future<Output = Result<S, E>>,
|
||||
S: Service,
|
||||
{
|
||||
srv: Cell<(T, F)>,
|
||||
srv: Rc<(T, F)>,
|
||||
_t: PhantomData<(C, R, S)>,
|
||||
}
|
||||
|
||||
impl<F, C, T, R, S, E> Clone for ApplyConfigService<F, C, T, R, S, E>
|
||||
where
|
||||
F: FnMut(C, &mut T) -> R,
|
||||
F: Fn(C, &T) -> R,
|
||||
T: Service,
|
||||
R: Future<Output = Result<S, E>>,
|
||||
S: Service,
|
||||
|
@ -87,7 +87,7 @@ where
|
|||
|
||||
impl<F, C, T, R, S, E> ServiceFactory for ApplyConfigService<F, C, T, R, S, E>
|
||||
where
|
||||
F: FnMut(C, &mut T) -> R,
|
||||
F: Fn(C, &T) -> R,
|
||||
T: Service,
|
||||
R: Future<Output = Result<S, E>>,
|
||||
S: Service,
|
||||
|
@ -102,28 +102,26 @@ where
|
|||
type Future = R;
|
||||
|
||||
fn new_service(&self, cfg: C) -> Self::Future {
|
||||
unsafe {
|
||||
let srv = self.srv.get_mut_unsafe();
|
||||
(srv.1)(cfg, &mut srv.0)
|
||||
}
|
||||
let srv = self.srv.as_ref();
|
||||
(srv.1)(cfg, &srv.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
||||
struct ApplyConfigServiceFactory<F, C, T, R, S>
|
||||
where
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
F: Fn(C, &T::Service) -> R,
|
||||
T: ServiceFactory<Config = ()>,
|
||||
R: Future<Output = Result<S, T::InitError>>,
|
||||
S: Service,
|
||||
{
|
||||
srv: Cell<(T, F)>,
|
||||
srv: Rc<(T, F)>,
|
||||
_t: PhantomData<(C, R, S)>,
|
||||
}
|
||||
|
||||
impl<F, C, T, R, S> Clone for ApplyConfigServiceFactory<F, C, T, R, S>
|
||||
where
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
F: Fn(C, &T::Service) -> R,
|
||||
T: ServiceFactory<Config = ()>,
|
||||
R: Future<Output = Result<S, T::InitError>>,
|
||||
S: Service,
|
||||
|
@ -138,7 +136,7 @@ where
|
|||
|
||||
impl<F, C, T, R, S> ServiceFactory for ApplyConfigServiceFactory<F, C, T, R, S>
|
||||
where
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
F: Fn(C, &T::Service) -> R,
|
||||
T: ServiceFactory<Config = ()>,
|
||||
T::InitError: From<T::Error>,
|
||||
R: Future<Output = Result<S, T::InitError>>,
|
||||
|
@ -157,7 +155,7 @@ where
|
|||
ApplyConfigServiceFactoryResponse {
|
||||
cfg: Some(cfg),
|
||||
store: self.srv.clone(),
|
||||
state: State::A(self.srv.get_ref().0.new_service(())),
|
||||
state: State::A(self.srv.as_ref().0.new_service(())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -165,14 +163,14 @@ where
|
|||
#[pin_project::pin_project]
|
||||
struct ApplyConfigServiceFactoryResponse<F, C, T, R, S>
|
||||
where
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
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: Cell<(T, F)>,
|
||||
store: Rc<(T, F)>,
|
||||
#[pin]
|
||||
state: State<T, R, S>,
|
||||
}
|
||||
|
@ -192,7 +190,7 @@ where
|
|||
|
||||
impl<F, C, T, R, S> Future for ApplyConfigServiceFactoryResponse<F, C, T, R, S>
|
||||
where
|
||||
F: FnMut(C, &mut T::Service) -> R,
|
||||
F: Fn(C, &T::Service) -> R,
|
||||
T: ServiceFactory<Config = ()>,
|
||||
T::InitError: From<T::Error>,
|
||||
R: Future<Output = Result<S, T::InitError>>,
|
||||
|
@ -215,7 +213,7 @@ where
|
|||
},
|
||||
State::B(srv) => match srv.poll_ready(cx)? {
|
||||
Poll::Ready(_) => {
|
||||
let fut = (this.store.get_mut().1)(this.cfg.take().unwrap(), srv);
|
||||
let fut = (this.store.as_ref().1)(this.cfg.take().unwrap(), srv);
|
||||
this.state.set(State::C(fut));
|
||||
self.poll(cx)
|
||||
}
|
||||
|
|
|
@ -145,17 +145,17 @@ where
|
|||
type Future = BoxFuture<Res, Err>;
|
||||
|
||||
#[inline]
|
||||
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.0.poll_ready(ctx)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_shutdown(&mut self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
self.0.poll_shutdown(cx, is_error)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
fn call(&self, req: Self::Request) -> Self::Future {
|
||||
Box::pin(self.0.call(req))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,61 +0,0 @@
|
|||
//! Custom cell impl, internal use only
|
||||
use std::task::{Context, Poll};
|
||||
use std::{cell::UnsafeCell, fmt, rc::Rc};
|
||||
|
||||
pub(crate) struct Cell<T> {
|
||||
inner: Rc<UnsafeCell<T>>,
|
||||
}
|
||||
|
||||
impl<T> Clone for Cell<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
inner: self.inner.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for Cell<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.inner.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Cell<T> {
|
||||
pub(crate) fn new(inner: T) -> Self {
|
||||
Self {
|
||||
inner: Rc::new(UnsafeCell::new(inner)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_ref(&self) -> &T {
|
||||
unsafe { &*self.inner.as_ref().get() }
|
||||
}
|
||||
|
||||
pub(crate) fn get_mut(&mut self) -> &mut T {
|
||||
unsafe { &mut *self.inner.as_ref().get() }
|
||||
}
|
||||
|
||||
#[allow(clippy::mut_from_ref)]
|
||||
pub(crate) unsafe fn get_mut_unsafe(&self) -> &mut T {
|
||||
&mut *self.inner.as_ref().get()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: crate::Service> crate::Service for Cell<T> {
|
||||
type Request = T::Request;
|
||||
type Response = T::Response;
|
||||
type Error = T::Error;
|
||||
type Future = T::Future;
|
||||
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.get_mut().poll_ready(cx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(&mut self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
self.get_mut().poll_shutdown(cx, is_error)
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
self.get_mut().call(req)
|
||||
}
|
||||
}
|
|
@ -6,17 +6,19 @@ use futures_util::future::{ok, Ready};
|
|||
|
||||
use crate::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
||||
|
||||
#[inline]
|
||||
/// Create `ServiceFactory` for function that can act as a `Service`
|
||||
pub fn fn_service<F, Fut, Req, Res, Err, Cfg>(
|
||||
f: F,
|
||||
) -> FnServiceFactory<F, Fut, Req, Res, Err, Cfg>
|
||||
where
|
||||
F: FnMut(Req) -> Fut + Clone,
|
||||
F: Fn(Req) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
FnServiceFactory::new(f)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Create `ServiceFactory` for function that can produce services
|
||||
///
|
||||
/// # Example
|
||||
|
@ -43,7 +45,7 @@ where
|
|||
/// });
|
||||
///
|
||||
/// // construct new service
|
||||
/// let mut srv = factory.new_service(()).await?;
|
||||
/// let srv = factory.new_service(()).await?;
|
||||
///
|
||||
/// // now we can use `div` service
|
||||
/// let result = srv.call((10, 20)).await?;
|
||||
|
@ -64,6 +66,7 @@ where
|
|||
FnServiceNoConfig::new(f)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Create `ServiceFactory` for function that accepts config argument and can produce services
|
||||
///
|
||||
/// Any function that has following form `Fn(Config) -> Future<Output = Service>` could
|
||||
|
@ -85,7 +88,7 @@ where
|
|||
/// });
|
||||
///
|
||||
/// // construct new service with config argument
|
||||
/// let mut srv = factory.new_service(10).await?;
|
||||
/// let srv = factory.new_service(10).await?;
|
||||
///
|
||||
/// let result = srv.call(10).await?;
|
||||
/// assert_eq!(result, 100);
|
||||
|
@ -107,7 +110,7 @@ where
|
|||
|
||||
pub struct FnService<F, Fut, Req, Res, Err>
|
||||
where
|
||||
F: FnMut(Req) -> Fut,
|
||||
F: Fn(Req) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
f: F,
|
||||
|
@ -116,7 +119,7 @@ where
|
|||
|
||||
impl<F, Fut, Req, Res, Err> FnService<F, Fut, Req, Res, Err>
|
||||
where
|
||||
F: FnMut(Req) -> Fut,
|
||||
F: Fn(Req) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
pub(crate) fn new(f: F) -> Self {
|
||||
|
@ -126,9 +129,10 @@ where
|
|||
|
||||
impl<F, Fut, Req, Res, Err> Clone for FnService<F, Fut, Req, Res, Err>
|
||||
where
|
||||
F: FnMut(Req) -> Fut + Clone,
|
||||
F: Fn(Req) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
Self::new(self.f.clone())
|
||||
}
|
||||
|
@ -136,7 +140,7 @@ where
|
|||
|
||||
impl<F, Fut, Req, Res, Err> Service for FnService<F, Fut, Req, Res, Err>
|
||||
where
|
||||
F: FnMut(Req) -> Fut,
|
||||
F: Fn(Req) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
type Request = Req;
|
||||
|
@ -144,20 +148,23 @@ where
|
|||
type Error = Err;
|
||||
type Future = Fut;
|
||||
|
||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
#[inline]
|
||||
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Req) -> Self::Future {
|
||||
#[inline]
|
||||
fn call(&self, req: Req) -> Self::Future {
|
||||
(self.f)(req)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, Fut, Req, Res, Err> IntoService<FnService<F, Fut, Req, Res, Err>> for F
|
||||
where
|
||||
F: FnMut(Req) -> Fut,
|
||||
F: Fn(Req) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
#[inline]
|
||||
fn into_service(self) -> FnService<F, Fut, Req, Res, Err> {
|
||||
FnService::new(self)
|
||||
}
|
||||
|
@ -165,7 +172,7 @@ where
|
|||
|
||||
pub struct FnServiceFactory<F, Fut, Req, Res, Err, Cfg>
|
||||
where
|
||||
F: FnMut(Req) -> Fut,
|
||||
F: Fn(Req) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
f: F,
|
||||
|
@ -174,7 +181,7 @@ where
|
|||
|
||||
impl<F, Fut, Req, Res, Err, Cfg> FnServiceFactory<F, Fut, Req, Res, Err, Cfg>
|
||||
where
|
||||
F: FnMut(Req) -> Fut + Clone,
|
||||
F: Fn(Req) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
fn new(f: F) -> Self {
|
||||
|
@ -184,9 +191,10 @@ where
|
|||
|
||||
impl<F, Fut, Req, Res, Err, Cfg> Clone for FnServiceFactory<F, Fut, Req, Res, Err, Cfg>
|
||||
where
|
||||
F: FnMut(Req) -> Fut + Clone,
|
||||
F: Fn(Req) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
Self::new(self.f.clone())
|
||||
}
|
||||
|
@ -194,7 +202,7 @@ where
|
|||
|
||||
impl<F, Fut, Req, Res, Err> Service for FnServiceFactory<F, Fut, Req, Res, Err, ()>
|
||||
where
|
||||
F: FnMut(Req) -> Fut + Clone,
|
||||
F: Fn(Req) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
type Request = Req;
|
||||
|
@ -202,11 +210,13 @@ where
|
|||
type Error = Err;
|
||||
type Future = Fut;
|
||||
|
||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
#[inline]
|
||||
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
#[inline]
|
||||
fn call(&self, req: Self::Request) -> Self::Future {
|
||||
(self.f)(req)
|
||||
}
|
||||
}
|
||||
|
@ -214,7 +224,7 @@ where
|
|||
impl<F, Fut, Req, Res, Err, Cfg> ServiceFactory
|
||||
for FnServiceFactory<F, Fut, Req, Res, Err, Cfg>
|
||||
where
|
||||
F: FnMut(Req) -> Fut + Clone,
|
||||
F: Fn(Req) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
type Request = Req;
|
||||
|
@ -226,6 +236,7 @@ where
|
|||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||
|
||||
#[inline]
|
||||
fn new_service(&self, _: Cfg) -> Self::Future {
|
||||
ok(FnService::new(self.f.clone()))
|
||||
}
|
||||
|
@ -237,6 +248,7 @@ where
|
|||
F: Fn(Req) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
{
|
||||
#[inline]
|
||||
fn into_factory(self) -> FnServiceFactory<F, Fut, Req, Res, Err, Cfg> {
|
||||
FnServiceFactory::new(self)
|
||||
}
|
||||
|
@ -270,6 +282,7 @@ where
|
|||
Fut: Future<Output = Result<Srv, Err>>,
|
||||
Srv: Service,
|
||||
{
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
FnServiceConfig {
|
||||
f: self.f.clone(),
|
||||
|
@ -293,6 +306,7 @@ where
|
|||
type InitError = Err;
|
||||
type Future = Fut;
|
||||
|
||||
#[inline]
|
||||
fn new_service(&self, cfg: Cfg) -> Self::Future {
|
||||
(self.f)(cfg)
|
||||
}
|
||||
|
@ -334,6 +348,7 @@ where
|
|||
type InitError = E;
|
||||
type Future = R;
|
||||
|
||||
#[inline]
|
||||
fn new_service(&self, _: C) -> Self::Future {
|
||||
(self.f)()
|
||||
}
|
||||
|
@ -345,6 +360,7 @@ where
|
|||
R: Future<Output = Result<S, E>>,
|
||||
S: Service,
|
||||
{
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
Self::new(self.f.clone())
|
||||
}
|
||||
|
@ -356,6 +372,7 @@ where
|
|||
R: Future<Output = Result<S, E>>,
|
||||
S: Service,
|
||||
{
|
||||
#[inline]
|
||||
fn into_factory(self) -> FnServiceNoConfig<F, C, S, R, E> {
|
||||
FnServiceNoConfig::new(self)
|
||||
}
|
||||
|
@ -374,7 +391,7 @@ mod tests {
|
|||
async fn test_fn_service() {
|
||||
let new_srv = fn_service(|()| ok::<_, ()>("srv"));
|
||||
|
||||
let mut srv = new_srv.new_service(()).await.unwrap();
|
||||
let srv = new_srv.new_service(()).await.unwrap();
|
||||
let res = srv.call(()).await;
|
||||
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||
assert!(res.is_ok());
|
||||
|
@ -383,7 +400,7 @@ mod tests {
|
|||
|
||||
#[ntex_rt::test]
|
||||
async fn test_fn_service_service() {
|
||||
let mut srv = fn_service(|()| ok::<_, ()>("srv"));
|
||||
let srv = fn_service(|()| ok::<_, ()>("srv"));
|
||||
|
||||
let res = srv.call(()).await;
|
||||
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||
|
@ -397,7 +414,7 @@ mod tests {
|
|||
ok::<_, ()>(fn_service(move |()| ok::<_, ()>(("srv", cfg))))
|
||||
});
|
||||
|
||||
let mut srv = new_srv.new_service(1).await.unwrap();
|
||||
let srv = new_srv.new_service(1).await.unwrap();
|
||||
let res = srv.call(()).await;
|
||||
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||
assert!(res.is_ok());
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#![deny(rust_2018_idioms, warnings)]
|
||||
#![allow(clippy::type_complexity)]
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::future::Future;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
@ -12,7 +11,6 @@ mod and_then_apply_fn;
|
|||
mod apply;
|
||||
mod apply_cfg;
|
||||
pub mod boxed;
|
||||
mod cell;
|
||||
mod fn_service;
|
||||
mod map;
|
||||
mod map_config;
|
||||
|
@ -95,10 +93,7 @@ pub trait Service {
|
|||
/// 1. `.poll_ready()` might be called on different task from actual service call.
|
||||
///
|
||||
/// 2. In case of chained services, `.poll_ready()` get called for all services at once.
|
||||
fn poll_ready(
|
||||
&mut self,
|
||||
ctx: &mut task::Context<'_>,
|
||||
) -> Poll<Result<(), Self::Error>>;
|
||||
fn poll_ready(&self, ctx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>>;
|
||||
|
||||
#[inline]
|
||||
#[allow(unused_variables)]
|
||||
|
@ -106,11 +101,7 @@ pub trait Service {
|
|||
///
|
||||
/// Returns `Ready` when the service is properly shutdowned. This method might be called
|
||||
/// after it returns `Ready`.
|
||||
fn poll_shutdown(
|
||||
&mut self,
|
||||
ctx: &mut task::Context<'_>,
|
||||
is_error: bool,
|
||||
) -> Poll<()> {
|
||||
fn poll_shutdown(&self, ctx: &mut task::Context<'_>, is_error: bool) -> Poll<()> {
|
||||
Poll::Ready(())
|
||||
}
|
||||
|
||||
|
@ -123,7 +114,7 @@ pub trait Service {
|
|||
///
|
||||
/// Calling `call` without calling `poll_ready` is permitted. The
|
||||
/// implementation must be resilient to this fact.
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future;
|
||||
fn call(&self, req: Self::Request) -> Self::Future;
|
||||
|
||||
#[inline]
|
||||
/// Map this service's output to a different type, returning a new service
|
||||
|
@ -199,6 +190,7 @@ pub trait ServiceFactory {
|
|||
/// Create and return a new service value asynchronously.
|
||||
fn new_service(&self, cfg: Self::Config) -> Self::Future;
|
||||
|
||||
#[inline]
|
||||
/// Map this service's output to a different type, returning a new service
|
||||
/// of the resulting type.
|
||||
fn map<F, R>(self, f: F) -> crate::map::MapServiceFactory<Self, F, R>
|
||||
|
@ -209,6 +201,7 @@ pub trait ServiceFactory {
|
|||
crate::map::MapServiceFactory::new(self, f)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Map this service's error to a different error, returning a new service.
|
||||
fn map_err<F, E>(self, f: F) -> crate::map_err::MapErrServiceFactory<Self, F, E>
|
||||
where
|
||||
|
@ -218,6 +211,7 @@ pub trait ServiceFactory {
|
|||
crate::map_err::MapErrServiceFactory::new(self, f)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Map this factory's init error to a different error, returning a new service.
|
||||
fn map_init_err<F, E>(self, f: F) -> crate::map_init_err::MapInitErr<Self, F, E>
|
||||
where
|
||||
|
@ -237,11 +231,18 @@ where
|
|||
type Error = S::Error;
|
||||
type Future = S::Future;
|
||||
|
||||
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
(**self).poll_ready(ctx)
|
||||
#[inline]
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
(**self).poll_ready(cx)
|
||||
}
|
||||
|
||||
fn call(&mut self, request: Self::Request) -> S::Future {
|
||||
#[inline]
|
||||
fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
(**self).poll_shutdown(cx, is_error)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn call(&self, request: Self::Request) -> S::Future {
|
||||
(**self).call(request)
|
||||
}
|
||||
}
|
||||
|
@ -255,16 +256,23 @@ where
|
|||
type Error = S::Error;
|
||||
type Future = S::Future;
|
||||
|
||||
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
|
||||
(**self).poll_ready(ctx)
|
||||
#[inline]
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
|
||||
(**self).poll_ready(cx)
|
||||
}
|
||||
|
||||
fn call(&mut self, request: Self::Request) -> S::Future {
|
||||
#[inline]
|
||||
fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
(**self).poll_shutdown(cx, is_error)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn call(&self, request: Self::Request) -> S::Future {
|
||||
(**self).call(request)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Service for RefCell<S>
|
||||
impl<S> Service for Rc<S>
|
||||
where
|
||||
S: Service,
|
||||
{
|
||||
|
@ -273,30 +281,17 @@ where
|
|||
type Error = S::Error;
|
||||
type Future = S::Future;
|
||||
|
||||
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.borrow_mut().poll_ready(ctx)
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
(**self).poll_ready(cx)
|
||||
}
|
||||
|
||||
fn call(&mut self, request: Self::Request) -> S::Future {
|
||||
self.borrow_mut().call(request)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Service for Rc<RefCell<S>>
|
||||
where
|
||||
S: Service,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Future = S::Future;
|
||||
|
||||
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.borrow_mut().poll_ready(ctx)
|
||||
#[inline]
|
||||
fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
(**self).poll_shutdown(cx, is_error)
|
||||
}
|
||||
|
||||
fn call(&mut self, request: Self::Request) -> S::Future {
|
||||
(&mut (**self).borrow_mut()).call(request)
|
||||
fn call(&self, request: Self::Request) -> S::Future {
|
||||
(**self).call(request)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@ where
|
|||
A: Clone,
|
||||
F: Clone,
|
||||
{
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
Map {
|
||||
service: self.service.clone(),
|
||||
|
@ -54,17 +55,17 @@ where
|
|||
type Future = MapFuture<A, F, Response>;
|
||||
|
||||
#[inline]
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.service.poll_ready(cx)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_shutdown(&mut self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
self.service.poll_shutdown(cx, is_error)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||
fn call(&self, req: A::Request) -> Self::Future {
|
||||
MapFuture::new(self.service.call(req), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
@ -135,6 +136,7 @@ where
|
|||
A: Clone,
|
||||
F: Clone,
|
||||
{
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
a: self.a.clone(),
|
||||
|
@ -158,6 +160,7 @@ where
|
|||
type InitError = A::InitError;
|
||||
type Future = MapServiceFuture<A, F, Res>;
|
||||
|
||||
#[inline]
|
||||
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
||||
MapServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
||||
}
|
||||
|
@ -217,25 +220,25 @@ mod tests {
|
|||
type Error = ();
|
||||
type Future = Ready<Result<(), ()>>;
|
||||
|
||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, _: ()) -> Self::Future {
|
||||
fn call(&self, _: ()) -> Self::Future {
|
||||
ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[ntex_rt::test]
|
||||
async fn test_poll_ready() {
|
||||
let mut srv = Srv.map(|_| "ok");
|
||||
let srv = Srv.map(|_| "ok");
|
||||
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
||||
assert_eq!(res, Poll::Ready(Ok(())));
|
||||
}
|
||||
|
||||
#[ntex_rt::test]
|
||||
async fn test_call() {
|
||||
let mut srv = Srv.map(|_| "ok");
|
||||
let srv = Srv.map(|_| "ok");
|
||||
let res = srv.call(()).await;
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), "ok");
|
||||
|
@ -244,7 +247,7 @@ mod tests {
|
|||
#[ntex_rt::test]
|
||||
async fn test_new_service() {
|
||||
let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map(|_| "ok");
|
||||
let mut srv = new_srv.new_service(&()).await.unwrap();
|
||||
let srv = new_srv.new_service(&()).await.unwrap();
|
||||
let res = srv.call(()).await;
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), ("ok"));
|
||||
|
|
|
@ -35,6 +35,7 @@ where
|
|||
A: Clone,
|
||||
F: Clone,
|
||||
{
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
MapErr {
|
||||
service: self.service.clone(),
|
||||
|
@ -55,17 +56,17 @@ where
|
|||
type Future = MapErrFuture<A, F, E>;
|
||||
|
||||
#[inline]
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.service.poll_ready(cx).map_err(&self.f)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_shutdown(&mut self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
self.service.poll_shutdown(cx, is_error)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||
fn call(&self, req: A::Request) -> Self::Future {
|
||||
MapErrFuture::new(self.service.call(req), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
@ -161,6 +162,7 @@ where
|
|||
type InitError = A::InitError;
|
||||
type Future = MapErrServiceFuture<A, F, E>;
|
||||
|
||||
#[inline]
|
||||
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
||||
MapErrServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
||||
}
|
||||
|
@ -219,25 +221,25 @@ mod tests {
|
|||
type Error = ();
|
||||
type Future = Ready<Result<(), ()>>;
|
||||
|
||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Err(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, _: ()) -> Self::Future {
|
||||
fn call(&self, _: ()) -> Self::Future {
|
||||
err(())
|
||||
}
|
||||
}
|
||||
|
||||
#[ntex_rt::test]
|
||||
async fn test_poll_ready() {
|
||||
let mut srv = Srv.map_err(|_| "error");
|
||||
let srv = Srv.map_err(|_| "error");
|
||||
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
||||
assert_eq!(res, Poll::Ready(Err("error")));
|
||||
}
|
||||
|
||||
#[ntex_rt::test]
|
||||
async fn test_call() {
|
||||
let mut srv = Srv.map_err(|_| "error");
|
||||
let srv = Srv.map_err(|_| "error");
|
||||
let res = srv.call(()).await;
|
||||
assert!(res.is_err());
|
||||
assert_eq!(res.err().unwrap(), "error");
|
||||
|
@ -246,7 +248,7 @@ mod tests {
|
|||
#[ntex_rt::test]
|
||||
async fn test_new_service() {
|
||||
let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map_err(|_| "error");
|
||||
let mut srv = new_srv.new_service(&()).await.unwrap();
|
||||
let srv = new_srv.new_service(&()).await.unwrap();
|
||||
let res = srv.call(()).await;
|
||||
assert!(res.is_err());
|
||||
assert_eq!(res.err().unwrap(), "error");
|
||||
|
|
|
@ -75,7 +75,7 @@ impl<T: Service> Pipeline<T> {
|
|||
Self: Sized,
|
||||
I: IntoService<U>,
|
||||
U: Service,
|
||||
F: FnMut(T::Response, &mut U) -> Fut,
|
||||
F: Fn(T::Response, &U) -> Fut,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<T::Error> + From<U::Error>,
|
||||
{
|
||||
|
@ -161,17 +161,17 @@ impl<T: Service> Service for Pipeline<T> {
|
|||
type Future = T::Future;
|
||||
|
||||
#[inline]
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), T::Error>> {
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), T::Error>> {
|
||||
self.service.poll_ready(cx)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_shutdown(&mut self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
self.service.poll_shutdown(cx, is_error)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn call(&mut self, req: T::Request) -> Self::Future {
|
||||
fn call(&self, req: T::Request) -> Self::Future {
|
||||
self.service.call(req)
|
||||
}
|
||||
}
|
||||
|
@ -240,7 +240,7 @@ impl<T: ServiceFactory> PipelineFactory<T> {
|
|||
T::Config: Clone,
|
||||
I: IntoServiceFactory<U>,
|
||||
U: ServiceFactory<Config = T::Config, InitError = T::InitError>,
|
||||
F: FnMut(T::Response, &mut U::Service) -> Fut + Clone,
|
||||
F: Fn(T::Response, &U::Service) -> Fut + Clone,
|
||||
Fut: Future<Output = Result<Res, Err>>,
|
||||
Err: From<T::Error> + From<U::Error>,
|
||||
{
|
||||
|
|
|
@ -4,13 +4,12 @@ use std::rc::Rc;
|
|||
use std::task::{Context, Poll};
|
||||
|
||||
use super::{Service, ServiceFactory};
|
||||
use crate::cell::Cell;
|
||||
|
||||
/// Service for the `then` combinator, chaining a computation onto the end of
|
||||
/// another service.
|
||||
///
|
||||
/// This is created by the `Pipeline::then` method.
|
||||
pub(crate) struct ThenService<A, B>(Cell<(A, B)>);
|
||||
pub(crate) struct ThenService<A, B>(Rc<(A, B)>);
|
||||
|
||||
impl<A, B> ThenService<A, B> {
|
||||
/// Create new `.then()` combinator
|
||||
|
@ -19,7 +18,7 @@ impl<A, B> ThenService<A, B> {
|
|||
A: Service,
|
||||
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
|
||||
{
|
||||
Self(Cell::new((a, b)))
|
||||
Self(Rc::new((a, b)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,8 +38,8 @@ where
|
|||
type Error = B::Error;
|
||||
type Future = ThenServiceResponse<A, B>;
|
||||
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
let srv = self.0.get_mut();
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
let srv = self.0.as_ref();
|
||||
let not_ready = !srv.0.poll_ready(cx)?.is_ready();
|
||||
if !srv.1.poll_ready(cx)?.is_ready() || not_ready {
|
||||
Poll::Pending
|
||||
|
@ -49,8 +48,8 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn poll_shutdown(&mut self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
let srv = self.0.get_mut();
|
||||
fn poll_shutdown(&self, cx: &mut Context<'_>, is_error: bool) -> Poll<()> {
|
||||
let srv = self.0.as_ref();
|
||||
|
||||
if srv.0.poll_shutdown(cx, is_error).is_ready()
|
||||
&& srv.1.poll_shutdown(cx, is_error).is_ready()
|
||||
|
@ -61,9 +60,9 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||
fn call(&self, req: A::Request) -> Self::Future {
|
||||
ThenServiceResponse {
|
||||
state: State::A(self.0.get_mut().0.call(req), Some(self.0.clone())),
|
||||
state: State::A(self.0.as_ref().0.call(req), Some(self.0.clone())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -84,7 +83,7 @@ where
|
|||
A: Service,
|
||||
B: Service<Request = Result<A::Response, A::Error>>,
|
||||
{
|
||||
A(#[pin] A::Future, Option<Cell<(A, B)>>),
|
||||
A(#[pin] A::Future, Option<Rc<(A, B)>>),
|
||||
B(#[pin] B::Future),
|
||||
Empty,
|
||||
}
|
||||
|
@ -104,9 +103,9 @@ where
|
|||
match this.state.as_mut().project() {
|
||||
State::A(fut, b) => match fut.poll(cx) {
|
||||
Poll::Ready(res) => {
|
||||
let mut b = b.take().unwrap();
|
||||
let b = b.take().unwrap();
|
||||
this.state.set(State::Empty); // drop fut A
|
||||
let fut = b.get_mut().1.call(res);
|
||||
let fut = b.as_ref().1.call(res);
|
||||
this.state.set(State::B(fut));
|
||||
self.poll(cx)
|
||||
}
|
||||
|
@ -272,12 +271,12 @@ mod tests {
|
|||
type Error = ();
|
||||
type Future = Ready<Result<Self::Response, Self::Error>>;
|
||||
|
||||
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: Result<&'static str, &'static str>) -> Self::Future {
|
||||
fn call(&self, req: Result<&'static str, &'static str>) -> Self::Future {
|
||||
match req {
|
||||
Ok(msg) => ok(msg),
|
||||
Err(_) => err(()),
|
||||
|
@ -293,12 +292,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(Err(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Result<&'static str, ()>) -> Self::Future {
|
||||
fn call(&self, req: Result<&'static str, ()>) -> Self::Future {
|
||||
match req {
|
||||
Ok(msg) => ok((msg, "ok")),
|
||||
Err(()) => ok(("srv2", "err")),
|
||||
|
@ -309,7 +308,7 @@ mod tests {
|
|||
#[ntex_rt::test]
|
||||
async fn test_poll_ready() {
|
||||
let cnt = Rc::new(Cell::new(0));
|
||||
let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt.clone()));
|
||||
let srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt.clone()));
|
||||
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
||||
assert_eq!(res, Poll::Ready(Err(())));
|
||||
assert_eq!(cnt.get(), 2);
|
||||
|
@ -318,7 +317,7 @@ mod tests {
|
|||
#[ntex_rt::test]
|
||||
async fn test_call() {
|
||||
let cnt = Rc::new(Cell::new(0));
|
||||
let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt));
|
||||
let srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt));
|
||||
|
||||
let res = srv.call(Ok("srv1")).await;
|
||||
assert!(res.is_ok());
|
||||
|
@ -335,7 +334,7 @@ mod tests {
|
|||
let cnt2 = cnt.clone();
|
||||
let blank = move || ready(Ok::<_, ()>(Srv1(cnt2.clone())));
|
||||
let factory = pipeline_factory(blank).then(move || ready(Ok(Srv2(cnt.clone()))));
|
||||
let mut srv = factory.new_service(&()).await.unwrap();
|
||||
let srv = factory.new_service(&()).await.unwrap();
|
||||
let res = srv.call(Ok("srv1")).await;
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(res.unwrap(), (("srv1", "ok")));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue