Crates.io | singlyton |
lib.rs | singlyton |
version | 4.1.1 |
source | src |
created_at | 2021-11-21 17:36:29.210958 |
updated_at | 2021-11-25 16:05:34.443491 |
description | Safe, single-threaded global state in Rust. |
homepage | |
repository | https://github.com/WilliamVenner/singlyton |
max_upload_size | |
id | 485374 |
size | 27,185 |
Safe, single-threaded global state in Rust.
Debug assertions are present to ensure:
RefCell
)Single-threaded global state is a bit of a boogeyman in Rust:
static mut
is heavily discouraged due to its easy ability to cause UB through aliasing.First, add singlyton
as a dependency of your project in your Cargo.toml
file:
[dependencies]
singlyton = "*"
Singleton
use singlyton::Singleton;
static SINGLETON: Singleton<&'static str> = Singleton::new("Hello");
debug_assert_eq!(*SINGLETON.get(), "Hello");
SINGLETON.replace("Test");
debug_assert_eq!(*SINGLETON.get(), "Test");
*SINGLETON.get_mut() = "Test 2";
debug_assert_eq!(*SINGLETON.get(), "Test 2");
SingletonUninit
use singlyton::SingletonUninit;
static SINGLETON: SingletonUninit<String> = SingletonUninit::uninit();
SINGLETON.init("Hello".to_string());
debug_assert_eq!(SINGLETON.get().as_str(), "Hello");
SINGLETON.replace("Test".to_string());
debug_assert_eq!(SINGLETON.get().as_str(), "Test");
*SINGLETON.get_mut() = "Test 2".to_string();
debug_assert_eq!(SINGLETON.get().as_str(), "Test 2");