Problem 1: Conditional Expressions
Because configuration flags can be interpreted as integers, they can be used as indexes in option arrays.
Notes:
CONFIG_FLAG type bool currently does not work in array index due to issue # 5873 ,
// error: can't cast str to uint static SELECTED: T = [VARIANT1, VARIANT2][1 - CONFIG_FLAG_BOOL as uint];
so you need to create another static element and then use it in conditional expressions.
static CONFIG_FLAG_UINT: uint = CONFIG_FLAG_BOOL as uint;
Problem 2: concatenating the compilation string pipeline
Simple C macros render Rust macros nicely, so you can use essentially the same approach for string concatenation as in C. The only difference is that you must use concat explicitly! instead of just placing literals next to each other.
#![feature(macro_rules)] // Define new macro-string with name $name assembled from arguments $arg macro_rules! define_str ( ($name: ident, $($arg: expr), +) => (macro_rules! $name ( () => (concat!($($arg), +)) )); ) // Some strings define_str!(ROOT_DIR, "/root") define_str!(SUB_DIR, ROOT_DIR!(), "/sub") define_str!(FILE_NAME, SUB_DIR!(), "/file") define_str!(NONSENSE, FILE_NAME!(), SUB_DIR!(), ROOT_DIR!()) fn main() { println!("{}", FILE_NAME!()); println!("{}", NONSENSE!()); }
Notes:
I would like to add !() macro names automatically inside define_str with an additional macro like this,
macro_rules! add_bang_and_parens ( ($arg: ident) => ($arg!()); ($arg: expr) => ($arg); )
but it seems that matching macro templates based on the "types" of arguments is currently not possible.
user2665887
source share