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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
#![no_std] #![warn(missing_docs)] //! A macro with two main purposes: //! - attaching static properties to `enum` variants //! - reducing the size of pointers to static records //! //! The advantage in both cases is that the `enum` itself contains no data, and //! can be as small as a byte. //! //! # Example //! //! (More complex enums are also supported. See [`enum_properties`#examples] for details.) //! //! ```rust //! use enum_properties::enum_properties; //! //! struct SolidProperties { //! verts: i32, //! edges: i32, //! faces: i32, //! } //! //! enum_properties! { //! #[derive(Clone, Copy, Debug)] //! enum PlatonicSolid: SolidProperties { //! Tetrahedron { //! verts: 4, //! edges: 6, //! faces: 4, //! }, //! Cube { //! verts: 8, //! edges: 12, //! faces: 6, //! }, //! Octahedron { //! verts: 6, //! edges: 12, //! faces: 8, //! }, //! Dodecahedron { //! verts: 20, //! edges: 30, //! faces: 12, //! }, //! Icosahedron { //! verts: 12, //! edges: 30, //! faces: 20, //! }, //! } //! } //! //! fn main() { //! let cube = PlatonicSolid::Cube; //! assert_eq!(cube.verts - cube.edges + cube.faces, 2); //! } //! ``` //! /// Defines a new `enum` and implements [`Deref`] for it. /// /// The `enum` will [`Deref`] to a variant-specific [`static` item]. /// /// To specify default properties, use the following syntax (inspired by /// [functional update syntax]): /// /// # Examples /// /// ```rust /// use enum_properties::enum_properties; /// /// pub struct EnemyProperties { /// pub health: i32, /// pub is_solid: bool, /// pub is_flying: bool, /// } /// /// const DEFAULT_ENEMY_PROPERTIES: EnemyProperties = EnemyProperties { /// health: 10, /// is_solid: true, /// is_flying: false, /// }; /// /// enum_properties! { /// #[derive(Clone, Copy, PartialEq, Eq, Debug)] /// pub enum EnemyKind: EnemyProperties { /// Skeleton { /// health: 15, /// }, /// Ghost { /// is_solid: false, /// is_flying: true, /// }, /// Bat { /// health: 1, /// is_flying: true, /// }, /// ..DEFAULT_ENEMY_PROPERTIES /// } /// } /// ``` /// [`Deref`]: https://doc.rust-lang.org/std/ops/trait.Deref.html /// [`static` item]: https://doc.rust-lang.org/reference/items/static-items.html /// [functional update syntax]: https://doc.rust-lang.org/reference/expressions/struct-expr.html#functional-update-syntax /// /// Non-unit variants and custom discriminants are supported too, by inserting the static initializer directly after the variant name: /// /// ```rust /// use enum_properties::enum_properties; /// /// pub struct EnemyProperties { /// pub base_health: i32, /// pub is_solid: bool, /// pub is_flying: bool, /// } /// /// const DEFAULT_ENEMY_PROPERTIES: EnemyProperties = EnemyProperties { /// base_health: 10, /// is_solid: true, /// is_flying: false, /// }; /// /// enum_properties! { /// #[derive(Clone, Copy, PartialEq, Eq, Debug)] /// pub enum EnemyKind: EnemyProperties { /// Skeleton { /// base_health: 15, /// } { /// current_health: i32, /// }, /// Ghost { /// is_solid: false, /// is_flying: true, /// } { /// is_spooky: bool, /// }, /// Bats { /// base_health: 1, /// is_flying: true, /// } ( /// u128, // Bat count (but please name this field in an actual program) /// ), /// ..DEFAULT_ENEMY_PROPERTIES /// } /// } /// ``` // #[macro_export] macro_rules! enum_properties { ( $(#[$($m:tt)*])* $public:vis enum $Enum:ident : $EnumProperties:ident { $( $variant:ident { $($field:ident : $value:expr),* $(, .. $default:expr)? $(,)? } $( $(@$is_struct_variant_marker:tt)? { $($struct_variant_content:tt)* } )? $(( $( $(@$tuple_variant_item_marker:tt)? $tuple_variant_item:ty ),* $(,)? ))? $(= $discriminant:expr)? ),* $(,)? } ) => { $(#[$($m)*])* $public enum $Enum { $( $variant $({$($struct_variant_content)*})? $(($($tuple_variant_item),*))? $(= $discriminant)? ),* } impl core::ops::Deref for $Enum { type Target = $EnumProperties; fn deref(&self) -> &Self::Target { match self { $( $Enum::$variant $({ .. $(@$is_struct_variant_marker)?})? $(($(_ $(@$tuple_variant_item_marker)?),*))? => &$EnumProperties { $($field: $value),* $(, .. $default)? } ),* } } } }; ( $(#[$($m:tt)*])* $public:vis enum $Enum:ident : $EnumProperties:ident { $( $variant:ident { $($field:ident : $value:expr),* $(,)? } $( $(@$is_struct_variant_marker:tt)? { $($struct_variant_content:tt)* } )? $(( $( $(@$tuple_variant_item_marker:tt)? $tuple_variant_item:ty ),* $(,)? ))? $(= $discriminant:expr)? ),* , .. $default:expr $(,)? } ) => { $(#[$($m)*])* $public enum $Enum { $( $variant $({$($struct_variant_content)*})? $(($($tuple_variant_item),*))? $(= $discriminant)? ),* } impl core::ops::Deref for $Enum { type Target = $EnumProperties; fn deref(&self) -> &Self::Target { match self { $( $Enum::$variant $({ .. $(@$is_struct_variant_marker)?})? $(($(_ $(@$tuple_variant_item_marker)?),*))? => &$EnumProperties { $($field: $value),* , .. $default } ),* } } } }; }