entrait
Experimental proc macro to ease development using Inversion of Control patterns in Rust.
entrait is used to generate a trait from the definition of a regular function.
The main use case for this is that other functions may depend upon the trait
instead of the concrete implementation, enabling better test isolation.
The macro looks like this:
which generates the trait MyFunction:
my_function's first and only parameter is a which is generic over some unknown type A. This would correspond to the self parameter in the trait. But what is this type supposed to be? We can generate an implementation in the same go, using for Type:
;
// Generated:
// trait MyFunction {
// fn my_function(&self);
// }
//
// impl MyFunction for App {
// fn my_function(&self) {
// my_function(self)
// }
// }
The advantage of this pattern comes into play when a function declares its dependencies, as trait bounds:
The functions may take any number of parameters, but the first one is always considered specially as the "dependency parameter".
Functions may also be non-generic, depending directly on the App:
These kinds of functions may be considered "leaves" of a dependency tree.
Plans
The goal of this project is to explore ideas around how to architect larger applications in Rust. The core idea is to architect around one shared "App object" which represents the actual runtime dependencies of an application (various database connection pools etc).
Concrete things to explore
- generate
mockallcode (at least this would be needed when there are multiple trait bounds)