Macro run_once

Source
macro_rules! run_once {
    ($expression:expr) => { ... };
}
Expand description

A macro that ensures the given expression is executed exactly once per call site.

This macro is useful for one-time initialization or setup code that should only run once, regardless of how many times the containing code is executed. It’s thread-safe and can be used in concurrent contexts.

§Example

use obel_platform::run_once;

fn initialize_resource() {
    run_once!({
        // Expensive initialization code here
        println!("Resource initialized");
    });
}

// Even if called multiple times, initialization happens only once
initialize_resource();
initialize_resource();

§Implementation Details

The macro creates a static OnceFlag that is unique to each macro invocation site. The flag is atomically checked and set, ensuring thread-safe one-time execution of the provided expression.