Rust famously has no way for modules/crates to self initialize, other than a direct funcall from main. Does anyone know of a framework, perhaps using a build script, that would enable main to call a single function that would in turn call some initialization function in every module (that declares such a function) across all crates in a project, so that it isn't necessary to maintain such a function by hand?
You could use the inventory
crate, though it comes with caveats
That looks like it's just the right tool! But what are the caveats?
Because I might be able to use linkme, which it mentions, instead. Does it have fewer (hopefully, no) caveats?
linkme doesn't work on wasm. It's also somewhat fragile around compiler changes and linker implementations. For example Doesn't add elements to distributed slice from external crate on macOS · Issue #61 · dtolnay/linkme · GitHub
I assume they mean the first three issues: Wasm, MacOS, multi-crate.
Is there anything that doesn't use any features of questionable stability/portability, like .init-array
and run-before-main?
I mentioned build scripts because I thought there might be something that would search through a project's source at build time to find declared init funs, then write out an include file with them in an array so that main would only have to know the name of that array. Making that work across crates could be tricky. But even if it worked only per crate, that would be useful - as then the project only has to worry about one item (array) per crate.