Hi, I want to store multiple async functions with the same signature in a HashMap, but I don't understand why this doesn't work. Here is my attempt:
use std::collections::HashMap;
use futures::Future;
use futures::prelude::*;
type Request = String;
type Response = String;
async fn foo_handler(request: Request) -> Response {
"success".to_string()
}
async fn bar_handler(request: Request) -> Response {
"fail".to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[async_std::test]
async fn test_handler() {
let mut handlers: HashMap<&str, Box<fn(Request) -> dyn Future<Output = Response>>> = HashMap::new();
//let mut handlers: HashMap<&str, Box<dyn Fn(Request) -> dyn Future<Output = Response>>> = HashMap::new();
handlers.insert("foo", Box::new(foo_handler));
handlers.insert("bar", Box::new(bar_handler));
}
}
error[E0308]: mismatched types
--> src/asynctest.rs:25:35
|
25 | handlers.insert("foo", Box::new(foo_handler));
| ^^^^^^^^^^^ expected trait core::future::future::Future, found opaque type
|
= note: expected type `fn(std::string::String) -> dyn core::future::future::Future<Output = std::string::String>`
found type `fn(std::string::String) -> impl core::future::future::Future {asynctest::foo_handler}`
error[E0308]: mismatched types
--> src/asynctest.rs:26:35
|
26 | handlers.insert("bar", Box::new(bar_handler));
| ^^^^^^^^^^^ expected trait core::future::future::Future, found opaque type
|
= note: expected type `fn(std::string::String) -> dyn core::future::future::Future<Output = std::string::String>`
found type `fn(std::string::String) -> impl core::future::future::Future {asynctest::bar_handler}`
Any ideas?