mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 09:42:40 +01:00
rename Reply to a AsyncResult
This commit is contained in:
parent
58079b5bbe
commit
acd7380865
@ -2,7 +2,7 @@ use std::cell::UnsafeCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use handler::{FromRequest, Handler, Reply, Responder, RouteHandler, WrapHandler};
|
||||
use handler::{AsyncResult, FromRequest, Handler, Responder, RouteHandler, WrapHandler};
|
||||
use header::ContentEncoding;
|
||||
use http::Method;
|
||||
use httprequest::HttpRequest;
|
||||
@ -39,7 +39,7 @@ impl<S: 'static> PipelineHandler<S> for Inner<S> {
|
||||
|
||||
fn handle(
|
||||
&mut self, req: HttpRequest<S>, htype: HandlerType,
|
||||
) -> Reply<HttpResponse> {
|
||||
) -> AsyncResult<HttpResponse> {
|
||||
match htype {
|
||||
HandlerType::Normal(idx) => {
|
||||
self.resources[idx].handle(req, Some(&mut self.default))
|
||||
@ -90,7 +90,7 @@ impl<S: 'static> HttpApplication<S> {
|
||||
}
|
||||
|
||||
#[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);
|
||||
unsafe { &mut *self.inner.get() }.handle(req, tp)
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ use serde_urlencoded;
|
||||
|
||||
use de::PathDeserializer;
|
||||
use error::{Error, ErrorBadRequest};
|
||||
use handler::{FromRequest, Reply};
|
||||
use handler::{AsyncResult, FromRequest};
|
||||
use httpmessage::{HttpMessage, MessageBody, UrlEncoded};
|
||||
use httprequest::HttpRequest;
|
||||
|
||||
@ -471,7 +471,7 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
|
||||
{
|
||||
s: PhantomData<S>,
|
||||
items: ($(Option<$T>,)+),
|
||||
futs: ($(Option<Reply<$T>>,)+),
|
||||
futs: ($(Option<AsyncResult<$T>>,)+),
|
||||
}
|
||||
|
||||
impl<S, $($T: FromRequest<S>),+> Future for $fut_type<S, $($T),+>
|
||||
|
@ -18,7 +18,7 @@ use mime;
|
||||
use mime_guess::{get_mime_type, guess_mime_type};
|
||||
|
||||
use error::Error;
|
||||
use handler::{Handler, Reply, Responder, RouteHandler, WrapHandler};
|
||||
use handler::{AsyncResult, Handler, Responder, RouteHandler, WrapHandler};
|
||||
use header;
|
||||
use http::{Method, StatusCode};
|
||||
use httpmessage::HttpMessage;
|
||||
@ -578,7 +578,7 @@ impl<S: 'static> 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 {
|
||||
if !self.accessible {
|
||||
|
119
src/handler.rs
119
src/handler.rs
@ -23,12 +23,12 @@ pub trait Handler<S>: 'static {
|
||||
/// Types that implement this trait can be used as the return type of a handler.
|
||||
pub trait Responder {
|
||||
/// The associated item which can be returned.
|
||||
type Item: Into<Reply<HttpResponse>>;
|
||||
type Item: Into<AsyncResult<HttpResponse>>;
|
||||
|
||||
/// The associated error which can be returned.
|
||||
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>;
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ pub trait FromRequest<S>: Sized {
|
||||
type Config: Default;
|
||||
|
||||
/// Future that resolves to a Self
|
||||
type Result: Into<Reply<Self>>;
|
||||
type Result: Into<AsyncResult<Self>>;
|
||||
|
||||
/// Convert request to a Self
|
||||
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result;
|
||||
@ -93,10 +93,10 @@ where
|
||||
A: Responder,
|
||||
B: Responder,
|
||||
{
|
||||
type Item = Reply<HttpResponse>;
|
||||
type Item = AsyncResult<HttpResponse>;
|
||||
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 {
|
||||
Either::A(a) => match a.respond_to(req) {
|
||||
Ok(val) => Ok(val.into()),
|
||||
@ -182,26 +182,26 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents reply process.
|
||||
/// Represents async result
|
||||
///
|
||||
/// Reply could be in tree different forms.
|
||||
/// * Message(T) - ready item
|
||||
/// * Error(Error) - error happen during reply process
|
||||
/// * Future<T, Error> - reply process completes in the future
|
||||
pub struct Reply<I, E = Error>(Option<ReplyResult<I, E>>);
|
||||
/// Result could be in tree different forms.
|
||||
/// * Ok(T) - ready item
|
||||
/// * Err(E) - error happen during reply process
|
||||
/// * Future<T, E> - reply process completes in the future
|
||||
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 Error = E;
|
||||
|
||||
fn poll(&mut self) -> Poll<I, E> {
|
||||
let res = self.0.take().expect("use after resolve");
|
||||
match res {
|
||||
ReplyResult::Ok(msg) => Ok(Async::Ready(msg)),
|
||||
ReplyResult::Err(err) => Err(err),
|
||||
ReplyResult::Future(mut fut) => match fut.poll() {
|
||||
AsyncResultItem::Ok(msg) => Ok(Async::Ready(msg)),
|
||||
AsyncResultItem::Err(err) => Err(err),
|
||||
AsyncResultItem::Future(mut fut) => match fut.poll() {
|
||||
Ok(Async::NotReady) => {
|
||||
self.0 = Some(ReplyResult::Future(fut));
|
||||
self.0 = Some(AsyncResultItem::Future(fut));
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
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),
|
||||
Err(E),
|
||||
Future(Box<Future<Item = I, Error = E>>),
|
||||
}
|
||||
|
||||
impl<I, E> Reply<I, E> {
|
||||
impl<I, E> AsyncResult<I, E> {
|
||||
/// Create async response
|
||||
#[inline]
|
||||
pub fn async<F>(fut: F) -> Reply<I, E>
|
||||
where
|
||||
F: Future<Item = I, Error = E> + 'static,
|
||||
{
|
||||
Reply(Some(ReplyResult::Future(Box::new(fut))))
|
||||
pub fn async(fut: Box<Future<Item = I, Error = E>>) -> AsyncResult<I, E> {
|
||||
AsyncResult(Some(AsyncResultItem::Future(fut)))
|
||||
}
|
||||
|
||||
/// Send response
|
||||
#[inline]
|
||||
pub fn response<R: Into<I>>(response: R) -> Reply<I, E> {
|
||||
Reply(Some(ReplyResult::Ok(response.into())))
|
||||
pub fn ok<R: Into<I>>(ok: R) -> AsyncResult<I, E> {
|
||||
AsyncResult(Some(AsyncResultItem::Ok(ok.into())))
|
||||
}
|
||||
|
||||
/// Send error
|
||||
#[inline]
|
||||
pub fn error<R: Into<E>>(err: R) -> Reply<I, E> {
|
||||
Reply(Some(ReplyResult::Err(err.into())))
|
||||
pub fn error<R: Into<E>>(err: R) -> AsyncResult<I, E> {
|
||||
AsyncResult(Some(AsyncResultItem::Err(err.into())))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn into(self) -> ReplyResult<I, E> {
|
||||
pub(crate) fn into(self) -> AsyncResultItem<I, E> {
|
||||
self.0.expect("use after resolve")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn as_msg(&self) -> &I {
|
||||
match self.0.as_ref().unwrap() {
|
||||
&ReplyResult::Ok(ref resp) => resp,
|
||||
&AsyncResultItem::Ok(ref resp) => resp,
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
@ -255,35 +252,35 @@ impl<I, E> Reply<I, E> {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn as_err(&self) -> Option<&E> {
|
||||
match self.0.as_ref().unwrap() {
|
||||
&ReplyResult::Err(ref err) => Some(err),
|
||||
&AsyncResultItem::Err(ref err) => Some(err),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Responder for Reply<HttpResponse> {
|
||||
type Item = Reply<HttpResponse>;
|
||||
impl Responder for AsyncResult<HttpResponse> {
|
||||
type Item = AsyncResult<HttpResponse>;
|
||||
type Error = Error;
|
||||
|
||||
fn respond_to(self, _: HttpRequest) -> Result<Reply<HttpResponse>, Error> {
|
||||
fn respond_to(self, _: HttpRequest) -> Result<AsyncResult<HttpResponse>, Error> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Responder for HttpResponse {
|
||||
type Item = Reply<HttpResponse>;
|
||||
type Item = AsyncResult<HttpResponse>;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn respond_to(self, _: HttpRequest) -> Result<Reply<HttpResponse>, Error> {
|
||||
Ok(Reply(Some(ReplyResult::Ok(self))))
|
||||
fn respond_to(self, _: HttpRequest) -> Result<AsyncResult<HttpResponse>, Error> {
|
||||
Ok(AsyncResult(Some(AsyncResultItem::Ok(self))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for Reply<T> {
|
||||
impl<T> From<T> for AsyncResult<T> {
|
||||
#[inline]
|
||||
fn from(resp: T) -> Reply<T> {
|
||||
Reply(Some(ReplyResult::Ok(resp)))
|
||||
fn from(resp: T) -> AsyncResult<T> {
|
||||
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]
|
||||
fn from(res: Result<Reply<T>, E>) -> Self {
|
||||
fn from(res: Result<AsyncResult<T>, E>) -> Self {
|
||||
match res {
|
||||
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]
|
||||
fn from(res: Result<T, E>) -> Self {
|
||||
match res {
|
||||
Ok(val) => Reply(Some(ReplyResult::Ok(val))),
|
||||
Err(err) => Reply(Some(ReplyResult::Err(err.into()))),
|
||||
Ok(val) => AsyncResult(Some(AsyncResultItem::Ok(val))),
|
||||
Err(err) => AsyncResult(Some(AsyncResultItem::Err(err.into()))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E: Into<Error>> From<Result<Box<Future<Item = T, Error = Error>>, E>>
|
||||
for Reply<T>
|
||||
for AsyncResult<T>
|
||||
{
|
||||
#[inline]
|
||||
fn from(res: Result<Box<Future<Item = T, Error = Error>>, E>) -> Self {
|
||||
match res {
|
||||
Ok(fut) => Reply(Some(ReplyResult::Future(fut))),
|
||||
Err(err) => Reply(Some(ReplyResult::Err(err.into()))),
|
||||
Ok(fut) => AsyncResult(Some(AsyncResultItem::Future(fut))),
|
||||
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]
|
||||
fn from(fut: Box<Future<Item = T, Error = Error>>) -> Reply<T> {
|
||||
Reply(Some(ReplyResult::Future(fut)))
|
||||
fn from(fut: Box<Future<Item = T, Error = Error>>) -> AsyncResult<T> {
|
||||
AsyncResult(Some(AsyncResultItem::Future(fut)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -349,26 +346,26 @@ where
|
||||
I: Responder + 'static,
|
||||
E: Into<Error> + 'static,
|
||||
{
|
||||
type Item = Reply<HttpResponse>;
|
||||
type Item = AsyncResult<HttpResponse>;
|
||||
type Error = Error;
|
||||
|
||||
#[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())
|
||||
.then(move |r| match r.respond_to(req) {
|
||||
Ok(reply) => match reply.into().into() {
|
||||
ReplyResult::Ok(resp) => ok(resp),
|
||||
AsyncResultItem::Ok(resp) => ok(resp),
|
||||
_ => panic!("Nested async replies are not supported"),
|
||||
},
|
||||
Err(e) => err(e),
|
||||
});
|
||||
Ok(Reply::async(fut))
|
||||
Ok(AsyncResult::async(Box::new(fut)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait defines object that could be registered as resource route
|
||||
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
|
||||
@ -402,11 +399,11 @@ where
|
||||
R: Responder + '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();
|
||||
match self.h.handle(req).respond_to(req2) {
|
||||
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,
|
||||
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 fut = (self.h)(req).map_err(|e| e.into()).then(move |r| {
|
||||
match r.respond_to(req2) {
|
||||
Ok(reply) => match reply.into().into() {
|
||||
ReplyResult::Ok(resp) => ok(resp),
|
||||
AsyncResultItem::Ok(resp) => ok(resp),
|
||||
_ => panic!("Nested async replies are not supported"),
|
||||
},
|
||||
Err(e) => err(e),
|
||||
}
|
||||
});
|
||||
Reply::async(fut)
|
||||
AsyncResult::async(Box::new(fut))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -200,7 +200,7 @@ pub mod dev {
|
||||
pub use body::BodyStream;
|
||||
pub use context::Drain;
|
||||
pub use extractor::{FormConfig, PayloadConfig};
|
||||
pub use handler::{Handler, Reply};
|
||||
pub use handler::{AsyncResult, Handler};
|
||||
pub use httpmessage::{MessageBody, UrlEncoded};
|
||||
pub use httpresponse::HttpResponseBuilder;
|
||||
pub use info::ConnectionInfo;
|
||||
|
@ -11,7 +11,7 @@ use application::Inner;
|
||||
use body::{Body, BodyStream};
|
||||
use context::{ActorHttpContext, Frame};
|
||||
use error::Error;
|
||||
use handler::{Reply, ReplyResult};
|
||||
use handler::{AsyncResult, AsyncResultItem};
|
||||
use header::ContentEncoding;
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
@ -28,8 +28,9 @@ pub(crate) enum HandlerType {
|
||||
pub(crate) trait PipelineHandler<S> {
|
||||
fn encoding(&self) -> ContentEncoding;
|
||||
|
||||
fn handle(&mut self, req: HttpRequest<S>, htype: HandlerType)
|
||||
-> Reply<HttpResponse>;
|
||||
fn handle(
|
||||
&mut self, req: HttpRequest<S>, htype: HandlerType,
|
||||
) -> AsyncResult<HttpResponse>;
|
||||
}
|
||||
|
||||
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> {
|
||||
#[inline]
|
||||
fn init(
|
||||
info: &mut PipelineInfo<S>, reply: Reply<HttpResponse>,
|
||||
info: &mut PipelineInfo<S>, reply: AsyncResult<HttpResponse>,
|
||||
) -> PipelineState<S, H> {
|
||||
match reply.into() {
|
||||
ReplyResult::Err(err) => RunMiddlewares::init(info, err.into()),
|
||||
ReplyResult::Ok(resp) => RunMiddlewares::init(info, resp),
|
||||
ReplyResult::Future(fut) => PipelineState::Handler(WaitingResponse {
|
||||
AsyncResultItem::Err(err) => RunMiddlewares::init(info, err.into()),
|
||||
AsyncResultItem::Ok(resp) => RunMiddlewares::init(info, resp),
|
||||
AsyncResultItem::Future(fut) => PipelineState::Handler(WaitingResponse {
|
||||
fut,
|
||||
_s: PhantomData,
|
||||
_h: PhantomData,
|
||||
|
@ -4,7 +4,7 @@ use std::rc::Rc;
|
||||
use http::{Method, StatusCode};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
use handler::{FromRequest, Handler, Reply, Responder};
|
||||
use handler::{AsyncResult, FromRequest, Handler, Responder};
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
use middleware::Middleware;
|
||||
@ -198,7 +198,7 @@ impl<S: 'static> ResourceHandler<S> {
|
||||
|
||||
pub(crate) fn handle(
|
||||
&mut self, mut req: HttpRequest<S>, default: Option<&mut ResourceHandler<S>>,
|
||||
) -> Reply<HttpResponse> {
|
||||
) -> AsyncResult<HttpResponse> {
|
||||
for route in &mut self.routes {
|
||||
if route.check(&mut req) {
|
||||
return if self.middlewares.is_empty() {
|
||||
@ -211,7 +211,7 @@ impl<S: 'static> ResourceHandler<S> {
|
||||
if let Some(resource) = default {
|
||||
resource.handle(req, None)
|
||||
} else {
|
||||
Reply::response(HttpResponse::new(StatusCode::NOT_FOUND))
|
||||
AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
22
src/route.rs
22
src/route.rs
@ -5,8 +5,8 @@ use std::rc::Rc;
|
||||
use futures::{Async, Future, Poll};
|
||||
|
||||
use error::Error;
|
||||
use handler::{AsyncHandler, FromRequest, Handler, Reply, ReplyResult, Responder,
|
||||
RouteHandler, WrapHandler};
|
||||
use handler::{AsyncHandler, AsyncResult, AsyncResultItem, FromRequest, Handler,
|
||||
Responder, RouteHandler, WrapHandler};
|
||||
use http::StatusCode;
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
@ -45,15 +45,15 @@ impl<S: 'static> Route<S> {
|
||||
}
|
||||
|
||||
#[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)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn compose(
|
||||
&mut self, req: HttpRequest<S>, mws: Rc<Vec<Box<Middleware<S>>>>,
|
||||
) -> Reply<HttpResponse> {
|
||||
Reply::async(Compose::new(req, mws, self.handler.clone()))
|
||||
) -> AsyncResult<HttpResponse> {
|
||||
AsyncResult::async(Box::new(Compose::new(req, mws, self.handler.clone())))
|
||||
}
|
||||
|
||||
/// Add match predicate to route.
|
||||
@ -243,7 +243,7 @@ impl<S: 'static> InnerHandler<S> {
|
||||
}
|
||||
|
||||
#[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
|
||||
let h = unsafe { &mut *self.0.as_ref().get() };
|
||||
h.handle(req)
|
||||
@ -415,11 +415,13 @@ struct WaitingResponse<S> {
|
||||
|
||||
impl<S: 'static> WaitingResponse<S> {
|
||||
#[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() {
|
||||
ReplyResult::Err(err) => RunMiddlewares::init(info, err.into()),
|
||||
ReplyResult::Ok(resp) => RunMiddlewares::init(info, resp),
|
||||
ReplyResult::Future(fut) => ComposeState::Handler(WaitingResponse {
|
||||
AsyncResultItem::Err(err) => RunMiddlewares::init(info, err.into()),
|
||||
AsyncResultItem::Ok(resp) => RunMiddlewares::init(info, resp),
|
||||
AsyncResultItem::Future(fut) => ComposeState::Handler(WaitingResponse {
|
||||
fut,
|
||||
_s: PhantomData,
|
||||
}),
|
||||
|
24
src/scope.rs
24
src/scope.rs
@ -5,7 +5,7 @@ use std::rc::Rc;
|
||||
use futures::{Async, Future, Poll};
|
||||
|
||||
use error::Error;
|
||||
use handler::{FromRequest, Reply, ReplyResult, Responder, RouteHandler};
|
||||
use handler::{AsyncResult, AsyncResultItem, FromRequest, Responder, RouteHandler};
|
||||
use http::Method;
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
@ -274,7 +274,7 @@ impl<S: 'static> 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 = if path == "" { "/" } else { path };
|
||||
|
||||
@ -287,12 +287,12 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
|
||||
let resource = unsafe { &mut *resource.get() };
|
||||
return resource.handle(req, Some(default));
|
||||
} else {
|
||||
return Reply::async(Compose::new(
|
||||
return AsyncResult::async(Box::new(Compose::new(
|
||||
req,
|
||||
Rc::clone(&self.middlewares),
|
||||
Rc::clone(&resource),
|
||||
Some(Rc::clone(&self.default)),
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -330,12 +330,12 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
|
||||
if self.middlewares.is_empty() {
|
||||
default.handle(req, None)
|
||||
} else {
|
||||
Reply::async(Compose::new(
|
||||
AsyncResult::async(Box::new(Compose::new(
|
||||
req,
|
||||
Rc::clone(&self.middlewares),
|
||||
Rc::clone(&self.default),
|
||||
None,
|
||||
))
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -346,7 +346,7 @@ struct Wrapper<S: 'static> {
|
||||
}
|
||||
|
||||
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
|
||||
.handle(req.change_state(Rc::clone(&self.state)))
|
||||
}
|
||||
@ -521,11 +521,13 @@ struct WaitingResponse<S> {
|
||||
|
||||
impl<S: 'static> WaitingResponse<S> {
|
||||
#[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() {
|
||||
ReplyResult::Ok(resp) => RunMiddlewares::init(info, resp),
|
||||
ReplyResult::Err(err) => RunMiddlewares::init(info, err.into()),
|
||||
ReplyResult::Future(fut) => ComposeState::Handler(WaitingResponse {
|
||||
AsyncResultItem::Ok(resp) => RunMiddlewares::init(info, resp),
|
||||
AsyncResultItem::Err(err) => RunMiddlewares::init(info, err.into()),
|
||||
AsyncResultItem::Future(fut) => ComposeState::Handler(WaitingResponse {
|
||||
fut,
|
||||
_s: PhantomData,
|
||||
}),
|
||||
|
16
src/test.rs
16
src/test.rs
@ -21,7 +21,7 @@ use application::{App, HttpApplication};
|
||||
use body::Binary;
|
||||
use client::{ClientConnector, ClientRequest, ClientRequestBuilder};
|
||||
use error::Error;
|
||||
use handler::{Handler, ReplyResult, Responder};
|
||||
use handler::{AsyncResultItem, Handler, Responder};
|
||||
use header::{Header, IntoHeaderValue};
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
@ -593,19 +593,17 @@ impl<S> TestRequest<S> {
|
||||
/// with generated request.
|
||||
///
|
||||
/// This method panics is handler returns actor or async result.
|
||||
pub fn run<H: Handler<S>>(
|
||||
self, mut h: H,
|
||||
) -> Result<HttpResponse, <<H as Handler<S>>::Result as Responder>::Error> {
|
||||
pub fn run<H: Handler<S>>(self, mut h: H) -> Result<HttpResponse, Error> {
|
||||
let req = self.finish();
|
||||
let resp = h.handle(req.clone());
|
||||
|
||||
match resp.respond_to(req.drop_state()) {
|
||||
Ok(resp) => match resp.into().into() {
|
||||
ReplyResult::Ok(resp) => Ok(resp),
|
||||
ReplyResult::Err(err) => Ok(err.into()),
|
||||
ReplyResult::Future(_) => panic!("Async handler is not supported."),
|
||||
AsyncResultItem::Ok(resp) => Ok(resp),
|
||||
AsyncResultItem::Err(err) => Err(err),
|
||||
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) {
|
||||
Ok(r) => match r.respond_to(req.drop_state()) {
|
||||
Ok(reply) => match reply.into().into() {
|
||||
ReplyResult::Ok(resp) => Ok(resp),
|
||||
AsyncResultItem::Ok(resp) => Ok(resp),
|
||||
_ => panic!("Nested async replies are not supported"),
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
|
134
src/with.rs
134
src/with.rs
@ -5,7 +5,7 @@ use std::ops::{Deref, DerefMut};
|
||||
use std::rc::Rc;
|
||||
|
||||
use error::Error;
|
||||
use handler::{FromRequest, Handler, Reply, ReplyResult, Responder};
|
||||
use handler::{AsyncResult, AsyncResultItem, FromRequest, Handler, Responder};
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
|
||||
@ -82,7 +82,7 @@ where
|
||||
T: FromRequest<S> + 'static,
|
||||
S: 'static,
|
||||
{
|
||||
type Result = Reply<HttpResponse>;
|
||||
type Result = AsyncResult<HttpResponse>;
|
||||
|
||||
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
|
||||
let mut fut = WithHandlerFut {
|
||||
@ -95,9 +95,9 @@ where
|
||||
};
|
||||
|
||||
match fut.poll() {
|
||||
Ok(Async::Ready(resp)) => Reply::response(resp),
|
||||
Ok(Async::NotReady) => Reply::async(fut),
|
||||
Err(e) => Reply::error::<Error>(e),
|
||||
Ok(Async::Ready(resp)) => AsyncResult::ok(resp),
|
||||
Ok(Async::NotReady) => AsyncResult::async(Box::new(fut)),
|
||||
Err(e) => AsyncResult::error::<Error>(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -136,9 +136,9 @@ where
|
||||
self.started = true;
|
||||
let reply = T::from_request(&self.req, self.cfg.as_ref()).into();
|
||||
match reply.into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(msg) => msg,
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(msg) => msg,
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.fut1 = Some(fut);
|
||||
return self.poll();
|
||||
}
|
||||
@ -157,9 +157,9 @@ where
|
||||
};
|
||||
|
||||
match item.into() {
|
||||
ReplyResult::Err(err) => Err(err),
|
||||
ReplyResult::Ok(resp) => Ok(Async::Ready(resp)),
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => Err(err),
|
||||
AsyncResultItem::Ok(resp) => Ok(Async::Ready(resp)),
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.fut2 = Some(fut);
|
||||
self.poll()
|
||||
}
|
||||
@ -207,7 +207,7 @@ where
|
||||
T2: FromRequest<S> + 'static,
|
||||
S: 'static,
|
||||
{
|
||||
type Result = Reply<HttpResponse>;
|
||||
type Result = AsyncResult<HttpResponse>;
|
||||
|
||||
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
|
||||
let mut fut = WithHandlerFut2 {
|
||||
@ -222,9 +222,9 @@ where
|
||||
fut3: None,
|
||||
};
|
||||
match fut.poll() {
|
||||
Ok(Async::Ready(resp)) => Reply::response(resp),
|
||||
Ok(Async::NotReady) => Reply::async(fut),
|
||||
Err(e) => Reply::response(e),
|
||||
Ok(Async::Ready(resp)) => AsyncResult::ok(resp),
|
||||
Ok(Async::NotReady) => AsyncResult::async(Box::new(fut)),
|
||||
Err(e) => AsyncResult::ok(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -268,9 +268,9 @@ where
|
||||
self.started = true;
|
||||
let reply = T1::from_request(&self.req, self.cfg1.as_ref()).into();
|
||||
let item1 = match reply.into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(msg) => msg,
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(msg) => msg,
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.fut1 = Some(fut);
|
||||
return self.poll();
|
||||
}
|
||||
@ -278,9 +278,9 @@ where
|
||||
|
||||
let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
|
||||
let item2 = match reply.into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(msg) => msg,
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(msg) => msg,
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.item = Some(item1);
|
||||
self.fut2 = Some(fut);
|
||||
return self.poll();
|
||||
@ -290,9 +290,9 @@ where
|
||||
let hnd: &mut F = unsafe { &mut *self.hnd.get() };
|
||||
match (*hnd)(item1, item2).respond_to(self.req.drop_state()) {
|
||||
Ok(item) => match item.into().into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.fut3 = Some(fut);
|
||||
return self.poll();
|
||||
}
|
||||
@ -306,9 +306,9 @@ where
|
||||
Async::Ready(item) => {
|
||||
let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
|
||||
let item2 = match reply.into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(msg) => msg,
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(msg) => msg,
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.item = Some(item);
|
||||
self.fut2 = Some(fut);
|
||||
return self.poll();
|
||||
@ -318,9 +318,9 @@ where
|
||||
let hnd: &mut F = unsafe { &mut *self.hnd.get() };
|
||||
match (*hnd)(item, item2).respond_to(self.req.drop_state()) {
|
||||
Ok(item) => match item.into().into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.fut3 = Some(fut);
|
||||
return self.poll();
|
||||
}
|
||||
@ -346,9 +346,9 @@ where
|
||||
};
|
||||
|
||||
match item.into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
ReplyResult::Future(fut) => self.fut3 = Some(fut),
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
AsyncResultItem::Future(fut) => self.fut3 = Some(fut),
|
||||
}
|
||||
|
||||
self.poll()
|
||||
@ -404,7 +404,7 @@ where
|
||||
T3: 'static,
|
||||
S: 'static,
|
||||
{
|
||||
type Result = Reply<HttpResponse>;
|
||||
type Result = AsyncResult<HttpResponse>;
|
||||
|
||||
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
|
||||
let mut fut = WithHandlerFut3 {
|
||||
@ -422,9 +422,9 @@ where
|
||||
fut4: None,
|
||||
};
|
||||
match fut.poll() {
|
||||
Ok(Async::Ready(resp)) => Reply::response(resp),
|
||||
Ok(Async::NotReady) => Reply::async(fut),
|
||||
Err(e) => Reply::response(e),
|
||||
Ok(Async::Ready(resp)) => AsyncResult::ok(resp),
|
||||
Ok(Async::NotReady) => AsyncResult::async(Box::new(fut)),
|
||||
Err(e) => AsyncResult::error(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -473,9 +473,9 @@ where
|
||||
self.started = true;
|
||||
let reply = T1::from_request(&self.req, self.cfg1.as_ref()).into();
|
||||
let item1 = match reply.into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(msg) => msg,
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(msg) => msg,
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.fut1 = Some(fut);
|
||||
return self.poll();
|
||||
}
|
||||
@ -483,9 +483,9 @@ where
|
||||
|
||||
let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
|
||||
let item2 = match reply.into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(msg) => msg,
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(msg) => msg,
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.item1 = Some(item1);
|
||||
self.fut2 = Some(fut);
|
||||
return self.poll();
|
||||
@ -494,9 +494,9 @@ where
|
||||
|
||||
let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into();
|
||||
let item3 = match reply.into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(msg) => msg,
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(msg) => msg,
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.item1 = Some(item1);
|
||||
self.item2 = Some(item2);
|
||||
self.fut3 = Some(fut);
|
||||
@ -507,9 +507,9 @@ where
|
||||
let hnd: &mut F = unsafe { &mut *self.hnd.get() };
|
||||
match (*hnd)(item1, item2, item3).respond_to(self.req.drop_state()) {
|
||||
Ok(item) => match item.into().into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.fut4 = Some(fut);
|
||||
return self.poll();
|
||||
}
|
||||
@ -525,9 +525,9 @@ where
|
||||
self.fut1.take();
|
||||
let reply = T2::from_request(&self.req, self.cfg2.as_ref()).into();
|
||||
let item2 = match reply.into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(msg) => msg,
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(msg) => msg,
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.fut2 = Some(fut);
|
||||
return self.poll();
|
||||
}
|
||||
@ -535,9 +535,9 @@ where
|
||||
|
||||
let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into();
|
||||
let item3 = match reply.into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(msg) => msg,
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(msg) => msg,
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.item2 = Some(item2);
|
||||
self.fut3 = Some(fut);
|
||||
return self.poll();
|
||||
@ -548,9 +548,9 @@ where
|
||||
.respond_to(self.req.drop_state())
|
||||
{
|
||||
Ok(item) => match item.into().into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.fut4 = Some(fut);
|
||||
return self.poll();
|
||||
}
|
||||
@ -568,9 +568,9 @@ where
|
||||
self.fut2.take();
|
||||
let reply = T3::from_request(&self.req, self.cfg3.as_ref()).into();
|
||||
let item3 = match reply.into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(msg) => msg,
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(msg) => msg,
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.item2 = Some(item);
|
||||
self.fut3 = Some(fut);
|
||||
return self.poll();
|
||||
@ -581,9 +581,9 @@ where
|
||||
.respond_to(self.req.drop_state())
|
||||
{
|
||||
Ok(item) => match item.into().into() {
|
||||
ReplyResult::Err(err) => return Err(err),
|
||||
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
ReplyResult::Future(fut) => {
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
AsyncResultItem::Future(fut) => {
|
||||
self.fut4 = Some(fut);
|
||||
return self.poll();
|
||||
}
|
||||
@ -612,9 +612,9 @@ where
|
||||
};
|
||||
|
||||
match item.into() {
|
||||
ReplyResult::Err(err) => return Ok(Async::Ready(err.into())),
|
||||
ReplyResult::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
ReplyResult::Future(fut) => self.fut4 = Some(fut),
|
||||
AsyncResultItem::Err(err) => return Err(err),
|
||||
AsyncResultItem::Ok(resp) => return Ok(Async::Ready(resp)),
|
||||
AsyncResultItem::Future(fut) => self.fut4 = Some(fut),
|
||||
}
|
||||
|
||||
self.poll()
|
||||
|
Loading…
Reference in New Issue
Block a user