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 singletonArcfor a transient type.
Use extract to resolve any injectable type through
the correct scope-aware path.
Implementations§
Source§impl FactoryCtx
impl FactoryCtx
Sourcepub async fn extract<T>(&self) -> Result<T, InjectableError>
pub async fn extract<T>(&self) -> Result<T, InjectableError>
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?)
})Sourcepub async fn resolve_external<T>(&self) -> Result<T, InjectableError>
pub async fn resolve_external<T>(&self) -> Result<T, InjectableError>
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))
})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 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))
})