Expand description

depcon

Dependency injection container

Documentation Build status Test coverage
crates.io Downloads Rust version
MIT license

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 module for guilt-free glob imports

Macros

Flag a type as the default provider for a service when crate::Container::auto is used. Prefer crate::auto_provide if codegen is enabled.

Implement [Provider]<Service> for a type. Prefer crate::provide if codegen is enabled.

Structs

Dependency injection container where the magic happens.

Static registration info used by Container::auto. Don’t instantiate manually!

Debug type representing a resolved service.

Debug type for identifying services & providers.

Enums

Error type for this crate.

Traits

Trait for injecting providers into a container. Use Injectable instead of implementing manually!

Trait for types providing a service. Don’t implement manually!

Attribute Macros

Procedural macro for #[auto_provide]

Procedural macro for #[provide]

Derive Macros

Procedural macro for #[derive(Injectable)]