macros - 如何匹配 Rust 宏中表达式的类型?

标签 macros rust

这只是伪代码:

macro_rules! attribute {
    $e: expr<f32> => { /* magical float stuff */ };
    $e: expr<i64> => { /* mystical int stuff */ };
};

我希望有一个不同的扩展宏,具体取决于我传递给宏的类型。

这就是它在 C++ 中的工作方式

template <typename T>
struct Attribute{ void operator(T)() {} };

template <>
struct Attribute<float> {
    void operator(float)(float) { /* magical float stuff */ }
};

template <>
struct Attribute<long> {
    void operator()(long) { /* mystical int stuff */ }
}

最佳答案

Rust 宏无法做到这一点。宏在句法级别运行,而不是在语义级别运行。这意味着尽管编译器知道它有一个表达式(语法),但在展开宏时它并不知道表达式值(语义)的类型。

解决方法是将预期的类型传递给宏:

macro_rules! attribute {
    ($e:expr, f32) => { /* magical float stuff */ };
    ($e:expr, i64) => { /* mystical int stuff */ };
}

fn main() {
    attribute!(2 + 2, i64);
}

或者,更简单地说,定义多个宏。


如果你想根据表达式的类型进行静态(编译时)分派(dispatch),你可以使用特征。使用必要的方法定义特征,然后为您需要的类型实现特征。如果 impl block 与特征定义在同一个 crate 中,则可以为任何 类型(包括来自其他库的原语和类型)实现特征。

trait Attribute {
    fn process(&self);
}

impl Attribute for f32 {
    fn process(&self) { /* TODO */ }
}

impl Attribute for i64 {
    fn process(&self) { /* TODO */ }
}

macro_rules! attribute {
    ($e:expr) => { Attribute::process(&$e) };
}

fn main() {
    attribute!(2 + 2);
}

注意:您也可以在宏的主体中编写 $e.process(),但这样宏可能会调用不相关的 process 方法。

关于macros - 如何匹配 Rust 宏中表达式的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58099243/

相关文章:

将一行函数转换为c中的宏

c - 如何编写一些代码来判断变量是否有符号

concurrency - Rust 中的进程隔离

从包装宏内调用 Malloc

c - C中for循环被抽象为Macro时的无限循环

rust - 如何从两个 DateTime/NaiveDateTime 获取持续时间?

rust - 如果累加器既不是复制类型也不是可变的,是否可以在 Rust 中使用 fold

generics - 生命周期子类型和泛型 : "lifetime parameter not allowed" error

iterator - 在 Rust 中,向量是迭代器吗?

c++ - 如何使用算术生成预处理器定义?