#macro-attributes #macro #attributes

macro qualifiers

Conditionally add qualifiers to item definitions

1 unstable release

0.1.0 Jul 8, 2025

#2378 in Rust patterns

BlueOak-1.0.0

19KB
515 lines

qualifiers

Provides a #[qualifiers] attribute macro to apply pub, const, async, unsafe, and other "qualifiers" to items.

This can be especially useful in tandem with #[cfg_attr].


lib.rs:

[macro@qualifiers] is a lightweight attribute to apply qualifiers to items.

use qualifiers::qualifiers;

// I'm public and `const` now!
#[qualifiers(pub const)]
fn multiply(x: i32, y: i32) -> i32 {
    x * y
}

// Every type of item is supported.
#[qualifiers(unsafe)]
pub trait Greeter {
    #[qualifiers(async)]
    fn greet(&self);
}

Qualifiers are any type of keyword that precedes an item definition. This crate currently supports the ones below:

  • pub, pub(crate), pub(in super), etc.
  • const
  • async
  • unsafe, safe
  • extern "C", etc.

This may not seem very useful on its own, but it becomes powerful when used in combination with cfg_attr.

#[cfg_attr(feature = "const", qualifiers(const))]
fn maybe_const() { /* ... */ }

Similar crates

const_fn provides an attribute to apply const to functions. It has a built-in feature to do this conditionally based on the detected compiler version. Like this crate, it has no dependencies.

qualifier_attr provides an attribute very similar to the one in this crate, but it may not be as lightweight due to its dependency on proc-macro2, quote, and syn. However, it supports the default keyword and applying qualifiers to struct fields, features this crate currently lacks.

Minimum supported Rust version

The MSRV is currently 1.56.

This may change between minor releases.

License

This crate is licensed under the Blue Oak Model License 1.0.0.

No runtime deps