diff options
| author | Jana Dönszelmann <jana@donsz.nl> | 2025-02-12 15:04:26 +0100 |
|---|---|---|
| committer | Jana Dönszelmann <jana@donsz.nl> | 2025-03-07 18:05:42 +0100 |
| commit | 4203e9c56d8aa6cdfe5f5a60bd510917b84188c7 (patch) | |
| tree | 27b22eb6210dc31354ed33215e3aded6b280b619 /compiler/rustc_attr_data_structures/src/lib.rs | |
| parent | 03eb45452305f2d52348279d0caa5fc1f12c438d (diff) | |
| download | rust-4203e9c56d8aa6cdfe5f5a60bd510917b84188c7.tar.gz rust-4203e9c56d8aa6cdfe5f5a60bd510917b84188c7.zip | |
depend more on attr_data_structures and move find_attr! there
Diffstat (limited to 'compiler/rustc_attr_data_structures/src/lib.rs')
| -rw-r--r-- | compiler/rustc_attr_data_structures/src/lib.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/compiler/rustc_attr_data_structures/src/lib.rs b/compiler/rustc_attr_data_structures/src/lib.rs index e4bb459e6df..f4986d1d187 100644 --- a/compiler/rustc_attr_data_structures/src/lib.rs +++ b/compiler/rustc_attr_data_structures/src/lib.rs @@ -149,3 +149,47 @@ print_tup!(A B C D E F G H); print_skip!(Span, ()); print_disp!(Symbol, u16, bool, NonZero<u32>); print_debug!(UintTy, IntTy, Align, AttrStyle, CommentKind, Transparency); + +/// Finds attributes in sequences of attributes by pattern matching. +/// +/// A little like `matches` but for attributes. +/// +/// ```rust,ignore (illustrative) +/// // finds the repr attribute +/// if let Some(r) = find_attr!(attrs, AttributeKind::Repr(r) => r) { +/// +/// } +/// +/// // checks if one has matched +/// if find_attr!(attrs, AttributeKind::Repr(_)) { +/// +/// } +/// ``` +/// +/// Often this requires you to first end up with a list of attributes. +/// A common way to get those is through `tcx.get_all_attrs(did)` +#[macro_export] +macro_rules! find_attr { + ($attributes_list: expr, $pattern: pat $(if $guard: expr)?) => {{ + $crate::find_attr!($attributes_list, $pattern $(if $guard)? => ()).is_some() + }}; + + ($attributes_list: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => {{ + fn check_attribute_iterator<'a>(_: &'_ impl IntoIterator<Item = &'a rustc_hir::Attribute>) {} + check_attribute_iterator(&$attributes_list); + + let find_attribute = |iter| { + for i in $attributes_list { + match i { + rustc_hir::Attribute::Parsed($pattern) $(if $guard)? => { + return Some($e); + } + _ => {} + } + } + + None + }; + find_attribute($attributes_list) + }}; +} |
