Modules
Imports, exports and visibility: what a module can see and what leaves it.
A module is a Rust type that declares what it imports, what it binds, which controllers it mounts and what it exports. Modules are how an application keeps a private binding private.
This page follows examples/06-modules.
Declaring a module
#[module(providers = [PasswordHasher, UserService], exports = [UserService])]
pub struct UsersModule;
/// Sees what `UsersModule` exports and nothing else of it.
#[module(imports = [UsersModule], providers = [InvoiceService], controllers = [InvoiceController])]
pub struct BillingModule;
#[module(imports = [BillingModule])]
pub struct AppModule;| Key | Meaning |
|---|---|
imports | Modules whose exports this module can read. |
providers | Types, values, factories and collection contributions this module binds. |
controllers | Handler types this module mounts on their transports. See request lifecycle. |
exports | The bindings that leave the module. |
global | A flag, covered in Configured modules. |
App::builder(AppModule) takes the root. Anything reachable through imports is registered.
Visibility
A module sees three things: its own bindings, the exports of its direct imports, and the exports of global modules. An export is the only way a binding leaves its module.
In the example UsersModule binds PasswordHasher and UserService and exports only the second. BillingModule imports UsersModule, so InvoiceService can read Dep<UserService>. PasswordHasher stays private to UsersModule.
Imports are not transitive. AppModule imports BillingModule and does not see UserService unless BillingModule re-exports it.
Re-exporting
A module passes an import's export onward with reexport. In #[module] an export entry is Type or reexport Type, and a qualified one is reexport Type @ Q. In the value API it is m.reexport::<T>(). The test below, from crates/ulo/tests/modules.rs, re-exports Svc through B and shows that only that key passes:
Mod::<A>::new(|m| {
m.import(Mod::<B>::new(|m| {
m.import(Mod::<C>::new(|m| {
m.singleton(|| async { Svc(3) });
m.singleton(|| async { Other });
m.export::<Svc>();
m.export::<Other>();
}));
m.reexport::<Svc>();
}));
m.singleton(|svc: Dep<Svc>| async move { Pool(svc.0) });
})A reads Svc and gets the instance C built. Other stops at B, because B re-exported Svc alone. A re-export is allowed only for a key the module can see without ambiguity.
No shadowing
A key a module binds and also sees through an import is ambiguous. One binding reached by two routes is one source. Two different bindings under one key visible to the same module are an Ambiguous error that names every source module and the line where each exported it.
Lookups outside a module
app.get::<T>() and exec.get::<T>() inside an execution the app opened use the root module's visibility. A subtree's private service is reached through app.module::<M>()?, which returns a ModuleRef limited to what M sees. Inside a transport call, the handler and the services it reads see what the controller's module sees.
Collections are the exception: Many<T> gathers contributions from every module.
What is refused
Reading a binding the module cannot see fails wire(). The reader is named, with its module. App, Dep, StartupError and WiringError are all exported from ulo:
#[controller]
struct Snooper {
_hasher: Dep<PasswordHasher>,
}
#[handlers]
impl Snooper {
#[ulo_http::get("/")]
fn snoop(&self) -> &'static str {
"never"
}
}
#[module(imports = [UsersModule], controllers = [Snooper])]
struct SnoopingModule;
let Err(StartupError::Wiring(errors)) = App::builder(SnoopingModule).wire() else {
panic!("wiring should fail");
};
let report = errors.to_string();
assert!(report.contains("missing dependency `PasswordHasher`"), "{report}");
assert!(report.contains("needed by Snooper (field `_hasher`) in SnoopingModule"), "{report}");The printed block is:
× missing dependency `PasswordHasher`
├─ needed by Snooper (field `_hasher`) in SnoopingModule
└─ help: import a module that exports `PasswordHasher`, or provide it in SnoopingModuleWhen the key exists in an import under a near spelling, the help line says which: for instance UsersModule exports dyn Repo + Send + Sync; the injection point reads dyn Repo``.
| Mistake | Reported as |
|---|---|
| Reading a key the module cannot see | Missing |
| Two modules in view export one key | Ambiguous, naming each exporter |
exports = [X] for a key the module does not bind | ExportNotBound; the help suggests reexport when an import provides it |
| A re-export of a key the module cannot see, or sees from several sources | ReexportNotVisible, ReexportAmbiguous |
| A module importing itself | ImportCycle, printed as "M imports itself". Modules importing each other in a loop wire. |
| A binding that is not a controller reads a controller's key | ControllerRead: a controller is the edge of the application, reached by its transport alone. Move the shared logic into a provider both read. |
All of these come from the one wire() pass, together with every other problem. See The wiring report.
Running the example
cargo run -p ex-06-modulesGET / answers invoice for ada registered with hashed:secret.