From 8e488b27d937dfc119fcb669eea7aac39e599294 Mon Sep 17 00:00:00 2001 From: "pokejofejr4th@gmail.com" Date: Sat, 24 Jun 2023 13:54:14 -0400 Subject: [PATCH] added features #1, #2, #3 --- strum_macros/src/macros/enum_map.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/strum_macros/src/macros/enum_map.rs b/strum_macros/src/macros/enum_map.rs index 1f0e3876..a021289c 100644 --- a/strum_macros/src/macros/enum_map.rs +++ b/strum_macros/src/macros/enum_map.rs @@ -33,6 +33,10 @@ pub fn enum_map_inner(ast: &DeriveInput) -> syn::Result { let mut get_matches_mut = Vec::new(); // match arms in the form `MyEnumMap::Variant => self.variant = new_value` let mut set_matches = Vec::new(); + // struct fields of the form `variant: func(MyEnum::Variant),* + let mut closure_fields = Vec::new(); + // struct fields of the form `variant: func(MyEnum::Variant, self.variant),` + let mut transform_fields = Vec::new(); for variant in variants { // skip disabled variants @@ -68,6 +72,8 @@ pub fn enum_map_inner(ast: &DeriveInput) -> syn::Result { get_matches.push(quote! {#name::#pascal_case => &self.#snake_case,}); get_matches_mut.push(quote! {#name::#pascal_case => &mut self.#snake_case,}); set_matches.push(quote! {#name::#pascal_case => self.#snake_case = new_value,}); + closure_fields.push(quote!{#snake_case: func(#name::#pascal_case),}); + transform_fields.push(quote!{#snake_case: func(#name::#pascal_case, self.#snake_case),}); snake_idents.push(snake_case); } @@ -91,6 +97,24 @@ pub fn enum_map_inner(ast: &DeriveInput) -> syn::Result { #(#snake_idents,)* } } + + #vis fn filled(value: T) -> #map_name { + #map_name { + #(#snake_idents: value.clone(),)* + } + } + + #vis fn from_closure T> -> #map_name { + #map_name { + #(#closure_fields)* + } + } + + #vis fn transform U> -> #map_name { + #map_name { + #(#transform_fields)* + } + } // // E.g. so that if you're using EnumIter as well, these functions work nicely // fn get(&self, variant: #name) -> &T {