dtolnay / dtolnay/syn

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

Open
#866 1 comment 2 reactions 0 assignees Claimed by @kraktus View on GitHub
Dominant language
Rust
Stars
3.4k
Forks
374
Avg merge
1d 5h
Merged PRs (30d)
2

Description

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

```rust
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.