| 1 | # `macro-attr` |
| 2 | |
| 3 | This crate provides the `macro_attr!` macro that enables the use of custom, macro-based attributes and derivations. Supercedes the `custom_derive` crate. |
| 4 | |
| 5 | **Links** |
| 6 | |
| 7 | * [Latest Release](https://crates.io/crates/macro-attr/) |
| 8 | * [Latest Docs](https://docs.rs/crate/macro-attr/) |
| 9 | * [Repository](https://github.com/DanielKeep/rust-custom-derive) |
| 10 | |
| 11 | ## Compatibility |
| 12 | |
| 13 | `macro-attr` is compatible with Rust 1.2 and higher. |
| 14 | |
| 15 | ## Example |
| 16 | |
| 17 | ```rust |
| 18 | #[macro_use] extern crate macro_attr; |
| 19 | |
| 20 | // Define some traits to be derived. |
| 21 | |
| 22 | trait TypeName { |
| 23 | fn type_name() -> &'static str; |
| 24 | } |
| 25 | |
| 26 | trait ReprType { |
| 27 | type Repr; |
| 28 | } |
| 29 | |
| 30 | // Define macros which derive implementations of these macros. |
| 31 | |
| 32 | macro_rules! TypeName { |
| 33 | // We can support any kind of item we want. |
| 34 | (() $(pub)* enum $name:ident $($tail:tt)*) => { TypeName! { @impl $name } }; |
| 35 | (() $(pub)* struct $name:ident $($tail:tt)*) => { TypeName! { @impl $name } }; |
| 36 | |
| 37 | // Inner rule to cut down on repetition. |
| 38 | (@impl $name:ident) => { |
| 39 | impl TypeName for $name { |
| 40 | fn type_name() -> &'static str { stringify!($name) } |
| 41 | } |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | macro_rules! ReprType { |
| 46 | // Note that we use a "derivation argument" here for the `$repr` type. |
| 47 | (($repr:ty) $(pub)* enum $name:ident $($tail:tt)*) => { |
| 48 | impl ReprType for $name { |
| 49 | type Repr = $repr; |
| 50 | } |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | // Here is a macro that *modifies* the item. |
| 55 | |
| 56 | macro_rules! rename_to { |
| 57 | ( |
| 58 | ($new_name:ident), |
| 59 | then $cb:tt, |
| 60 | $(#[$($attrs:tt)*])* |
| 61 | enum $_old_name:ident $($tail:tt)* |
| 62 | ) => { |
| 63 | macro_attr_callback! { |
| 64 | $cb, |
| 65 | $(#[$($attrs)*])* |
| 66 | enum $new_name $($tail)* |
| 67 | } |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | macro_attr! { |
| 72 | #[allow(dead_code)] |
| 73 | #[derive(Clone, Copy, Debug, ReprType!(u8), TypeName!)] |
| 74 | #[rename_to!(Bar)] |
| 75 | #[repr(u8)] |
| 76 | enum Foo { A, B } |
| 77 | } |
| 78 | |
| 79 | fn main() { |
| 80 | let bar = Bar::B; |
| 81 | let v = bar as <Bar as ReprType>::Repr; |
| 82 | let msg = format!("{}: {:?} ({:?})", Bar::type_name(), bar, v); |
| 83 | assert_eq!(msg, "Bar: B (1)"); |
| 84 | } |
| 85 | ``` |
| 86 | |
| 87 | ## License |
| 88 | |
| 89 | Licensed under either of |
| 90 | |
| 91 | * MIT license (see [LICENSE](LICENSE) or <http://opensource.org/licenses/MIT>) |
| 92 | * Apache License, Version 2.0 (see [LICENSE](LICENSE) or <http://www.apache.org/licenses/LICENSE-2.0>) |
| 93 | |
| 94 | at your option. |
| 95 | |
| 96 | ### Contribution |
| 97 | |
| 98 | Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions. |