derive_alias/
lib.rs

1//! Provides a way to alias mutliple derives as one:
2//! ```rust
3//! use derive_alias::derive_alias;
4//!
5//! // Generates a macro (`derive_cmp`) that will attach the listed derives to a given item
6//! derive_alias! {
7//!     derive_cmp => #[derive(Eq, PartialEq, Ord, PartialOrd)]
8//! }
9//!
10//! // Attach the derives to `Foo`
11//! derive_cmp! { struct Foo; }
12//! ```
13//!
14//! You can create multiple aliases at a time:
15//! ```rust
16//! # use derive_alias::derive_alias;
17//!
18//! derive_alias! {
19//!     derive_cmp => #[derive(Eq, PartialEq, Ord, PartialOrd)],
20//!     derive_other => #[derive(Copy, Clone)]
21//! }
22//!
23//! derive_cmp! { struct Foo; }
24//! derive_other! { struct Bar; }
25//! ```
26
27/// Refer to the [crate level documentation](crate) for details.
28#[macro_export]
29macro_rules! derive_alias {
30    ($($name:ident => #[derive($($derive:ident),*)] $(,)?)*) => {
31        $(
32            macro_rules! $name {
33                ($i:item) => {
34                    #[derive($($derive),*)]
35                    $i
36                }
37            }
38        )*
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use std::fmt::Debug;
45
46    derive_alias! {
47        derive_one => #[derive(Copy, Clone, Debug)],
48        derive_two => #[derive(Eq, PartialEq, Ord, PartialOrd)]
49    }
50
51    derive_one! {
52        struct Foo;
53    }
54
55    derive_two! {
56        struct Bar;
57    }
58
59    #[test]
60    fn a_test() {
61        assert_impl_one(Foo);
62        assert_impl_two(Bar);
63    }
64
65    fn assert_impl_one<T>(_: T)
66    where
67        T: Copy + Clone + Debug,
68    {
69    }
70
71    fn assert_impl_two<T>(_: T)
72    where
73        T: Eq + PartialEq + Ord + PartialOrd,
74    {
75    }
76
77    #[allow(unused_macros)]
78    mod test_trailing_comma {
79        derive_alias! {
80            foo => #[derive()]
81            bar => #[derive()]
82            baz => #[derive()]
83        }
84
85        derive_alias! {
86            foo => #[derive()],
87            bar => #[derive()],
88            baz => #[derive()],
89        }
90    }
91}