1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/src/with.rs

299 lines
8.3 KiB
Rust
Raw Normal View History

2018-04-14 01:02:01 +02:00
use futures::{Async, Future, Poll};
2018-03-27 08:10:31 +02:00
use std::cell::UnsafeCell;
use std::marker::PhantomData;
2018-04-14 01:02:01 +02:00
use std::rc::Rc;
2018-03-27 08:10:31 +02:00
use error::Error;
2018-05-04 01:22:08 +02:00
use handler::{AsyncResult, AsyncResultItem, FromRequest, Handler, Responder};
2018-03-27 08:10:31 +02:00
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
2018-06-21 07:47:01 +02:00
pub(crate) struct With<T, S, F, R>
2018-04-14 01:02:01 +02:00
where
F: Fn(T) -> R,
T: FromRequest<S>,
S: 'static,
2018-03-27 08:10:31 +02:00
{
2018-06-21 07:20:21 +02:00
hnd: Rc<WithHnd<T, S, F, R>>,
2018-06-21 07:47:01 +02:00
cfg: Rc<T::Config>,
2018-06-21 07:20:21 +02:00
}
pub struct WithHnd<T, S, F, R>
where
F: Fn(T) -> R,
T: FromRequest<S>,
S: 'static,
{
hnd: Rc<UnsafeCell<F>>,
_t: PhantomData<T>,
2018-03-31 18:58:33 +02:00
_s: PhantomData<S>,
2018-03-27 08:10:31 +02:00
}
2018-03-31 18:58:33 +02:00
impl<T, S, F, R> With<T, S, F, R>
2018-04-14 01:02:01 +02:00
where
F: Fn(T) -> R,
T: FromRequest<S>,
S: 'static,
2018-03-27 08:10:31 +02:00
{
2018-06-21 07:47:01 +02:00
pub fn new(f: F, cfg: T::Config) -> Self {
2018-04-14 01:02:01 +02:00
With {
2018-06-21 07:47:01 +02:00
cfg: Rc::new(cfg),
2018-06-21 07:20:21 +02:00
hnd: Rc::new(WithHnd {
hnd: Rc::new(UnsafeCell::new(f)),
_t: PhantomData,
_s: PhantomData,
}),
2018-04-14 01:02:01 +02:00
}
2018-03-27 08:10:31 +02:00
}
}
2018-03-31 18:58:33 +02:00
impl<T, S, F, R> Handler<S> for With<T, S, F, R>
2018-04-14 01:02:01 +02:00
where
F: Fn(T) -> R + 'static,
R: Responder + 'static,
T: FromRequest<S> + 'static,
S: 'static,
2018-03-27 08:10:31 +02:00
{
2018-05-04 01:22:08 +02:00
type Result = AsyncResult<HttpResponse>;
2018-03-27 08:10:31 +02:00
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
2018-04-14 01:02:01 +02:00
let mut fut = WithHandlerFut {
2018-03-29 18:26:01 +02:00
req,
started: false,
hnd: Rc::clone(&self.hnd),
2018-04-04 07:06:18 +02:00
cfg: self.cfg.clone(),
2018-03-29 18:26:01 +02:00
fut1: None,
fut2: None,
};
match fut.poll() {
2018-05-04 01:22:08 +02:00
Ok(Async::Ready(resp)) => AsyncResult::ok(resp),
Ok(Async::NotReady) => AsyncResult::async(Box::new(fut)),
2018-05-04 20:44:22 +02:00
Err(e) => AsyncResult::err(e),
}
2018-03-27 08:10:31 +02:00
}
}
2018-03-31 18:58:33 +02:00
struct WithHandlerFut<T, S, F, R>
2018-04-14 01:02:01 +02:00
where
F: Fn(T) -> R,
R: Responder,
T: FromRequest<S> + 'static,
S: 'static,
2018-03-27 08:10:31 +02:00
{
2018-03-29 18:26:01 +02:00
started: bool,
2018-06-21 07:20:21 +02:00
hnd: Rc<WithHnd<T, S, F, R>>,
2018-06-21 07:47:01 +02:00
cfg: Rc<T::Config>,
2018-03-27 08:10:31 +02:00
req: HttpRequest<S>,
2018-04-14 01:02:01 +02:00
fut1: Option<Box<Future<Item = T, Error = Error>>>,
fut2: Option<Box<Future<Item = HttpResponse, Error = Error>>>,
2018-03-27 08:10:31 +02:00
}
2018-03-31 18:58:33 +02:00
impl<T, S, F, R> Future for WithHandlerFut<T, S, F, R>
2018-04-14 01:02:01 +02:00
where
F: Fn(T) -> R,
R: Responder + 'static,
T: FromRequest<S> + 'static,
S: 'static,
2018-03-27 08:10:31 +02:00
{
type Item = HttpResponse;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if let Some(ref mut fut) = self.fut2 {
2018-04-14 01:02:01 +02:00
return fut.poll();
2018-03-27 08:10:31 +02:00
}
2018-03-29 18:26:01 +02:00
let item = if !self.started {
self.started = true;
let reply = T::from_request(&self.req, self.cfg.as_ref()).into();
2018-05-02 02:19:15 +02:00
match reply.into() {
2018-05-04 01:22:08 +02:00
AsyncResultItem::Err(err) => return Err(err),
AsyncResultItem::Ok(msg) => msg,
AsyncResultItem::Future(fut) => {
2018-05-02 02:19:15 +02:00
self.fut1 = Some(fut);
return self.poll();
2018-04-14 01:02:01 +02:00
}
2018-03-29 18:26:01 +02:00
}
} else {
match self.fut1.as_mut().unwrap().poll()? {
Async::Ready(item) => item,
Async::NotReady => return Ok(Async::NotReady),
}
2018-03-27 08:10:31 +02:00
};
2018-06-21 07:20:21 +02:00
let fut = {
// clone handler, inicrease ref counter
let h = self.hnd.as_ref().hnd.clone();
// Enforce invariants before entering unsafe code.
// Only two references could exists With struct owns one, and line above
2018-06-21 07:23:13 +02:00
if Rc::weak_count(&h) != 0 || Rc::strong_count(&h) != 2 {
2018-06-21 07:20:21 +02:00
panic!("Multiple copies of handler are in use")
}
let hnd: &mut F = unsafe { &mut *h.as_ref().get() };
let item = match (*hnd)(item).respond_to(&self.req) {
Ok(item) => item.into(),
Err(e) => return Err(e.into()),
};
2018-03-27 08:10:31 +02:00
2018-06-21 07:20:21 +02:00
match item.into() {
AsyncResultItem::Err(err) => return Err(err),
AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
AsyncResultItem::Future(fut) => fut,
2018-03-31 18:58:33 +02:00
}
2018-06-21 07:20:21 +02:00
};
self.fut2 = Some(fut);
self.poll()
2018-03-27 08:10:31 +02:00
}
}
2018-03-28 23:24:32 +02:00
2018-06-21 07:47:01 +02:00
pub(crate) struct WithAsync<T, S, F, R, I, E>
where
F: Fn(T) -> R,
R: Future<Item = I, Error = E>,
I: Responder,
E: Into<E>,
T: FromRequest<S>,
S: 'static,
{
2018-06-21 07:20:21 +02:00
hnd: Rc<WithHnd<T, S, F, R>>,
2018-06-21 07:47:01 +02:00
cfg: Rc<T::Config>,
}
impl<T, S, F, R, I, E> WithAsync<T, S, F, R, I, E>
where
F: Fn(T) -> R,
R: Future<Item = I, Error = E>,
I: Responder,
E: Into<Error>,
T: FromRequest<S>,
S: 'static,
{
2018-06-21 07:47:01 +02:00
pub fn new(f: F, cfg: T::Config) -> Self {
WithAsync {
2018-06-21 07:47:01 +02:00
cfg: Rc::new(cfg),
2018-06-21 07:20:21 +02:00
hnd: Rc::new(WithHnd {
hnd: Rc::new(UnsafeCell::new(f)),
_s: PhantomData,
_t: PhantomData,
}),
}
}
}
impl<T, S, F, R, I, E> Handler<S> for WithAsync<T, S, F, R, I, E>
where
F: Fn(T) -> R + 'static,
R: Future<Item = I, Error = E> + 'static,
I: Responder + 'static,
E: Into<Error> + 'static,
T: FromRequest<S> + 'static,
S: 'static,
{
type Result = AsyncResult<HttpResponse>;
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
let mut fut = WithAsyncHandlerFut {
req,
started: false,
hnd: Rc::clone(&self.hnd),
2018-06-21 07:47:01 +02:00
cfg: Rc::clone(&self.cfg),
fut1: None,
fut2: None,
fut3: None,
};
match fut.poll() {
Ok(Async::Ready(resp)) => AsyncResult::ok(resp),
Ok(Async::NotReady) => AsyncResult::async(Box::new(fut)),
Err(e) => AsyncResult::err(e),
}
}
}
struct WithAsyncHandlerFut<T, S, F, R, I, E>
where
F: Fn(T) -> R,
R: Future<Item = I, Error = E> + 'static,
I: Responder + 'static,
E: Into<Error> + 'static,
T: FromRequest<S> + 'static,
S: 'static,
{
started: bool,
2018-06-21 07:20:21 +02:00
hnd: Rc<WithHnd<T, S, F, R>>,
2018-06-21 07:47:01 +02:00
cfg: Rc<T::Config>,
req: HttpRequest<S>,
fut1: Option<Box<Future<Item = T, Error = Error>>>,
fut2: Option<R>,
fut3: Option<Box<Future<Item = HttpResponse, Error = Error>>>,
}
impl<T, S, F, R, I, E> Future for WithAsyncHandlerFut<T, S, F, R, I, E>
where
F: Fn(T) -> R,
R: Future<Item = I, Error = E> + 'static,
I: Responder + 'static,
E: Into<Error> + 'static,
T: FromRequest<S> + 'static,
S: 'static,
{
type Item = HttpResponse;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if let Some(ref mut fut) = self.fut3 {
return fut.poll();
}
if self.fut2.is_some() {
return match self.fut2.as_mut().unwrap().poll() {
Ok(Async::NotReady) => Ok(Async::NotReady),
Ok(Async::Ready(r)) => match r.respond_to(&self.req) {
Ok(r) => match r.into().into() {
AsyncResultItem::Err(err) => Err(err),
AsyncResultItem::Ok(resp) => Ok(Async::Ready(resp)),
AsyncResultItem::Future(fut) => {
self.fut3 = Some(fut);
self.poll()
}
},
Err(e) => Err(e.into()),
},
Err(e) => Err(e.into()),
};
}
let item = if !self.started {
self.started = true;
let reply = T::from_request(&self.req, self.cfg.as_ref()).into();
match reply.into() {
AsyncResultItem::Err(err) => return Err(err),
AsyncResultItem::Ok(msg) => msg,
AsyncResultItem::Future(fut) => {
self.fut1 = Some(fut);
return self.poll();
}
}
} else {
match self.fut1.as_mut().unwrap().poll()? {
Async::Ready(item) => item,
Async::NotReady => return Ok(Async::NotReady),
}
};
2018-06-21 07:20:21 +02:00
self.fut2 = {
// clone handler, inicrease ref counter
let h = self.hnd.as_ref().hnd.clone();
// Enforce invariants before entering unsafe code.
// Only two references could exists With struct owns one, and line above
2018-06-21 07:23:13 +02:00
if Rc::weak_count(&h) != 0 || Rc::strong_count(&h) != 2 {
2018-06-21 07:20:21 +02:00
panic!("Multiple copies of handler are in use")
}
let hnd: &mut F = unsafe { &mut *h.as_ref().get() };
Some((*hnd)(item))
};
self.poll()
}
}