Is it possible to implement "extensions"?


#1

Hi, is it possible to implement “extensions” such that that a master crate’s behaviour is only defined when it is compiled?
I tried, but I can’t even word that elegantly

For example, I want to have an application crate that uses clap-rs, but at compile time do a lookup for all the “extension” crates that implement X, and generate a Subcommand for each of them.

I don’t know if what I’m asking is the right question, but the alternative is to always change the master crate when adding a new subcommand, and I prefer to not have to touch it to add functionality.

Maybe the right question is “Is there a way to mimic a service registry at compile time?”


#2

I think one of the way to do this is to provide a macro

register_extensions! {
    ($($expr:ext),*) => { }
}

in the library crate and then “call” it in the final binary crate:

register_extensions!(foo::bar, baz::quuz::fizz);

fn main() { }

Cargo does something similar: https://github.com/rust-lang/cargo/blob/8709867918ff3b24a56b2d2d77113458a397a2e7/src/bin/cargo.rs#L74