| Crates.io | singlyton |
| lib.rs | singlyton |
| version | 4.1.1 |
| created_at | 2021-11-21 17:36:29.210958+00 |
| updated_at | 2021-11-25 16:05:34.443491+00 |
| 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 = "*"
Singletonuse 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");
SingletonUninituse 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");