macro_rules! magic_statics {
{ $($vis:vis static $ident:ident: $ty:ty = $expr:expr;)* } => { ... };
{ $($vis:vis static mut $ident:ident: $ty:ty = $expr:expr;)* } => { ... };
{ $($vis:vis static ref $ident:ident: $ty:ty = $expr:expr;)* } => { ... };
}Expand description
Defines new magic statics.
Magic statics are initialized manually using the magic_static::init! macro or magic_static::main attribute macro.
§Safety
The following behaviour is considered undefined:
- Initializing magic statics from multiple threads concurrently.
- Spawning new threads and accessing magic statics during initialization from them.
- Interior mutability of magic statics where the mutability is not synchronized across multiple threads (e.g. with a Mutex or RwLock.) This is not a problem for single-threaded applications.
§Example
mod foo {
magic_statics! {
pub(super) static ref MAGIC: usize = {
println!("Magic!");
42
};
pub(super) static ref BAR: std::sync::Mutex<()> = std::sync::Mutex::new(());
}
}
// You can also modularize your magic statics in a group at the module level like so:
// See `main()` for how to initialize these magic statics.
mod baz {
magic_statics_mod! {
pub(super) static ref MAGIC: usize = {
println!("Magic!");
42
};
pub(super) static ref BAR: std::sync::Mutex<()> = std::sync::Mutex::new(());
}
}
// You can also decorate statics to make them magic statics
#[magic_static]
static FOO_BAR: std::thread::JoinHandle<()> = {
std::thread::spawn(move || {
loop { println!("HELP I CANT STOP SPINNING"); }
})
};
#[magic_static::main(
FOO_BAR,
foo::MAGIC,
foo::BAR,
mod baz // This will initialize all magic statics in the `baz` module
)]
fn main() {
println!("Hello, world!");
}