ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

Core Concepts

Providers and injection

ProvidersTraits and qualifiersCollections and optional dependenciesScopes

Modules

ModulesConfigured modulesDynamic and lazy modules

Running the application

Application phasesLifecycle hooksShutdownStandalone executions

Diagnostics and the value API

The wiring reportWithout macros

Protocols

Integrations

Operating

Reference

Core Concepts

Configured modules

Global modules, modules that take a value, and one module imported twice under two qualifiers.

A module often needs a value from the application: a database URL, a feature flag. The module takes the value as a struct field, and two different values are two modules. .keyed::<Q>() imports one module twice under two qualifiers, and a global module's exports are visible without an import.

This page follows examples/07-configured-modules.

A module that takes a value

A module can be a struct with fields. Its identity is its type and its value: two values of one type are two modules, and the same value imported twice is one. The struct derives Clone, PartialEq, Eq, Hash, and #[module] can read self in its lists.

#[derive(Clone, PartialEq, Eq, Hash)]
#[module(providers = [Store { url: self.url }], exports = [Store])]
pub struct StoreModule {
    url: &'static str,
}

impl StoreModule {
    pub fn for_root(url: &'static str) -> Self {
        StoreModule { url }
    }
}

for_root is a convention for the constructor of a module that is configured once at the root. Nothing in the framework depends on the name.

A Secret<_> field in a configured module is registered for redaction automatically, so its text is replaced in every error report. The text of a configuration value is never printed in a module name, because it may hold credentials.

Importing the same module twice

An equal value imported twice is one module, and only the first registration runs. Two values of one type are two modules, and both export the same key. A consumer that reads that key sees two sources and wire() refuses it as ambiguous, naming both modules as StoreModule and StoreModule #2:

#[module(imports = [StoreModule::for_root("same"), StoreModule::for_root("same")])]
struct Twice;

#[module(imports = [StoreModule::for_root("one"), StoreModule::for_root("two")])]
struct TwoConfigurations;

let [WiringError::Ambiguous { key, module, sources, .. }] = errors.iter().collect::<Vec<_>>()[..] else {
    panic!("expected one ambiguity, got {errors}");
};
assert_eq!(key.to_string(), "Store");
assert_eq!(module.to_string(), "TwoConfigurations");
let names: Vec<String> = sources.iter().map(|(m, _)| m.to_string()).collect();
assert_eq!(names, ["StoreModule", "StoreModule #2"]);

Two instances under two qualifiers

.keyed::<Q>() wraps a module so its exports leave under a qualifier, and its identity carries that qualifier too. Inside the module, injection points stay unqualified; at the export boundary Store becomes Store @ Primary.

pub struct Primary;
pub struct Replica;

/// `keyed` wraps a module so its exports leave under a qualifier and its identity carries it.
pub fn primary() -> Keyed<Primary, StoreModule> {
    StoreModule::for_root("postgres://primary").keyed::<Primary>()
}

pub fn replica() -> Keyed<Replica, StoreModule> {
    StoreModule::for_root("postgres://replica").keyed::<Replica>()
}

#[module(imports = [ConfigModule, primary(), replica()], controllers = [LedgerController])]
pub struct AppModule;

Consumers read Dep<Store, Primary> and Dep<Store, Replica>. Re-exports leave under the qualifier too. Contributions are not exports and keep their key, so two keyed modules contribute two entries to one collection.

A bare StoreModule::for_root(url) beside StoreModule::for_root(url).keyed::<Primary>() is two modules with two separate instances. One instance under two names is an alias, not a second import.

To fetch a keyed module's handle, name the type and the qualifier:

let primary = server.handle.module_keyed::<StoreModule, Primary>().unwrap();
assert_eq!(primary.name().to_string(), "StoreModule @ Primary");

What is refused

MistakeReported as
A module keyed twice, m.keyed::<A>().keyed::<B>()NestedKeyed, naming both qualifiers; key it once.
A keyed module declares an execution inputKeyedInput: inputs are app-wide and belong to transports.
Equal identities imported with different Module::settingsDifferingSettings, naming the module, both import sites and each setting that differs with both values.
A setting name written twice in one module's Module::settingsDuplicateSetting, naming the setting and both with calls.

Builder knobs that do not belong to a module's identity, such as a store's connect timeout, are the module's settings. Two imports of one identity must agree on them, since only the first registers.

Global modules

global on a module makes its exports visible to every module without an import.

pub struct AppConfig {
    pub name: &'static str,
}

/// Global: every module sees what it exports without importing it.
#[module(global, providers = [AppConfig { name: "ledger" }], exports = [AppConfig])]
pub struct ConfigModule;

The global module still has to be imported once, somewhere, so that it registers: the example's AppModule lists ConfigModule in imports. The test shows Banner's module reaching AppConfig without importing it:

/// Reads `AppConfig` without importing `ConfigModule`: only a global export reaches it.
#[injectable]
struct Banner {
    config: Dep<AppConfig>,
}

#[module(providers = [Banner], exports = [Banner])]
struct BannerModule;

#[module(imports = [ConfigModule, BannerModule])]
struct WithBanner;

A global module is a way to share configuration and cross-cutting services. Prefer ordinary imports where a dependency is specific to a few modules, because an import documents who depends on whom.

The core itself binds dyn Timer and dyn Runtime in a global module, so any provider can read them as Dep<dyn Timer>. A module that binds dyn Timer itself is refused as two sources for one key.

Running the example

cargo run -p ex-07-configured-modules

GET /config answers ledger, GET /primary answers postgres://primary, GET /replica answers postgres://replica.

Modules

Imports, exports and visibility: what a module can see and what leaves it.

Dynamic and lazy modules

Modules built at runtime from a value, module handles, and modules loaded after the application has started.

On this page

A module that takes a valueImporting the same module twiceTwo instances under two qualifiersWhat is refusedGlobal modulesRunning the example