Crate singleton_registry

Source
Expand description

§Singleton Registry

A thread-safe dependency injection registry for storing and retrieving global instances. Currently designed for write-once, read-many pattern.

This crate provides a type-safe way to register and retrieve instances of any type that implements Send + Sync + 'static.

§Quick Start

use singleton_registry::{register, get};
use std::sync::Arc;

// Register a value
register("Hello, World!".to_string());

// Retrieve the value
let message: Arc<String> = get().unwrap();
assert_eq!(&*message, "Hello, World!");

§Features

  • Thread-safe: All operations are safe to use across multiple threads
  • Type-safe: Values are stored and retrieved with full type information
  • Zero-cost abstractions: Minimal runtime overhead
  • Tracing support: Optional callback system for monitoring registry operations

§Main Functions

  • register - Register a value in the global registry
  • register_arc - Register an Arc-wrapped value (more efficient if you already have an Arc)
  • get - Retrieve a value as Arc
  • get_cloned - Retrieve a cloned value (requires Clone)
  • contains - Check if a type is registered
  • set_trace_callback - Set up tracing for registry operations

Enums§

RegistryEvent
Events emitted by the dependency-injection registry.

Functions§

clear_trace_callback
Clears the tracing callback (disables registry tracing).
contains
Checks if a value of type T is registered in the global registry.
get
Retrieves a value of type T from the global registry.
get_cloned
Retrieves a clone of the value stored in the registry for the given type.
register
Registers a value of type T in the global registry.
register_arc
Registers an Arc<T> in the global registry.
set_trace_callback
Sets a tracing callback that will be invoked on every registry interaction.

Type Aliases§

TraceCallback
Type alias for the user-supplied tracing callback.