Skip to main content

Crate global_registry

Crate global_registry 

Source
Expand description

§Global Registry

A thread-safe global type registration system for Rust that allows you to:

  • Register types globally by their TypeId
  • Associate metadata or factory functions with types
  • Retrieve registered information at runtime

§Quick Start

use global_registry::{TypeRegistry, Registrable};
 
// Define a trait for your registered types
trait Handler: Send + Sync {
    fn handle(&self, data: &str) -> String;
}

#[derive(Clone)]
struct EmailHandler;
impl Handler for EmailHandler {
    fn handle(&self, data: &str) -> String {
        format!("Email: {}", data)
    }
}

// Register the type
global_registry::register!(EmailHandler, EmailHandler);

// Use the registry
let result = TypeRegistry::global().get_cloned::<EmailHandler>().unwrap().handle("test");
assert_eq!(result, "Email: test");

Macros§

get
Macro to get an Arc reference to a registered type from the global registry
get_cloned
Macro to get a cloned copy of a registered type from the global registry
register
Macro to register a type with a value in the global registry
register_arc
Macro to register an Arc value in the global registry Takes a raw value and wraps it in Arc::new() internally
register_factory
Macro to register a type with a factory function

Structs§

FactoryRegistry
A registry specifically for factory functions
TypeRegistry
The main type registry that stores registered types and their associated data

Enums§

RegistryError
Error types for the registry

Traits§

Registrable
A trait for types that can be registered in the global registry

Type Aliases§

Factory
Type alias for a factory function