#initialization #synchronization-primitive #double #guard #init #guarding #syncronization

init_guard

A Synchronization Primitive for guarding against double initialization

1 stable release

1.3.0 Aug 20, 2020
1.2.1 Aug 20, 2020
1.1.1 Aug 20, 2020
1.0.1 Aug 20, 2020

#982 in Concurrency

Download history 6/week @ 2024-02-26 8/week @ 2024-03-11 80/week @ 2024-04-01

88 downloads per month

MIT license

5KB

INIT GUARD

The Init_Guard Crate provides a Synchronization Primitive, that can be used to guard against double initialization.

For this, the init_guard macro is exported. The init_guard macro creates a new module that contains everything needed for the init_guard.

The Module contains two public Methods, init() and has_init().

has_init()

The has_init function has the following definition:

fn has_init() -> bool

The has_init function returns true, if the init_guard was already initialized.

init()

The init function has the following definition:

fn init() -> Result<(),()>

The init function returns Ok, if the init_guard was succesfully initialized and Err, if it was already initialized before

Usage Example

init_guard!(HAS_LOGGER_INIT); // Create the init_guard

fn init_logger() -> Result<(),String> {
    match HAS_LOGGER_INIT::init() {
        Ok(_) => {},
        Err(_) => {return Err("Logger is already initialized!".to_string())}
    }
    // Everything after this is now safe from double initialization

    // Do your actual logger initialization here
}

Dependencies

~11KB