proc-singleton

Crates.ioproc-singleton
lib.rsproc-singleton
version0.2.0
created_at2025-04-10 06:58:47.181795+00
updated_at2025-06-01 02:08:32.72245+00
descriptionRust proc-macros for implement Singleton Pattern
homepagehttps://github.com/jonhteper/proc-singleton
repositoryhttps://github.com/jonhteper/proc-singleton
max_upload_size
id1627724
size12,590
JP (jonhteper)

documentation

README

proc-singleton

Rust proc-macros for impl singleton

macros

derive macro usage

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);
}

attribute macro usage

 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);
 }
Commit count: 5

cargo fmt