Function register

Source
pub fn register<T: Send + Sync + 'static>(value: T)
Expand description

Registers a value of type T in the global registry.

This is a convenience wrapper around di_register_arc that takes ownership of the value and wraps it in an Arc automatically.

§Safety

If the registry’s lock is poisoned (which can happen if a thread panicked while holding the lock), this function will recover the lock and continue execution. This is safe because the registry is used in a read-only manner after the initial registration phase in main.rs.

§Arguments

  • value - The value to register. Must implement Send + Sync + 'static.

§Examples

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

// Register a primitive
register(42i32);

// Register a string
register("Hello".to_string());

// Retrieve values
let num: Arc<i32> = get().expect("Failed to get i32");
let s: Arc<String> = get().expect("Failed to get String");

assert_eq!(*num, 42);
assert_eq!(&*s, "Hello");