aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-04-29 16:33:13 +0800
committer魏曹先生 <1992414357@qq.com>2026-04-29 16:33:13 +0800
commit376ed696ec2ff676a22bafa44c63e2c6fe606647 (patch)
treec8c30fbf054f9dd50eb858386d208f9147e20dbc
parent4fb83212cefa1a7818d8531fdfe937db10203471 (diff)
Use small integer repr for gen_program! enum
Remove the default __FallBack variant and Debug derive from pack types.
-rw-r--r--CHANGELOG.md8
-rw-r--r--dev_tools/src/bin/docsify-sidebar-gen.rs6
-rw-r--r--mingling_macros/src/lib.rs18
-rw-r--r--mingling_macros/src/pack.rs3
4 files changed, 25 insertions, 10 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7ba069e..eb30d4b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@
1. Fixed a build failure on **Windows** caused by `mingling_core/src/program.rs`
2. **\[picker\]** Fixed an issue where the `Pickable` trait for `Yes` and `True` types could not correctly parse explicit boolean `--value true`
+#### Optimizings:
+
+1. **\[macros\]** Optimized the memory usage of the `gen_program!()` macro: the internal generated enum now uses the smallest possible integer representation (`u8`, `u16`, `u32`, or `u128`) based on the number of packed types, instead of always using `u32`.
+
#### Features:
1. **\[mingling\]** Added the scaffolding tool `mling`, which can quickly deploy and test your command-line programs
@@ -164,6 +168,10 @@ let (name, age) = Picker::new(prev.inner)
.unpack(); // will return Result<Value, Route>
```
+7. **\[macros\]** The enum generated by `gen_program!()` no longer has a default variant (`__FallBack`), and the `#[default]` attribute has been removed accordingly.
+
+8. **\[macros\]** Removed `#[derive(Debug)]` from generated pack types to remove unnecessary trait bounds.
+
---
### Release 0.1.6 **\[YANKED\]**
diff --git a/dev_tools/src/bin/docsify-sidebar-gen.rs b/dev_tools/src/bin/docsify-sidebar-gen.rs
index 9020ca4..a525283 100644
--- a/dev_tools/src/bin/docsify-sidebar-gen.rs
+++ b/dev_tools/src/bin/docsify-sidebar-gen.rs
@@ -31,10 +31,10 @@ fn gen_sidebar() {
if !entries.is_empty() {
sub_dirs.insert(dir_name, entries);
}
- } else if path.extension().map_or(false, |ext| ext == "md") {
+ } else if path.extension().is_some_and(|ext| ext == "md") {
let title = extract_title(&path);
let relative = path
- .strip_prefix(&repo_root.join("docs"))
+ .strip_prefix(repo_root.join("docs"))
.unwrap()
.to_string_lossy()
.replace('\\', "/");
@@ -87,7 +87,7 @@ fn collect_markdown_files(dir: &Path) -> Vec<SidebarEntry> {
if let Ok(read_dir) = std::fs::read_dir(dir) {
for entry in read_dir.flatten() {
let path = entry.path();
- if path.extension().map_or(false, |ext| ext == "md") {
+ if path.extension().is_some_and(|ext| ext == "md") {
let title = extract_title(&path);
let relative = path
.strip_prefix(&docs_root)
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::<proc_macro2::TokenStream>(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,
}