I'm trying to create a hash map of function objects but I can't get the compiler to accept my code. This is what I have:
fn plugin_dir(p: &PluginContext, c: &Config) -> String {
p.plugin_dir.to_string()
}
fn variable_mapping() -> HashMap<String, Box<dyn Fn(&PluginContext, &Config) -> String>> {
let mut result = HashMap::new();
result.insert("PLUGIN_DIR".to_string(), Box::new(plugin_dir));
result
}
which I assumed would be correct, but the compiler does not accept it and gives me this error message:
error[E0308]: mismatched types
--> src/plugin/variables.rs:22:5
|
17 | fn variable_mapping() -> HashMap<String, Box<dyn Fn(&PluginContext, &Config) -> String>> {
| --------------------------------------------------------------- expected `HashMap<std::string::String, Box<(dyn for<'r, 's> Fn(&'r PluginContext, &'s Config) -> std::string::String + 'static)>>` because of return type
...
22 | result
| ^^^^^^ expected trait object `dyn Fn`, found fn item
|
= note: expected struct `HashMap<_, Box<(dyn for<'r, 's> Fn(&'r PluginContext, &'s Config) -> std::string::String + 'static)>>`
found struct `HashMap<_, Box<for<'r, 's> fn(&'r PluginContext, &'s Config) -> std::string::String {plugin_dir}>>
What am I missing here?