Rust宏可以创建新的标识符吗?

时间:2014-12-11 03:44:24

标签: macros code-generation rust

我想创建一个setter / getter函数对,其中名称是根据共享组件自动生成的,但我找不到生成新名称的宏规则的任何示例。

有没有办法生成fn get_$iden()SomeEnum::XX_GET_$enum_iden等代码?

4 个答案:

答案 0 :(得分:6)

我的mashup包提供了一种稳定的方式来创建适用于任何Rust版本> = 1.15.0的新标识符。

#[macro_use]
extern crate mashup;

macro_rules! make_a_struct_and_getters {
    ($name:ident { $($field:ident),* }) => {
        // Define the struct. This expands to:
        //
        //     pub struct S {
        //         a: String,
        //         b: String,
        //         c: String,
        //     }
        pub struct $name {
            $(
                $field: String,
            )*
        }

        // Use mashup to define a substitution macro `m!` that replaces every
        // occurrence of the tokens `"get" $field` in its input with the
        // concatenated identifier `get_ $field`.
        mashup! {
            $(
                m["get" $field] = get_ $field;
            )*
        }

        // Invoke the substitution macro to build an impl block with getters.
        // This expands to:
        //
        //     impl S {
        //         pub fn get_a(&self) -> &str { &self.a }
        //         pub fn get_b(&self) -> &str { &self.b }
        //         pub fn get_c(&self) -> &str { &self.c }
        //     }
        m! {
            impl $name {
                $(
                    pub fn "get" $field(&self) -> &str {
                        &self.$field
                    }
                )*
            }
        }
    }
}

make_a_struct_and_getters!(S { a, b, c });

答案 1 :(得分:6)

如果您使用Rust> = 1.31.0,建议您使用paste条板箱,该条板箱提供了一种在宏中创建级联标识符的稳定方法。

macro_rules! make_a_struct_and_getters {
    ($name:ident { $($field:ident),* }) => {
        // Define the struct. This expands to:
        //
        //     pub struct S {
        //         a: String,
        //         b: String,
        //         c: String,
        //     }
        pub struct $name {
            $(
                $field: String,
            )*
        }

        paste::item! {
            // An impl block with getters. Stuff in [<...>] is concatenated
            // together as one identifier. This expands to:
            //
            //     impl S {
            //         pub fn get_a(&self) -> &str { &self.a }
            //         pub fn get_b(&self) -> &str { &self.b }
            //         pub fn get_c(&self) -> &str { &self.c }
            //     }
            impl $name {
                $(
                    pub fn [<get_ $field>](&self) -> &str {
                        &self.$field
                    }
                )*
            }
        }
    };
}

make_a_struct_and_getters!(S { a, b, c });

答案 2 :(得分:5)

不,不是Rust 1.22。


如果你可以使用夜间建筑......

是:concat_idents!(get_, $iden)等等,您可以创建新的标识符。

但不是:解析器不允许在任何地方进行宏调用,因此您可能尝试执行此操作的许多地方都不起作用。在这种情况下,你很遗憾地靠自己。例如,fn concat_idents!(get_, $iden)(…) { … }将不起作用。

答案 3 :(得分:0)

有一个鲜为人知的 crate gensym,它可以生成唯一的 UUID 名称并将它们作为第一个参数传递给宏,后跟逗号:

macro_rules! gen_fn {
    ($a:ty, $b:ty) => {
        gensym::gensym!{ _gen_fn!{ $a, $b } }
    };
}

macro_rules! _gen_fn {
    ($gensym:ident, $a:ty, $b:ty) => {
        fn $gensym(a: $a, b: $b) {
            unimplemented!()
        }
    };
}

mod test {
    gen_fn!{ u64, u64 }
    gen_fn!{ u64, u64 }
}

如果您只需要一个唯一的名称,而您并不关心它是什么,那么这可能很有用。我用它来解决一个问题,即每次调用宏都需要创建一个唯一的静态来保存单例结构。我不能使用粘贴,因为我没有唯一的标识符,我可以首先将其粘贴在一起。