#proc-macro #pub #extern #conditional #async #cfg-attr

macro qualifier_attr

Procedural macro attributes for adding "qualifiers" (pub, async, unsafe, const, extern "C", ...) to various items

10 releases

0.2.2 Aug 13, 2023
0.2.1 Aug 13, 2023
0.1.6 Apr 4, 2023
0.1.2 Mar 29, 2023

#283 in Rust patterns

Download history 22615/week @ 2024-01-05 19355/week @ 2024-01-12 24989/week @ 2024-01-19 19444/week @ 2024-01-26 21477/week @ 2024-02-02 15839/week @ 2024-02-09 18710/week @ 2024-02-16 20719/week @ 2024-02-23 20876/week @ 2024-03-01 22293/week @ 2024-03-08 23654/week @ 2024-03-15 22599/week @ 2024-03-22 39683/week @ 2024-03-29 59453/week @ 2024-04-05 31716/week @ 2024-04-12 25244/week @ 2024-04-19

160,100 downloads per month
Used in 730 crates (7 directly)

MIT/Apache

54KB
1.5K SLoC

qualifier_attr

Latest Version Downloads Documentation License Dependency Status

Procedural macro attributes for adding "qualifiers" to various items.

At the moment, the crate supports the following "qualifiers":

  • pub, pub(crate), etc. - visibility and restriction
  • default - default implementations (a feature of specialization)
  • async - asynchronous code, e.g. async fn
  • unsafe - unsafe code, e.g. unsafe fn, unsafe trait
  • const - code that may run at compile time, e.g. const fn
  • extern "ABI" - specifying an ABI, e.g. extern "C" fn

Limitations

  • It seems that rust-analyzer will sometimes complain when the attribute is used with modules.

Examples

#[macro_use]
extern crate qualifier_attr;

// We can add a qualifier to a function
// with an attribute.
#[qualifiers(const)]
fn const_fn() -> u32 {
    42
}

const CONST_RES: u32 = const_fn();

// It's not so impressive on its own,
// but with `cfg_attr`, it can be conditional.
#[cfg_attr(feature = "extern_c", no_mangle, qualifiers(pub, extern "C"))]
fn extern_c_fn() -> u32 {
    42
}

// It even works with types, imports, and more!
mod foo {
    #[qualifiers(pub)]
    struct Foo {
        x: i32,
        y: i32,
    }
}

#[qualifiers(pub)]
use foo::Foo;

// Traits and implementations too!?
#[cfg_attr(feature = "unsafe_quux", qualifiers(unsafe))]
trait Quux {
    fn quux_the_thing();
}

#[cfg_attr(feature = "unsafe_quux", qualifiers(unsafe))]
impl Quux for Foo {
    fn quux_the_thing() {
        println!("The thing was quuxed.");
    }
}

// You can add qualifiers to the fields of a
// struct as well with this special attribute.
#[field_qualifiers(x(pub), y(pub))]
struct Point2 {
    x: i32,
    y: i32,
}

#[field_qualifiers(_0(pub), _1(pub), _2(pub))]
struct Point3(i32, i32, i32);

Learn more about cfg_attr here.

Note on legacy attributes

Before version 0.2.0, this crate provided a separate attribute for each kind of item. While this should generally be a small win for compile times, it is generally not justified by the additional complexity. The legacy attributes are still available behind the default legacy_attrs feature flag, but their use is currently discouraged. In order to disable the legacy attributes, add the following to your Cargo.toml:

[dependencies]
qualifier_attr = { version = "0.2", default-features = false }

Similar crates

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~320–770KB
~18K SLoC