【问题标题】:Translating C preprocessor to Rust将 C 预处理器翻译成 Rust
【发布时间】:2014-08-24 17:04:22
【问题描述】:

我正在将一些 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 重写此类代码的正确/最不痛苦的方法是什么?

【问题讨论】:

  • @mafso 好的,我会将我的编辑重新格式化为答案,但我仍然对其他(也许更好)解决方案感兴趣。
  • 将标题更改为更相关

标签: c rust


【解决方案1】:

问题 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);
)

但在宏中,基于参数“类型”的模式匹配似乎目前是不可能的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多