pub trait Provider<T>:
Send
+ Sync
+ 'static {
// Required method
fn provide<'life0, 'async_trait>(
ctx: &'life0 ResolveContext,
) -> Pin<Box<dyn Future<Output = Result<T, InjectableError>> + Send + 'async_trait>>
where 'life0: 'async_trait;
}Expand description
A provider that can asynchronously construct a value of type T.
§Generated Implementation
The #[derive(Injectable)] macro generates a provider struct and
implements this trait. The generated provide method:
- Extracts each constructor parameter via
Extract::extract(ctx) - Calls the constructor with the extracted values
- Invokes
post_constructhooks if present - Returns the fully constructed value
§No Runtime Lookup
All dependency resolution happens through static dispatch. There is
no HashMap<TypeId, Box<dyn Any>>, no downcasting, no reflection.
§Example (Generated Code)
ⓘ
pub struct UserServiceProvider;
#[async_trait]
impl Provider<UserService> for UserServiceProvider {
async fn provide(ctx: &ResolveContext) -> InjectableResult<UserService> {
let db = Inject::<Database>::extract(ctx).await?;
let cache = Inject::<Cache>::extract(ctx).await?;
let instance = UserService::new(db, cache).await;
instance.post_construct().await;
Ok(instance)
}
}Required Methods§
Sourcefn provide<'life0, 'async_trait>(
ctx: &'life0 ResolveContext,
) -> Pin<Box<dyn Future<Output = Result<T, InjectableError>> + Send + 'async_trait>>where
'life0: 'async_trait,
fn provide<'life0, 'async_trait>(
ctx: &'life0 ResolveContext,
) -> Pin<Box<dyn Future<Output = Result<T, InjectableError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Asynchronously provide a value of type T.
This method extracts all dependencies from the context, constructs the value, and runs lifecycle hooks.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".