pub struct ResolveContext { /* private fields */ }Expand description
The resolution context passed through provider chains.
This struct holds:
- A reference to the typed singleton store
- A reference to the dynamic provider registry (for external types)
- A list of registered destructors (for
#[injectable(pre_destruct)]hooks)
§Resolution Strategy
When resolve::<T>() is called:
- If
T: Injectable, useT::Provider::provide()(fully static) - If
Tis in the provider registry, use itsDynProvider(for external types) - Otherwise, return
MissingDependencyerror
§Type Safety
The singleton store uses generated typed fields (no Any/TypeId).
The provider registry uses TypeId internally but this is never
exposed to users — the public API is fully typed.
Implementations§
Source§impl ResolveContext
impl ResolveContext
Sourcepub fn new(
store: Arc<dyn SingletonStore>,
registry: Arc<ProviderRegistry>,
) -> ResolveContext
pub fn new( store: Arc<dyn SingletonStore>, registry: Arc<ProviderRegistry>, ) -> ResolveContext
Create a new ResolveContext with the given singleton store and registry.
Sourcepub fn from_store(store: Arc<dyn SingletonStore>) -> ResolveContext
pub fn from_store(store: Arc<dyn SingletonStore>) -> ResolveContext
Create a ResolveContext with only a store (no dynamic providers).
Sourcepub fn store(&self) -> &Arc<dyn SingletonStore> ⓘ
pub fn store(&self) -> &Arc<dyn SingletonStore> ⓘ
Get a reference to the underlying singleton store.
Sourcepub fn registry(&self) -> &ProviderRegistry
pub fn registry(&self) -> &ProviderRegistry
Get a reference to the provider registry.
Sourcepub async fn extract<T>(&self) -> Result<T, InjectableError>
pub async fn extract<T>(&self) -> Result<T, InjectableError>
Extract a value using the scope-safe crate::Extract path.
This is the recommended way to resolve a type inside a factory closure
or DynProvider::with_ctx. Unlike the old ctx.resolve::<T>(), this
respects singleton / transient scope and goes through the full singleton
cache machinery.
§Example
DynProvider::with_ctx(|ctx| async move {
let config: Inject<AppConfig> = ctx.extract().await?;
Ok(Database::connect(&config.db_url).await?)
})Sourcepub async fn clone_from_singleton<T>(&self) -> Result<T, InjectableError>where
T: Injectable + Clone,
pub async fn clone_from_singleton<T>(&self) -> Result<T, InjectableError>where
T: Injectable + Clone,
Extract an owned singleton value by cloning from the singleton cache.
Called by the generated impl Extract for T where T: Clone for singleton
types — this avoids the #[async_trait] macro which has trouble with
concrete-type where T: Clone bounds on impl blocks.
Sourcepub async fn resolve_external<T>(&self) -> Result<T, InjectableError>
pub async fn resolve_external<T>(&self) -> Result<T, InjectableError>
Resolve an external type from the provider registry using the default token.
Use this for types that don’t implement Injectable but have been registered
via ContainerBuilder::register("", DynProvider::…).
For named tokens use resolve_external_with_token.
§Example
let client = ctx.resolve_external::<reqwest::Client>().await?;Sourcepub async fn resolve_external_with_token<T>(
&self,
token: &str,
) -> Result<T, InjectableError>
pub async fn resolve_external_with_token<T>( &self, token: &str, ) -> Result<T, InjectableError>
Resolve an external type from the provider registry using an explicit token.
Use this when multiple providers of the same type are registered under
different tokens (e.g., "primary" vs "replica" database pools).
§Example
let primary: Pool = ctx.resolve_external_with_token("primary").await?;
let replica: Pool = ctx.resolve_external_with_token("replica").await?;Sourcepub async fn try_resolve_external<T>(
&self,
) -> Option<Result<T, InjectableError>>
pub async fn try_resolve_external<T>( &self, ) -> Option<Result<T, InjectableError>>
Try to resolve a type from the registry using the default token.
Returns None if no provider is registered, rather than an error.
Sourcepub async fn try_resolve_external_with_token<T>(
&self,
token: &str,
) -> Option<Result<T, InjectableError>>
pub async fn try_resolve_external_with_token<T>( &self, token: &str, ) -> Option<Result<T, InjectableError>>
Try to resolve a type from the registry using an explicit token.
Returns None if no provider is registered for (T, token).
Sourcepub fn register_destructor(&self, instance: Arc<dyn PreDestruct>)
pub fn register_destructor(&self, instance: Arc<dyn PreDestruct>)
Register a destructor for an instance that implements PreDestruct.
This is called by the generated provider code when
#[injectable(has_pre_destruct)] is specified. The destructor
will be called during container shutdown.
Sourcepub fn register_destructor_with_name(
&self,
type_name: &'static str,
instance: Arc<dyn PreDestruct>,
)
pub fn register_destructor_with_name( &self, type_name: &'static str, instance: Arc<dyn PreDestruct>, )
Register a destructor with a type name for debugging.
Sourcepub async fn run_destructors(&self) -> Result<(), Vec<InjectableError>>
pub async fn run_destructors(&self) -> Result<(), Vec<InjectableError>>
Run all registered pre_destruct hooks in reverse order.
This should be called during container shutdown. Instances are destroyed in reverse construction order (last constructed, first destroyed).
All destructors are called even if some fail (best-effort cleanup).
Any errors are collected and returned as
InjectableError::ShutdownFailed.
Sourcepub async fn destructor_count(&self) -> usize
pub async fn destructor_count(&self) -> usize
Returns the number of registered destructors.
Trait Implementations§
Source§impl Clone for ResolveContext
impl Clone for ResolveContext
Source§fn clone(&self) -> ResolveContext
fn clone(&self) -> ResolveContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more