Attribute Macro magic_static_macro::main [−][src]
#[main]Expand description
An attribute that can be attached to your main function which initializes magic statics in the specified order.
Panics
This will panic if any of the magic statics have already been initialized.
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_static! {
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 like so:
mod baz {
magic_static! {
pub(super) static ref MAGIC: usize = {
println!("Magic!");
42
};
pub(super) static ref BAR: std::sync::Mutex<()> = std::sync::Mutex::new(());
}
#[magic_static::main(
MAGIC,
BAR
)]
// The `magic_statics!` macro (NOT `magic_static!`) can generate this function for you
pub fn magic_static() {}
}
#[magic_static::main(
foo::MAGIC,
foo::BAR,
mod baz // This will initialize all magic statics in `baz`
)]
fn main() {
println!("Hello, world!");
}