Expand description
§depcon
Dependency injection container
§Quickstart
use depcon::prelude::*;
use std::sync::Arc;
// 1. Define your services!
trait Database {}
trait Repository {}
// 2. Define providers, using #[derive(Injectable)].
// Use Arc<dyn Trait> for service dependencies.
#[derive(Injectable)]
struct DatabaseImpl {}
#[derive(Injectable)]
struct RepositoryImpl {
db: Arc<dyn Database>,
}
// 3. Implement services, using #[auto_provide].
#[auto_provide]
impl Database for DatabaseImpl {}
#[auto_provide]
impl Repository for RepositoryImpl {}
// 4. Create your container, and you're off to the races!
fn main() {
let mut container = Container::auto().unwrap();
let result = container.resolve::<dyn Repository>();
assert!(result.is_ok());
let repository: Arc<dyn Repository> = result.unwrap();
}
Re-exports§
pub use inventory;
Modules§
- prelude
- Prelude module for guilt-free glob imports
Macros§
- auto_
register - Flag a type as the default provider for a service when
crate::Container::auto
is used. Prefercrate::auto_provide
if codegen is enabled. - provide_
trait - Implement
[Provider]<Service>
for a type. Prefercrate::provide
if codegen is enabled.
Structs§
- Container
- Dependency injection container where the magic happens.
- Default
Provider Hook - Static registration info used by
Container::auto
. Don’t instantiate manually! - Resolution
- Debug type representing a resolved service.
- Type
Info - Debug type for identifying services & providers.
Enums§
- Error
- Error type for this crate.
Traits§
- Injectable
- Trait for injecting providers into a container. Use
Injectable
instead of implementing manually! - Provider
- Trait for types providing a service. Don’t implement manually!
Attribute Macros§
- auto_
provide - Procedural macro for
#[auto_provide]
- provide
- Procedural macro for
#[provide]
Derive Macros§
- Injectable
- Procedural macro for
#[derive(Injectable)]