我正在将一些C代码移植到Rust,并且该代码包含很多这样的东西:
#define CONFIG_FLAG_NUMBER_23 1 #define THIS 10 #define THAT 11 #define THIS_AND_THAT (THIS + THAT) #if CONFIG_FLAG_NUMBER_23 #define THIS_OR_THAT THIS #else #define THIS_OR_THAT THAT #endif #define ROOT_DIR "/root" #define FILE_ONE ROOT_DIR "/file_one" #define FILE_TWO ROOT_DIR "/file_two"
我决定删除宏并用常量表达式替换它们,但是在Rust中尝试这样做并不是很成功:
static CONFIG_FLAG: bool = true; static THIS: int = 10; static THAT: int = 11; static THIS_AND_THAT: int = THIS + THAT; // Okay, it works // 1: Conditions static THIS_OR_THAT: int = CONFIG_FLAG ? THIS : THAT; // Doesn't work, no conditional operator static THIS_OR_THAT: int = if CONFIG_FLAG { THIS } else { THAT }; // Doesn't work, "if" is not basic enough for compile time // 2: Strings static ROOT_DIR: &'static str = "/root"; static FILE_ONE: &'static str = ROOT_DIR + "/file_one"; // Doesn't work, static strs can't be Added static FILE_TWO: String = ROOT_DIR.to_string() + "/file_two"; // Don't even think about allocations in constant expressions! static FILE_THREE: &'static str = concat!(ROOT_DIR, "/file_three"); // Doesn't work, concat! works only with literals
在Rust中重写这些代码的正确/最不痛苦的方法是什么?
问题1:条件表达式
由于配置标志可以解释为整数,因此它们可以用作变体数组中的索引。
// Configuration flag static CONFIG_FLAG: uint = 1; // Variants of any static type type T = &'static str; static VARIANT1: T = "True"; static VARIANT2: T = "False"; // Now you can select, but you have to invert either flag, or variants, or your brain static SELECTED: T = [VARIANT1, VARIANT2][1 - CONFIG_FLAG]; fn main() { println!("{}", SELECTED); }
笔记:
由于问题#5873 ,bool类型的CONFIG_FLAG
目前在数组索引中不起作用,
// error: can't cast str to uint static SELECTED: T = [VARIANT1, VARIANT2][1 - CONFIG_FLAG_BOOL as uint];
因此,您必须再创建一个静态项,然后在条件中使用它。
static CONFIG_FLAG_UINT: uint = CONFIG_FLAG_BOOL as uint;
问题2:编译时间字符串连接
简单的C宏很好地映射到Rust宏,因此您可以使用与C中基本相同的字符串连接方法。唯一的区别是您必须明确使用concat! 而不是仅仅将文字放在一起。
#![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!()); }
笔记:
我想在define_str
自动添加!()
到宏名称,并附加宏,如下所示,
macro_rules! add_bang_and_parens ( ($arg: ident) => ($arg!()); ($arg: expr) => ($arg); )
但似乎在基于参数“类型”的宏模式匹配中目前是不可能的。
以上就是c/c++开发分享将C预处理器转换为Rust相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/560720.html