RegistryApi

Trait RegistryApi 

Source
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§

Source

fn trace() -> &'static LazyLock<Mutex<Option<Arc<dyn Fn(&RegistryEvent) + Send + Sync>>>>

Access the trace callback static.

This method must be implemented to provide access to the registry’s trace callback.

Source

fn storage() -> &'static LazyLock<Mutex<HashMap<TypeId, Arc<dyn Any + Send + Sync>>>>

Access the storage static.

This method must be implemented to provide access to the registry’s storage.

Provided Methods§

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 T is not found in the registry
  • Type mismatch (extremely rare)
  • Registry lock is poisoned
Source

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 T is not found in the registry
  • Type mismatch
Source

fn contains<T: Send + Sync + 'static>(&self) -> Result<bool, RegistryError>

Check if a type is registered in the registry.

Returns Ok(true) if the type is registered, Ok(false) if not found.

§Errors
  • Registry lock is poisoned

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.

Implementors§