macro-attr

This crate provides the macro_attr! macro that enables the use of custom, macro-based attributes and derivations. Supercedes the custom_derive crate.

Links

Compatibility

macro-attr is compatible with Rust 1.2 and higher.

Example

```rust

[macrouse] extern crate macroattr;

// 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.

macrorules! renameto { ( ($newname:ident), then $cb:tt, $(#[$($attrs:tt)*])* enum $oldname:ident $($tail:tt)* ) => { macroattrcallback! { $cb, $(#[$($attrs)*])* enum $newname $($tail)* } }; }

macroattr! { #[allow(deadcode)] #[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 ::Repr; let msg = format!("{}: {:?} ({:?})", Bar::typename(), bar, v); asserteq!(msg, "Bar: B (1)"); } ```

License

Licensed under either of

at your option.

Contribution

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.