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

vnjpjtjt  于 12个月前  发布在  其他
关注(0)|答案(4)|浏览(86)

我想创建一个setter/getter函数对,其中名称是基于共享组件自动生成的,但我找不到任何宏规则生成新名称的示例。
有没有办法生成像fn get_$iden()SomeEnum::XX_GET_$enum_iden这样的代码?

ujv3wf0j

ujv3wf0j1#

如果你使用的是Rust >= 1.31.0,我推荐使用我的paste crate,它提供了一种稳定的方式来在宏中创建连接的标识符。

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

字符串

dy2hfwbg

dy2hfwbg2#

我的mashup crate提供了一种稳定的方式来创建新的标识符,可以与任何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 });

字符串

3qpi33ja

3qpi33ja3#

不,不是Rust 1.22。
如果您可以使用夜间构建…
是:concat_idents!(get_, $iden)等将允许您创建新的标识符。
但是不行:解析器并不允许在任何地方调用宏,因此您可能想在许多地方这样做都不起作用。在这种情况下,你只能靠自己了。例如,fn concat_idents!(get_, $iden)(…) { … }就不能工作。

y1aodyip

y1aodyip4#

有一个鲜为人知的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 }
}

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

相关问题