A proc macro for designing loosely coupled Rust applications.
entrait
is used to generate an implemented trait from the definition of regular functions.
The emergent pattern that results from its use enable the following things:
* Zero-cost loose coupling and inversion of control
* Dependency graph as a compile time concept
* Mock library integrations
* Clean, readable, boilerplate-free code
The resulting pattern is referred to as the entrait pattern (see also: philosophy).
The macro looks like this:
```rust
fn my_function
which generates a new single-method trait named MyFunction
, with the method signature derived from the original function.
Entrait is a pure append-only macro: It will never alter the syntax of your function.
The new language items it generates will appear below the function.
In the first example, my_function
has a single parameter called deps
which is generic over a type D
, and represents dependencies injected into the function.
The dependency parameter is always the first parameter, which is analogous to the &self
parameter of the generated trait method.
To add a dependency, we just introduce a trait bound, now expressable as impl Trait
.
This is demonstrated by looking at one function calling another:
```rust
fn foo(deps: &impl Bar) { println!("{}", deps.bar(42)); }
fn bar
Other frameworks might represent multiple dependencies by having one value for each one, but entrait represents all dependencies within the same value. When the dependency parameter is generic, its trait bounds specifiy what methods we expect to be callable inside the function.
Multiple bounds can be expressed using the &(impl A + B)
syntax.
The single-value dependency design means that it is always the same reference that is passed around everywhere. But a reference to what, exactly? This is what we have managed to abstract away, which is the whole point.
When we want to compile a working application, we need an actual type to inject into the various entrait entrypoints. Two things will be important:
Entrait generates implemented traits, and the type to use for linking it all together is Impl<T>
:
```rust
fn foo(deps: &impl Bar) -> i32 { deps.bar() }
fn bar(_deps: &impl std::any::Any) -> i32 { 42 }
let app = Impl::new(()); assert_eq!(42, app.foo()); ```
đŸ”¬ Inspect the generated code đŸ”¬
The linking happens in the generated impl block for Impl<T>
, putting the entire impl under a where clause derived from the original dependency bounds:
rust
impl<T: Sync> Foo for Impl<T> where Self: Bar {
fn foo(&self) -> i32 {
foo(self) // <---- calls your function
}
}
Impl
is generic, so we can put whatever type we want into it.
Normally this would be some type that represents the global state/configuration of the running application.
But if dependencies can only be traits, and we always abstract away this type, how can this state ever be accessed?
To reduce the number of generated traits, entrait can be used as a mod
attribute.
When used in this mode, the macro will look for non-private functions directly within the module scope, to be represented as methods on the resulting trait.
This mode works mostly identically to the standalone function mode.
```rust
mod my_module {
pub fn foo(deps: &impl super::SomeTrait) {}
pub fn bar(deps: &impl super::OtherTrait) {}
}
``
This examples generates a
MyModuletrait containing the methods
fooand
bar`.
So far we have only seen generic trait-based dependencies, but the dependency can also be a concrete type:
```rust struct Config(i32);
fn usetheconfig(config: &Config) -> i32 { config.0 }
fn doubleit(deps: &impl UseTheConfig) -> i32 { deps.usethe_config() * 2 }
asserteq!(42, Impl::new(Config(21)).doubleit()); ```
The parameter of use_the_config
is in the first position, so it represents the dependency.
We will notice two interesting things:
* Functions that depend on UseTheConfig
, either directly or indirectly, now have only one valid dependency type: Impl<Config>
1.
* Inside use_the_config
, we have a &Config
reference instead of &Impl<Config>
. This means we cannot call other entraited functions, because they are not implemented for Config
.
The last point means that a concrete dependency is the end of the line, a leaf in the dependency graph.
Typically, functions with a concrete dependency should be kept small and avoid extensive business logic. They ideally function as accessors, providing a loosely coupled abstraction layer over concrete application state.
Unimock
The whole point of entrait is to provide inversion of control, so that alternative dependency implementations can be used when unit testing function bodies. While test code can contain manual trait implementations, the most ergonomic way to test is to use a mocking library, which provides more features with less code.
Entrait works best together with unimock, as these two crates have been designed from the start with each other in mind.
Unimock exports a single mock struct which can be passed as argument to every function that accept a generic deps
parameter
(given that entrait is used with unimock support everywhere).
To enable mocking of entraited functions, they get reified and defined as a type called Fn
inside a module with the same identifier as the function: entraited_function::Fn
.
This works the same way for entraited modules, only that we already have a module to export from.
Unimock support is enabled by passing the unimock
option to entrait (#[entrait(Foo, unimock)]
), or turning on the unimock
feature, which makes all entraited functions mockable, even in upstream crates.
```rust
fn foo
mod mymod {
pub fn bar
fn my_func(deps: &(impl Foo + MyMod)) -> i32 { deps.foo() + deps.bar() }
let mockeddeps = unimock::mock([ foo::Fn.eachcall(matching!()).returns(40).inanyorder(), mymod::bar::Fn.eachcall(matching!()).returns(2).inanyorder(), ]);
asserteq!(42, myfunc(&mocked_deps)); ```
Entrait with unimock supports un-mocking. This means that the test environment can be partially mocked!
```rust
fn sayhello(deps: &impl FetchPlanetName, planetid: u32) -> Result
fn fetchplanetname(deps: &impl FetchPlanet, planetid: u32) -> Result
pub struct Planet { name: String }
fn fetchplanet(deps: &(), planetid: u32) -> Result let hellostring = sayhello(
&unimock::spy([
fetchplanet::Fn
.eachcall(matching!())
.answers(|| Ok(Planet {
name: "World".tostring(),
}))
.inany_order(),
]),
123456,
).unwrap(); asserteq!("Hello World!", hellostring);
``` This example used If you instead wish to use a more established mocking crate, there is also support for mockall.
Note that mockall has some limitations.
Multiple trait bounds are not supported, and deep tests will not work.
Also, mockall tends to generate a lot of code, often an order of magnitude more than unimock. Enabling mockall is done using the ```rust fn foo fn my_func(deps: &impl Foo) -> u32 {
deps.foo()
} fn main() {
let mut deps = MockFoo::new();
deps.expectfoo().returning(|| 42);
asserteq!(42, my_func(&deps));
}
``` A common technique for Rust application development is to divide them into multiple crates.
Entrait does its best to provide great support for this kind of architecture.
This would be very trivial to do and wouldn't even be worth mentioning here if it wasn't for concrete deps. Further up, concrete dependency was mentioned as leaves of a depdendency tree. Let's imagine we have
an app built from two crates: A ```rust
mod lib {
//! lib.rs - pretend this is a separate crate
pub struct LibConfig {
pub foo: String,
} } // main.rs
struct App {
lib_config: lib::LibConfig,
} fn main() {
use entrait::*; }
``` How can this be made to work at all? Let's deconstruct what is happening: The clue to get around this problem lies in trait delegation.
Trait delegation is an implementation of a trait that contains no logic, but just forwards each method to another implementation of the same trait, through some kind of indirection. A leaf dependency is a trait which gets delegated from In our example, An alternative way to achieve something similar to the above is to use the entrait macro directly on a trait. A typical use case for this is to put core abstractions in some "core" crate, letting other libraries use those core abstractions as dependencies. ```rust
// core_crate trait System {
fn current_time(&self) -> u128;
} // lib_crate fn computesomething(deps: &impl System) {
let systemtime = deps.current_time();
// do something with the time...
} // main.rs
struct App;
impl System for App {
fn currenttime(&self) -> u128 {
std::time::SystemTime::now()
.durationsince(std::time::UNIXEPOCH)
.unwrap()
.asmillis()
}
} Impl::new(App).compute_something();
``` This is similar to defining a leaf dependency for a concrete type, only in this case, The reason that the (NB: This example's purpose is to demonstrate entrait, not to be a guide on how to deal with system time. It should contain some ideas for how to mock time, though!) Most application configuration is data-based, but some applications also require dynamically changing implementation.
Rust can express dynamically changing implementation using Entrait supports this for leaf dependencies.
As an example, imagine a trait for abstracting away how to read some config: ```rust trait ReadConfig {
fn read_config(&self) -> String;
}
``` How to read this config could be different depending on which platform the application runs on, and the application would like to support several implementations of this trait. The problem we face here, is that the generated delegation code for The solution is to borrow a Instead of generating a ```rust trait ReadConfig: 'static {
fn read_config(&self) -> String;
} fn dosomething(deps: &impl ReadConfig) {
let config = deps.readconfig();
} struct App {
read_config: Box impl ReadConfig for String {
fn read_config(&self) -> String {
self.clone()
}
} impl std::borrow::Borrow let app = Impl::new(App {
readconfig: Box::new("hard-coded!".tostring()),
});
app.do_something();
``` by default, entrait generates a trait that is module-private (no visibility keyword).
To change this, just put a visibility specifier before the trait name: ```rust
use entrait::*; fn foo Since Rust at the time of writing does not natively support async methods in traits, you may opt in to having ```rust async fn foo There is a cargo feature to automatically apply Entrait has experimental support for zero-cost futures. A nightly Rust compiler is needed for this feature. The entrait option is called ```rust use entrait::*; async fn foo There is a feature for turning this on everywhere: Some macros are used to transform the body of a function, or generate a body from scratch.
For example, we can use ```rust async fn fetch_thing(#[path] param: String) -> feignhttp::Result Here we had to use the Most often, you will only need to generate mock implementations for test code, and skip this for production code.
A notable exception to this is when building libraries.
When an application consists of several crates, downstream crates would likely want to mock out functionality from libraries. Entrait calls this exporting, and it unconditionally turns on autogeneration of mock implementations: ```rust fn bar(deps: &()) {}
fn foo(deps: &()) {}
``` It is also possible to reduce noise by doing | Feature | Implies | Description |
| ------------------- | ------------- | ------------------- |
| The To understand the entrait model and how to achieve Dependency Injection (DI) with it, we can compare it with a more widely used and classical alternative pattern:
Object-Oriented DI. In object-oriented DI, each named dependency is a separate object instance.
Each dependency exports a set of public methods, and internally points to a set of private dependencies.
A working application is built by fully instantiating such an object graph of interconnected dependencies. Entrait was built to address two drawbacks inherent to this design: This section lists known limitations of entrait: Cyclic dependency graphs are impossible with entrait.
In fact, this is not a limit of entrait itself, but with Rust's trait solver.
It is not able to prove that a type implements a trait if it needs to prove that it does in order to prove it. While this is a limitation, it is not necessarily a bad one.
One might say that a layered application architecture should never contain cycles.
If you do need recursive algorithms, you could model this as utility functions outside of the entraited APIs of the application.unimock::spy
to create a mocker that works mostly like Impl
, except that the call graph can be short-circuited at arbitrary, run-time configurable points.
The example code goes through three layers (say_hello => fetch_planet_name => fetch_planet
), and only the deepest one gets mocked out.Alternative mocking: Mockall
mockall
entrait option.
There is no cargo feature to turn this on implicitly, because mockall doesn't work well when it's re-exported through another crate.[entrait(Foo, mockall)]
Modular applications consisting of several crates
main
which depends on a lib
:#[entrait_export(pub GetFoo)]
fn get_foo(config: &LibConfig) -> &str {
&config.foo
}
#[entrait_export(pub LibFunction)]
fn lib_function(deps: &impl GetFoo) {
let foo = deps.get_foo();
}
let app = Impl::new(App {
lib_config: lib::LibConfig {
foo: "value".to_string(),
}
});
use lib::LibFunction;
app.lib_function();
LibConfig
.GetFoo
.GetFoo
may call lib_function
.App
, which contains LibConfig
.Impl<App>
, which means it can call entraited functions.LibFunction
requires the caller to implement GetFoo
.GetFoo
is somehow only implemented for Impl<LibConfig>
, not Impl<App>
.Impl<T>
to T
, conditional on where T: Trait
.GetFoo
is automatically implemented for Impl<T> where T: GetFoo
and for LibConfig
.
This means it is definitely implemented for Impl<LibConfig>
.
To make the trait implemented for Impl<App>
, we only have to implement it for App
. That impl will be another delegation, to LibConfig
:rust
impl GetFoo for App {
fn get_foo(&self) -> &str {
self.lib_config.get_foo()
}
}
Using entrait with a trait
[entrait]
[entrait(ComputeSomething)]
core_crate
really has no type available to use.
We know that System
eventually has to be implemented for the application type, and that can happen in the main crate.#[entrait]
attribute has to be present in core_crate
, is that it needs to define a blanket implementation for Impl<T>
(as well as mocks),
and those need to live in the same crate that defined the trait.
If not, this would have violated the orphan rule.dyn
leaf dependenciesdyn Trait
.[entrait]
Impl<T>
will require that T: ReadConfig
.
In other words, there can only be one implementation of this trait per T
.
This means that we would have to create a separate application type for each way of reading config.
This doesn't scale well, and leads to combinatorial type explosion when we have several such traits to implement.
Additionally, this approach would lead to several copies of our entire generic application written with the entrait pattern, because it would need to get monomorphized for each type.dyn
reference to this trait.
Since Impl<T>
delegations does not know anything about the concrete T
, we need to describe in an abstracted way how to borrow it.
For this, Rust has the Borrow trait.T: ReadConfig
bound, we can generate a T: Borrow<dyn ReadConfig>
bound, by using delegate_by = Borrow
.[entrait(delegate_by = Borrow)]
[entrait(DoSomething)]
Options and features
Trait visibility
[entrait(pub Foo)] // <-- public trait
async
support#[async_trait]
generated for your trait.
Enable the async-trait
cargo feature and pass the async_trait
option like this:[entrait(Foo, async_trait)]
#[async_trait]
to every generated async trait: use-async-trait
.Zero-cost async inversion of control - preview mode
associated_future
, and depends on generic_associated_types
and type_alias_impl_trait
.
This feature generates an associated future inside the trait, and the implementations use impl Trait
syntax to infer
the resulting type of the future:![feature(genericassociatedtypes)]
![feature(typealiasimpl_trait)]
[entrait(Foo, associated_future)]
use-associated-future
.Integrating with other
fn
-targeting macros, and no_deps
feignhttp
to generate an HTTP client. Entrait will try as best as it
can to co-exist with macros like these. Since entrait
is a higher-level macro that does not touch fn bodies (it does not even try to parse them),
entrait should be processed after, which means it should be placed before lower level macros. Example:[entrait(FetchThing, no_deps)]
[feignhttp::get("https://my.api.org/api/{param}")]
no_deps
entrait option.
This is used to tell entrait that the function does not have a deps
parameter as its first input.
Instead, all the function's inputs get promoted to the generated trait method.Conditional compilation of mocks
[entrait_export(pub Bar)]
or
rust[entrait(pub Foo, export)]
use entrait::entrait_export as entrait
.Feature overview
unimock
| | Adds the [unimock] dependency, and turns on Unimock implementations for all traits. |
| use-async-trait
| async_trait
| Automatically applies the [asynctrait] macro to async trait methods. |
| use-associated-future
| | Automatically transforms the return type of async trait methods into an associated future by using type-alias-impl-trait syntax. Requires a nightly compiler. |
| async-trait
| | Pulls in the [asynctrait] optional dependency, enabling the async_trait
entrait option (macro parameter). |"Philosophy"
entrait
crate is central to the entrait pattern, an opinionated yet flexible and Rusty way to build testable applications/business logic.
DomainServices
.
There will typically be one such class per domain object, with a lot of methods in each.
This results in dependency graphs with fewer nodes overall, but the number of possible call graphs is much larger.
A common problem with this is that the actual dependencies—the functions actually getting called—are encapsulated
and hidden away from public interfaces.
To construct valid dependency mocks in unit tests, a developer will have to read through full function bodies instead of looking at signatures.entrait
solves this by:
Limitations
Cyclic dependency graphs