Rust: Why is lazy_static not working with some types

57 Views Asked by At

I need to create some global mutable variables, using lazy_static! with some types works, but with some other types it doesn't. The mutex is to change value.

This works:

lazy_static! {
    static ref foo: Mutex<String>: Mutex::new("".to_string());
}

This doesn't (the Function is js_sys::Function)

lazy_static! {
    static ref bar: Mutex<Option<Function>> = Mutex::new(None);
}

The error is: *mut u8 is not safe to send between threads

My current work-around is using thread_local! for global mutable vars, but I need threads. How to use lazy_static with all types?

0

There are 0 best solutions below