# c2rust 条件编译转化对应的实际例子:

// C
#ifdef ABCD
    printf("1: yes\n");
#else
    printf("1: no\n");
#endif
#ifndef ABCD
    printf("2: no1\n");
#elif ABCD == 2
    printf("2: yes\n");
#else
    printf("2: no2\n");
#endif
// Rust example
my_def!(F_ABCD: bool, true);
my_def!(ABCD: i32, 1);
my_def!(F_DCBA: bool, true);
my_def!(DCBA: i32, 2);       
/*
#ifdef ABCD    
	{}
#else
	{}
*/
cfg_if::cfg_if! {
	if F_ABCD == true {
		// specific code
	} else {
		// generic code
	}
}
/*
#ifdef ABCD 
	{}
#elif ABCD == 3
	{}
#else
	{}
*/     
cfg_if::cfg_if! {
	if F_ABCD == true  {
		// some code
	} else if F_ABCD == true && ABCD == 3 {
		// some code
	} else {
		// some code
	}
}
/*     
#if  define(ABCD) && ABCD == 1 || DCBA == 2
	{}
#else
	{}
*/ 
cfg_if::cfg_if! {
	if F_ABCD == true && ABCD == 1 || F_DCBA == true && DCBA == 2 {
		// some code
	} else {
		// some code
	}
}
/*     
#if  !define(DDD) && ABCD == 1 || DCBA == 2
	{}
#elif DCBA == 3 
	{}
#else
	{}
*/ 
cfg_if::cfg_if! {
	if !(F_DDD == true) && F_ABCD == true && ABCD == 1 || F_DCBA == true && DCBA == 2 {
		// some code
	} else if F_DCBA == true && DCBA == 3 {
        // some code
    } else {
		// some code
	}
}
                
/*
#ifndef ABCD
	{}
#else
	{}
*/
cfg_if::cfg_if! {
	if !(F_ABCD == true) {
		// some code
	} else {
		// some code
	}
}
/*
#ifndef ABCD 
	{}
#elif ABCD == 3
	{}
#else
	{}
*/ 
cfg_if::cfg_if! {
	if !(F_ABCD == true) {
		// some code
	} else if F_ABCD == true && ABCD == 3 {
		// some code
	} else {
		// some code
	}
}
// “#ifdef 标识符” 与 “#if defined 标识符” 实质上等价。
// “#ifndef 标识符” 与 “#if !defined 标识符” 实质上等价。

声明宏 c_conditional 用于 rust 条件编译的选择,结合 rust 常量宏的定义方法进行条件编译:

// Rust
my_def!(MAX_SIZE: i32, 1024);
my_def!(MY_STR: &'static str, "root/api/");
my_def!(MYTY, i32);
my_def!(MYNIL: bool, true);

声明宏 c_conditional 具体的定义及使用示例:

#[macro_export]
macro_rules! c_conditional {
    (if $condition:expr => { $($if_block:tt)* } $(else if $elif_condition:expr => { $($elif_block:tt)* })* $(else => { $($else_block:tt)* })?) => {
        if $condition {
            $($if_block)*
        } 
        $(else if $elif_condition {
            $($elif_block)*
        })*
        $(else {
            $($else_block)*
        })?
    };
}
// 定义一些变量
const F_ABCD: bool = true;
const ABCD: i32 = 1;
const F_DCBA: bool = true;
const DCBA: i32 = 2;
const F_DDD: bool = false;
// 定义一个测试函数,用于测试声明宏
fn test_macro() {
    c_conditional!(
        if F_ABCD == true => { // specific code
            println!("F_ABCD is true");
        }
        else => { // generic code
            println!("F_ABCD is false");
        }
    );
    
    c_conditional!(
        if !(F_DCBA == true) => { // some code
            println!("F_DCBA is false");
        }
        else if F_ABCD == true && ABCD == 3 => { // some code
            println!("F_ABCD is true and ABCD is 3");
        }
        else => { // some code
            println!("F_ABCD is true but ABCD is not 3");
        }
    );
    
    c_conditional!(
        if F_ABCD == true && ABCD == 1 || F_DCBA == true && DCBA == 2 => { // some code
            println!("F_ABCD is true and ABCD is 1 or F_DCBA is true and DCBA is 2");
        }
        else => { // some code
            println!("F_ABCD is false or ABCD is not 1 and F_DCBA is false or DCBA is not 2");
        }
    );
    
    c_conditional!(
        if !(F_DDD == true) && F_ABCD == true && ABCD == 1 || F_DCBA == true && DCBA == 2 => { // some code
            println!("F_DDD is false and F_ABCD is true and ABCD is 1 or F_DCBA is true and DCBA is 2");
        }
        else if F_DCBA == true && DCBA == 3 => { // some code
            println!("F_DDD is false and F_DCBA is true and DCBA is 3");
        }
        else => { // some code
            println!("F_DDD is true or F_ABCD is false or ABCD is not 1 and F_DCBA is false or DCBA is not 2 or 3");
        }
    );
    
    c_conditional!(
        if !(F_ABCD == true) => { // some code
            println!("F_ABCD is false");
        }
        else if F_ABCD == true && ABCD == 3 => { // some code
            println!("F_ABCD is true and ABCD is 3");
        }
        else => { // some code
            println!("F_ABCD is true and ABCD is not 3");
        }
    );
    c_conditional!(
        if !(F_ABCD == true) => { // some code
            c_conditional!(
                if !(F_ABCD == true) => { // some code
                    println!("F_ABCD is false");
                }
                else if F_ABCD == true && ABCD == 3 => { // some code
                    println!("F_ABCD is true and ABCD is 3");
                }
                else => { // some code
                    println!("F_ABCD is true and ABCD is not 3");
                }
            );
        }
        else if F_ABCD == true && ABCD == 3 => { // some code
            println!("F_ABCD is true and ABCD is 3");
        }
        else => { // some code
            println!("111");
            c_conditional!(
                if !(F_ABCD == true) => { // some code
                    println!("F_ABCD is false");
                }
                else if F_ABCD == true && ABCD == 3 => { // some code
                    println!("F_ABCD is true and ABCD is 3");
                }
                else => { // some code
                    println!("000000   F_ABCD is true and ABCD is not 3");
                }
            );
        }
    );
}
//main 函数
fn main() {
    test_macro();
}
/* 输出
F_ABCD is true
F_ABCD is true but ABCD is not 3
F_ABCD is true and ABCD is 1 or F_DCBA is true and DCBA is 2
F_DDD is false and F_ABCD is true and ABCD is 1 or F_DCBA is true and DCBA is 2
F_ABCD is true and ABCD is not 3
111
000000   F_ABCD is true and ABCD is not 3
*/