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
impl ModuleRegistry
Sourcepub fn register<T: SharedModule + 'static>(
&self,
name: &str,
module: T,
) -> RustfResult<()>
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 instancemodule- The module instance to register
§Returns
Ok(())if registration succeededErrif 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"))?;Sourcepub fn get(&self, name: &str) -> RustfResult<Arc<dyn SharedModule>>
pub fn get(&self, name: &str) -> RustfResult<Arc<dyn SharedModule>>
Sourcepub async fn shutdown_all(&self) -> Result<()>
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)
Sourcepub fn list_modules(&self) -> Vec<(String, SharedModuleType)>
pub fn list_modules(&self) -> Vec<(String, SharedModuleType)>
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for ModuleRegistry
impl !UnwindSafe for ModuleRegistry
impl Freeze for ModuleRegistry
impl Send for ModuleRegistry
impl Sync for ModuleRegistry
impl Unpin for ModuleRegistry
impl UnsafeUnpin for ModuleRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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