pub trait RegistryApi {
// Required methods
fn trace( ) -> &'static LazyLock<Mutex<Option<Arc<dyn Fn(&RegistryEvent) + Send + Sync>>>>;
fn storage( ) -> &'static LazyLock<Mutex<HashMap<TypeId, Arc<dyn Any + Send + Sync>>>>;
// Provided methods
fn set_trace_callback(
&self,
callback: impl Fn(&RegistryEvent) + Send + Sync + 'static,
) { ... }
fn clear_trace_callback(&self) { ... }
fn emit_event(&self, event: &RegistryEvent) { ... }
fn register<T: Send + Sync + 'static>(&self, value: T) { ... }
fn register_arc<T: Send + Sync + 'static>(&self, value: Arc<T>) { ... }
fn get<T: Send + Sync + 'static>(&self) -> Result<Arc<T>, RegistryError> { ... }
fn get_cloned<T: Send + Sync + Clone + 'static>(
&self,
) -> Result<T, RegistryError> { ... }
fn contains<T: Send + Sync + 'static>(&self) -> Result<bool, RegistryError> { ... }
}Expand description
Core trait defining registry behavior.
Provides default implementations for all registry operations, requiring only
two accessor methods (storage and trace) to be implemented by the implementor.
The registry stores singleton instances indexed by their type (TypeId).
Each type can have at most one instance stored at any given time.
Required Methods§
Provided Methods§
Sourcefn set_trace_callback(
&self,
callback: impl Fn(&RegistryEvent) + Send + Sync + 'static,
)
fn set_trace_callback( &self, callback: impl Fn(&RegistryEvent) + Send + Sync + 'static, )
Set a tracing callback for registry operations.
The callback will be invoked for every registry operation (register, get, contains).
§Lock Poisoning Recovery
If the trace lock is poisoned (due to a panic while holding the lock), this method automatically recovers by extracting the inner value. This is safe because trace operations are non-critical and idempotent.
§Safety Restrictions
The callback must NOT call any registry methods on the same registry, as this will cause a deadlock. The callback is invoked while holding the trace lock.
Sourcefn clear_trace_callback(&self)
fn clear_trace_callback(&self)
Clear the tracing callback.
After calling this, no tracing events will be emitted. Note: This does not affect registered values, only the tracing callback.
§Lock Poisoning Recovery
If the trace lock is poisoned, this method automatically recovers.
Sourcefn emit_event(&self, event: &RegistryEvent)
fn emit_event(&self, event: &RegistryEvent)
Convenience wrapper to emit a registry event using the current callback.
If a trace callback is set, this method will invoke it with the provided event.
§Lock Poisoning Recovery
Lock poisoning is automatically recovered by extracting the inner value.
§Panics
If the callback itself panics, the panic will propagate to the caller. The registry lock is not held during callback execution, so this won’t poison the registry storage.
Sourcefn register<T: Send + Sync + 'static>(&self, value: T)
fn register<T: Send + Sync + 'static>(&self, value: T)
Register a value in the registry.
Takes ownership of the value and wraps it in an Arc automatically.
If a value of the same type is already registered, it will be replaced.
§Design Note
This method does not return a Result because registration is designed
for the “write-once” pattern during application startup (and rarely at runtime for rewrite). Lock poisoning
is automatically recovered. If registration must succeed, ensure your
application initialization doesn’t panic while holding registry locks.
Sourcefn register_arc<T: Send + Sync + 'static>(&self, value: Arc<T>)
fn register_arc<T: Send + Sync + 'static>(&self, value: Arc<T>)
Register an Arc-wrapped value in the registry.
More efficient than register when you already have an Arc,
as it avoids creating an additional reference count.
§Lock Poisoning Recovery
If the storage lock is poisoned, this method automatically recovers. This is safe because the insert operation is idempotent.
Sourcefn get<T: Send + Sync + 'static>(&self) -> Result<Arc<T>, RegistryError>
fn get<T: Send + Sync + 'static>(&self) -> Result<Arc<T>, RegistryError>
Retrieve a value from the registry.
Returns Ok(Arc<T>) if the type is found.
§Errors
- Type
Tis not found in the registry - Type mismatch (extremely rare)
- Registry lock is poisoned
Sourcefn get_cloned<T: Send + Sync + Clone + 'static>(
&self,
) -> Result<T, RegistryError>
fn get_cloned<T: Send + Sync + Clone + 'static>( &self, ) -> Result<T, RegistryError>
Retrieve a cloned value from the registry.
Returns an owned value by cloning the value stored in the registry.
The type T must implement Clone.
§Errors
- Type
Tis not found in the registry - Type mismatch
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.