Skip to main content

FactoryCtx

Struct FactoryCtx 

Source
pub struct FactoryCtx(/* private fields */);
Expand description

Scope-safe resolution context for factory closures.

Passed to DynProvider::with_ctx closures instead of the raw Arc<ResolveContext>. Only exposes operations that go through the full Extract machinery and therefore respect singleton / transient scope.

§What is intentionally absent

FactoryCtx does not expose:

  • resolve::<T>() — calls the provider directly, bypassing the singleton cache and creating a fresh instance on every call regardless of scope.
  • resolve_singleton_arc::<T>() — accesses the raw singleton cache, which would allow users to pull a singleton Arc for a transient type.

Use extract to resolve any injectable type through the correct scope-aware path.

Implementations§

Source§

impl FactoryCtx

Source

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

Extract any type that implements Extract.

This is the scope-safe extraction path — identical to what the #[injectable(inject)] annotation generates for struct fields and constructor parameters. Singleton types return the cached Arc; transient types get a fresh instance.

§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 resolve_external<T>(&self) -> Result<T, InjectableError>
where T: Send + Sync + 'static,

Resolve a type registered via DynProvider.

Use this when you need a value that was registered with ContainerBuilder::register(DynProvider::…) rather than via #[injectable].

§Example
DynProvider::with_ctx(|ctx| async move {
    let pool: sqlx::SqlitePool = ctx.resolve_external().await?;
    Ok(MyRepo::new(pool))
})
Source

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

Resolve a type registered via a named DynProvider token.

Use this when multiple providers of the same type are registered under different tokens.

§Example
DynProvider::with_ctx(|ctx| async move {
    let primary: Pool = ctx.resolve_external_with_token("primary").await?;
    let replica:  Pool = ctx.resolve_external_with_token("replica").await?;
    Ok(Router::new(primary, replica))
})

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> 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, 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.