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

Collections and optional dependencies

Many<dyn T> gathers every contribution to a key, and Option<Dep<T>> reads what may not be bound.

Many<dyn T> gathers every contribution to a key, and a service reads every plugin without knowing which modules provide them. Option<Dep<T>> reads a dependency the application may leave out.

This page follows examples/04-collections-and-optional.

Contributing to a collection

into dyn Plugin: [..] in a providers list contributes to the collection under dyn Plugin. A module contributes without knowing what else does. A consumer reads the whole collection as Many<dyn Plugin>.

#[injectable]
pub struct Host {
    pub plugins: Many<dyn Plugin>,
    pub audit: Option<Dep<AuditLog>>,
    pub mailer: Option<Dep<dyn Mailer>>,
}

/// Contributes to the collection without knowing what else does.
#[module(providers = [into dyn Plugin: [ExtraPlugin]])]
pub struct ExtrasModule;

#[module(
    imports = [ExtrasModule],
    providers = [Host, into dyn Plugin: [MetricsPlugin, TracingPlugin]],
    controllers = [HostController],
)]
pub struct AppModule;

Many<T> derefs to a slice of Arc<T>, so self.host.plugins.iter() walks the plugins.

Collections are the exception to module visibility. Many<T> gathers contributions from every module in the application, because plugins and global enhancers are app-wide.

Order

The order is one rule for every reader. It is depth-first post-order over imports from the root, imports in the order written, then declaration order inside a module. A module's contributions follow those of everything it imports. For the example the order is extra, metrics, tracing, and the test shows that rewriting the module's own list changes it:

#[module(
    imports = [ExtrasModule],
    providers = [Host, AuditLog, NullMailer as dyn Mailer, into dyn Plugin: [TracingPlugin, MetricsPlugin]],
    controllers = [HostController],
)]
struct AuditedModule;

// "extra,tracing,metrics"

Two modules with no import edge between them are ordered by where the walk reaches them.

Many<T> is eager: reading it constructs every contribution. A collection nothing contributes to reads as empty, not as an error.

A binding of T as a single instance and a contribution to T cannot share a key. wire() refuses the mix as a KindMix error naming both locations.

Optional dependencies

Option<Dep<T>> is None exactly when T would have failed with LookupError::NotFound: no module binds the key, or an extension was never written, or an input was not seeded. Every other error still propagates: a construction failure, an ambiguous key, an ExecutionRequired.

In the example nothing binds AuditLog or Mailer, so both reads are None. The test then binds them and both become Some:

assert_eq!(get(server.addr, "/optional").await.body, "audit=false mailer=false");
// with AuditLog and NullMailer as dyn Mailer bound:
assert_eq!(get(server.addr, "/optional").await.body, "audit=true mailer=true");

Option wraps any injection point, so Option<Ext<T>> and Option<Dep<RequestHead>> work the same way. The second is how a provider shared across transports reads an input that only some of them seed.

Running the example

cargo run -p ex-04-collections-and-optional

GET /plugins answers extra,metrics,tracing.

Traits and qualifiers

Binding a provider under a trait, and telling two instances of one type apart with a qualifier.

Scopes

How long an instance lives, and what the container refuses when scopes conflict.

On this page

Contributing to a collectionOrderOptional dependenciesRunning the example