宏是否可以采用常量表达式并 "inline" 生成有效的 LiteralPattern?

Is it possible for a macro to take a constant expression and "inline" it to generate a valid LiteralPattern?

我想根据其中一个字段对复杂常量进行模式匹配。您可以使用 PathPattern 匹配常量,但不允许选择另一个常量的常量字段。

是否可以编写一个宏来获取常量表达式的结果并在编译时“内联”它,以便生成的代码是有效的LiteralPattern

考虑这个工作代码:

pub struct Instruction {
    code: u8,
    width: usize,
}

pub const CMP_REG_U8: Instruction = Instruction { code: 3, width: 3 };
pub const CMP_REG_U32: Instruction = Instruction { code: 4, width: 6 };
pub const INVALID: Instruction = Instruction { code: 0, width: 0 };

fn main() {
    let opcode = 3u8;
    
    let inst = match opcode {
        3 => CMP_REG_U8,
        4 => CMP_REG_U32,
        _ => INVALID,
    };

    println!("inst.code: {} inst.width: {}", inst.code, inst.width);
}

我希望能够基本上这样写:

let inst = match opcode {
    CMP_REG_U8.code => CMP_REG_U8,
    CMP_REG_U32.code => CMP_REG_U32,
    _ => INVALID,
};

这样我就不必在匹配语句中输入幻数,也不必重复它们。

我意识到我可以使用守卫:

let inst = match opcode {
    x if x == CMP_REG_U8.code => CMP_REG_U8,
    x if x == CMP_REG_U32.code => CMP_REG_U32,
    _ => INVALID,
};

...但这似乎有点冗长,我担心如何说服编译器给我跳转 table 实现。 (想象一下,我们不只是返回指令,而是想执行一段代码并能够引用相关数据,例如 inst.width,与将值硬编码为文字相比,没有性能损失。)

所以最重要的是,是否可以使用“文字化”常量的宏来解决这个问题?

let inst = match opcode {
    as_literal!(CMP_REG_U8.code) => exec_op(CMP_REG_U8),
    as_literal!(CMP_REG_U32.code) => exec_op(CMP_REG_U32),
    _ => INVALID,
};

这样匹配臂将全部是硬编码的整数,这应该使编译器非常容易地内联 exec_op 所做的任何事情,将 width 等识别为该上下文中的常量,并且做一个计算转到或任何去那里。

我听说 Rust 中的宏只允许你做你可以自己输入的事情。但是,我当然可以输入 3 而不是 CMP_REG_U8.code —— 问题是 Rust 是否可以为我做到这一点。需要在编译时解决这个问题,生成的模式才能在匹配中有效。

您可以使用一个宏生成另一个:

macro_rules! define_consts {
    ($(
        $vis: vis const $name: ident : Instruction = Instruction { code: $code: literal, width: $width: literal };
    )*) => {
        $(
            $vis const $name: Instruction = Instruction { code: $code, width: $width };
        )*
        
        macro_rules! as_literal {
            $(($name . code) => { $code });*
        }
    }
}

使用您的常量定义调用它:

define_consts! {
    pub const CMP_REG_U8: Instruction = Instruction { code: 3, width: 3 };
    pub const CMP_REG_U32: Instruction = Instruction { code: 4, width: 6 };
    pub const INVALID: Instruction = Instruction { code: 0, width: 0 };
}

将生成所写的常量和一个宏 as_literal!,它可以满足您的需求:

fn exec_op(inst: Instruction) -> Instruction {
    inst
}

fn main() {
    let opcode = 3u8;
    
    let inst = match opcode {
        as_literal!(CMP_REG_U8.code) => exec_op(CMP_REG_U8),
        as_literal!(CMP_REG_U32.code) => exec_op(CMP_REG_U32),
        _ => INVALID,
    };

    println!("inst.code: {} inst.width: {}", inst.code, inst.width); // 3
}

虽然我不确定这有多有价值!鉴于模式都是文字,使用 if...else if... 而不是 match 最终可能会产生相同的程序集。