1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00

rename Reply to a AsyncResult

This commit is contained in:
Nikolay Kim 2018-05-03 16:22:08 -07:00
parent 58079b5bbe
commit acd7380865
11 changed files with 176 additions and 176 deletions

View File

@ -2,7 +2,7 @@ use std::cell::UnsafeCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use handler::{FromRequest, Handler, Reply, Responder, RouteHandler, WrapHandler}; use handler::{AsyncResult, FromRequest, Handler, Responder, RouteHandler, WrapHandler};
use header::ContentEncoding; use header::ContentEncoding;
use http::Method; use http::Method;
use httprequest::HttpRequest; use httprequest::HttpRequest;
@ -39,7 +39,7 @@ impl<S: 'static> PipelineHandler<S> for Inner<S> {
fn handle( fn handle(
&mut self, req: HttpRequest<S>, htype: HandlerType, &mut self, req: HttpRequest<S>, htype: HandlerType,
) -> Reply<HttpResponse> { ) -> AsyncResult<HttpResponse> {
match htype { match htype {
HandlerType::Normal(idx) => { HandlerType::Normal(idx) => {
self.resources[idx].handle(req, Some(&mut self.default)) self.resources[idx].handle(req, Some(&mut self.default))
@ -90,7 +90,7 @@ impl<S: 'static> HttpApplication<S> {
} }
#[cfg(test)] #[cfg(test)]
pub(crate) fn run(&mut self, mut req: HttpRequest<S>) -> Reply<HttpResponse> { pub(crate) fn run(&mut self, mut req: HttpRequest<S>) -> AsyncResult<HttpResponse> {
let tp = self.get_handler(&mut req); let tp = self.get_handler(&mut req);
unsafe { &mut *self.inner.get() }.handle(req, tp) unsafe { &mut *self.inner.get() }.handle(req, tp)
} }

View File

@ -12,7 +12,7 @@ use serde_urlencoded;
use de::PathDeserializer; use de::PathDeserializer;
use error::{Error, ErrorBadRequest}; use error::{Error, ErrorBadRequest};
use handler::{FromRequest, Reply}; use handler::{AsyncResult, FromRequest};
use httpmessage::{HttpMessage, MessageBody, UrlEncoded}; use httpmessage::{HttpMessage, MessageBody, UrlEncoded};
use httprequest::HttpRequest; use httprequest::HttpRequest;
@ -471,7 +471,7 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
{ {
s: PhantomData<S>, s: PhantomData<S>,
items: ($(Option<$T>,)+), items: ($(Option<$T>,)+),
futs: ($(Option<Reply<$T>>,)+), futs: ($(Option<AsyncResult<$T>>,)+),
} }
impl<S, $($T: FromRequest<S>),+> Future for $fut_type<S, $($T),+> impl<S, $($T: FromRequest<S>),+> Future for $fut_type<S, $($T),+>

View File

@ -18,7 +18,7 @@ use mime;
use mime_guess::{get_mime_type, guess_mime_type}; use mime_guess::{get_mime_type, guess_mime_type};
use error::Error; use error::Error;
use handler::{Handler, Reply, Responder, RouteHandler, WrapHandler}; use handler::{AsyncResult, Handler, Responder, RouteHandler, WrapHandler};
use header; use header;
use http::{Method, StatusCode}; use http::{Method, StatusCode};
use httpmessage::HttpMessage; use httpmessage::HttpMessage;
@ -578,7 +578,7 @@ impl<S: 'static> StaticFiles<S> {
} }
impl<S: 'static> Handler<S> for StaticFiles<S> { impl<S: 'static> Handler<S> for StaticFiles<S> {
type Result = Result<Reply<HttpResponse>, Error>; type Result = Result<AsyncResult<HttpResponse>, Error>;
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result { fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
if !self.accessible { if !self.accessible {

View File

@ -23,12 +23,12 @@ pub trait Handler<S>: 'static {
/// Types that implement this trait can be used as the return type of a handler. /// Types that implement this trait can be used as the return type of a handler.
pub trait Responder { pub trait Responder {
/// The associated item which can be returned. /// The associated item which can be returned.
type Item: Into<Reply<HttpResponse>>; type Item: Into<AsyncResult<HttpResponse>>;
/// The associated error which can be returned. /// The associated error which can be returned.
type Error: Into<Error>; type Error: Into<Error>;
/// Convert itself to `Reply` or `Error`. /// Convert itself to `AsyncResult` or `Error`.
fn respond_to(self, req: HttpRequest) -> Result<Self::Item, Self::Error>; fn respond_to(self, req: HttpRequest) -> Result<Self::Item, Self::Error>;
} }
@ -40,7 +40,7 @@ pub trait FromRequest<S>: Sized {
type Config: Default; type Config: Default;
/// Future that resolves to a Self /// Future that resolves to a Self
type Result: Into<Reply<Self>>; type Result: Into<AsyncResult<Self>>;
/// Convert request to a Self /// Convert request to a Self
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result; fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result;
@ -93,10 +93,10 @@ where
A: Responder, A: Responder,
B: Responder, B: Responder,
{ {
type Item = Reply<HttpResponse>; type Item = AsyncResult<HttpResponse>;
type Error = Error; type Error = Error;
fn respond_to(self, req: HttpRequest) -> Result<Reply<HttpResponse>, Error> { fn respond_to(self, req: HttpRequest) -> Result<AsyncResult<HttpResponse>, Error> {
match self { match self {
Either::A(a) => match a.respond_to(req) { Either::A(a) => match a.respond_to(req) {
Ok(val) => Ok(val.into()), Ok(val) => Ok(val.into()),
@ -182,26 +182,26 @@ where
} }
} }
/// Represents reply process. /// Represents async result
/// ///
/// Reply could be in tree different forms. /// Result could be in tree different forms.
/// * Message(T) - ready item /// * Ok(T) - ready item
/// * Error(Error) - error happen during reply process /// * Err(E) - error happen during reply process
/// * Future<T, Error> - reply process completes in the future /// * Future<T, E> - reply process completes in the future
pub struct Reply<I, E = Error>(Option<ReplyResult<I, E>>); pub struct AsyncResult<I, E = Error>(Option<AsyncResultItem<I, E>>);
impl<I, E> Future for Reply<I, E> { impl<I, E> Future for AsyncResult<I, E> {
type Item = I; type Item = I;
type Error = E; type Error = E;
fn poll(&mut self) -> Poll<I, E> { fn poll(&mut self) -> Poll<I, E> {
let res = self.0.take().expect("use after resolve"); let res = self.0.take().expect("use after resolve");
match res { match res {
ReplyResult::Ok(msg) => Ok(Async::Ready(msg)), AsyncResultItem::Ok(msg) => Ok(Async::Ready(msg)),
ReplyResult::Err(err) => Err(err), AsyncResultItem::Err(err) => Err(err),
ReplyResult::Future(mut fut) => match fut.poll() { AsyncResultItem::Future(mut fut) => match fut.poll() {
Ok(Async::NotReady) => { Ok(Async::NotReady) => {
self.0 = Some(ReplyResult::Future(fut)); self.0 = Some(AsyncResultItem::Future(fut));
Ok(Async::NotReady) Ok(Async::NotReady)
} }
Ok(Async::Ready(msg)) => Ok(Async::Ready(msg)), Ok(Async::Ready(msg)) => Ok(Async::Ready(msg)),
@ -211,43 +211,40 @@ impl<I, E> Future for Reply<I, E> {
} }
} }
pub(crate) enum ReplyResult<I, E> { pub(crate) enum AsyncResultItem<I, E> {
Ok(I), Ok(I),
Err(E), Err(E),
Future(Box<Future<Item = I, Error = E>>), Future(Box<Future<Item = I, Error = E>>),
} }
impl<I, E> Reply<I, E> { impl<I, E> AsyncResult<I, E> {
/// Create async response /// Create async response
#[inline] #[inline]
pub fn async<F>(fut: F) -> Reply<I, E> pub fn async(fut: Box<Future<Item = I, Error = E>>) -> AsyncResult<I, E> {
where AsyncResult(Some(AsyncResultItem::Future(fut)))
F: Future<Item = I, Error = E> + 'static,
{
Reply(Some(ReplyResult::Future(Box::new(fut))))
} }
/// Send response /// Send response
#[inline] #[inline]
pub fn response<R: Into<I>>(response: R) -> Reply<I, E> { pub fn ok<R: Into<I>>(ok: R) -> AsyncResult<I, E> {
Reply(Some(ReplyResult::Ok(response.into()))) AsyncResult(Some(AsyncResultItem::Ok(ok.into())))
} }
/// Send error /// Send error
#[inline] #[inline]
pub fn error<R: Into<E>>(err: R) -> Reply<I, E> { pub fn error<R: Into<E>>(err: R) -> AsyncResult<I, E> {
Reply(Some(ReplyResult::Err(err.into()))) AsyncResult(Some(AsyncResultItem::Err(err.into())))
} }
#[inline] #[inline]
pub(crate) fn into(self) -> ReplyResult<I, E> { pub(crate) fn into(self) -> AsyncResultItem<I, E> {
self.0.expect("use after resolve") self.0.expect("use after resolve")
} }
#[cfg(test)] #[cfg(test)]
pub(crate) fn as_msg(&self) -> &I { pub(crate) fn as_msg(&self) -> &I {
match self.0.as_ref().unwrap() { match self.0.as_ref().unwrap() {
&ReplyResult::Ok(ref resp) => resp, &AsyncResultItem::Ok(ref resp) => resp,
_ => panic!(), _ => panic!(),
} }
} }
@ -255,35 +252,35 @@ impl<I, E> Reply<I, E> {
#[cfg(test)] #[cfg(test)]
pub(crate) fn as_err(&self) -> Option<&E> { pub(crate) fn as_err(&self) -> Option<&E> {
match self.0.as_ref().unwrap() { match self.0.as_ref().unwrap() {
&ReplyResult::Err(ref err) => Some(err), &AsyncResultItem::Err(ref err) => Some(err),
_ => None, _ => None,
} }
} }
} }
impl Responder for Reply<HttpResponse> { impl Responder for AsyncResult<HttpResponse> {
type Item = Reply<HttpResponse>; type Item = AsyncResult<HttpResponse>;
type Error = Error; type Error = Error;
fn respond_to(self, _: HttpRequest) -> Result<Reply<HttpResponse>, Error> { fn respond_to(self, _: HttpRequest) -> Result<AsyncResult<HttpResponse>, Error> {
Ok(self) Ok(self)
} }
} }
impl Responder for HttpResponse { impl Responder for HttpResponse {
type Item = Reply<HttpResponse>; type Item = AsyncResult<HttpResponse>;
type Error = Error; type Error = Error;
#[inline] #[inline]
fn respond_to(self, _: HttpRequest) -> Result<Reply<HttpResponse>, Error> { fn respond_to(self, _: HttpRequest) -> Result<AsyncResult<HttpResponse>, Error> {
Ok(Reply(Some(ReplyResult::Ok(self)))) Ok(AsyncResult(Some(AsyncResultItem::Ok(self))))
} }
} }
impl<T> From<T> for Reply<T> { impl<T> From<T> for AsyncResult<T> {
#[inline] #[inline]
fn from(resp: T) -> Reply<T> { fn from(resp: T) -> AsyncResult<T> {
Reply(Some(ReplyResult::Ok(resp))) AsyncResult(Some(AsyncResultItem::Ok(resp)))
} }
} }
@ -302,42 +299,42 @@ impl<T: Responder, E: Into<Error>> Responder for Result<T, E> {
} }
} }
impl<T, E: Into<Error>> From<Result<Reply<T>, E>> for Reply<T> { impl<T, E: Into<Error>> From<Result<AsyncResult<T>, E>> for AsyncResult<T> {
#[inline] #[inline]
fn from(res: Result<Reply<T>, E>) -> Self { fn from(res: Result<AsyncResult<T>, E>) -> Self {
match res { match res {
Ok(val) => val, Ok(val) => val,
Err(err) => Reply(Some(ReplyResult::Err(err.into()))), Err(err) => AsyncResult(Some(AsyncResultItem::Err(err.into()))),
} }
} }
} }
impl<T, E: Into<Error>> From<Result<T, E>> for Reply<T> { impl<T, E: Into<Error>> From<Result<T, E>> for AsyncResult<T> {
#[inline] #[inline]
fn from(res: Result<T, E>) -> Self { fn from(res: Result<T, E>) -> Self {
match res { match res {
Ok(val) => Reply(Some(ReplyResult::Ok(val))), Ok(val) => AsyncResult(Some(AsyncResultItem::Ok(val))),
Err(err) => Reply(Some(ReplyResult::Err(err.into()))), Err(err) => AsyncResult(Some(AsyncResultItem::Err(err.into()))),
} }
} }
} }
impl<T, E: Into<Error>> From<Result<Box<Future<Item = T, Error = Error>>, E>> impl<T, E: Into<Error>> From<Result<Box<Future<Item = T, Error = Error>>, E>>
for Reply<T> for AsyncResult<T>
{ {
#[inline] #[inline]
fn from(res: Result<Box<Future<Item = T, Error = Error>>, E>) -> Self { fn from(res: Result<Box<Future<Item = T, Error = Error>>, E>) -> Self {
match res { match res {
Ok(fut) => Reply(Some(ReplyResult::Future(fut))), Ok(fut) => AsyncResult(Some(AsyncResultItem::Future(fut))),
Err(err) => Reply(Some(ReplyResult::Err(err.into()))), Err(err) => AsyncResult(Some(AsyncResultItem::Err(err.into()))),
} }
} }
} }
impl<T> From<Box<Future<Item = T, Error = Error>>> for Reply<T> { impl<T> From<Box<Future<Item = T, Error = Error>>> for AsyncResult<T> {
#[inline] #[inline]
fn from(fut: Box<Future<Item = T, Error = Error>>) -> Reply<T> { fn from(fut: Box<Future<Item = T, Error = Error>>) -> AsyncResult<T> {
Reply(Some(ReplyResult::Future(fut))) AsyncResult(Some(AsyncResultItem::Future(fut)))
} }
} }
@ -349,26 +346,26 @@ where
I: Responder + 'static, I: Responder + 'static,
E: Into<Error> + 'static, E: Into<Error> + 'static,
{ {
type Item = Reply<HttpResponse>; type Item = AsyncResult<HttpResponse>;
type Error = Error; type Error = Error;
#[inline] #[inline]
fn respond_to(self, req: HttpRequest) -> Result<Reply<HttpResponse>, Error> { fn respond_to(self, req: HttpRequest) -> Result<AsyncResult<HttpResponse>, Error> {
let fut = self.map_err(|e| e.into()) let fut = self.map_err(|e| e.into())
.then(move |r| match r.respond_to(req) { .then(move |r| match r.respond_to(req) {
Ok(reply) => match reply.into().into() { Ok(reply) => match reply.into().into() {
ReplyResult::Ok(resp) => ok(resp), AsyncResultItem::Ok(resp) => ok(resp),
_ => panic!("Nested async replies are not supported"), _ => panic!("Nested async replies are not supported"),
}, },
Err(e) => err(e), Err(e) => err(e),
}); });
Ok(Reply::async(fut)) Ok(AsyncResult::async(Box::new(fut)))
} }
} }
/// Trait defines object that could be registered as resource route /// Trait defines object that could be registered as resource route
pub(crate) trait RouteHandler<S>: 'static { pub(crate) trait RouteHandler<S>: 'static {
fn handle(&mut self, req: HttpRequest<S>) -> Reply<HttpResponse>; fn handle(&mut self, req: HttpRequest<S>) -> AsyncResult<HttpResponse>;
} }
/// Route handler wrapper for Handler /// Route handler wrapper for Handler
@ -402,11 +399,11 @@ where
R: Responder + 'static, R: Responder + 'static,
S: 'static, S: 'static,
{ {
fn handle(&mut self, req: HttpRequest<S>) -> Reply<HttpResponse> { fn handle(&mut self, req: HttpRequest<S>) -> AsyncResult<HttpResponse> {
let req2 = req.drop_state(); let req2 = req.drop_state();
match self.h.handle(req).respond_to(req2) { match self.h.handle(req).respond_to(req2) {
Ok(reply) => reply.into(), Ok(reply) => reply.into(),
Err(err) => Reply::response(err.into()), Err(err) => AsyncResult::ok(err.into()),
} }
} }
} }
@ -448,18 +445,18 @@ where
E: Into<Error> + 'static, E: Into<Error> + 'static,
S: 'static, S: 'static,
{ {
fn handle(&mut self, req: HttpRequest<S>) -> Reply<HttpResponse> { fn handle(&mut self, req: HttpRequest<S>) -> AsyncResult<HttpResponse> {
let req2 = req.drop_state(); let req2 = req.drop_state();
let fut = (self.h)(req).map_err(|e| e.into()).then(move |r| { let fut = (self.h)(req).map_err(|e| e.into()).then(move |r| {
match r.respond_to(req2) { match r.respond_to(req2) {
Ok(reply) => match reply.into().into() { Ok(reply) => match reply.into().into() {
ReplyResult::Ok(resp) => ok(resp), AsyncResultItem::Ok(resp) => ok(resp),
_ => panic!("Nested async replies are not supported"), _ => panic!("Nested async replies are not supported"),
}, },
Err(e) => err(e), Err(e) => err(e),
} }
}); });
Reply::async(fut) AsyncResult::async(Box::new(fut))
} }
} }

View File

@ -200,7 +200,7 @@ pub mod dev {
pub use body::BodyStream; pub use body::BodyStream;
pub use context::Drain; pub use context::Drain;
pub use extractor::{FormConfig, PayloadConfig}; pub use extractor::{FormConfig, PayloadConfig};
pub use handler::{Handler, Reply}; pub use handler::{AsyncResult, Handler};
pub use httpmessage::{MessageBody, UrlEncoded}; pub use httpmessage::{MessageBody, UrlEncoded};
pub use httpresponse::HttpResponseBuilder; pub use httpresponse::HttpResponseBuilder;
pub use info::ConnectionInfo; pub use info::ConnectionInfo;

View File

@ -11,7 +11,7 @@ use application::Inner;
use body::{Body, BodyStream}; use body::{Body, BodyStream};
use context::{ActorHttpContext, Frame}; use context::{ActorHttpContext, Frame};
use error::Error; use error::Error;
use handler::{Reply, ReplyResult}; use handler::{AsyncResult, AsyncResultItem};
use header::ContentEncoding; use header::ContentEncoding;
use httprequest::HttpRequest; use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
@ -28,8 +28,9 @@ pub(crate) enum HandlerType {
pub(crate) trait PipelineHandler<S> { pub(crate) trait PipelineHandler<S> {
fn encoding(&self) -> ContentEncoding; fn encoding(&self) -> ContentEncoding;
fn handle(&mut self, req: HttpRequest<S>, htype: HandlerType) fn handle(
-> Reply<HttpResponse>; &mut self, req: HttpRequest<S>, htype: HandlerType,
) -> AsyncResult<HttpResponse>;
} }
pub(crate) struct Pipeline<S, H>(PipelineInfo<S>, PipelineState<S, H>); pub(crate) struct Pipeline<S, H>(PipelineInfo<S>, PipelineState<S, H>);
@ -321,12 +322,12 @@ struct WaitingResponse<S, H> {
impl<S: 'static, H> WaitingResponse<S, H> { impl<S: 'static, H> WaitingResponse<S, H> {
#[inline] #[inline]
fn init( fn init(
info: &mut PipelineInfo<S>, reply: Reply<HttpResponse>, info: &mut PipelineInfo<S>, reply: AsyncResult<HttpResponse>,
) -> PipelineState<S, H> { ) -> PipelineState<S, H> {
match reply.into() { match reply.into() {
ReplyResult::Err(err) => RunMiddlewares::init(info, err.into()), AsyncResultItem::Err(err) => RunMiddlewares::init(info, err.into()),
ReplyResult::Ok(resp) => RunMiddlewares::init(info, resp), AsyncResultItem::Ok(resp) => RunMiddlewares::init(info, resp),
ReplyResult::Future(fut) => PipelineState::Handler(WaitingResponse { AsyncResultItem::Future(fut) => PipelineState::Handler(WaitingResponse {
fut, fut,
_s: PhantomData, _s: PhantomData,
_h: PhantomData, _h: PhantomData,

View File

@ -4,7 +4,7 @@ use std::rc::Rc;
use http::{Method, StatusCode}; use http::{Method, StatusCode};
use smallvec::SmallVec; use smallvec::SmallVec;
use handler::{FromRequest, Handler, Reply, Responder}; use handler::{AsyncResult, FromRequest, Handler, Responder};
use httprequest::HttpRequest; use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
use middleware::Middleware; use middleware::Middleware;
@ -198,7 +198,7 @@ impl<S: 'static> ResourceHandler<S> {
pub(crate) fn handle( pub(crate) fn handle(
&mut self, mut req: HttpRequest<S>, default: Option<&mut ResourceHandler<S>>, &mut self, mut req: HttpRequest<S>, default: Option<&mut ResourceHandler<S>>,
) -> Reply<HttpResponse> { ) -> AsyncResult<HttpResponse> {
for route in &mut self.routes { for route in &mut self.routes {
if route.check(&mut req) { if route.check(&mut req) {
return if self.middlewares.is_empty() { return if self.middlewares.is_empty() {
@ -211,7 +211,7 @@ impl<S: 'static> ResourceHandler<S> {
if let Some(resource) = default { if let Some(resource) = default {
resource.handle(req, None) resource.handle(req, None)
} else { } else {
Reply::response(HttpResponse::new(StatusCode::NOT_FOUND)) AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND))
} }
} }
} }

View File

@ -5,8 +5,8 @@ use std::rc::Rc;
use futures::{Async, Future, Poll}; use futures::{Async, Future, Poll};
use error::Error; use error::Error;
use handler::{AsyncHandler, FromRequest, Handler, Reply, ReplyResult, Responder, use handler::{AsyncHandler, AsyncResult, AsyncResultItem, FromRequest, Handler,
RouteHandler, WrapHandler}; Responder, RouteHandler, WrapHandler};
use http::StatusCode; use http::StatusCode;
use httprequest::HttpRequest; use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
@ -45,15 +45,15 @@ impl<S: 'static> Route<S> {
} }
#[inline] #[inline]
pub(crate) fn handle(&mut self, req: HttpRequest<S>) -> Reply<HttpResponse> { pub(crate) fn handle(&mut self, req: HttpRequest<S>) -> AsyncResult<HttpResponse> {
self.handler.handle(req) self.handler.handle(req)
} }
#[inline] #[inline]
pub(crate) fn compose( pub(crate) fn compose(
&mut self, req: HttpRequest<S>, mws: Rc<Vec<Box<Middleware<S>>>>, &mut self, req: HttpRequest<S>, mws: Rc<Vec<Box<Middleware<S>>>>,
) -> Reply<HttpResponse> { ) -> AsyncResult<HttpResponse> {
Reply::async(Compose::new(req, mws, self.handler.clone())) AsyncResult::async(Box::new(Compose::new(req, mws, self.handler.clone())))
} }
/// Add match predicate to route. /// Add match predicate to route.
@ -243,7 +243,7 @@ impl<S: 'static> InnerHandler<S> {
} }
#[inline] #[inline]
pub fn handle(&self, req: HttpRequest<S>) -> Reply<HttpResponse> { pub fn handle(&self, req: HttpRequest<S>) -> AsyncResult<HttpResponse> {
// reason: handler is unique per thread, handler get called from async code only // reason: handler is unique per thread, handler get called from async code only
let h = unsafe { &mut *self.0.as_ref().get() }; let h = unsafe { &mut *self.0.as_ref().get() };
h.handle(req) h.handle(req)
@ -415,11 +415,13 @@ struct WaitingResponse<S> {
impl<S: 'static> WaitingResponse<S> { impl<S: 'static> WaitingResponse<S> {
#[inline] #[inline]
fn init(info: &mut ComposeInfo<S>, reply: Reply<HttpResponse>) -> ComposeState<S> { fn init(
info: &mut ComposeInfo<S>, reply: AsyncResult<HttpResponse>,
) -> ComposeState<S> {
match reply.into() { match reply.into() {
ReplyResult::Err(err) => RunMiddlewares::init(info, err.into()), AsyncResultItem::Err(err) => RunMiddlewares::init(info, err.into()),
ReplyResult::Ok(resp) => RunMiddlewares::init(info, resp), AsyncResultItem::Ok(resp) => RunMiddlewares::init(info, resp),
ReplyResult::Future(fut) => ComposeState::Handler(WaitingResponse { AsyncResultItem::Future(fut) => ComposeState::Handler(WaitingResponse {
fut, fut,
_s: PhantomData, _s: PhantomData,
}), }),

View File

@ -5,7 +5,7 @@ use std::rc::Rc;
use futures::{Async, Future, Poll}; use futures::{Async, Future, Poll};
use error::Error; use error::Error;
use handler::{FromRequest, Reply, ReplyResult, Responder, RouteHandler}; use handler::{AsyncResult, AsyncResultItem, FromRequest, Responder, RouteHandler};
use http::Method; use http::Method;
use httprequest::HttpRequest; use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
@ -274,7 +274,7 @@ impl<S: 'static> Scope<S> {
} }
impl<S: 'static> RouteHandler<S> for Scope<S> { impl<S: 'static> RouteHandler<S> for Scope<S> {
fn handle(&mut self, mut req: HttpRequest<S>) -> Reply<HttpResponse> { fn handle(&mut self, mut req: HttpRequest<S>) -> AsyncResult<HttpResponse> {
let path = unsafe { &*(&req.match_info()["tail"] as *const _) }; let path = unsafe { &*(&req.match_info()["tail"] as *const _) };
let path = if path == "" { "/" } else { path }; let path = if path == "" { "/" } else { path };
@ -287,12 +287,12 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
let resource = unsafe { &mut *resource.get() }; let resource = unsafe { &mut *resource.get() };
return resource.handle(req, Some(default)); return resource.handle(req, Some(default));
} else { } else {
return Reply::async(Compose::new( return AsyncResult::async(Box::new(Compose::new(
req, req,
Rc::clone(&self.middlewares), Rc::clone(&self.middlewares),
Rc::clone(&resource), Rc::clone(&resource),
Some(Rc::clone(&self.default)), Some(Rc::clone(&self.default)),
)); )));
} }
} }
} }
@ -330,12 +330,12 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
if self.middlewares.is_empty() { if self.middlewares.is_empty() {
default.handle(req, None) default.handle(req, None)
} else { } else {
Reply::async(Compose::new( AsyncResult::async(Box::new(Compose::new(
req, req,
Rc::clone(&self.middlewares), Rc::clone(&self.middlewares),
Rc::clone(&self.default), Rc::clone(&self.default),
None, None,
)) )))
} }
} }
} }
@ -346,7 +346,7 @@ struct Wrapper<S: 'static> {
} }
impl<S: 'static, S2: 'static> RouteHandler<S2> for Wrapper<S> { impl<S: 'static, S2: 'static> RouteHandler<S2> for Wrapper<S> {
fn handle(&mut self, req: HttpRequest<S2>) -> Reply<HttpResponse> { fn handle(&mut self, req: HttpRequest<S2>) -> AsyncResult<HttpResponse> {
self.scope self.scope
.handle(req.change_state(Rc::clone(&self.state))) .handle(req.change_state(Rc::clone(&self.state)))
} }
@ -521,11 +521,13 @@ struct WaitingResponse<S> {
impl<S: 'static> WaitingResponse<S> { impl<S: 'static> WaitingResponse<S> {
#[inline] #[inline]
fn init(info: &mut ComposeInfo<S>, reply: Reply<HttpResponse>) -> ComposeState<S> { fn init(
info: &mut ComposeInfo<S>, reply: AsyncResult<HttpResponse>,
) -> ComposeState<S> {
match reply.into() { match reply.into() {
ReplyResult::Ok(resp) => RunMiddlewares::init(info, resp), AsyncResultItem::Ok(resp) => RunMiddlewares::init(info, resp),
ReplyResult::Err(err) => RunMiddlewares::init(info, err.into()), AsyncResultItem::Err(err) => RunMiddlewares::init(info, err.into()),
ReplyResult::Future(fut) => ComposeState::Handler(WaitingResponse { AsyncResultItem::Future(fut) => ComposeState::Handler(WaitingResponse {
fut, fut,
_s: PhantomData, _s: PhantomData,
}), }),

View File

@ -21,7 +21,7 @@ use application::{App, HttpApplication};
use body::Binary; use body::Binary;
use client::{ClientConnector, ClientRequest, ClientRequestBuilder}; use client::{ClientConnector, ClientRequest, ClientRequestBuilder};
use error::Error; use error::Error;
use handler::{Handler, ReplyResult, Responder}; use handler::{AsyncResultItem, Handler, Responder};
use header::{Header, IntoHeaderValue}; use header::{Header, IntoHeaderValue};
use httprequest::HttpRequest; use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
@ -593,19 +593,17 @@ impl<S> TestRequest<S> {
/// with generated request. /// with generated request.
/// ///
/// This method panics is handler returns actor or async result. /// This method panics is handler returns actor or async result.
pub fn run<H: Handler<S>>( pub fn run<H: Handler<S>>(self, mut h: H) -> Result<HttpResponse, Error> {
self, mut h: H,
) -> Result<HttpResponse, <<H as Handler<S>>::Result as Responder>::Error> {
let req = self.finish(); let req = self.finish();
let resp = h.handle(req.clone()); let resp = h.handle(req.clone());
match resp.respond_to(req.drop_state()) { match resp.respond_to(req.drop_state()) {
Ok(resp) => match resp.into().into() { Ok(resp) => match resp.into().into() {
ReplyResult::Ok(resp) => Ok(resp), AsyncResultItem::Ok(resp) => Ok(resp),
ReplyResult::Err(err) => Ok(err.into()), AsyncResultItem::Err(err) => Err(err),
ReplyResult::Future(_) => panic!("Async handler is not supported."), AsyncResultItem::Future(_) => panic!("Async handler is not supported."),
}, },
Err(err) => Err(err), Err(err) => Err(err.into()),
} }
} }
@ -627,7 +625,7 @@ impl<S> TestRequest<S> {
match core.run(fut) { match core.run(fut) {
Ok(r) => match r.respond_to(req.drop_state()) { Ok(r) => match r.respond_to(req.drop_state()) {
Ok(reply) => match reply.into().into() { Ok(reply) => match reply.into().into() {
ReplyResult::Ok(resp) => Ok(resp), AsyncResultItem::Ok(resp) => Ok(resp),
_ => panic!("Nested async replies are not supported"), _ => panic!("Nested async replies are not supported"),
}, },
Err(e) => Err(e), Err(e) => Err(e),

View File

@ -5,7 +5,7 @@ use std::ops::{Deref, DerefMut};
use std::rc::Rc; use std::rc::Rc;
use error::Error; use error::Error;
use handler::{FromRequest, Handler, Reply, ReplyResult, Responder}; use handler::{AsyncResult, AsyncResultItem, FromRequest, Handler, Responder};
use httprequest::HttpRequest; use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
@ -82,7 +82,7 @@ where
T: FromRequest<S> + 'static, T: FromRequest<S> + 'static,
S: 'static, S: 'static,
{ {
type Result = Reply<HttpResponse>; type Result = AsyncResult<HttpResponse>;
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result { fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
let mut fut = WithHandlerFut { let mut fut = WithHandlerFut {
@ -95,9 +95,9 @@ where
}; };
match fut.poll() { match fut.poll() {
Ok(Async::Ready(resp)) => Reply::response(resp), Ok(Async::Ready(resp)) => AsyncResult::ok(resp),
Ok(Async::NotReady) => Reply::async(fut), Ok(Async::NotReady) => AsyncResult::async(Box::new(fut)),
Err(e) => Reply::error::<Error>(e), Err(e) => AsyncResult::error::<Error>(e),
} }
} }
} }
@ -136,9 +136,9 @@ where
self.started = true; self.started = true;
let reply = T::from_request(&self.req, self.cfg.as_ref()).into(); let reply = T::from_request(&self.req, self.cfg.as_ref()).into();
match reply.into() { match reply.into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(msg) => msg, AsyncResultItem::Ok(msg) => msg,
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.fut1 = Some(fut); self.fut1 = Some(fut);
return self.poll(); return self.poll();
} }
@ -157,9 +157,9 @@ where
}; };
match item.into() { match item.into() {
ReplyResult::Err(err) => Err(err), AsyncResultItem::Err(err) => Err(err),
ReplyResult::Ok(resp) => Ok(Async::Ready(resp)), AsyncResultItem::Ok(resp) => Ok(Async::Ready(resp)),
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.fut2 = Some(fut); self.fut2 = Some(fut);
self.poll() self.poll()
} }
@ -207,7 +207,7 @@ where
T2: FromRequest<S> + 'static, T2: FromRequest<S> + 'static,
S: 'static, S: 'static,
{ {
type Result = Reply<HttpResponse>; type Result = AsyncResult<HttpResponse>;
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result { fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
let mut fut = WithHandlerFut2 { let mut fut = WithHandlerFut2 {
@ -222,9 +222,9 @@ where
fut3: None, fut3: None,
}; };
match fut.poll() { match fut.poll() {
Ok(Async::Ready(resp)) => Reply::response(resp), Ok(Async::Ready(resp)) => AsyncResult::ok(resp),
Ok(Async::NotReady) => Reply::async(fut), Ok(Async::NotReady) => AsyncResult::async(Box::new(fut)),
Err(e) => Reply::response(e), Err(e) => AsyncResult::ok(e),
} }
} }
} }
@ -268,9 +268,9 @@ where
self.started = true; self.started = true;
let reply = T1::from_request(&self.req, self.cfg1.as_ref()).into(); let reply = T1::from_request(&self.req, self.cfg1.as_ref()).into();
let item1 = match reply.into() { let item1 = match reply.into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(msg) => msg, AsyncResultItem::Ok(msg) => msg,
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.fut1 = Some(fut); self.fut1 = Some(fut);
return self.poll(); return self.poll();
} }
@ -278,9 +278,9 @@ where
let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into(); let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
let item2 = match reply.into() { let item2 = match reply.into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(msg) => msg, AsyncResultItem::Ok(msg) => msg,
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.item = Some(item1); self.item = Some(item1);
self.fut2 = Some(fut); self.fut2 = Some(fut);
return self.poll(); return self.poll();
@ -290,9 +290,9 @@ where
let hnd: &mut F = unsafe { &mut *self.hnd.get() }; let hnd: &mut F = unsafe { &mut *self.hnd.get() };
match (*hnd)(item1, item2).respond_to(self.req.drop_state()) { match (*hnd)(item1, item2).respond_to(self.req.drop_state()) {
Ok(item) => match item.into().into() { Ok(item) => match item.into().into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)), AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.fut3 = Some(fut); self.fut3 = Some(fut);
return self.poll(); return self.poll();
} }
@ -306,9 +306,9 @@ where
Async::Ready(item) => { Async::Ready(item) => {
let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into(); let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
let item2 = match reply.into() { let item2 = match reply.into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(msg) => msg, AsyncResultItem::Ok(msg) => msg,
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.item = Some(item); self.item = Some(item);
self.fut2 = Some(fut); self.fut2 = Some(fut);
return self.poll(); return self.poll();
@ -318,9 +318,9 @@ where
let hnd: &mut F = unsafe { &mut *self.hnd.get() }; let hnd: &mut F = unsafe { &mut *self.hnd.get() };
match (*hnd)(item, item2).respond_to(self.req.drop_state()) { match (*hnd)(item, item2).respond_to(self.req.drop_state()) {
Ok(item) => match item.into().into() { Ok(item) => match item.into().into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)), AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.fut3 = Some(fut); self.fut3 = Some(fut);
return self.poll(); return self.poll();
} }
@ -346,9 +346,9 @@ where
}; };
match item.into() { match item.into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)), AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
ReplyResult::Future(fut) => self.fut3 = Some(fut), AsyncResultItem::Future(fut) => self.fut3 = Some(fut),
} }
self.poll() self.poll()
@ -404,7 +404,7 @@ where
T3: 'static, T3: 'static,
S: 'static, S: 'static,
{ {
type Result = Reply<HttpResponse>; type Result = AsyncResult<HttpResponse>;
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result { fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
let mut fut = WithHandlerFut3 { let mut fut = WithHandlerFut3 {
@ -422,9 +422,9 @@ where
fut4: None, fut4: None,
}; };
match fut.poll() { match fut.poll() {
Ok(Async::Ready(resp)) => Reply::response(resp), Ok(Async::Ready(resp)) => AsyncResult::ok(resp),
Ok(Async::NotReady) => Reply::async(fut), Ok(Async::NotReady) => AsyncResult::async(Box::new(fut)),
Err(e) => Reply::response(e), Err(e) => AsyncResult::error(e),
} }
} }
} }
@ -473,9 +473,9 @@ where
self.started = true; self.started = true;
let reply = T1::from_request(&self.req, self.cfg1.as_ref()).into(); let reply = T1::from_request(&self.req, self.cfg1.as_ref()).into();
let item1 = match reply.into() { let item1 = match reply.into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(msg) => msg, AsyncResultItem::Ok(msg) => msg,
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.fut1 = Some(fut); self.fut1 = Some(fut);
return self.poll(); return self.poll();
} }
@ -483,9 +483,9 @@ where
let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into(); let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
let item2 = match reply.into() { let item2 = match reply.into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(msg) => msg, AsyncResultItem::Ok(msg) => msg,
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.item1 = Some(item1); self.item1 = Some(item1);
self.fut2 = Some(fut); self.fut2 = Some(fut);
return self.poll(); return self.poll();
@ -494,9 +494,9 @@ where
let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into(); let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into();
let item3 = match reply.into() { let item3 = match reply.into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(msg) => msg, AsyncResultItem::Ok(msg) => msg,
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.item1 = Some(item1); self.item1 = Some(item1);
self.item2 = Some(item2); self.item2 = Some(item2);
self.fut3 = Some(fut); self.fut3 = Some(fut);
@ -507,9 +507,9 @@ where
let hnd: &mut F = unsafe { &mut *self.hnd.get() }; let hnd: &mut F = unsafe { &mut *self.hnd.get() };
match (*hnd)(item1, item2, item3).respond_to(self.req.drop_state()) { match (*hnd)(item1, item2, item3).respond_to(self.req.drop_state()) {
Ok(item) => match item.into().into() { Ok(item) => match item.into().into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)), AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.fut4 = Some(fut); self.fut4 = Some(fut);
return self.poll(); return self.poll();
} }
@ -525,9 +525,9 @@ where
self.fut1.take(); self.fut1.take();
let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into(); let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
let item2 = match reply.into() { let item2 = match reply.into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(msg) => msg, AsyncResultItem::Ok(msg) => msg,
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.fut2 = Some(fut); self.fut2 = Some(fut);
return self.poll(); return self.poll();
} }
@ -535,9 +535,9 @@ where
let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into(); let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into();
let item3 = match reply.into() { let item3 = match reply.into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(msg) => msg, AsyncResultItem::Ok(msg) => msg,
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.item2 = Some(item2); self.item2 = Some(item2);
self.fut3 = Some(fut); self.fut3 = Some(fut);
return self.poll(); return self.poll();
@ -548,9 +548,9 @@ where
.respond_to(self.req.drop_state()) .respond_to(self.req.drop_state())
{ {
Ok(item) => match item.into().into() { Ok(item) => match item.into().into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)), AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.fut4 = Some(fut); self.fut4 = Some(fut);
return self.poll(); return self.poll();
} }
@ -568,9 +568,9 @@ where
self.fut2.take(); self.fut2.take();
let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into(); let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into();
let item3 = match reply.into() { let item3 = match reply.into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(msg) => msg, AsyncResultItem::Ok(msg) => msg,
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.item2 = Some(item); self.item2 = Some(item);
self.fut3 = Some(fut); self.fut3 = Some(fut);
return self.poll(); return self.poll();
@ -581,9 +581,9 @@ where
.respond_to(self.req.drop_state()) .respond_to(self.req.drop_state())
{ {
Ok(item) => match item.into().into() { Ok(item) => match item.into().into() {
ReplyResult::Err(err) => return Err(err), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)), AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
ReplyResult::Future(fut) => { AsyncResultItem::Future(fut) => {
self.fut4 = Some(fut); self.fut4 = Some(fut);
return self.poll(); return self.poll();
} }
@ -612,9 +612,9 @@ where
}; };
match item.into() { match item.into() {
ReplyResult::Err(err) => return Ok(Async::Ready(err.into())), AsyncResultItem::Err(err) => return Err(err),
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)), AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
ReplyResult::Future(fut) => self.fut4 = Some(fut), AsyncResultItem::Future(fut) => self.fut4 = Some(fut),
} }
self.poll() self.poll()