linkme
Safe, zero-cost distributed slices for Rust, gathered by the linker across your dependency graph.
Repository Health
Technical Analysis
linkme is a Rust library that provides distributed slices and distributed statics: collections of static elements that the linker gathers into a contiguous section of the final binary. Elements can be registered from anywhere in the dependency graph, so downstream crates can contribute entries to a slice declared upstream without the declaring crate knowing about them.
The implementation relies on link_section attributes and platform-specific linker support rather than life-before-main or runtime initialization, making it a zero-cost, safe abstraction that resolves entirely at compile and link time. It powers plugin-registration and inventory-style patterns such as test/benchmark collection and command registries.
What You Get
- The
#[distributed_slice]attribute for declaring linker-gathered static slices. - Distributed statics for aggregating individual values across crates.
- A zero-cost abstraction with no life-before-main or runtime initialization.
- Cross-platform linker support (Linux, macOS, Windows, and more) behind a safe API.
Common Use Cases
- Automatic registration of tests, benchmarks, or examples across crates.
- Plugin and command registries where downstream crates contribute entries.
- Inventory patterns that collect values without a central registration list.
Under The Hood
Architecture - The public crate (src/lib.rs, distributed_slice.rs, private.rs) defines the DistributedSlice type and re-exports the proc-macro from the companion impl/ crate; the macro emits statics annotated with platform-specific #[link_section] names so the linker places them contiguously, and a build.rs probes target support. There is no runtime registration — assembly happens entirely at link time.
Tech Stack - Pure Rust with a proc-macro companion crate (linkme-impl) built on proc-macro2, quote, and syn; a small linker script and build.rs handle platform detection. It has no runtime dependencies beyond the standard library.
Code Quality - The repo carries an integration test suite under tests/ exercising slices and statics across crate boundaries, plus CI across platforms; the code is small, focused, and maintained by dtolnay, whose crates set a high bar for API and documentation quality.
API Design - The surface is intentionally minimal — one attribute macro and a slice type that derefs to &[T] — so usage reads like ordinary Rust; extensive docs.rs documentation and examples make the linker mechanics almost invisible to callers.