Skip to main content

ModuleRegistry

Struct ModuleRegistry 

Source
pub struct ModuleRegistry { /* private fields */ }
Expand description

Thread-safe mutable module registry using DashMap for concurrent access

The ModuleRegistry provides a mutable, concurrent-friendly registry for shared modules using named registration. This allows multiple instances of the same type to be registered with different unique identifiers.

§Named Registration Pattern

Modules are registered with unique string identifiers:

  • Each registration must have a unique name
  • Multiple instances of the same type can coexist
  • Type safety is enforced at compilation time (only SharedModule implementers can register)

§Example

ⓘ
use rustf::shared::MODULE;

// Initialize the module system
MODULE::init();

// Register multiple instances of the same type
MODULE::register("email-primary", EmailService::new("primary@example.com"))?;
MODULE::register("email-backup", EmailService::new("backup@example.com"))?;

// Access by name
let primary = MODULE::get("email-primary")?;
let backup = MODULE::get("email-backup")?;

Implementations§

Source§

impl ModuleRegistry

Source

pub fn new() -> Self

Create a new empty module registry

Source

pub fn register<T: SharedModule + 'static>( &self, name: &str, module: T, ) -> RustfResult<()>

Register a shared module with the registry using a unique name

This is the primary API for registering modules. Only modules that implement the SharedModule trait can be registered, enforcing type safety at compile time.

Multiple instances of the same type can be registered with different names, allowing for scenarios like multiple email services with different configurations.

§Type Parameters
  • T - The module type to register (must implement SharedModule + ’static)
§Arguments
  • name - Unique identifier for this module instance
  • module - The module instance to register
§Returns
  • Ok(()) if registration succeeded
  • Err if a module with the same name is already registered
§Example
ⓘ
MODULE::register("email-service-primary", EmailService::new("primary@example.com"))?;
MODULE::register("email-service-backup", EmailService::new("backup@example.com"))?;
Source

pub fn get(&self, name: &str) -> RustfResult<Arc<dyn SharedModule>>

Get a shared module by name

§Arguments
  • name - The module name to retrieve
§Returns
  • Ok(Arc<dyn SharedModule>) if the module is registered
  • Err if the module is not found
Source

pub fn get_opt(&self, name: &str) -> Option<Arc<dyn SharedModule>>

Try to get a shared module by name (returns Option)

§Arguments
  • name - The module name to retrieve
§Returns
  • Some(Arc<dyn SharedModule>) if the module is registered
  • None if the module is not found
Source

pub async fn shutdown_all(&self) -> Result<()>

Shutdown all registered modules

This should be called during application shutdown to allow modules to clean up resources properly.

§Returns
  • Ok(()) when complete (ignores individual module shutdown errors)
Source

pub fn list_modules(&self) -> Vec<(String, SharedModuleType)>

List all registered modules

§Returns

Vector of (module_name, module_type) tuples

Source

pub fn contains(&self, name: &str) -> bool

Check if a module is registered by name

§Arguments
  • name - The module name to check
§Returns
  • true if the module is registered
  • false otherwise

Trait Implementations§

Source§

impl Default for ModuleRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more