refactor: change function definitions to const for improved performance and clarity

This commit is contained in:
Tunglies
2025-11-06 10:47:25 +08:00
parent aaf3ebe547
commit 5a8e83cd49
14 changed files with 18 additions and 17 deletions

View File

@@ -238,3 +238,4 @@ redundant_clone = "deny"
# option_if_let_else = "deny" // 过于激进,暂时不开启 # option_if_let_else = "deny" // 过于激进,暂时不开启
needless_pass_by_ref_mut = "deny" needless_pass_by_ref_mut = "deny"
needless_collect = "deny" needless_collect = "deny"
missing_const_for_fn = "deny"

View File

@@ -17,7 +17,7 @@ mod platform {
mod platform { mod platform {
use super::CmdResult; use super::CmdResult;
pub fn invoke_uwp_tool() -> CmdResult { pub const fn invoke_uwp_tool() -> CmdResult {
Ok(()) Ok(())
} }
} }

View File

@@ -611,6 +611,6 @@ impl PrfItem {
} }
// 向前兼容,默认为订阅启用自动更新 // 向前兼容,默认为订阅启用自动更新
fn default_allow_auto_update() -> Option<bool> { const fn default_allow_auto_update() -> Option<bool> {
Some(true) Some(true)
} }

View File

@@ -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() self.current.as_ref()
} }
/// get items ref /// get items ref
pub fn get_items(&self) -> Option<&Vec<PrfItem>> { pub const fn get_items(&self) -> Option<&Vec<PrfItem>> {
self.items.as_ref() self.items.as_ref()
} }

View File

@@ -531,7 +531,7 @@ impl IVerge {
patch!(enable_external_controller); patch!(enable_external_controller);
} }
pub fn get_singleton_port() -> u16 { pub const fn get_singleton_port() -> u16 {
crate::constants::network::ports::SINGLETON_SERVER crate::constants::network::ports::SINGLETON_SERVER
} }

View File

@@ -45,7 +45,7 @@ enum Operation {
} }
impl Operation { impl Operation {
fn timeout(&self) -> u64 { const fn timeout(&self) -> u64 {
match self { match self {
Self::Upload => TIMEOUT_UPLOAD, Self::Upload => TIMEOUT_UPLOAD,
Self::Download => TIMEOUT_DOWNLOAD, Self::Download => TIMEOUT_DOWNLOAD,

View File

@@ -84,7 +84,7 @@ impl fmt::Display for SystemHotkey {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
impl SystemHotkey { impl SystemHotkey {
pub fn function(self) -> HotkeyFunction { pub const fn function(self) -> HotkeyFunction {
match self { match self {
Self::CmdQ => HotkeyFunction::Quit, Self::CmdQ => HotkeyFunction::Quit,
Self::CmdW => HotkeyFunction::Hide, Self::CmdW => HotkeyFunction::Hide,

View File

@@ -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!( matches!(
kind, kind,
std::io::ErrorKind::ConnectionAborted std::io::ErrorKind::ConnectionAborted

View File

@@ -449,7 +449,7 @@ impl ServiceManager {
Self(ServiceStatus::Unavailable("Need Checks".into())) Self(ServiceStatus::Unavailable("Need Checks".into()))
} }
pub fn config() -> Option<clash_verge_service_ipc::IpcConfig> { pub const fn config() -> Option<clash_verge_service_ipc::IpcConfig> {
Some(clash_verge_service_ipc::IpcConfig { Some(clash_verge_service_ipc::IpcConfig {
default_timeout: Duration::from_millis(30), default_timeout: Duration::from_millis(30),
retry_delay: Duration::from_millis(250), retry_delay: Duration::from_millis(250),

View File

@@ -16,7 +16,7 @@ pub struct CoreConfigValidator {
} }
impl CoreConfigValidator { impl CoreConfigValidator {
pub fn new() -> Self { pub const fn new() -> Self {
Self { Self {
is_processing: AtomicBool::new(false), is_processing: AtomicBool::new(false),
} }

View File

@@ -36,7 +36,7 @@ impl From<u8> for LightweightState {
} }
impl LightweightState { impl LightweightState {
fn as_u8(self) -> u8 { const fn as_u8(self) -> u8 {
self as u8 self as u8
} }
} }

View File

@@ -20,7 +20,7 @@ impl Drop for CommandChildGuard {
} }
impl CommandChildGuard { impl CommandChildGuard {
pub fn new(child: CommandChild) -> Self { pub const fn new(child: CommandChild) -> Self {
Self(Some(child)) Self(Some(child))
} }

View File

@@ -506,7 +506,7 @@ pub fn init_scheme() -> Result<()> {
Ok(()) Ok(())
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub fn init_scheme() -> Result<()> { pub const fn init_scheme() -> Result<()> {
Ok(()) Ok(())
} }

View File

@@ -26,7 +26,7 @@ pub struct HttpResponse {
} }
impl 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 { Self {
status, status,
headers, headers,
@@ -34,11 +34,11 @@ impl HttpResponse {
} }
} }
pub fn status(&self) -> StatusCode { pub const fn status(&self) -> StatusCode {
self.status self.status
} }
pub fn headers(&self) -> &HeaderMap { pub const fn headers(&self) -> &HeaderMap {
&self.headers &self.headers
} }