Sometimes there are things scattered across the codebase, and it's desirable to have a list of all things.
Examples: tests, entry points, web server routes, Google-style flags, performance counters, feature toggles.
Maintaining the list of things manually is error-prone. It is also problematic if things are in multiple crates.
Inspired by C# partial classes, I propose something like this:
partial const ALL_THINGS: &[Thing] = &[..];
// "[..]" means this is a definition of a partial slice constant.
// It's a compilation error to have more than one definition.
partial const ALL_THINGS = &[.., Thing(1), ..];
// "[.., x, ..]" means it's an extension or a partial slice constant.
// It has to be defined elsewhere.
partial const ALL_THINGS = &[.., Thing(2), ..];
// Another extension of the same constant.
The compiler transforms it into
const ALL_THINGS: &[Thing] = &[Thing(2), Thing(1)];
// order unspecified
Using this mechanism, say testcase discovery can be achieved by having #[test]
macro
expand
#[test]
fn my_test() {
stuff
}
into
const ALL_TESTS = &[.., ("my_test", &my_test), ..];
fn my_test() {
stuff
}