mirror of
https://github.com/fafhrd91/actix-web
synced 2024-12-18 01:43:58 +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)
|
||||
}
|
||||
|
||||
async fn index_async(req: HttpRequest) -> Result<&'static str, Error> {
|
||||
async fn index_async(req: HttpRequest) -> &'static str {
|
||||
println!("REQ: {:?}", req);
|
||||
Ok("Hello world!\r\n")
|
||||
"Hello world!\r\n"
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
@ -26,7 +26,7 @@ fn main() -> std::io::Result<()> {
|
||||
App::new()
|
||||
.wrap(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
||||
.wrap(middleware::Compress::default())
|
||||
// .wrap(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(index)
|
||||
.service(no_params)
|
||||
.service(
|
||||
|
@ -119,21 +119,19 @@ impl<T: Responder> Future for HandlerServiceResponse<T> {
|
||||
}
|
||||
|
||||
/// Async handler converter factory
|
||||
pub trait AsyncFactory<T, R, O, E>: Clone + 'static
|
||||
pub trait AsyncFactory<T, R, O>: Clone + 'static
|
||||
where
|
||||
R: Future<Output = Result<O, E>>,
|
||||
R: Future<Output = O>,
|
||||
O: Responder,
|
||||
E: Into<Error>,
|
||||
{
|
||||
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
|
||||
F: Fn() -> R + Clone + 'static,
|
||||
R: Future<Output = Result<O, E>>,
|
||||
R: Future<Output = O>,
|
||||
O: Responder,
|
||||
E: Into<Error>,
|
||||
{
|
||||
fn call(&self, _: ()) -> R {
|
||||
(self)()
|
||||
@ -141,23 +139,21 @@ where
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct AsyncHandler<F, T, R, O, E>
|
||||
pub struct AsyncHandler<F, T, R, O>
|
||||
where
|
||||
F: AsyncFactory<T, R, O, E>,
|
||||
R: Future<Output = Result<O, E>>,
|
||||
F: AsyncFactory<T, R, O>,
|
||||
R: Future<Output = O>,
|
||||
O: Responder,
|
||||
E: Into<Error>,
|
||||
{
|
||||
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
|
||||
F: AsyncFactory<T, R, O, E>,
|
||||
R: Future<Output = Result<O, E>>,
|
||||
F: AsyncFactory<T, R, O>,
|
||||
R: Future<Output = O>,
|
||||
O: Responder,
|
||||
E: Into<Error>,
|
||||
{
|
||||
pub fn new(hnd: F) -> Self {
|
||||
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
|
||||
F: AsyncFactory<T, R, O, E>,
|
||||
R: Future<Output = Result<O, E>>,
|
||||
F: AsyncFactory<T, R, O>,
|
||||
R: Future<Output = O>,
|
||||
O: Responder,
|
||||
E: Into<Error>,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
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
|
||||
F: AsyncFactory<T, R, O, E>,
|
||||
R: Future<Output = Result<O, E>>,
|
||||
F: AsyncFactory<T, R, O>,
|
||||
R: Future<Output = O>,
|
||||
O: Responder,
|
||||
E: Into<Error>,
|
||||
{
|
||||
type Request = (T, HttpRequest);
|
||||
type Response = ServiceResponse;
|
||||
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>> {
|
||||
Poll::Ready(Ok(()))
|
||||
@ -209,11 +203,10 @@ where
|
||||
|
||||
#[doc(hidden)]
|
||||
#[pin_project]
|
||||
pub struct AsyncHandlerServiceResponse<T, R, E>
|
||||
pub struct AsyncHandlerServiceResponse<T, R>
|
||||
where
|
||||
T: Future<Output = Result<R, E>>,
|
||||
T: Future<Output = R>,
|
||||
R: Responder,
|
||||
E: Into<Error>,
|
||||
{
|
||||
#[pin]
|
||||
fut: T,
|
||||
@ -222,11 +215,10 @@ where
|
||||
req: Option<HttpRequest>,
|
||||
}
|
||||
|
||||
impl<T, R, E> Future for AsyncHandlerServiceResponse<T, R, E>
|
||||
impl<T, R> Future for AsyncHandlerServiceResponse<T, R>
|
||||
where
|
||||
T: Future<Output = Result<R, E>>,
|
||||
T: Future<Output = R>,
|
||||
R: Responder,
|
||||
E: Into<Error>,
|
||||
{
|
||||
type Output = Result<ServiceResponse, Infallible>;
|
||||
|
||||
@ -247,16 +239,12 @@ where
|
||||
}
|
||||
|
||||
match this.fut.poll(cx) {
|
||||
Poll::Ready(Ok(res)) => {
|
||||
Poll::Ready(res) => {
|
||||
let fut = res.respond_to(this.req.as_ref().unwrap());
|
||||
self.as_mut().project().fut2.set(Some(fut));
|
||||
self.poll(cx)
|
||||
}
|
||||
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,
|
||||
Res: Future<Output = Result<O, E1>>,
|
||||
Res: Future<Output = O>,
|
||||
O: Responder,
|
||||
E1: Into<Error>,
|
||||
{
|
||||
fn call(&self, param: ($($T,)+)) -> Res {
|
||||
(self)($(param.$n,)+)
|
||||
|
@ -264,13 +264,12 @@ where
|
||||
/// App::new().service(web::resource("/").route(web::route().to_async(index)));
|
||||
/// ```
|
||||
#[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
|
||||
F: AsyncFactory<I, R, O, E>,
|
||||
F: AsyncFactory<I, R, U>,
|
||||
I: FromRequest + 'static,
|
||||
R: Future<Output = Result<O, E>> + 'static,
|
||||
O: Responder + 'static,
|
||||
E: Into<Error> + 'static,
|
||||
R: Future<Output = U> + 'static,
|
||||
U: Responder + 'static,
|
||||
{
|
||||
self.routes.push(Route::new().to_async(handler));
|
||||
self
|
||||
|
15
src/route.rs
15
src/route.rs
@ -261,13 +261,12 @@ impl Route {
|
||||
/// }
|
||||
/// ```
|
||||
#[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
|
||||
F: AsyncFactory<T, R, O, E>,
|
||||
F: AsyncFactory<T, R, U>,
|
||||
T: FromRequest + 'static,
|
||||
R: Future<Output = Result<O, E>> + 'static,
|
||||
O: Responder + 'static,
|
||||
E: Into<Error> + 'static,
|
||||
R: Future<Output = U> + 'static,
|
||||
U: Responder + 'static,
|
||||
{
|
||||
self.service = Box::new(RouteNewService::new(Extract::new(AsyncHandler::new(
|
||||
handler,
|
||||
@ -410,7 +409,7 @@ mod tests {
|
||||
.route(web::post().to_async(|| {
|
||||
async {
|
||||
delay_for(Duration::from_millis(100)).await;
|
||||
Ok::<_, Error>(HttpResponse::Created())
|
||||
HttpResponse::Created()
|
||||
}
|
||||
}))
|
||||
.route(web::delete().to_async(|| {
|
||||
@ -423,9 +422,9 @@ mod tests {
|
||||
.service(web::resource("/json").route(web::get().to_async(|| {
|
||||
async {
|
||||
delay_for(Duration::from_millis(25)).await;
|
||||
Ok::<_, Error>(web::Json(MyObject {
|
||||
web::Json(MyObject {
|
||||
name: "test".to_string(),
|
||||
}))
|
||||
})
|
||||
}
|
||||
}))),
|
||||
)
|
||||
|
@ -265,13 +265,12 @@ where
|
||||
/// 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
|
||||
F: AsyncFactory<I, R, O, E>,
|
||||
F: AsyncFactory<I, R, U>,
|
||||
I: FromRequest + 'static,
|
||||
R: Future<Output = Result<O, E>> + 'static,
|
||||
O: Responder + 'static,
|
||||
E: Into<Error> + 'static,
|
||||
R: Future<Output = U> + 'static,
|
||||
U: Responder + 'static,
|
||||
{
|
||||
Route::new().to_async(handler)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user