Warning
This post was published 41 days ago. The information described in this article may have changed.
I would like to build programs, that are composed from two parts: Generators and Consumers.
Lets call them "partial binaries" that are eventually composed into a true, executable binary.
For example two generators:
fn generate() -> u32 {
13
}
fn generate() -> u32 {
125
}
And (for brevity just one) consumers:
fn consume(num: u32) {
if num == 42 {
println!("My favorite number!");
} else {
println!("Not my favorite number: {}", num);
}
}
Eventually, there will be many generator and consumers and
I would like to 'magically' execute a pair of them: random_prime + is_favorite
results in stdout Not my favorite...
.
Question: How would you do this?
I will go into further detail below how I would build this. But any suggestion different form my approach below is equally welcome.
My results so far:
By compiling all consumers and generators as libraries, I could eventually assemble the executable by linking together generate
and consume
.
Throwing in the glue code obviously:
fn main() {
consume(generate());
}
I have not fully build this out, but I feel like this would come to the desired result.
The big downside is that this requires a large number of crates just to compile them into libraries - one crate for each consumer/generator.
Compiling random_primes
to an dedicated partial binary artifact would be much more elegant and cargo allows the definition of many binaries in a single crate.
I just need to rip out the traditional linker step and eventually link them manually with the glue code.
I was able to get this somewhat to work by running
cargo rustc --bin random_prime -- --emit=obj
cargo rustc --bin random_cube -- --emit=obj
cargo rustc --bin is_favorite -- --emit=obj
which yields the object files. These can then be linked with the glue code to an executable (details omitted for brevity).
From my perspective this appears to be conceptually correct, although fairly elaborate.
What do you think? Is there an easier way?
Additional notes:
1 post - 1 participant
🏷️ rust_feed