Statically configure a dependency resource-wise


I'm starting a project where I'll have at least two crates, one A depending on the other B. The quirck here is that the crate B will be no_std and the heap usage will be forbidden.

In this scenario the crate A will need to pass some values to B at compilation time, for B to dimension the structures properly.

Is there a way to pass configuration parameters to a library dependency?. I'm thinking about collecting all the parameters inside an xml file at a known location (maybe using envvars) which the of the B crate can read and configure at compile time.

While I think that would work I hope there is a better way like cargo features but with key-value pairs.

For the dimensioning I was thinking about generating code from, and placing the result in the target folder, visible to the src that will be compiled. IDK if this is possible though

The only requirement is to use stable Rust for this.

Thanks in advance

To answer the second question, as stated HERE this is solved with

include!(concat!(env!("OUT_DIR"), "/"));

You should just use const-generic parameters generally. Is there a reason it wouldnā€™t work for you? e.g.

struct TypeFromStructB<const N: usize> {
    /* ... */

Thanks. I'll analyze the fit of const-generics in my use case. Anyhow, the question is more open/ not constrained to defining sizes but also other things like new datatypes, factories, etc

Well, generally this kind of pattern should be avoided. Other than feature flags and target platform downstream users should not really be able to affect the compilation of the upstream crates.

1 Like

I'm noticing an issue with const generics. i'll be passing -lots- of parameters to crate B, the strategy would be having defaults for the parameters not explictly defined in an attempt to reduce the clog

This makes a lot of sense. I am having a hard time trying to forget the C mindset

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.