6 releases

0.3.0 Jun 2, 2020
0.2.0 Apr 2, 2019
0.1.3 Apr 2, 2019
0.1.2 Oct 29, 2018
0.1.1 Sep 19, 2018

#646 in Rust patterns

Download history 388/week @ 2022-06-06 234/week @ 2022-06-13 207/week @ 2022-06-20 261/week @ 2022-06-27 259/week @ 2022-07-04 231/week @ 2022-07-11 274/week @ 2022-07-18 292/week @ 2022-07-25 284/week @ 2022-08-01 271/week @ 2022-08-08 360/week @ 2022-08-15 252/week @ 2022-08-22 247/week @ 2022-08-29 306/week @ 2022-09-05 300/week @ 2022-09-12 307/week @ 2022-09-19

1,160 downloads per month


546 lines

aerosol GHA Build Status

Simple dependency injection for Rust


The two main exports of this crate are the define_context! and define_interface! macros.

Contexts are containers for multiple dependencies, allowing them to be passed around as one with relative ease. Interfaces are specialized traits which place constraints on contexts, indicating exactly what dependencies a context must provide.

Contexts are typically created at the top level of an application, as they specify exactly what concrete versions of all dependencies are going to be used. A single context is created with a precise set of depenencies, and is then threaded through the rest of the application as a generic parameter.

Interfaces are used at every level of an application, as they allow each piece of code to independently specify what dependencies are required. Interfaces can "inherit" the dependencies of other interfaces, with the idea being that this inheritance will form a tree, such that there will be some "root interface" which contains the union of all dependencies required by the whole application.

This pattern allows dependencies to be added or removed from any part of the application without having to modify the code at every level, to thread or un-thread the new or old dependencies through.