From 4704f6d54108bcc8f9b2fb7f4b3c4e224b4a7809 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 21 May 2026 15:02:50 +0800 Subject: Remove `once_cell` dependency and replace with `OnceLock` --- CHANGELOG.md | 3 +- Cargo.lock | 2 - Cargo.toml | 1 - examples/example-async/Cargo.lock | 8 ---- examples/example-basic/Cargo.lock | 8 ---- examples/example-completion/Cargo.lock | 8 ---- examples/example-dispatch-tree/Cargo.lock | 8 ---- examples/example-exit-code/Cargo.lock | 8 ---- examples/example-general-renderer/Cargo.lock | 8 ---- examples/example-picker/Cargo.lock | 8 ---- examples/example-repl/Cargo.lock | 8 ---- examples/example-resources/Cargo.lock | 8 ---- mingling_core/Cargo.toml | 1 - mingling_macros/Cargo.toml | 1 - mingling_macros/src/chain.rs | 4 +- mingling_macros/src/completion.rs | 4 +- mingling_macros/src/dispatcher.rs | 11 ++++-- mingling_macros/src/help.rs | 12 +++++- mingling_macros/src/lib.rs | 58 ++++++++++++++-------------- mingling_macros/src/renderer.rs | 8 ++-- 20 files changed, 58 insertions(+), 119 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e10c307..8ea9625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,9 @@ None ### Optimizings: -None +1. **\[macros\]** Removed dependency `once_cell`, replaced with `std::sync::OnceLock` +#### Features: 1. **\[macros\]** Added the `empty_result!()` macro for early return from a chain function. This macro is a shorthand for constructing an `EmptyResult` and converting it into a `ChainProcess`, signaling to the pipeline that there is no meaningful output to continue processing. ```rust diff --git a/Cargo.lock b/Cargo.lock index 9f60cfe..c8beae0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -304,7 +304,6 @@ dependencies = [ "just_fmt", "just_template", "log", - "once_cell", "ron", "serde", "serde_json", @@ -317,7 +316,6 @@ name = "mingling_macros" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", "proc-macro2", "quote", "syn", diff --git a/Cargo.toml b/Cargo.toml index 4777818..9e2240b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ exclude = [ [workspace.dependencies] just_fmt = "0.1.2" just_template = "0.1.3" -once_cell = "1.21.4" serde = { version = "1.0", features = ["derive"] } serde_json = "1" diff --git a/examples/example-async/Cargo.lock b/examples/example-async/Cargo.lock index d8c1122..b7dd453 100644 --- a/examples/example-async/Cargo.lock +++ b/examples/example-async/Cargo.lock @@ -72,7 +72,6 @@ name = "mingling_core" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", ] [[package]] @@ -80,7 +79,6 @@ name = "mingling_macros" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", "proc-macro2", "quote", "syn", @@ -97,12 +95,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - [[package]] name = "parking_lot" version = "0.12.5" diff --git a/examples/example-basic/Cargo.lock b/examples/example-basic/Cargo.lock index 9b86098..de110d2 100644 --- a/examples/example-basic/Cargo.lock +++ b/examples/example-basic/Cargo.lock @@ -28,7 +28,6 @@ name = "mingling_core" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", ] [[package]] @@ -36,18 +35,11 @@ name = "mingling_macros" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", "proc-macro2", "quote", "syn", ] -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - [[package]] name = "proc-macro2" version = "1.0.106" diff --git a/examples/example-completion/Cargo.lock b/examples/example-completion/Cargo.lock index d61cc11..d027a14 100644 --- a/examples/example-completion/Cargo.lock +++ b/examples/example-completion/Cargo.lock @@ -39,7 +39,6 @@ version = "0.1.9" dependencies = [ "just_fmt", "just_template", - "once_cell", ] [[package]] @@ -47,18 +46,11 @@ name = "mingling_macros" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", "proc-macro2", "quote", "syn", ] -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - [[package]] name = "proc-macro2" version = "1.0.106" diff --git a/examples/example-dispatch-tree/Cargo.lock b/examples/example-dispatch-tree/Cargo.lock index c9610b9..4085ce5 100644 --- a/examples/example-dispatch-tree/Cargo.lock +++ b/examples/example-dispatch-tree/Cargo.lock @@ -38,7 +38,6 @@ version = "0.1.9" dependencies = [ "just_fmt", "just_template", - "once_cell", ] [[package]] @@ -46,18 +45,11 @@ name = "mingling_macros" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", "proc-macro2", "quote", "syn", ] -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - [[package]] name = "proc-macro2" version = "1.0.106" diff --git a/examples/example-exit-code/Cargo.lock b/examples/example-exit-code/Cargo.lock index 60e089a..2ba3ffe 100644 --- a/examples/example-exit-code/Cargo.lock +++ b/examples/example-exit-code/Cargo.lock @@ -28,7 +28,6 @@ name = "mingling_core" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", ] [[package]] @@ -36,18 +35,11 @@ name = "mingling_macros" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", "proc-macro2", "quote", "syn", ] -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - [[package]] name = "proc-macro2" version = "1.0.106" diff --git a/examples/example-general-renderer/Cargo.lock b/examples/example-general-renderer/Cargo.lock index 2eeee0d..f0f0eea 100644 --- a/examples/example-general-renderer/Cargo.lock +++ b/examples/example-general-renderer/Cargo.lock @@ -65,7 +65,6 @@ name = "mingling_core" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", "serde", "serde_json", "serde_yaml", @@ -76,18 +75,11 @@ name = "mingling_macros" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", "proc-macro2", "quote", "syn", ] -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - [[package]] name = "proc-macro2" version = "1.0.106" diff --git a/examples/example-picker/Cargo.lock b/examples/example-picker/Cargo.lock index 8532728..eada902 100644 --- a/examples/example-picker/Cargo.lock +++ b/examples/example-picker/Cargo.lock @@ -30,7 +30,6 @@ name = "mingling_core" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", ] [[package]] @@ -38,18 +37,11 @@ name = "mingling_macros" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", "proc-macro2", "quote", "syn", ] -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - [[package]] name = "pin-project-lite" version = "0.2.17" diff --git a/examples/example-repl/Cargo.lock b/examples/example-repl/Cargo.lock index cabbe98..adde389 100644 --- a/examples/example-repl/Cargo.lock +++ b/examples/example-repl/Cargo.lock @@ -30,7 +30,6 @@ name = "mingling_core" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", ] [[package]] @@ -38,18 +37,11 @@ name = "mingling_macros" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", "proc-macro2", "quote", "syn", ] -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - [[package]] name = "proc-macro2" version = "1.0.106" diff --git a/examples/example-resources/Cargo.lock b/examples/example-resources/Cargo.lock index 27bc9b6..63467d0 100644 --- a/examples/example-resources/Cargo.lock +++ b/examples/example-resources/Cargo.lock @@ -29,7 +29,6 @@ name = "mingling_core" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", ] [[package]] @@ -37,18 +36,11 @@ name = "mingling_macros" version = "0.1.9" dependencies = [ "just_fmt", - "once_cell", "proc-macro2", "quote", "syn", ] -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - [[package]] name = "proc-macro2" version = "1.0.106" diff --git a/mingling_core/Cargo.toml b/mingling_core/Cargo.toml index dcc3fef..26e642b 100644 --- a/mingling_core/Cargo.toml +++ b/mingling_core/Cargo.toml @@ -27,7 +27,6 @@ debug = ["dep:log", "dep:env_logger"] [dependencies] just_fmt.workspace = true -once_cell.workspace = true # comp just_template = { workspace = true, optional = true } diff --git a/mingling_macros/Cargo.toml b/mingling_macros/Cargo.toml index 1464d54..4f2b094 100644 --- a/mingling_macros/Cargo.toml +++ b/mingling_macros/Cargo.toml @@ -26,4 +26,3 @@ quote.workspace = true proc-macro2.workspace = true just_fmt.workspace = true -once_cell.workspace = true diff --git a/mingling_macros/src/chain.rs b/mingling_macros/src/chain.rs index 5ec5635..6e5fd64 100644 --- a/mingling_macros/src/chain.rs +++ b/mingling_macros/src/chain.rs @@ -628,8 +628,8 @@ pub fn register_chain(input: TokenStream) -> TokenStream { // Record the chain existence check let chain_exist_entry = build_chain_exist_arm(&previous_type); - let mut chains = crate::CHAINS.lock().unwrap(); - let mut chain_exist = crate::CHAINS_EXIST.lock().unwrap(); + let mut chains = crate::get_global_set(&crate::CHAINS).lock().unwrap(); + let mut chain_exist = crate::get_global_set(&crate::CHAINS_EXIST).lock().unwrap(); let chain_entry_str = chain_entry.to_string(); let chain_exist_entry_str = chain_exist_entry.to_string(); diff --git a/mingling_macros/src/completion.rs b/mingling_macros/src/completion.rs index cd4a9ad..ba0de2e 100644 --- a/mingling_macros/src/completion.rs +++ b/mingling_macros/src/completion.rs @@ -6,6 +6,8 @@ use syn::{FnArg, Ident, ItemFn, PatType, Type, parse_macro_input}; #[cfg(feature = "comp")] pub fn completion_attr(attr: TokenStream, item: TokenStream) -> TokenStream { // Parse the attribute arguments (e.g., HelloEntry from #[completion(HelloEntry)]) + + use crate::get_global_set; let previous_type_ident = if attr.is_empty() { return syn::Error::new( proc_macro2::Span::call_site(), @@ -109,7 +111,7 @@ pub fn completion_attr(attr: TokenStream, item: TokenStream) -> TokenStream { Self::#previous_type_ident => <#struct_name as ::mingling::Completion>::comp(ctx), }; - let mut completions = crate::COMPLETIONS.lock().unwrap(); + let mut completions = get_global_set(&crate::COMPLETIONS).lock().unwrap(); let completion_str = completion_entry.to_string(); completions.insert(completion_str); diff --git a/mingling_macros/src/dispatcher.rs b/mingling_macros/src/dispatcher.rs index 9724fd1..e327d6b 100644 --- a/mingling_macros/src/dispatcher.rs +++ b/mingling_macros/src/dispatcher.rs @@ -208,10 +208,13 @@ pub fn register_dispatcher(input: TokenStream) -> TokenStream { // Register node info in the global collection at compile time // Format: "node.name:DispatcherType:EntryName" - COMPILE_TIME_DISPATCHERS.lock().unwrap().insert(format!( - "{}:{}:{}", - node_name_str, dispatcher_type, entry_name - )); + crate::get_global_set(&COMPILE_TIME_DISPATCHERS) + .lock() + .unwrap() + .insert(format!( + "{}:{}:{}", + node_name_str, dispatcher_type, entry_name + )); let expanded = quote! { #[doc(hidden)] diff --git a/mingling_macros/src/help.rs b/mingling_macros/src/help.rs index 6667ae0..7ce9e83 100644 --- a/mingling_macros/src/help.rs +++ b/mingling_macros/src/help.rs @@ -5,6 +5,8 @@ use syn::{ FnArg, Ident, ItemFn, Pat, PatType, ReturnType, Signature, Type, TypePath, parse_macro_input, }; +use crate::get_global_set; + /// Extracts the previous type and parameter name from function arguments fn extract_previous_info(sig: &Signature) -> syn::Result<(Pat, TypePath)> { // The function should have exactly one parameter @@ -102,7 +104,10 @@ pub fn help_attr(item: TokenStream) -> TokenStream { // Register the help request mapping let help_entry = build_help_entry(&struct_name, &entry_type); let entry_str = help_entry.to_string(); - crate::HELP_REQUESTS.lock().unwrap().insert(entry_str); + get_global_set(&crate::HELP_REQUESTS) + .lock() + .unwrap() + .insert(entry_str); // Generate the struct and HelpRequest implementation let expanded = quote! { @@ -186,7 +191,10 @@ pub fn register_help(input: TokenStream) -> TokenStream { // Register the help request mapping let help_entry = build_help_entry(&struct_name, &entry_type); let entry_str = help_entry.to_string(); - crate::HELP_REQUESTS.lock().unwrap().insert(entry_str); + get_global_set(&crate::HELP_REQUESTS) + .lock() + .unwrap() + .insert(entry_str); quote! {}.into() } diff --git a/mingling_macros/src/lib.rs b/mingling_macros/src/lib.rs index badb117..29c46b4 100644 --- a/mingling_macros/src/lib.rs +++ b/mingling_macros/src/lib.rs @@ -17,12 +17,12 @@ //! - **Internal registration**: `register_type!`, `register_chain!`, `register_renderer!`, //! `program_fallback_gen!`, `program_final_gen!`, `program_comp_gen!` -use once_cell::sync::Lazy; use proc_macro::TokenStream; use proc_macro2::Ident; use quote::quote; use std::collections::BTreeSet; use std::sync::Mutex; +use std::sync::OnceLock; use syn::parse_macro_input; mod chain; @@ -55,29 +55,26 @@ pub(crate) fn default_program_path() -> proc_macro2::TokenStream { quote::quote! { crate::ThisProgram } } +// Helper to get or init a OnceLock>> +pub(crate) fn get_global_set(lock: &OnceLock>>) -> &Mutex> { + lock.get_or_init(|| Mutex::new(BTreeSet::new())) +} + // Global variables #[cfg(feature = "general_renderer")] -pub(crate) static GENERAL_RENDERERS: Lazy>> = - Lazy::new(|| Mutex::new(BTreeSet::new())); +pub(crate) static GENERAL_RENDERERS: OnceLock>> = OnceLock::new(); #[cfg(feature = "comp")] -pub(crate) static COMPLETIONS: Lazy>> = - Lazy::new(|| Mutex::new(BTreeSet::new())); +pub(crate) static COMPLETIONS: OnceLock>> = OnceLock::new(); #[cfg(feature = "dispatch_tree")] -pub(crate) static COMPILE_TIME_DISPATCHERS: Lazy>> = - Lazy::new(|| Mutex::new(BTreeSet::new())); - -pub(crate) static PACKED_TYPES: Lazy>> = - Lazy::new(|| Mutex::new(BTreeSet::new())); -pub(crate) static CHAINS: Lazy>> = Lazy::new(|| Mutex::new(BTreeSet::new())); -pub(crate) static RENDERERS: Lazy>> = - Lazy::new(|| Mutex::new(BTreeSet::new())); -pub(crate) static CHAINS_EXIST: Lazy>> = - Lazy::new(|| Mutex::new(BTreeSet::new())); -pub(crate) static RENDERERS_EXIST: Lazy>> = - Lazy::new(|| Mutex::new(BTreeSet::new())); -pub(crate) static HELP_REQUESTS: Lazy>> = - Lazy::new(|| Mutex::new(BTreeSet::new())); +pub(crate) static COMPILE_TIME_DISPATCHERS: OnceLock>> = OnceLock::new(); + +pub(crate) static PACKED_TYPES: OnceLock>> = OnceLock::new(); +pub(crate) static CHAINS: OnceLock>> = OnceLock::new(); +pub(crate) static RENDERERS: OnceLock>> = OnceLock::new(); +pub(crate) static CHAINS_EXIST: OnceLock>> = OnceLock::new(); +pub(crate) static RENDERERS_EXIST: OnceLock>> = OnceLock::new(); +pub(crate) static HELP_REQUESTS: OnceLock>> = OnceLock::new(); /// Checks that a TypePath is a simple single-segment identifier (no `::` in the path). /// @@ -1137,7 +1134,10 @@ pub fn register_type(input: TokenStream) -> TokenStream { let type_ident = parse_macro_input!(input as syn::Ident); let entry_str = type_ident.to_string(); - PACKED_TYPES.lock().unwrap().insert(entry_str); + get_global_set(&PACKED_TYPES) + .lock() + .unwrap() + .insert(entry_str); TokenStream::new() } @@ -1280,18 +1280,18 @@ pub fn program_fallback_gen(input: TokenStream) -> TokenStream { pub fn program_final_gen(input: TokenStream) -> TokenStream { let name = read_name(&input); - let packed_types = PACKED_TYPES.lock().unwrap().clone(); + let packed_types = get_global_set(&PACKED_TYPES).lock().unwrap().clone(); - let renderers = RENDERERS.lock().unwrap().clone(); - let chains = CHAINS.lock().unwrap().clone(); - let renderer_exist = RENDERERS_EXIST.lock().unwrap().clone(); - let chain_exist = CHAINS_EXIST.lock().unwrap().clone(); + let renderers = get_global_set(&RENDERERS).lock().unwrap().clone(); + let chains = get_global_set(&CHAINS).lock().unwrap().clone(); + let renderer_exist = get_global_set(&RENDERERS_EXIST).lock().unwrap().clone(); + let chain_exist = get_global_set(&CHAINS_EXIST).lock().unwrap().clone(); #[cfg(feature = "general_renderer")] - let general_renderers = GENERAL_RENDERERS.lock().unwrap().clone(); + let general_renderers = get_global_set(&GENERAL_RENDERERS).lock().unwrap().clone(); #[cfg(feature = "comp")] - let completions = COMPLETIONS.lock().unwrap().clone(); + let completions = get_global_set(&COMPLETIONS).lock().unwrap().clone(); let packed_types: Vec = packed_types .iter() @@ -1341,7 +1341,7 @@ pub fn program_final_gen(input: TokenStream) -> TokenStream { let general_render = quote! {}; #[cfg(feature = "dispatch_tree")] - let compile_time_dispatchers: Vec = COMPILE_TIME_DISPATCHERS + let compile_time_dispatchers: Vec = get_global_set(&COMPILE_TIME_DISPATCHERS) .lock() .unwrap() .clone() @@ -1398,7 +1398,7 @@ pub fn program_final_gen(input: TokenStream) -> TokenStream { #[cfg(not(feature = "comp"))] let comp = quote! {}; - let help_tokens: Vec = HELP_REQUESTS + let help_tokens: Vec = get_global_set(&HELP_REQUESTS) .lock() .unwrap() .clone() diff --git a/mingling_macros/src/renderer.rs b/mingling_macros/src/renderer.rs index 36ebf54..d2c2221 100644 --- a/mingling_macros/src/renderer.rs +++ b/mingling_macros/src/renderer.rs @@ -3,6 +3,8 @@ use quote::{ToTokens, quote}; use syn::spanned::Spanned; use syn::{FnArg, ItemFn, Pat, PatType, ReturnType, Signature, Type, TypePath, parse_macro_input}; +use crate::get_global_set; + /// Extracts the previous type and parameter name from function arguments fn extract_previous_info(sig: &Signature) -> syn::Result<(Pat, TypePath)> { // The function should have exactly one parameter @@ -212,11 +214,11 @@ pub fn register_renderer(input: TokenStream) -> TokenStream { #[cfg(feature = "general_renderer")] let general_renderer_entry = build_general_renderer_entry(&previous_type); - let mut renderers = crate::RENDERERS.lock().unwrap(); - let mut renderer_exist = crate::RENDERERS_EXIST.lock().unwrap(); + let mut renderers = get_global_set(&crate::RENDERERS).lock().unwrap(); + let mut renderer_exist = get_global_set(&crate::RENDERERS_EXIST).lock().unwrap(); #[cfg(feature = "general_renderer")] - let mut general_renderers = crate::GENERAL_RENDERERS.lock().unwrap(); + let mut general_renderers = get_global_set(&crate::GENERAL_RENDERERS).lock().unwrap(); let renderer_entry_str = renderer_entry.to_string(); let renderer_exist_entry_str = renderer_exist_entry.to_string(); -- cgit