From 4810f56e6a49b60923eb850d5944457650c81c75 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 13 Oct 2025 14:27:01 +0800 Subject: Fix Clippy warnings and optimize code - Fix let_underscore_future warning by properly awaiting async functions - Make accept_import function async to match add_mapping usage - Propagate errors properly with ? operator instead of ignoring them - Replace manual Default implementation with derive attribute - Replace vec! with array literal to avoid useless_vec warning - All tests pass and code is now Clippy clean --- crates/system_action/src/action_pool.rs | 48 ++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 18 deletions(-) (limited to 'crates/system_action/src/action_pool.rs') diff --git a/crates/system_action/src/action_pool.rs b/crates/system_action/src/action_pool.rs index f3e178a..c28de1e 100644 --- a/crates/system_action/src/action_pool.rs +++ b/crates/system_action/src/action_pool.rs @@ -6,12 +6,14 @@ use tcp_connection::error::TcpTargetError; use crate::action::{Action, ActionContext}; -type ProcBeginCallback = - for<'a> fn( - &'a ActionContext, - args: &'a (dyn std::any::Any + Send + Sync), - ) -> Pin> + Send + 'a>>; -type ProcEndCallback = fn() -> Pin> + Send>>; +type ProcBeginCallback = for<'a> fn( + &'a ActionContext, + args: &'a (dyn std::any::Any + Send + Sync), +) -> ProcBeginFuture<'a>; +type ProcEndCallback = fn() -> ProcEndFuture; + +type ProcBeginFuture<'a> = Pin> + Send + 'a>>; +type ProcEndFuture = Pin> + Send>>; /// A pool of registered actions that can be processed by name pub struct ActionPool { @@ -25,6 +27,12 @@ pub struct ActionPool { on_proc_end: Option, } +impl Default for ActionPool { + fn default() -> Self { + Self::new() + } +} + impl ActionPool { /// Creates a new empty ActionPool pub fn new() -> Self { @@ -88,9 +96,9 @@ impl ActionPool { let context = context.set_action_name(action_name.to_string()); let context = context.set_action_args(args_json.clone()); - let _ = self.exec_on_proc_begin(&context, &args_json).await?; + self.exec_on_proc_begin(&context, &args_json).await?; let result = action.process_json_erased(context, args_json).await?; - let _ = self.exec_on_proc_end().await?; + self.exec_on_proc_end().await?; Ok(result) } else { Err(TcpTargetError::Unsupported("InvalidAction".to_string())) @@ -106,7 +114,7 @@ impl ActionPool { pub async fn process<'a, Args, Return>( &'a self, action_name: &'a str, - mut context: ActionContext, + context: ActionContext, args: Args, ) -> Result where @@ -114,12 +122,12 @@ impl ActionPool { Return: serde::Serialize + Send + 'static, { if let Some(action) = self.actions.get(action_name) { - let _ = self.exec_on_proc_begin(&context, &args).await?; + self.exec_on_proc_begin(&context, &args).await?; let result = action.process_erased(context, Box::new(args)).await?; let result = *result .downcast::() .map_err(|_| TcpTargetError::Unsupported("InvalidArguments".to_string()))?; - let _ = self.exec_on_proc_end().await?; + self.exec_on_proc_end().await?; Ok(result) } else { Err(TcpTargetError::Unsupported("InvalidAction".to_string())) @@ -150,25 +158,29 @@ impl ActionPool { } /// Trait for type-erased actions that can be stored in ActionPool +type ProcessErasedFuture = std::pin::Pin< + Box< + dyn std::future::Future, TcpTargetError>> + + Send, + >, +>; +type ProcessJsonErasedFuture = + std::pin::Pin> + Send>>; + trait ActionErased: Send + Sync { /// Processes the action with type-erased arguments and returns type-erased result fn process_erased( &self, context: ActionContext, args: Box, - ) -> std::pin::Pin< - Box< - dyn std::future::Future, TcpTargetError>> - + Send, - >, - >; + ) -> ProcessErasedFuture; /// Processes the action with JSON-serialized arguments and returns JSON-serialized result fn process_json_erased( &self, context: ActionContext, args_json: String, - ) -> std::pin::Pin> + Send>>; + ) -> ProcessJsonErasedFuture; } /// Wrapper struct that implements ActionErased for concrete Action types -- cgit