I am trying to create a static Map<&'static str, &dyn Fn([u32; 6]) -> String> with phf crate, code as follow:
pub mod c2i {
fn add(args: [u32; 6]) -> String {
format!("add ${}, ${}, ${}", args[2], args[0], args[1])
}
fn addi(args: [u32; 6]) -> String {
format!("addi ${}, ${}, {:04x}", args[1], args[0], args[4])
}
static INST_MAP: phf::Map<&'static str, &dyn Fn([u32; 6]) -> String> = phf::phf_map! {
"001000" => &addi,
"_100000" => &add,
};
}
but it has error:
error[E0277]: `dyn std::ops::Fn([u32; 6]) -> std::string::String` cannot be shared between threads safely
--> src/util.rs:13:5
|
13 | / static INST_MAP: phf::Map<&'static str, &dyn Fn([u32; 6]) -> String> = phf::phf_map! {
14 | | "001000" => &addi,
15 | | "_100000" => &add,
16 | | };
| |______^ `dyn std::ops::Fn([u32; 6]) -> std::string::String` cannot be shared between threads safely
|
= help: within `phf::map::Map<&str, &dyn std::ops::Fn([u32; 6]) -> std::string::String>`, the trait `std::marker::Sync` is not implemented for `dyn std::ops::Fn([u32; 6]) -> std::string::String`
= note: required because it appears within the type `&dyn std::ops::Fn([u32; 6]) -> std::string::String`
= note: required because it appears within the type `(&str, &dyn std::ops::Fn([u32; 6]) -> std::string::String)`
= note: required because it appears within the type `[(&str, &dyn std::ops::Fn([u32; 6]) -> std::string::String)]`
= note: required because it appears within the type `&'static [(&str, &dyn std::ops::Fn([u32; 6]) -> std::string::String)]`
= note: required because it appears within the type `phf::Slice<(&str, &dyn std::ops::Fn([u32; 6]) -> std::string::String)>`
= note: required because it appears within the type `phf::map::Map<&str, &dyn std::ops::Fn([u32; 6]) -> std::string::String>`
= note: shared static variables must have a type that implements `Sync`
error: aborting due to previous error;