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:
- What will be the correct type definition for
MethodHandler? - 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?