Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attrs() and attrs_mut() getters to syn::Item #866

Open
Robbepop opened this issue Jul 1, 2020 · 1 comment · May be fixed by #1281
Open

Add attrs() and attrs_mut() getters to syn::Item #866

Robbepop opened this issue Jul 1, 2020 · 1 comment · May be fixed by #1281

Comments

@Robbepop
Copy link

Robbepop commented Jul 1, 2020

While developing certain proc macros using syn I found getters that return the attribute slice for a generic syn::Item to be missing. The implementation is straight forward and simple, however, daunting:

Example through some extension trait

impl Attrs for syn::Item {
    fn attrs(&self) -> &[syn::Attribute] {
        use syn::Item;
        match self {
            Item::Const(syn::ItemConst { attrs, .. })
            | Item::Enum(syn::ItemEnum { attrs, .. })
            | Item::ExternCrate(syn::ItemExternCrate { attrs, .. })
            | Item::Fn(syn::ItemFn { attrs, .. })
            | Item::ForeignMod(syn::ItemForeignMod { attrs, .. })
            | Item::Impl(syn::ItemImpl { attrs, .. })
            | Item::Macro(syn::ItemMacro { attrs, .. })
            | Item::Macro2(syn::ItemMacro2 { attrs, .. })
            | Item::Mod(syn::ItemMod { attrs, .. })
            | Item::Static(syn::ItemStatic { attrs, .. })
            | Item::Struct(syn::ItemStruct { attrs, .. })
            | Item::Trait(syn::ItemTrait { attrs, .. })
            | Item::TraitAlias(syn::ItemTraitAlias { attrs, .. })
            | Item::Type(syn::ItemType { attrs, .. })
            | Item::Union(syn::ItemUnion { attrs, .. })
            | Item::Use(syn::ItemUse { attrs, .. }) => attrs,
            _ => &[],
        }
    }
}

Note that a getter for this would also have the benefit of such dependent crates to not care if syn ever adds new variants. If syn provided this dependencies could simply make use of syn handling the update.

Would you accept a PR that adds these getters? If so I'd be happy to file a PR.

@dtolnay
Copy link
Owner

dtolnay commented Jul 23, 2020

Seems reasonable. Please send a PR.

Repository owner deleted a comment from Robbepop Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants