Skip to main content

ResolveContext

Struct ResolveContext 

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

  1. If T: Injectable, use T::Provider::provide() (fully static)
  2. If T is in the provider registry, use its DynProvider (for external types)
  3. Otherwise, return MissingDependency error

§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

Source

pub fn new( store: Arc<dyn SingletonStore>, registry: Arc<ProviderRegistry>, ) -> Self

Create a new ResolveContext with the given singleton store and registry.

Source

pub fn from_store(store: Arc<dyn SingletonStore>) -> Self

Create a ResolveContext with only a store (no dynamic providers).

Source

pub fn store(&self) -> &Arc<dyn SingletonStore>

Get a reference to the underlying singleton store.

Source

pub fn registry(&self) -> &ProviderRegistry

Get a reference to the provider registry.

Source

pub async fn extract<T>(&self) -> InjectableResult<T>
where T: Extract + Send + Sync + 'static,

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?)
})
Source

pub async fn clone_from_singleton<T: Injectable + Clone>( &self, ) -> InjectableResult<T>

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.

Source

pub async fn resolve_external<T: Send + Sync + 'static>( &self, ) -> InjectableResult<T>

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?;
Source

pub async fn resolve_external_with_token<T: Send + Sync + 'static>( &self, token: &str, ) -> InjectableResult<T>

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?;
Source

pub async fn try_resolve_external<T: Send + Sync + 'static>( &self, ) -> Option<InjectableResult<T>>

Try to resolve a type from the registry using the default token.

Returns None if no provider is registered, rather than an error.

Source

pub async fn try_resolve_external_with_token<T: Send + Sync + 'static>( &self, token: &str, ) -> Option<InjectableResult<T>>

Try to resolve a type from the registry using an explicit token.

Returns None if no provider is registered for (T, token).

Source

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.

Source

pub fn register_destructor_with_name( &self, type_name: &'static str, instance: Arc<dyn PreDestruct>, )

Register a destructor with a type name for debugging.

Source

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.

Source

pub async fn destructor_count(&self) -> usize

Returns the number of registered destructors.

Trait Implementations§

Source§

impl Clone for ResolveContext

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ResolveContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.