【问题标题】:Using macros, how to get unique names for struct fields?使用宏,如何获取结构字段的唯一名称?
【发布时间】:2018-04-29 03:48:03
【问题描述】:

假设我有一些宏被这样调用:

my_macro!(Blah, (a, b, c));

它会输出如下内容:

struct Blah {
    a: i32,
    b: i32,
    c: i32
}
impl Blah {
    fn foo() -> i32 {
        a + b + c
    }
}

(人工示例)

这些字段将是结构私有的,但我需要允许重新定义。所以,输入

my_macro!(Blah, (a, b, c, a));

会生成类似的东西:

struct Blah {
    a1: i32,
    b: i32,
    c: i32,
    a2: i32
}
impl Blah {
    fn foo() -> i32 {
        a1 + b + c + a2
    }
}

命名方案不需要遵循任何逻辑模式。

这可能吗?

【问题讨论】:

    标签: macros rust


    【解决方案1】:

    我的mashup crate 为您提供了一种将my_macro!(Blah, (a, b, c, a)) 扩展到字段x_axx_bxxx_cxxxx_d 的方法,前提是该命名约定适合您。我们为每个字段添加一个额外的x,后跟一个下划线,然后是原始字段名称,这样就不会出现任何字段名称冲突的情况。这种方法适用于任何 >= 1.15.0 的 Rust 版本。


    #[macro_use]
    extern crate mashup;
    
    macro_rules! my_macro {
        ($name:ident, ($($field:ident),*)) => {
            my_macro_helper!($name (x) () $($field)*);
        };
    }
    
    macro_rules! my_macro_helper {
        // In the recursive case: append another `x` into our prefix.
        ($name:ident ($($prefix:tt)*) ($($past:tt)*) $next:ident $($rest:ident)*) => {
            my_macro_helper!($name ($($prefix)* x) ($($past)* [$($prefix)* _ $next]) $($rest)*);
        };
    
        // When there are no fields remaining.
        ($name:ident ($($prefix:tt)*) ($([$($field:tt)*])*)) => {
            // Use mashup to define a substitution macro `m!` that replaces every
            // occurrence of the tokens `"concat" $($field)*` in its input with the
            // resulting concatenated identifier.
            mashup! {
                $(
                    m["concat" $($field)*] = $($field)*;
                )*
            }
    
            // Invoke the substitution macro to build a struct and foo method.
            // This expands to:
            //
            //     pub struct Blah {
            //         x_a: i32,
            //         xx_b: i32,
            //         xxx_c: i32,
            //         xxxx_a: i32,
            //     }
            //
            //     impl Blah {
            //         pub fn foo(&self) -> i32 {
            //             0 + self.x_a + self.xx_b + self.xxx_c + self.xxxx_a
            //         }
            //     }
            m! {
                pub struct $name {
                    $(
                        "concat" $($field)*: i32,
                    )*
                }
    
                impl $name {
                    pub fn foo(&self) -> i32 {
                        0 $(
                            + self."concat" $($field)*
                        )*
                    }
                }
            }
        };
    }
    
    my_macro!(Blah, (a, b, c, a));
    
    fn main() {}
    

    【讨论】:

      【解决方案2】:

      不使用编译器插件,不,我不相信这是可能的。两个原因:

      1. 您不能构造标识符。有concat_idents!,但由于宏的扩展方式,在这种情况下没用。

      2. 您不能进行非文字比较。也就是说,无法计算出之前已经看到过a 一次的宏。

      你能得到的最接近的方法是用一个固定的标识符列表直接替换所有提供的标识符,但这可能不是你想要的;在这种情况下,只需指定需要 4 个字段并生成一个固定大小的数组 [i32; 4] 会更容易。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-11
        • 1970-01-01
        • 2017-04-13
        • 2016-09-05
        • 2011-03-06
        • 2021-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多