0

I'd like to write a custom attribute for a const field, which will later be accessed throughout my entire library.

Example

// default declaration in `my_lib`...
pub const INITIAL_VEC_CAPACITY: usize = 10;
//...but can be overriden by dependent crates...
#[mylib_initial_vec_capacity]
pub const INITIAL_VEC_CAPACITY: usize = 5;
//...then can be accessed within my crate:
pub fn do_something() {
    let mut vec = Vec::with_capacity(macros::INITIAL_VEC_CAPACITY);
    /* do stuff with vec */
}

How would I go about achieving this?

ImajinDevon
  • 287
  • 1
  • 11

1 Answers1

0

You can use features to allow the user the compilation process of your library.

https://doc.rust-lang.org/cargo/reference/features-examples.html

Other than that I think you should use some configuration object the user passes into your code or is able to configure from outside using one of the following crates

AlexN
  • 1,613
  • 8
  • 21