This is for testing and practice. I've global struct which doesn't need thread safe access to the main object since individual fields are using Mutex. Following is example code: Rust Playground
Problem is:
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
--> src/main.rs:21:8
|
21 | w: Mutex::new(100),
| ^^^^^^^^^^^^^^^
Is there a way to initialize mutex for the field? Unsafe code solution is acceptable.
I tried lazy_static, but it needs mutex on the main object to make it updateable which is not needed.
That's because you defined update to take &mut self. Threadsafe APIs always take &self.
&mut in rust doesn't really mean "mutable" so much as it means "unique." It means nothing else is pointing to the same data. That's obviously impossible to guarantee for anything shared between threads, which is why & is used.