patterns icon indicating copy to clipboard operation
patterns copied to clipboard

Singleton

Open simonsan opened this issue 4 years ago • 1 comments

Tracking issue for merging: https://github.com/lpxxn/rust-design-pattern/blob/master/creational/singleton.rs

Example:

use std::mem::MaybeUninit;
use std::sync::{Mutex, Once};

#[derive(Debug)]
struct Config {
    db_connection_str: String,
}

fn get_config() -> &'static Mutex<Config> {
    static mut CONF: MaybeUninit<Mutex<Config>> = MaybeUninit::uninit();
    static ONCE: Once = Once::new();

    ONCE.call_once(|| unsafe {
        CONF.as_mut_ptr().write(Mutex::new(Config {
            db_connection_str: "test config".to_string(),
        }));
    });

    unsafe { &*CONF.as_ptr() }
}

fn main() {
    let f1 = get_config();
    println!("{:?}", f1);
    // modify
    {
        let mut conf = f1.lock().unwrap();
        conf.db_connection_str = "hello".to_string();
    }

    let f2 = get_config();
    println!("{:?}", f2);
    let conf2 = f2.lock().unwrap();

    assert_eq!(conf2.db_connection_str, "hello".to_string())
}

simonsan avatar Feb 22 '21 11:02 simonsan

Tracking discussion on merging: https://github.com/lpxxn/rust-design-pattern/issues/7

simonsan avatar Feb 22 '21 11:02 simonsan