Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async function as a function pointer type: what is the correct type definition?

I have a set of handlers that are defined as

async fn handler1(req: Request<'_>, state: web::Data<State>) -> Result<HttpResponse, Error<'_>> {
    ...
}
async fn handler2(req: Request<'_>, state: web::Data<State>) -> Result<HttpResponse, Error<'_>> {
    ...
}

What I'm trying is to return a pointer to such handler from a match statement:

type MethodHandler = fn(req: Request<'_>, state: web::Data<State>) -> Result<HttpResponse, Error<'_>>;

fn select_handler(method: &str) -> Option<MethodHandler> {
    match method {
        "handler1" => Some(handler1),
        "handler2" => Some(handler2),
        _ => None
    }
}

It does not goes very well because of async nature of handlers:

error[E0308]: mismatched types
    --> src/handlers.rs:384:34
     |
1084 |         "handler1" => Some(handler1),
     |                                  ^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found opaque type
     |
     = note: expected fn pointer `for<'r> fn(handlers::Request<'r>, actix_web::web::Data<_>) -> std::result::Result<HttpResponse, handlers::Error<'r>>`
                   found fn item `for<'_> fn(handlers::Request<'_>, actix_web::web::Data<_>) -> impl futures_util::Future {handler1}`

error[E0308]: mismatched types
    --> src/handlers.rs:385:38
     |
1085 |         "handler1" => Some(handler1),
     |                                      ^^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found opaque type
     |
     = note: expected fn pointer `for<'r> fn(handlers::Request<'r>, actix_web::web::Data<_>) -> std::result::Result<HttpResponse, handlers::Error<'r>>`
                   found fn item `for<'_> fn(handlers::Request<'_>, actix_web::web::Data<_>) -> impl futures_util::Future {handler2}`

So I have two questions:

  1. What will be the correct type definition for MethodHandler?
  2. Maybe I'm totally trying to invent the wheel here and there are already some libraries / common patters for what I'm trying to do?
like image 515
Darkkey Avatar asked Nov 19 '25 06:11

Darkkey


1 Answers

As was pointed out in the comments, each async block and function returns an anonymous type that you cannot name. Although even if you could, each of them is unique so you wouldn't be able to have a single return type anyway. You will need to box the futures and use dynamic dispatch.

The easiest solution is BoxFuture from the futures crate which handles pinning and lifetimes for you. This will make your type definition

type MethodHandler = fn(req: Request<'_>, state: State)
                -> BoxFuture<Result<HttpResponse, Error<'_>>>;

and you will need to de-sugar your async functions to return a boxed future instead by using FutureExt::boxed:

fn handler1(_req: Request<'_>, _state: State) -> BoxFuture<Result<HttpResponse, Error<'_>>> {
    async move {
        // Old function body
    }.boxed()
}

You can find a compiling example, although missing the real types, at this playground.

like image 190
apilat Avatar answered Nov 21 '25 22:11

apilat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!