Skip to main content

find_attr

Macro find_attr 

Source
macro_rules! find_attr {
    ($tcx: expr, crate, $pattern: pat $(if $guard: expr)?) => { ... };
    ($tcx: expr, crate, $pattern: pat $(if $guard: expr)? => $e: expr) => { ... };
    ($tcx: expr, $def_id: expr, $pattern: pat $(if $guard: expr)?) => { ... };
    ($tcx: expr, $def_id: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => { ... };
    ($attributes_list: expr, $pattern: pat $(if $guard: expr)?) => { ... };
    ($attributes_list: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => { ... };
}
Expand description

Finds attributes in sequences of attributes by pattern matching.

A little like matches but for attributes.

// 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. Often these are available through the tcx.

As a convenience, this macro can do that for you!

Instead of providing an attribute list, provide the tcx and a DefId.

find_attr!(tcx, def_id, <pattern>)

Another common case is finding attributes applied to the root of the current crate. For that, use the shortcut:

find_attr!(tcx, crate, <pattern>)