4 releases

Uses old Rust 2015

0.1.2 Jan 3, 2015
0.1.1 Dec 29, 2014
0.1.0 Dec 29, 2014
0.0.1 Dec 25, 2014

#182 in Value formatting

42 downloads per month

MIT license

74KB
1.5K SLoC

Dependency Injection container for Rust

Build Status

This library implements dependency injection container for Rust mimicking the way it is done in other languages and frameworks.

It differs from other popular implementations by providing a simple way to group factories together using one_of method.

Example

let mut registry = di::Registry::new();

registry
    .one_of("values", || -> i32 { 1 })
    .with_id("a")
    .insert();

registry
    .one_of("values", |a: i32| -> i32 { a + 2 })
    .with_arg("a")
    .insert();

match registry.compile() {
    Ok(container) => {
        if let Some(a) = container.get::<Vec<i32>>("values") {
            assert_eq!(vec![ 1, 1 + 2 ], a.take());
        }
    },
    Err(errors) => di::error_printer::pretty_print(&errors),
}

Of course, ungrouped dependencies are also available.

Documentation

Usage

Put this in your Cargo.toml:

[dependencies]
di = "*"

And this in your crate root:

extern crate di;

License

MIT

Dependencies

~54KB