Refactor string handling to use into() instead of to_string() for improved performance and consistency across the codebase. This change affects various modules including app.rs, clash.rs, config.rs, core.rs, service.rs, and others, ensuring that string conversions are streamlined and more idiomatic.

This commit is contained in:
Tunglies
2025-10-14 09:24:39 +08:00
parent 44eb781060
commit 924e7d1022
22 changed files with 167 additions and 161 deletions

View File

@@ -108,7 +108,7 @@ pub fn find_target_icons(target: &str) -> Result<Option<String>> {
match matching_files.first() {
Some(first_path) => {
let first = path_to_str(first_path)?;
Ok(Some(first.to_string()))
Ok(Some(first.into()))
}
None => Ok(None),
}

View File

@@ -22,13 +22,13 @@ pub fn get_supported_languages() -> Vec<String> {
if let Some(file_name) = entry.file_name().to_str()
&& let Some(lang) = file_name.strip_suffix(".json")
{
languages.push(lang.to_string());
languages.push(lang.into());
}
}
}
if languages.is_empty() {
languages.push(DEFAULT_LANGUAGE.to_string());
languages.push(DEFAULT_LANGUAGE.into());
}
languages
}
@@ -52,7 +52,7 @@ fn get_system_language() -> String {
.map(|locale| locale.to_lowercase())
.and_then(|locale| locale.split(['_', '-']).next().map(String::from))
.filter(|lang| get_supported_languages().contains(lang))
.unwrap_or_else(|| DEFAULT_LANGUAGE.to_string())
.unwrap_or_else(|| DEFAULT_LANGUAGE.into())
}
pub async fn t(key: &str) -> String {
@@ -69,7 +69,7 @@ pub async fn t(key: &str) -> String {
&& cache.0 == current_lang
&& let Some(text) = cache.1.get(key).and_then(|val| val.as_str())
{
return text.to_string();
return text.into();
}
}
@@ -79,7 +79,7 @@ pub async fn t(key: &str) -> String {
*cache = (current_lang.clone(), new_json);
if let Some(text) = cache.1.get(key).and_then(|val| val.as_str()) {
return text.to_string();
return text.into();
}
}
@@ -87,12 +87,12 @@ pub async fn t(key: &str) -> String {
&& let Some(default_json) = load_lang_file(DEFAULT_LANGUAGE)
&& let Ok(mut cache) = TRANSLATIONS.write()
{
*cache = (DEFAULT_LANGUAGE.to_string(), default_json);
*cache = (DEFAULT_LANGUAGE.into(), default_json);
if let Some(text) = cache.1.get(key).and_then(|val| val.as_str()) {
return text.to_string();
return text.into();
}
}
key.to_string()
key.into()
}

View File

@@ -102,7 +102,7 @@ pub async fn service_writer_config() -> Result<WriterConfig> {
verge.app_log_max_count.unwrap_or(8),
)
};
let service_log_dir = dirs::path_to_str(&service_log_dir()?)?.to_string();
let service_log_dir = dirs::path_to_str(&service_log_dir()?)?.into();
Ok(WriterConfig {
directory: service_log_dir,
@@ -437,7 +437,7 @@ pub async fn init_resources() -> Result<()> {
};
if src_path.exists() && !dest_path.exists() {
handle_copy(src_path.clone(), dest_path.clone(), file.to_string()).await;
handle_copy(src_path.clone(), dest_path.clone(), (*file).into()).await;
continue;
}
@@ -447,12 +447,12 @@ pub async fn init_resources() -> Result<()> {
match (src_modified, dest_modified) {
(Ok(src_modified), Ok(dest_modified)) => {
if src_modified > dest_modified {
handle_copy(src_path.clone(), dest_path.clone(), file.to_string()).await;
handle_copy(src_path.clone(), dest_path.clone(), (*file).into()).await;
}
}
_ => {
logging!(debug, Type::Setup, "failed to get modified '{}'", file);
handle_copy(src_path.clone(), dest_path.clone(), file.to_string()).await;
handle_copy(src_path.clone(), dest_path.clone(), (*file).into()).await;
}
};
}
@@ -506,7 +506,7 @@ pub async fn startup_script() -> Result<()> {
let script_path = {
let verge = Config::verge().await;
let verge = verge.latest_ref();
verge.startup_script.clone().unwrap_or("".to_string())
verge.startup_script.clone().unwrap_or("".into())
};
if script_path.is_empty() {

View File

@@ -75,8 +75,8 @@ pub fn embed_server() {
} else {
logging!(error, Type::Window, "轻量模式退出失败,无法恢复应用窗口");
};
Ok::<_, warp::Rejection>(warp::reply::with_status(
"ok".to_string(),
Ok::<_, warp::Rejection>(warp::reply::with_status::<String>(
"ok".into(),
warp::http::StatusCode::OK,
))
});
@@ -88,7 +88,7 @@ pub fn embed_server() {
.latest_ref()
.pac_file_content
.clone()
.unwrap_or(DEFAULT_PAC.to_string());
.unwrap_or(DEFAULT_PAC.into());
let mixed_port = verge_config
.latest_ref()
@@ -115,7 +115,7 @@ pub fn embed_server() {
tokio::task::spawn_local(async move {
logging_error!(Type::Setup, resolve::resolve_scheme(param).await);
});
warp::reply::with_status("ok".to_string(), warp::http::StatusCode::OK)
warp::reply::with_status::<String>("ok".into(), warp::http::StatusCode::OK)
});
let commands = visible.or(scheme).or(pac);