singleton-registry
A thread-safe singleton registry for Rust.
Store and retrieve any type globally - structs, primitives, functions, or closures.
Each type can have only one instance registered at a time (true singleton pattern).
Designed for write-once, read-many pattern with zero-cost abstractions.
Features
- Thread-safe: All operations are safe to use across multiple threads
- Type-safe: Values are stored and retrieved with full type information
- True singleton: Only one instance per type - later registrations override previous ones
- Zero-cost abstractions: Minimal runtime overhead
- Tracing support: Optional callback system for monitoring registry operations
- No external dependencies: Pure Rust implementation
Quick Start
use ;
use Arc;
// Register a value (only one String can be registered)
register;
// Later registration of same type overrides the previous one
register;
// Retrieve the latest value
let message: = get.unwrap;
assert_eq!;
Advanced Usage
use ;
use Arc;
// Register different types
register;
register;
// Register a function pointer
let multiply_by_two: fn = ;
register;
// Check if a type is registered
assert!;
// Retrieve values
let number: = get.unwrap;
let config: = get.unwrap;
let doubler: = get.unwrap;
// Use the function
let result = doubler; // returns 42
// Set up tracing
set_trace_callback;
API Reference
register(value)- Register a value in the global registryregister_arc(arc_value)- Register an Arc-wrapped value (more efficient)get::<T>()- Retrieve a value asArc<T>get_cloned::<T>()- Retrieve a cloned value (requiresClone)contains::<T>()- Check if a type is registeredset_trace_callback(callback)- Set up tracing for registry operationsclear_trace_callback()- Disable tracing
Use Cases
- Application singletons (Config, Logger, DatabasePool, etc.)
- Global variables and constants shared across modules
- Function helpers and utility closures accessible anywhere
- Service location for shared components
- Cross-cutting concerns shared across modules
Design Philosophy
- Simple: Clean API without complex macros or derive attributes
- Safe: Values stored in
Arc<T>with full type checking - Global: One central registry shared across the entire program
- Singleton: Each type can only have one registered instance - true singleton behavior
- Override-friendly: Later registrations replace previous ones for the same type
- Efficient: Write-once, read-many pattern optimized for performance
Installation
Add this to your Cargo.toml:
[]
= "1.0"
License
BSD-3-Clause