I only just got pinged so I hadn't seen this thread until now, but since my crates were mentioned: I feel strongly that the linkme
form of the API is the right one for Rust, and that executing code at runtime before main is unnecessary and should not be added to Rust. The distributed slice elements in linkme
are each static
so they are mandated to be compile time constructible (link time technically, since you can have references to other statics, unlike in const).
Basically these comments got it right:
- From "life before main" to "common life in main" - #7 by scottmcm
- From "life before main" to "common life in main" - #13 by CAD97
And yeah, it's gonna need compiler support, along the lines of:
The only extension I'd make to that comment is that on the platforms where we can't count on the linker to handle building the slices, rustc can solve this on its own by propagating the elements through rmeta (as if they were macros) through all the layers of the dependency graph until the point that the rustc invocation that's compiling main
can get all the final slices all put together, prior to any linking.
The way this works is equivalent to if every crate implicitly got the following in its root module:
pub(rustc) mod distributed_slice_elements {
// for each of my direct dependencies:
pub(rustc) use $the_dependency::distributed_slice_elements::*;
// (except deduplicated in the case of diamond dependencies)
}