Function get

Source
pub fn get<T: Send + Sync + 'static>() -> Result<Arc<T>, String>
Expand description

Retrieves a value of type T from the global registry.

§Returns

  • Ok(Arc<T>) if the type is found and the downcast is successful
  • Err(String) in the following cases:
    • Failed to acquire the registry lock
    • Type T is not found in the registry
    • Type mismatch (found a different type with the same TypeId)

§Examples

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

// Register and retrieve a value
register(42i32);
let num: Arc<i32> = get().expect("Failed to get i32");
assert_eq!(*num, 42);

// Handle missing value
let result: Result<Arc<String>, _> = get();
assert!(result.is_err());