From d4634b01e3f33b3ee52b1501f5ade739a1796d08 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 25 Apr 2026 19:02:17 +0800 Subject: Remove redundant generic parameter from Program struct --- mingling_core/src/program.rs | 56 +++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 34 deletions(-) (limited to 'mingling_core/src/program.rs') diff --git a/mingling_core/src/program.rs b/mingling_core/src/program.rs index a671e9b..c9cffbe 100644 --- a/mingling_core/src/program.rs +++ b/mingling_core/src/program.rs @@ -35,7 +35,7 @@ pub use string_vec::*; static THIS_PROGRAM: OnceLock>> = OnceLock::new(); /// Returns a reference to the current program instance, panics if not set. -pub fn this() -> &'static Program +pub fn this() -> &'static Program where C: ProgramCollect + 'static, { @@ -43,27 +43,23 @@ where } /// Returns a reference to the current program instance, if set. -fn try_get_this_program() -> Option<&'static Program> +fn try_get_this_program() -> Option<&'static Program> where C: ProgramCollect + 'static, { - THIS_PROGRAM - .get()? - .as_ref()? - .downcast_ref::>() + THIS_PROGRAM.get()?.as_ref()?.downcast_ref::>() } /// Program, used to define the behavior of the entire command-line program #[derive(Default)] -pub struct Program +pub struct Program where C: ProgramCollect, { pub(crate) collect: std::marker::PhantomData, - pub(crate) group: std::marker::PhantomData, pub(crate) args: Vec, - pub(crate) dispatcher: Vec + Send + Sync>>, + pub(crate) dispatcher: Vec + Send + Sync>>, pub stdout_setting: ProgramStdoutSetting, pub user_context: ProgramUserContext, @@ -72,9 +68,9 @@ where pub general_renderer_name: GeneralRendererSetting, } -impl Program +impl Program where - C: ProgramCollect, + C: ProgramCollect, { /// Creates a new Program instance, initializing command-line arguments from the environment. pub fn new() -> Self { @@ -98,7 +94,6 @@ where pub fn new_with_args(args: impl Into) -> Self { Program { collect: std::marker::PhantomData, - group: std::marker::PhantomData, args: args.into().into(), dispatcher: Vec::new(), stdout_setting: Default::default(), @@ -110,22 +105,21 @@ where } /// Returns a reference to the current program instance, if set. - pub fn this_program() -> &'static Program + pub fn this_program() -> &'static Program where C: 'static, - G: 'static, { THIS_PROGRAM .get() .unwrap() .as_ref() .unwrap() - .downcast_ref::>() + .downcast_ref::>() .unwrap() } /// Get all registered dispatcher names from the program - pub fn get_nodes(&self) -> Vec<(String, &(dyn Dispatcher + Send + Sync))> { + pub fn get_nodes(&self) -> Vec<(String, &(dyn Dispatcher + Send + Sync))> { get_nodes(self) } @@ -133,7 +127,7 @@ where pub fn dispatch_args_dynamic( &self, args: impl Into, - ) -> Result, ChainProcessError> { + ) -> Result, ChainProcessError> { match exec::dispatch_args_dynamic(self, args.into().into()) { Ok(ok) => Ok(ok), Err(e) => Err(e.into()), @@ -143,16 +137,15 @@ where // Async program #[cfg(feature = "async")] -impl Program +impl Program where - C: ProgramCollect, + C: ProgramCollect, { /// Sets the current program instance and runs the provided async function. async fn set_instance_and_run(self, f: F) -> Fut::Output where C: 'static + Send + Sync, - G: 'static + Send + Sync, - F: FnOnce(&'static Program) -> Fut + Send + Sync, + F: FnOnce(&'static Program) -> Fut + Send + Sync, Fut: Future + Send, { THIS_PROGRAM.get_or_init(|| Some(Box::new(self))); @@ -161,7 +154,7 @@ where .unwrap() .as_ref() .unwrap() - .downcast_ref::>() + .downcast_ref::>() .unwrap(); f(program).await } @@ -170,7 +163,6 @@ where pub async fn exec_without_render(mut self) -> Result where C: 'static + Send + Sync, - G: 'static + Send + Sync, { self.args = self.args.iter().skip(1).cloned().collect(); self.set_instance_and_run(|p| async { crate::exec::exec(p).await.map_err(|e| e.into()) }) @@ -181,7 +173,6 @@ where pub async fn exec(self) where C: 'static + Send + Sync, - G: 'static + Send + Sync, { let stdout_setting = self.stdout_setting.clone(); let result = match self.exec_without_render().await { @@ -216,16 +207,15 @@ where // Sync program #[cfg(not(feature = "async"))] -impl Program +impl Program where - C: ProgramCollect, + C: ProgramCollect, { /// Sets the current program instance and runs the provided function. fn set_instance_and_run(self, f: F) -> R where C: 'static + Send + Sync, - G: 'static + Send + Sync, - F: FnOnce(&'static Program) -> R + Send + Sync, + F: FnOnce(&'static Program) -> R + Send + Sync, { THIS_PROGRAM.get_or_init(|| Some(Box::new(self))); let program = THIS_PROGRAM @@ -233,7 +223,7 @@ where .unwrap() .as_ref() .unwrap() - .downcast_ref::>() + .downcast_ref::>() .unwrap(); f(program) } @@ -242,7 +232,6 @@ where pub fn exec_without_render(mut self) -> Result where C: 'static + Send + Sync, - G: 'static + Send + Sync, { self.args = self.args.iter().skip(1).cloned().collect(); self.set_instance_and_run(|p| crate::exec::exec(p).map_err(|e| e.into())) @@ -252,7 +241,6 @@ where pub fn exec(self) where C: 'static + Send + Sync, - G: 'static + Send + Sync, { let stdout_setting = self.stdout_setting.clone(); let result = match self.exec_without_render() { @@ -403,9 +391,9 @@ macro_rules! __dispatch_program_chains { } /// Get all registered dispatcher names from the program -pub fn get_nodes, G>( - program: &Program, -) -> Vec<(String, &(dyn Dispatcher + Send + Sync))> { +pub fn get_nodes>( + program: &Program, +) -> Vec<(String, &(dyn Dispatcher + Send + Sync))> { program .dispatcher .iter() -- cgit