Singlyton
Safe, single-threaded global state in Rust.
Install / Use
/learn @WilliamVenner/SinglytonREADME
Singlyton
Safe, single-threaded global state in Rust.
Debug assertions are present to ensure:
- Borrow checking (see
RefCell) - Thread safety (two threads cannot access the same singleton)
- Sound usage of uninitialized memory
Why?
Single-threaded global state is a bit of a boogeyman in Rust:
static mutis heavily discouraged due to its easy ability to cause UB through aliasing.- Thread locals can be slow for performance critical contexts, are nonsense to use in a single-threaded environment, and may not be available on all platforms
- Working around Rust's thread-safety mechanisms in single-threaded contexts can be ugly, annoying and unnecessary
Usage
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");
Related Skills
node-connect
354.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
112.3kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
354.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
354.3kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
