1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
//! This module defines helpers for defining singletons and associated enum types. A singleton is //! a type with one possible value. It is used mainly for a type level programming purposes. /// Defines singleton types. For the following input: /// ```compile_fail /// define_singletons!{ /// /// A Foo! /// Foo, /// /// A Bar! /// Bar, /// } /// ``` /// /// It expands to: /// /// ``` /// #[allow(missing_docs)] /// #[derive(Copy, Clone, Debug)] /// #[doc = r###"A Foo!"###] /// pub struct Foo; /// impl Default for Foo { /// fn default() -> Self { /// Self /// } /// } /// #[allow(missing_docs)] /// #[derive(Copy, Clone, Debug)] /// #[doc = r###"A Bar!"###] /// pub struct Bar; /// impl Default for Bar { /// fn default() -> Self { /// Self /// } /// } /// /// ``` #[macro_export] macro_rules! define_singletons { ( $( $(#$meta:tt)* $name:ident ),* $(,)? ) => {$( #[allow(missing_docs)] #[derive(Copy,Clone,Debug,PartialEq,Eq)] $(#$meta)* pub struct $name; impl Default for $name { fn default() -> Self { Self } } )*} } /// Defines an associated enum type for predefined singletons. /// /// For the following input: /// ```compile_fail /// define_singleton_enum!{ /// MyEnum { /// /// A Foo! /// Foo, /// /// A Bar! /// Bar, /// } /// } /// ``` /// /// It expands to: /// /// ```compile_fail /// #[allow(missing_docs)] /// #[derive(Copy, Clone, Debug)] /// pub enum MyEnum { /// #[doc = r###"A Foo!"###] /// Foo, /// #[doc = r###"A Bar!"###] /// Bar, /// } /// impl From<Foo> for MyEnum { /// fn from(_: Foo) -> Self { /// Self::Foo /// } /// } /// impl From<PhantomData<Foo>> for MyEnum { /// fn from(_: PhantomData<Foo>) -> Self { /// Self::Foo /// } /// } /// impl From<Bar> for MyEnum { /// fn from(_: Bar) -> Self { /// Self::Bar /// } /// } /// impl From<PhantomData<Bar>> for MyEnum { /// fn from(_: PhantomData<Bar>) -> Self { /// Self::Bar /// } /// } /// ``` #[macro_export] macro_rules! define_singleton_enum_from { ( $(#$meta:tt)* $name:ident { $( $(#$field_meta:tt)* $field:ident ),* $(,)? } ) => { #[allow(missing_docs)] #[derive(Copy,Clone,Debug,PartialEq,Eq)] $(#$meta)* pub enum $name { $( $(#$field_meta)* $field ),* } $( impl From<$field> for $name { fn from(_:$field) -> Self { Self::$field } } impl From<PhantomData<$field>> for $name { fn from(_:PhantomData<$field>) -> Self { Self::$field } } )* } } /// Defines singletons and an associated enum type. /// It expands to the same as `define_singletons` and `define_singleton_enum_from`. #[macro_export] macro_rules! define_singleton_enum { ( $(#$meta:tt)* $name:ident { $( $(#$field_meta:tt)* $field:ident ),* $(,)? } ) => { $crate::define_singletons! { $($(#$field_meta)* $field),* } $crate::define_singleton_enum_from! { $(#$meta)* $name {$($(#$field_meta)* $field),*}} } }