Crate depcon

Source
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
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. Prefer crate::auto_provide if codegen is enabled.
provide_trait
Implement [Provider]<Service> for a type. Prefer crate::provide if codegen is enabled.

Structs§

Container
Dependency injection container where the magic happens.
DefaultProviderHook
Static registration info used by Container::auto. Don’t instantiate manually!
Resolution
Debug type representing a resolved service.
TypeInfo
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)]