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 Item::attrs to get the attributes of an item if available #1281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kraktus
Copy link

@kraktus kraktus commented Dec 22, 2022

Hello, thanks for the crate!

When looking at Item I was surprised not to find APIs that ease its use, without having to pattern-match every time, like for rustc_hir::ItemKind. This PR add a convenience method to have an easy access to the attributes of an item.

I don't know the policy about new APIs, so feel free to close if deemed out-of-scope. About the feature-flag to use and the documentation I can fix what's needed, when for parsing as a first guess.

As a side note test_expr_size and test_type_size fail on arm64 (M1).

@dtolnay dtolnay linked an issue Jan 23, 2023 that may be closed by this pull request
@@ -347,6 +347,32 @@ impl Item {
_ => unreachable!(),
}
}

#[cfg(feature = "parsing")]
pub fn attrs(&self) -> Option<&Vec<Attribute>> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a similar implementation of this in a crate here: parse_attributes.rs#L336

I was able to have it simply return &[Attribute], and for Item::Verbatim it just returns &[]. Not sure if that is better or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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