#di #ioc #dependency-injection #design-pattern

rustyinject

Zero-cost, compile-time DI framework for Rust

2 releases

0.1.1 Jun 13, 2024
0.1.0 Jun 13, 2024

#1655 in Rust patterns

MIT license

29KB
661 lines

rustyinject

Rustyinject is a compile-time DI library for Rust.

Overview

Dependency Injection is a design pattern used to implement IoC (Inversion of Control), allowing the creation, storage, and retrieval of dependencies in a flexible and decoupled manner. This provides a container for DI that can:

  • Store singleton instances and provide them.
  • Provide cloned instances of singletons.
  • Create instances using factory methods.

Usage

Here is an example of how to use the DI container:

use rustyinject::{DependencyContainer, injector::{factories::ConstructorFactory, Injector}};

struct MyService {
    // Some fields
}

impl ConstructorFactory for MyService {
    type Dependencies<'a> = (); // Specify your dependencies here.

    fn build(dependencies: Self::Dependencies<'_>) -> Self {
        Self {
           // Some fields
        }
    }
}

let container = DependencyContainer::default()
    .with_constructor_factory::<MyService>();

let my_service: MyService = (&container).inject();

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

License

This project is licensed under the MIT License. See the LICENSE file for details.

No runtime deps