可以在结构声明之后*派生属性吗? [英] Possible to derive attributes *after* a struct declaration?

查看:88
本文介绍了可以在结构声明之后*派生属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用宏来扩展基本结构:

I'm using a macro to extend a primitive struct:

pub struct MyTypedNumber(pub u32);
struct_impl_my_features!(MyTypedNumber);

struct_impl_my_features宏可以实现功能& MyTypedNumber的特征,但是在某些情况下,使用#[derive(PartialEq, Eq)]是有用的-例如.

The struct_impl_my_features macro can implement functions & traits for MyTypedNumber, however there is a case where its useful to use #[derive(PartialEq, Eq)] - for example.

在声明该结构后是否可以使用#[derive(...)]?

Is it possible to use #[derive(...)] after the struct is already declared?

另一种方法是将结构定义作为item参数传递给宏:

An alternative is to pass in the struct definition as an item argument to a macro:

struct_impl_my_features!(
    pub struct MyTypedNumber(pub u32);,
    MyTypedNumber
);

这很有效,尽管它很笨重,并且意味着声明和宏扩展必须在一起,但它可能是最好的选择.

This works, so it may be the best option, although it is rather clunky and means the declaration and macro extension must be together.

请参见此完整示例,该宏称为struct_bitflag_impl(第二个示例).

See this complete example, the macro is called struct_bitflag_impl (second example).

我通过手动实现PartialEqEq来解决此问题,但是遇到了Rust需要 #[derive(...)]用作match语句中的常量的情况:

I worked around this by manually implementing PartialEq and Eq, however I ran into a case where Rust needs #[derive(...)] to be used as constants within match statement:

= warning: this was previously accepted by the compiler but is being phased out;
  it will become a hard error in a future release!
= note: for more information,
  see RFC 1445 <https://github.com/rust-lang/rfcs/pull/1445>

推荐答案

您提供的完整示例"链接确实显示了具有宏前缀属性的示例(请参见第二个宏). /p>

The "complete example" link you provide does show an example of having the macro prefix attributes (see the second macro).

    #[derive(PartialEq, Eq, Copy, Clone, Debug)]
    $struct_p_def

但是,如果相反,您希望能够为每个结构提供派生属性(例如,仅某些结构需要派生PartialEq),则可以在第二个 example—属性被视为item宏形式的一部分.例如

However, if instead you want to be able to provide derive-attributes per struct (e.g., only some of your structs need to derive PartialEq), you can pass the derive expression in the first part of your second struct_impl_my_features! example—the attributes are considered part of the item macro form. E.g.,

struct_impl_my_features!(
    #[derive(PartialEq, Eq)]
    pub struct MyTypedNumber(pub u32);,
    MyTypedNumber
);

更新

对不起,对于您的主要问题,我没有答案.据我所知,这是不可能的.但是,如果您最关心的是笨拙,并且结构都具有相似的形式,则可以通过将其添加到宏的顶部来使宏调用更好:

Sorry, I don't have an answer to your main question; as far as I know, it is not possible. However, if your primary concern is the clunkiness, and if your structs are all of similar form, you could make your macro call nicer by adding this to the top of your macro:

($x:ident ( $($v:tt)* ) ) => {
    struct_impl_my_features!(pub struct $x( $($v)* );, $x)
};

然后像这样调用它:

struct_impl_my_features!(MyTypedNumber(pub u32));

这篇关于可以在结构声明之后*派生属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆