Skip to main content

nestrs_core/
module_ref.rs

1use crate::ProviderRegistry;
2use std::sync::Arc;
3
4/// NestJS [`ModuleRef`](https://docs.nestjs.com/fundamentals/module-ref) analogue: typed access to the
5/// root [`ProviderRegistry`] after the application graph is built.
6///
7/// Obtain it from [`NestApplication::module_ref`](https://docs.rs/nestrs/latest/nestrs/struct.NestApplication.html#method.module_ref)
8/// in the main `nestrs` crate (after `NestFactory::create`). Use [`Self::get`] for dynamic resolution
9/// of registered providers by type.
10///
11/// **Docs:** the repository mdBook **Fundamentals** chapter (`docs/src/fundamentals.md`) describes
12/// patterns alongside [`DiscoveryService`](crate::DiscoveryService) and lifecycle ordering.
13#[derive(Clone)]
14pub struct ModuleRef {
15    registry: Arc<ProviderRegistry>,
16}
17
18impl ModuleRef {
19    pub fn new(registry: Arc<ProviderRegistry>) -> Self {
20        Self { registry }
21    }
22
23    pub fn into_inner(self) -> Arc<ProviderRegistry> {
24        self.registry
25    }
26
27    pub fn get<T: Send + Sync + 'static>(&self) -> Arc<T> {
28        self.registry.get()
29    }
30
31    pub fn registry(&self) -> &ProviderRegistry {
32        self.registry.as_ref()
33    }
34
35    pub fn registry_arc(&self) -> &Arc<ProviderRegistry> {
36        &self.registry
37    }
38}