From 5a8e83cd49933b2bb67218921c32b3d104a33c1d Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 6 Nov 2025 10:47:25 +0800 Subject: [PATCH] refactor: change function definitions to const for improved performance and clarity --- src-tauri/Cargo.toml | 3 ++- src-tauri/src/cmd/uwp.rs | 2 +- src-tauri/src/config/prfitem.rs | 2 +- src-tauri/src/config/profiles.rs | 4 ++-- src-tauri/src/config/verge.rs | 2 +- src-tauri/src/core/backup.rs | 2 +- src-tauri/src/core/hotkey.rs | 2 +- src-tauri/src/core/manager/config.rs | 2 +- src-tauri/src/core/service.rs | 2 +- src-tauri/src/core/validate.rs | 2 +- src-tauri/src/module/lightweight.rs | 2 +- src-tauri/src/process/guard.rs | 2 +- src-tauri/src/utils/init.rs | 2 +- src-tauri/src/utils/network.rs | 6 +++--- 14 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 8e19c814..404dccb1 100755 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -237,4 +237,5 @@ significant_drop_in_scrutinee = "deny" redundant_clone = "deny" # option_if_let_else = "deny" // 过于激进,暂时不开启 needless_pass_by_ref_mut = "deny" -needless_collect = "deny" \ No newline at end of file +needless_collect = "deny" +missing_const_for_fn = "deny" \ No newline at end of file diff --git a/src-tauri/src/cmd/uwp.rs b/src-tauri/src/cmd/uwp.rs index 0f073bf9..96db7eed 100644 --- a/src-tauri/src/cmd/uwp.rs +++ b/src-tauri/src/cmd/uwp.rs @@ -17,7 +17,7 @@ mod platform { mod platform { use super::CmdResult; - pub fn invoke_uwp_tool() -> CmdResult { + pub const fn invoke_uwp_tool() -> CmdResult { Ok(()) } } diff --git a/src-tauri/src/config/prfitem.rs b/src-tauri/src/config/prfitem.rs index b5460c60..38a4fd75 100644 --- a/src-tauri/src/config/prfitem.rs +++ b/src-tauri/src/config/prfitem.rs @@ -611,6 +611,6 @@ impl PrfItem { } // 向前兼容,默认为订阅启用自动更新 -fn default_allow_auto_update() -> Option { +const fn default_allow_auto_update() -> Option { Some(true) } diff --git a/src-tauri/src/config/profiles.rs b/src-tauri/src/config/profiles.rs index 18226402..0ee3557e 100644 --- a/src-tauri/src/config/profiles.rs +++ b/src-tauri/src/config/profiles.rs @@ -102,12 +102,12 @@ impl IProfiles { } } - pub fn get_current(&self) -> Option<&String> { + pub const fn get_current(&self) -> Option<&String> { self.current.as_ref() } /// get items ref - pub fn get_items(&self) -> Option<&Vec> { + pub const fn get_items(&self) -> Option<&Vec> { self.items.as_ref() } diff --git a/src-tauri/src/config/verge.rs b/src-tauri/src/config/verge.rs index 7f0ef98b..3a19e451 100644 --- a/src-tauri/src/config/verge.rs +++ b/src-tauri/src/config/verge.rs @@ -531,7 +531,7 @@ impl IVerge { patch!(enable_external_controller); } - pub fn get_singleton_port() -> u16 { + pub const fn get_singleton_port() -> u16 { crate::constants::network::ports::SINGLETON_SERVER } diff --git a/src-tauri/src/core/backup.rs b/src-tauri/src/core/backup.rs index db753218..02316758 100644 --- a/src-tauri/src/core/backup.rs +++ b/src-tauri/src/core/backup.rs @@ -45,7 +45,7 @@ enum Operation { } impl Operation { - fn timeout(&self) -> u64 { + const fn timeout(&self) -> u64 { match self { Self::Upload => TIMEOUT_UPLOAD, Self::Download => TIMEOUT_DOWNLOAD, diff --git a/src-tauri/src/core/hotkey.rs b/src-tauri/src/core/hotkey.rs index f1e5bb5d..0c38cc70 100755 --- a/src-tauri/src/core/hotkey.rs +++ b/src-tauri/src/core/hotkey.rs @@ -84,7 +84,7 @@ impl fmt::Display for SystemHotkey { #[cfg(target_os = "macos")] impl SystemHotkey { - pub fn function(self) -> HotkeyFunction { + pub const fn function(self) -> HotkeyFunction { match self { Self::CmdQ => HotkeyFunction::Quit, Self::CmdW => HotkeyFunction::Hide, diff --git a/src-tauri/src/core/manager/config.rs b/src-tauri/src/core/manager/config.rs index e0f9bd51..295230ab 100644 --- a/src-tauri/src/core/manager/config.rs +++ b/src-tauri/src/core/manager/config.rs @@ -137,7 +137,7 @@ impl CoreManager { } } - fn is_connection_io_error(kind: std::io::ErrorKind) -> bool { + const fn is_connection_io_error(kind: std::io::ErrorKind) -> bool { matches!( kind, std::io::ErrorKind::ConnectionAborted diff --git a/src-tauri/src/core/service.rs b/src-tauri/src/core/service.rs index 427015d8..9f892423 100644 --- a/src-tauri/src/core/service.rs +++ b/src-tauri/src/core/service.rs @@ -449,7 +449,7 @@ impl ServiceManager { Self(ServiceStatus::Unavailable("Need Checks".into())) } - pub fn config() -> Option { + pub const fn config() -> Option { Some(clash_verge_service_ipc::IpcConfig { default_timeout: Duration::from_millis(30), retry_delay: Duration::from_millis(250), diff --git a/src-tauri/src/core/validate.rs b/src-tauri/src/core/validate.rs index c22a5d04..bfb31b62 100644 --- a/src-tauri/src/core/validate.rs +++ b/src-tauri/src/core/validate.rs @@ -16,7 +16,7 @@ pub struct CoreConfigValidator { } impl CoreConfigValidator { - pub fn new() -> Self { + pub const fn new() -> Self { Self { is_processing: AtomicBool::new(false), } diff --git a/src-tauri/src/module/lightweight.rs b/src-tauri/src/module/lightweight.rs index 48baeef0..30dd141c 100644 --- a/src-tauri/src/module/lightweight.rs +++ b/src-tauri/src/module/lightweight.rs @@ -36,7 +36,7 @@ impl From for LightweightState { } impl LightweightState { - fn as_u8(self) -> u8 { + const fn as_u8(self) -> u8 { self as u8 } } diff --git a/src-tauri/src/process/guard.rs b/src-tauri/src/process/guard.rs index a776094f..0ffb8999 100644 --- a/src-tauri/src/process/guard.rs +++ b/src-tauri/src/process/guard.rs @@ -20,7 +20,7 @@ impl Drop for CommandChildGuard { } impl CommandChildGuard { - pub fn new(child: CommandChild) -> Self { + pub const fn new(child: CommandChild) -> Self { Self(Some(child)) } diff --git a/src-tauri/src/utils/init.rs b/src-tauri/src/utils/init.rs index ba6b27d9..cb8d3e6c 100644 --- a/src-tauri/src/utils/init.rs +++ b/src-tauri/src/utils/init.rs @@ -506,7 +506,7 @@ pub fn init_scheme() -> Result<()> { Ok(()) } #[cfg(target_os = "macos")] -pub fn init_scheme() -> Result<()> { +pub const fn init_scheme() -> Result<()> { Ok(()) } diff --git a/src-tauri/src/utils/network.rs b/src-tauri/src/utils/network.rs index fe18ca83..c4598a7b 100644 --- a/src-tauri/src/utils/network.rs +++ b/src-tauri/src/utils/network.rs @@ -26,7 +26,7 @@ pub struct HttpResponse { } impl HttpResponse { - pub fn new(status: StatusCode, headers: HeaderMap, body: String) -> Self { + pub const fn new(status: StatusCode, headers: HeaderMap, body: String) -> Self { Self { status, headers, @@ -34,11 +34,11 @@ impl HttpResponse { } } - pub fn status(&self) -> StatusCode { + pub const fn status(&self) -> StatusCode { self.status } - pub fn headers(&self) -> &HeaderMap { + pub const fn headers(&self) -> &HeaderMap { &self.headers }