#structure #data-structures

nightly macro he_di_derive

Code generation for he_di dependency injection / inversion of control framework

4 releases

Uses old Rust 2015

0.2.1 Jun 29, 2017
0.2.0 Jun 24, 2017
0.1.1 Jun 20, 2017
0.1.0 Jun 20, 2017

#496 in #structure


Used in he_di

MIT/Apache

42KB
649 lines

he_di provides a derive macro to generate implementations of the Component traits for data structures defined in your crate, allowing them to be 'registered' and 'resolved' from a Container

This functionality is based on Rust's #[derive] mechanism, just like what you would use to automatically derive implementations of the built-in Clone, Copy, Debug, or other traits. It is able to generate implementations for most structs. For now enums including ones with elaborate generic types or trait bounds. On rare occasions, for an especially convoluted type you may need to implement the trait manually.

How-to

These derives require a Rust compiler version 1.15 or newer.

  • Add he_di = "0.2" as a dependency in Cargo.toml.
  • Add he_di_derive = "0.2" as a dependency in Cargo.toml.
  • If you have a main.rs, add #[macro_use] extern crate he_di_derive there.
  • If you have a lib.rs, add #[macro_use] extern crate he_di_derive there.
  • Use #[derive(Component)] on structs that you want to flag as Component which you want to inject or be injected.
  • Specify the interface this Component is implementing through #[interface(MyTrait)]

#[derive(Component)] macro

Supported attributes

  • interface: for a struct, name of the Trait that this Component will implement
  • inject: for a struct's property, tag a property as being a dependency to another Component (currently only supports Box<Interface> synthax)

Dependencies

~2MB
~46K SLoC