Skip to main content

Singleton

Derive Macro Singleton 

Source
#[derive(Singleton)]
{
    // Attributes available to this derive:
    #[singleton]
}
Expand description

Derives a singleton implementation for a struct based on a static variable.

ยงExamples

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

static IDENT: LazyLock<Identifier> = LazyLock::new(|| {
    Identifier {
        id: Uuid::new_v4(),
    }
});
#[derive(Singleton)]
#[singleton(IDENT)]
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);
}