#injection #dependencies

rdi

Simple dependency injection library for rust

1 unstable release

0.1.0 Dec 10, 2020

#7 in #dependency

Apache-2.0

6KB
117 lines

rust-commons

Modular commons library for the rust programming language. This is a mono repo to ease maintenance.

Document for each crates lives at docs.rs

Libraries

rdi

Dependency injection for the rust programming language.

Contributing

Note: This repository uses nightly rust to use some unstable options of rustc.

License

Apache 2.0


lib.rs:

Dependency injection for the rust.

This is currently in state of proof-of-concept. It currently does not support

  • providing variables to the injector
  • injection of variable into other method in injector.
  • returning references while defining injection rules.

Note: It works by cloing all components, so you have to return Arc<T> or Rc<T>. This is because the injector cannot know how much time it will be injected.

Usage

 fn main() {
     let injector = ok_injector();
     let my_handler = injector.inject(handler);

     my_handler()
 }

 pub trait Db {
     fn call(&self);
 }

 #[inject]
 pub fn handler(#[inject] db: Arc<dyn Db>) {
     db.call()
 }

 struct OkDb {}

 impl Db for OkDb {
     fn call(&self) {}
 }

 #[injector]
 fn ok_injector() {
     fn db() -> Arc<dyn Db> {
         Arc::new(OkDb {})
     }
 }

Dependencies

~1.5MB
~34K SLoC