From 376ed696ec2ff676a22bafa44c63e2c6fe606647 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 29 Apr 2026 16:33:13 +0800 Subject: Use small integer repr for gen_program! enum Remove the default __FallBack variant and Debug derive from pack types. --- mingling_macros/src/lib.rs | 18 +++++++++++++----- mingling_macros/src/pack.rs | 3 +-- 2 files changed, 14 insertions(+), 7 deletions(-) (limited to 'mingling_macros') diff --git a/mingling_macros/src/lib.rs b/mingling_macros/src/lib.rs index 8ba028b..a8b3b15 100644 --- a/mingling_macros/src/lib.rs +++ b/mingling_macros/src/lib.rs @@ -346,19 +346,27 @@ pub fn program_final_gen(input: TokenStream) -> TokenStream { .map(|s| syn::parse_str::(s).unwrap()) .collect(); + let num_variants = packed_types.len(); + let repr_type = if num_variants <= u8::MAX as usize { + quote! { u8 } + } else if num_variants <= u16::MAX as usize { + quote! { u16 } + } else if num_variants <= u32::MAX as usize { + quote! { u32 } + } else { + quote! { u128 } + }; + let expanded = quote! { - #[derive(Debug, Default, PartialEq, Eq, Clone)] - #[repr(u32)] + #[derive(Debug, PartialEq, Eq, Clone)] + #[repr(#repr_type)] pub enum #name { - #[default] - __FallBack, #(#packed_types),* } impl ::std::fmt::Display for #name { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { match self { - #name::__FallBack => write!(f, "__FallBack"), #(#name::#packed_types => write!(f, stringify!(#packed_types)),)* } } diff --git a/mingling_macros/src/pack.rs b/mingling_macros/src/pack.rs index 9da16de..f7bdfe3 100644 --- a/mingling_macros/src/pack.rs +++ b/mingling_macros/src/pack.rs @@ -79,7 +79,6 @@ pub fn pack(input: TokenStream) -> TokenStream { // Generate the struct definition #[cfg(not(feature = "general_renderer"))] let struct_def = quote! { - #[derive(Debug)] pub struct #type_name { pub(crate) inner: #inner_type, } @@ -87,7 +86,7 @@ pub fn pack(input: TokenStream) -> TokenStream { #[cfg(feature = "general_renderer")] let struct_def = quote! { - #[derive(Debug, serde::Serialize)] + #[derive(serde::Serialize)] pub struct #type_name { pub(crate) inner: #inner_type, } -- cgit