#shared-state #singleton #state-management #shared #container

shared_singleton

The shared_singleton trait provides singleton pattern state management with shared container

3 unstable releases

0.2.1 Mar 3, 2024
0.2.0 Mar 3, 2024
0.1.0 Oct 9, 2020

#335 in Rust patterns

Download history 284/week @ 2023-11-27 1/week @ 2023-12-04 48/week @ 2023-12-11 80/week @ 2023-12-18 143/week @ 2023-12-25 271/week @ 2024-01-01 70/week @ 2024-01-08 105/week @ 2024-01-15 196/week @ 2024-01-22 200/week @ 2024-01-29 95/week @ 2024-02-05 268/week @ 2024-02-12 163/week @ 2024-02-19 167/week @ 2024-02-26 71/week @ 2024-03-04 47/week @ 2024-03-11

449 downloads per month

MIT license

11KB
155 lines

Shared Singleton

The shared_singleton trait provides singleton pattern state management with shared container.

Implement the singleton pattern with very short code are available.
You can choose the better shared container from Rc<T>, Rc<Cell<T>>, Rc<RefCell<T>>, Arc<T>, Arc<Mutex<T>> and Arc<RwLock<T>>.

Examples

  • Rc<T>
use shared_singleton::*;

struct Foo(usize);
impl_singleton_rc!(Foo, Foo(1));

let x1 = Foo::singleton();
let x2 = Foo::singleton();

assert_eq!(1, Foo::singleton().0);

assert!(std::rc::Rc::ptr_eq(&x1, &x2));
  • Rc<Cell<T>>
use shared_singleton::*;

#[derive(Clone, Copy)]
struct Foo(usize);
impl_singleton_rc_cell!(Foo, Foo(1));

let x1 = Foo::singleton();
let x2 = Foo::singleton();

assert_eq!(1, x1.get().0);
x2.set(Foo(2));
assert_eq!(2, x1.get().0);

assert!(std::rc::Rc::ptr_eq(&x1, &x2));
  • Rc<RefCell<T>>
use shared_singleton::*;

struct Foo(usize);
impl_singleton_rc_refcell!(Foo, Foo(1));

let x1 = Foo::singleton();
let x2 = Foo::singleton();

assert_eq!(1, x1.borrow().0);
x2.borrow_mut().0 = 2;
assert_eq!(2, x1.borrow().0);

assert!(std::rc::Rc::ptr_eq(&x1, &x2));
  • Arc<T>
use shared_singleton::*;

struct Foo(usize);
impl_singleton_arc!(Foo, Foo(1));

let x1 = Foo::singleton();
let x2 = Foo::singleton();

assert_eq!(1, x1.0);

assert!(std::sync::Arc::ptr_eq(&x1, &x2));
  • Arc<Mutex<T>>
use shared_singleton::*;

struct Foo(usize);
impl_singleton_arc_mutex!(Foo, Foo(1));

let x1 = Foo::singleton();
let x2 = Foo::singleton();

assert!(std::sync::Arc::ptr_eq(&x1, &x2));

let t1 = std::thread::spawn(move || {
    x1.lock().unwrap().0 = 2;
    println!("{}", x1.lock().unwrap().0);
});

let t2 = std::thread::spawn(move || {
    x2.lock().unwrap().0 = 3;
    println!("{}", x2.lock().unwrap().0);
});

t1.join().unwrap();
t2.join().unwrap();
  • Arc<RwLock<T>>
use shared_singleton::*;

struct Foo(usize);
impl_singleton_arc_rwlock!(Foo, Foo(1));

let x1 = Foo::singleton();
let x2 = Foo::singleton();

assert!(std::sync::Arc::ptr_eq(&x1, &x2));

let t1 = std::thread::spawn(move || {
    x1.write().unwrap().0 = 2;
    println!("{}", x1.read().unwrap().0);
});

let t2 = std::thread::spawn(move || {
    x2.write().unwrap().0 = 3;
    println!("{}", x2.read().unwrap().0);
});

t1.join().unwrap();
t2.join().unwrap();

Optional Features

tokio

tokio::sync::Mutex and tokio::sync::RwLock are available.

  • tokio::sync::Mutex
use shared_singleton::*;

struct Foo(usize);
impl_singleton_arc_mutex_tokio!(Foo, Foo(1));

#[tokio::main]
async fn main() {
    let x1 = Foo::singleton();
    let x2 = Foo::singleton();

    assert!(std::sync::Arc::ptr_eq(&x1, &x2));

    let t1 = tokio::spawn(async move {
        x1.lock().await.0 = 2;
        println!("{}", x1.lock().await.0);
    });

    let t2 = tokio::spawn(async move {
        x2.lock().await.0 = 3;
        println!("{}", x2.lock().await.0);
    });

    t1.await.unwrap();
    t2.await.unwrap();
}
  • tokio::sync::RwLock
use shared_singleton::*;

struct Foo(usize);
impl_singleton_arc_rwlock_tokio!(Foo, Foo(1));

#[tokio::main]
async fn main() {
    let x1 = Foo::singleton();
    let x2 = Foo::singleton();

    assert!(std::sync::Arc::ptr_eq(&x1, &x2));

    let t1 = tokio::spawn(async move {
        x1.write().await.0 = 2;
        println!("{}", x1.read().await.0);
    });

    let t2 = tokio::spawn(async move {
        x2.write().await.0 = 3;
        println!("{}", x2.read().await.0);
    });

    t1.await.unwrap();
    t2.await.unwrap();
}

Dependencies

~0–1.1MB
~19K SLoC