mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
use response instead of result for asyn c handlers
This commit is contained in:
parent
55698f2524
commit
53c5151692
@ -8,9 +8,9 @@ fn index(req: HttpRequest, name: web::Path<String>) -> String {
|
|||||||
format!("Hello: {}!\r\n", name)
|
format!("Hello: {}!\r\n", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn index_async(req: HttpRequest) -> Result<&'static str, Error> {
|
async fn index_async(req: HttpRequest) -> &'static str {
|
||||||
println!("REQ: {:?}", req);
|
println!("REQ: {:?}", req);
|
||||||
Ok("Hello world!\r\n")
|
"Hello world!\r\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
@ -26,7 +26,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
App::new()
|
App::new()
|
||||||
.wrap(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
.wrap(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
||||||
.wrap(middleware::Compress::default())
|
.wrap(middleware::Compress::default())
|
||||||
// .wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.service(index)
|
.service(index)
|
||||||
.service(no_params)
|
.service(no_params)
|
||||||
.service(
|
.service(
|
||||||
|
@ -119,21 +119,19 @@ impl<T: Responder> Future for HandlerServiceResponse<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Async handler converter factory
|
/// Async handler converter factory
|
||||||
pub trait AsyncFactory<T, R, O, E>: Clone + 'static
|
pub trait AsyncFactory<T, R, O>: Clone + 'static
|
||||||
where
|
where
|
||||||
R: Future<Output = Result<O, E>>,
|
R: Future<Output = O>,
|
||||||
O: Responder,
|
O: Responder,
|
||||||
E: Into<Error>,
|
|
||||||
{
|
{
|
||||||
fn call(&self, param: T) -> R;
|
fn call(&self, param: T) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, R, O, E> AsyncFactory<(), R, O, E> for F
|
impl<F, R, O> AsyncFactory<(), R, O> for F
|
||||||
where
|
where
|
||||||
F: Fn() -> R + Clone + 'static,
|
F: Fn() -> R + Clone + 'static,
|
||||||
R: Future<Output = Result<O, E>>,
|
R: Future<Output = O>,
|
||||||
O: Responder,
|
O: Responder,
|
||||||
E: Into<Error>,
|
|
||||||
{
|
{
|
||||||
fn call(&self, _: ()) -> R {
|
fn call(&self, _: ()) -> R {
|
||||||
(self)()
|
(self)()
|
||||||
@ -141,23 +139,21 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct AsyncHandler<F, T, R, O, E>
|
pub struct AsyncHandler<F, T, R, O>
|
||||||
where
|
where
|
||||||
F: AsyncFactory<T, R, O, E>,
|
F: AsyncFactory<T, R, O>,
|
||||||
R: Future<Output = Result<O, E>>,
|
R: Future<Output = O>,
|
||||||
O: Responder,
|
O: Responder,
|
||||||
E: Into<Error>,
|
|
||||||
{
|
{
|
||||||
hnd: F,
|
hnd: F,
|
||||||
_t: PhantomData<(T, R, O, E)>,
|
_t: PhantomData<(T, R, O)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, T, R, O, E> AsyncHandler<F, T, R, O, E>
|
impl<F, T, R, O> AsyncHandler<F, T, R, O>
|
||||||
where
|
where
|
||||||
F: AsyncFactory<T, R, O, E>,
|
F: AsyncFactory<T, R, O>,
|
||||||
R: Future<Output = Result<O, E>>,
|
R: Future<Output = O>,
|
||||||
O: Responder,
|
O: Responder,
|
||||||
E: Into<Error>,
|
|
||||||
{
|
{
|
||||||
pub fn new(hnd: F) -> Self {
|
pub fn new(hnd: F) -> Self {
|
||||||
AsyncHandler {
|
AsyncHandler {
|
||||||
@ -167,12 +163,11 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, T, R, O, E> Clone for AsyncHandler<F, T, R, O, E>
|
impl<F, T, R, O> Clone for AsyncHandler<F, T, R, O>
|
||||||
where
|
where
|
||||||
F: AsyncFactory<T, R, O, E>,
|
F: AsyncFactory<T, R, O>,
|
||||||
R: Future<Output = Result<O, E>>,
|
R: Future<Output = O>,
|
||||||
O: Responder,
|
O: Responder,
|
||||||
E: Into<Error>,
|
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
AsyncHandler {
|
AsyncHandler {
|
||||||
@ -182,17 +177,16 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, T, R, O, E> Service for AsyncHandler<F, T, R, O, E>
|
impl<F, T, R, O> Service for AsyncHandler<F, T, R, O>
|
||||||
where
|
where
|
||||||
F: AsyncFactory<T, R, O, E>,
|
F: AsyncFactory<T, R, O>,
|
||||||
R: Future<Output = Result<O, E>>,
|
R: Future<Output = O>,
|
||||||
O: Responder,
|
O: Responder,
|
||||||
E: Into<Error>,
|
|
||||||
{
|
{
|
||||||
type Request = (T, HttpRequest);
|
type Request = (T, HttpRequest);
|
||||||
type Response = ServiceResponse;
|
type Response = ServiceResponse;
|
||||||
type Error = Infallible;
|
type Error = Infallible;
|
||||||
type Future = AsyncHandlerServiceResponse<R, O, E>;
|
type Future = AsyncHandlerServiceResponse<R, O>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
@ -209,11 +203,10 @@ where
|
|||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[pin_project]
|
#[pin_project]
|
||||||
pub struct AsyncHandlerServiceResponse<T, R, E>
|
pub struct AsyncHandlerServiceResponse<T, R>
|
||||||
where
|
where
|
||||||
T: Future<Output = Result<R, E>>,
|
T: Future<Output = R>,
|
||||||
R: Responder,
|
R: Responder,
|
||||||
E: Into<Error>,
|
|
||||||
{
|
{
|
||||||
#[pin]
|
#[pin]
|
||||||
fut: T,
|
fut: T,
|
||||||
@ -222,11 +215,10 @@ where
|
|||||||
req: Option<HttpRequest>,
|
req: Option<HttpRequest>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, R, E> Future for AsyncHandlerServiceResponse<T, R, E>
|
impl<T, R> Future for AsyncHandlerServiceResponse<T, R>
|
||||||
where
|
where
|
||||||
T: Future<Output = Result<R, E>>,
|
T: Future<Output = R>,
|
||||||
R: Responder,
|
R: Responder,
|
||||||
E: Into<Error>,
|
|
||||||
{
|
{
|
||||||
type Output = Result<ServiceResponse, Infallible>;
|
type Output = Result<ServiceResponse, Infallible>;
|
||||||
|
|
||||||
@ -247,16 +239,12 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
match this.fut.poll(cx) {
|
match this.fut.poll(cx) {
|
||||||
Poll::Ready(Ok(res)) => {
|
Poll::Ready(res) => {
|
||||||
let fut = res.respond_to(this.req.as_ref().unwrap());
|
let fut = res.respond_to(this.req.as_ref().unwrap());
|
||||||
self.as_mut().project().fut2.set(Some(fut));
|
self.as_mut().project().fut2.set(Some(fut));
|
||||||
self.poll(cx)
|
self.poll(cx)
|
||||||
}
|
}
|
||||||
Poll::Pending => Poll::Pending,
|
Poll::Pending => Poll::Pending,
|
||||||
Poll::Ready(Err(e)) => {
|
|
||||||
let res: Response = e.into().into();
|
|
||||||
Poll::Ready(Ok(ServiceResponse::new(this.req.take().unwrap(), res)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -387,11 +375,10 @@ macro_rules! factory_tuple ({ $(($n:tt, $T:ident)),+} => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Func, $($T,)+ Res, O, E1> AsyncFactory<($($T,)+), Res, O, E1> for Func
|
impl<Func, $($T,)+ Res, O> AsyncFactory<($($T,)+), Res, O> for Func
|
||||||
where Func: Fn($($T,)+) -> Res + Clone + 'static,
|
where Func: Fn($($T,)+) -> Res + Clone + 'static,
|
||||||
Res: Future<Output = Result<O, E1>>,
|
Res: Future<Output = O>,
|
||||||
O: Responder,
|
O: Responder,
|
||||||
E1: Into<Error>,
|
|
||||||
{
|
{
|
||||||
fn call(&self, param: ($($T,)+)) -> Res {
|
fn call(&self, param: ($($T,)+)) -> Res {
|
||||||
(self)($(param.$n,)+)
|
(self)($(param.$n,)+)
|
||||||
|
@ -264,13 +264,12 @@ where
|
|||||||
/// App::new().service(web::resource("/").route(web::route().to_async(index)));
|
/// App::new().service(web::resource("/").route(web::route().to_async(index)));
|
||||||
/// ```
|
/// ```
|
||||||
#[allow(clippy::wrong_self_convention)]
|
#[allow(clippy::wrong_self_convention)]
|
||||||
pub fn to_async<F, I, R, O, E>(mut self, handler: F) -> Self
|
pub fn to_async<F, I, R, U>(mut self, handler: F) -> Self
|
||||||
where
|
where
|
||||||
F: AsyncFactory<I, R, O, E>,
|
F: AsyncFactory<I, R, U>,
|
||||||
I: FromRequest + 'static,
|
I: FromRequest + 'static,
|
||||||
R: Future<Output = Result<O, E>> + 'static,
|
R: Future<Output = U> + 'static,
|
||||||
O: Responder + 'static,
|
U: Responder + 'static,
|
||||||
E: Into<Error> + 'static,
|
|
||||||
{
|
{
|
||||||
self.routes.push(Route::new().to_async(handler));
|
self.routes.push(Route::new().to_async(handler));
|
||||||
self
|
self
|
||||||
|
15
src/route.rs
15
src/route.rs
@ -261,13 +261,12 @@ impl Route {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[allow(clippy::wrong_self_convention)]
|
#[allow(clippy::wrong_self_convention)]
|
||||||
pub fn to_async<F, T, R, O, E>(mut self, handler: F) -> Self
|
pub fn to_async<F, T, R, U>(mut self, handler: F) -> Self
|
||||||
where
|
where
|
||||||
F: AsyncFactory<T, R, O, E>,
|
F: AsyncFactory<T, R, U>,
|
||||||
T: FromRequest + 'static,
|
T: FromRequest + 'static,
|
||||||
R: Future<Output = Result<O, E>> + 'static,
|
R: Future<Output = U> + 'static,
|
||||||
O: Responder + 'static,
|
U: Responder + 'static,
|
||||||
E: Into<Error> + 'static,
|
|
||||||
{
|
{
|
||||||
self.service = Box::new(RouteNewService::new(Extract::new(AsyncHandler::new(
|
self.service = Box::new(RouteNewService::new(Extract::new(AsyncHandler::new(
|
||||||
handler,
|
handler,
|
||||||
@ -410,7 +409,7 @@ mod tests {
|
|||||||
.route(web::post().to_async(|| {
|
.route(web::post().to_async(|| {
|
||||||
async {
|
async {
|
||||||
delay_for(Duration::from_millis(100)).await;
|
delay_for(Duration::from_millis(100)).await;
|
||||||
Ok::<_, Error>(HttpResponse::Created())
|
HttpResponse::Created()
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
.route(web::delete().to_async(|| {
|
.route(web::delete().to_async(|| {
|
||||||
@ -423,9 +422,9 @@ mod tests {
|
|||||||
.service(web::resource("/json").route(web::get().to_async(|| {
|
.service(web::resource("/json").route(web::get().to_async(|| {
|
||||||
async {
|
async {
|
||||||
delay_for(Duration::from_millis(25)).await;
|
delay_for(Duration::from_millis(25)).await;
|
||||||
Ok::<_, Error>(web::Json(MyObject {
|
web::Json(MyObject {
|
||||||
name: "test".to_string(),
|
name: "test".to_string(),
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
}))),
|
}))),
|
||||||
)
|
)
|
||||||
|
@ -265,13 +265,12 @@ where
|
|||||||
/// web::to_async(index))
|
/// web::to_async(index))
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn to_async<F, I, R, O, E>(handler: F) -> Route
|
pub fn to_async<F, I, R, U>(handler: F) -> Route
|
||||||
where
|
where
|
||||||
F: AsyncFactory<I, R, O, E>,
|
F: AsyncFactory<I, R, U>,
|
||||||
I: FromRequest + 'static,
|
I: FromRequest + 'static,
|
||||||
R: Future<Output = Result<O, E>> + 'static,
|
R: Future<Output = U> + 'static,
|
||||||
O: Responder + 'static,
|
U: Responder + 'static,
|
||||||
E: Into<Error> + 'static,
|
|
||||||
{
|
{
|
||||||
Route::new().to_async(handler)
|
Route::new().to_async(handler)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user