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

How do I match the type of an expression in a Rust macro?

这只是伪代码:

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);
}

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


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

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 方法。

如前所述,您不能根据 expr 的类型进行不同的扩展。但作为一种解决方法,您可以使用 any module 并尝试从 Any 特征向下转换:

use std::any::Any;

macro_rules! attribute {
    ( $e:expr ) => {
        if let Some(f) = (&$e as &Any).downcast_ref::<f32>() {
            println!("`{}` is f32.", f);
        } else if let Some(f) = (&$e as &Any).downcast_ref::<f64>() {
            println!("`{}` is f64.", f);
        } else {
            println!("I dunno what is `{:?}` :(", $e);
        }
    };
}

fn main() {
    attribute!(0f32);
    attribute!(0f64);
    attribute!(0);
}

显示:

`0` is f32.
`0` is f64.
I dunno what is `0` :(

虽然这里的所有答案都是正确的,但我想提供一个更类似于您的 C++ 版本的答案。 Rust 提供了自己版本的模板、泛型,它们可以像使用模板一样使用。 因此,要为某些类型定义一个结构并实现函数:

struct Attribute<T> {
    value: T,
}

impl Attribute<u32> {
    fn call(&self) {
        println!("{} is a u32", self.value);
    }
}

impl Attribute<f64> {
    fn call(&self) {
        println!("{} is a f64", self.value);
    }
}

impl Attribute<String> {
    fn call(&self) {
        println!("{} is a string", self.value);
    }
}

我们会这样使用它:

fn main() {
    let a = Attribute{
        value: 5_u32
    };


    a.call();
}

或者像这样:

Attribute{value: 6.5}.call()

遗憾的是,Rust 在其稳定版本中不提供 () 运算符重载。您仍然可以定义一个宏来完成这项工作:

macro_rules! attribute {
    ( $e:expr ) => {
        Attribute{value: $e}.call(); 
    };
}

并这样使用它:

attribute!("Hello World!".to_string());

我建议使用 中所示的基于特征的方法,因为它不使用结构,而是使用特征,这被认为是更好的做法。这个答案在很多情况下可能仍然有用。