Crate macro_attr [−] [src]
This crate provides the macro_attr!
macro that enables the use of custom, macro-based attributes and derivations. Supercedes the custom_derive
crate.
Compatibility
macro-attr
is compatible with Rust 1.2 and higher.
Quick Example
To use it, make sure you link to the crate like so:
#[macro_use] extern crate macro_attr;
The macro_attr!
macro should be used to wrap an entire single item (enum
, struct
, etc.) declaration, including its attributes (both derive
and others). All attributes and derivations which whose names end with !
will be assumed to be implemented by macros, and treated accordingly.
For example:
#[macro_use] extern crate macro_attr; // Define some traits to be derived. trait TypeName { fn type_name() -> &'static str; } trait ReprType { type Repr; } // Define macros which derive implementations of these macros. macro_rules! TypeName { // We can support any kind of item we want. (() $(pub)* enum $name:ident $($tail:tt)*) => { TypeName! { @impl $name } }; (() $(pub)* struct $name:ident $($tail:tt)*) => { TypeName! { @impl $name } }; // Inner rule to cut down on repetition. (@impl $name:ident) => { impl TypeName for $name { fn type_name() -> &'static str { stringify!($name) } } }; } macro_rules! ReprType { // Note that we use a "derivation argument" here for the `$repr` type. (($repr:ty) $(pub)* enum $name:ident $($tail:tt)*) => { impl ReprType for $name { type Repr = $repr; } }; } // Here is a macro that *modifies* the item. macro_rules! rename_to { ( ($new_name:ident), then $cb:tt, $(#[$($attrs:tt)*])* enum $_old_name:ident $($tail:tt)* ) => { macro_attr_callback! { $cb, $(#[$($attrs)*])* enum $new_name $($tail)* } }; } macro_attr! { #[allow(dead_code)] #[derive(Clone, Copy, Debug, ReprType!(u8), TypeName!)] #[rename_to!(Bar)] #[repr(u8)] enum Foo { A, B } } fn main() { let bar = Bar::B; let v = bar as <Bar as ReprType>::Repr; let msg = format!("{}: {:?} ({:?})", Bar::type_name(), bar, v); assert_eq!(msg, "Bar: B (1)"); }
Macros
macro_attr |
When given an item definition, including its attributes, this macro parses said attributes and dispatches any attributes or derivations suffixed with |
macro_attr_callback |
This macro invokes a "callback" macro, merging arguments together. |