Crates.io | apply_attr |
lib.rs | apply_attr |
version | 0.2.4 |
source | src |
created_at | 2016-05-17 23:41:08.622118 |
updated_at | 2017-12-03 16:39:09.23768 |
description | A syntax extension providing higher-order attributes to Rust. |
homepage | https://github.com/regexident/apply_attr |
repository | https://github.com/regexident/apply_attr |
max_upload_size | |
id | 5097 |
size | 43,033 |
A syntax extension providing higher-order attributes to Rust.
Sometimes it would be desirable to be able to apply certain attributes to all items in a scope (mod
, trait
or impl
). The apply_attr
crate aims to provide a versatile API for this.
Possible use-cases would be:
xyz
use #[derive(PartialEq)]
.impl
with #[inline(never)]
(for profiling, e.g.).Add the most recent version of apply_attr
to your dependencies in your project's Cargo.toml
.
Then add …
#![feature(custom_attribute)]
#![feature(plugin)]
#![plugin(apply_attr)]
… to your crate's root file (e.g. lib.rs
, main.rs
).
Once that's done you're ready to play!
#![feature(custom_attribute)]
#![feature(plugin)]
#![plugin(apply_attr)]
// Make all top-level structs as well as those
// within top-level mods implement `PartialEq`:
#![apply_attr(to(structs, mods(structs)), as_default(derive(PartialEq)))]
pub struct Foo;
mod Bar {
pub struct Baz;
// ...
}
// Disable inlining when `no_inline` feature is present:
#[cfg_attr(feature = "no_inline", apply_attr(to(fns), as_override(inline(never))))]
impl Blee {
fn foo(&self) { ... }
fn bar(&self) { ... }
fn baz(&self) { ... }
fn blee(&self) { ... }
}
fn main() {
Foo == Foo;
Bar::Baz == Bar::Baz;
}
The apply_attr
syntax extension provides a single higher-order attribute,
conveniently named apply_attr
expecting two arguments:
to(...)
(with ...
being a list of zero or more selectors).as_default(...)
or as_override(...)
(with ...
being a list of zero or more attributes).Resulting either of:
#[apply_attr(to(...), as_default(...))]
#[apply_attr(to(...), as_override(...))]
The first argument (to(...)
) accepts a nested list of item selectors.
Selectors behave similar to CSS selectors:
As such a CSS selector like div > span, p
would translate to to(div(span), p)
.
The following selectors are supported:
consts
crates
def_impls
enums
fgn_mods
fns
impls
macros
mods
statics
structs
traits
types
uses
With the following ones allowing for nesting:
mods(...)
impls(...)
traits(...)
Nested selectors denote direct ancestry equivalent to CSS's outer > inner
path operator.
Attributes can either be applied as using as_default(...)
, in which case …
#[apply_attr(to(fns), as_default(inline(never)))]
impl Foo {
#[inline(always)]
fn foo() { ... }
}
… will be turned into …
impl Foo {
#[inline(always)]
fn foo() { ... }
}
… upon completion.
Or using as_override(...)
, in which case …
#[apply_attr(to(fns), as_override(inline(never)))]
impl Foo {
#[inline(always)]
fn foo() { ... }
}
… will be turned into …
impl Foo {
#[inline(never)]
fn foo() { ... }
}
… upon completion.
To see how the attributes were applied compile your crate using this (requires nightly
):
cargo rustc -- -Z unstable-options --pretty=expanded
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
See also the list of contributors who participated in this project.
This project is licensed under the MPL-2.0 – see the LICENSE.md file for details.