繁体   English   中英

宏是否可以采用常量表达式并“内联”它以生成有效的 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,
};

...但这似乎有点冗长,我担心如何说服编译器给我一个跳转表实现。 (想象一下,我们不只是返回指令,而是希望执行一段代码并能够引用相关数据,如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所做的任何exec_op ,将width等识别为该上下文中的常量,并执行计算的 goto 或其他任何操作。

我听说 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最终可能会产生相同的程序集。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM