Skip to main content

singleton_from_static

Attribute Macro singleton_from_static 

Source
#[singleton_from_static]
Expand description

Create a singleton from a static variable.

ยงExamples

use std::sync::LazyLock;
use uuid::Uuid;
use proc_singleton::singleton_from_static;

#[singleton_from_static(Identifier)]
static IDENT: LazyLock<Identifier> = LazyLock::new(|| {
    Identifier {
        id: Uuid::new_v4(),
    }
});

struct Identifier {
    id: Uuid,
}

fn main() {
    let instance = Identifier::get_instance();
    let ptr = instance as *const Identifier;
    let same_ptr = Identifier::get_instance() as *const Identifier;

    assert_eq!(ptr, same_ptr);
}