Comment thread for my post, Context and capabilities in Rust.
What if Rust code could instead declare the context it needs, and the compiler took care of making sure that context was provided?
This general concept is addressed by a few different approaches in other languages:
- Dependency injection
- Implicit arguments
- Effects
The approach proposed here works like implicit arguments, with some syntactic and conceptual similarities to effects in other languages that make it both more convenient and integrate better into Rust’s type system. It can also be used to implement dependency injection. This approach:
- Has zero runtime overhead; it compiles down to the code you would write anyway
- Statically guarantees that any context you need is available
- Allows passing references to objects on the stack
- Integrates with the trait system
- Follows simple rules