From 5b6be5cd3b9057a41e02f4608220e7e563c52fdf Mon Sep 17 00:00:00 2001 From: Rean Fei Date: Fri, 21 Jun 2024 13:28:52 +0800 Subject: [PATCH] Change type to `&str` --- src/cli/self_update/windows.rs | 45 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/cli/self_update/windows.rs b/src/cli/self_update/windows.rs index 912b5943fbe..64c79a026cb 100644 --- a/src/cli/self_update/windows.rs +++ b/src/cli/self_update/windows.rs @@ -457,11 +457,11 @@ pub(crate) fn wait_for_parent() -> Result<()> { pub(crate) fn do_add_to_path(process: &Process) -> Result<()> { let new_path = _with_path_cargo_home_bin(_add_to_path, process)?; - _apply_new_path(new_path)?; + _apply_new_path(new_path.as_deref())?; do_add_to_programs(process) } -fn _apply_new_path(new_path: Option) -> Result<()> { +fn _apply_new_path(new_path: Option<&str>) -> Result<()> { use std::ptr; use windows_sys::Win32::Foundation::*; use windows_sys::Win32::UI::WindowsAndMessaging::{ @@ -478,7 +478,7 @@ fn _apply_new_path(new_path: Option) -> Result<()> { if new_path.is_empty() { environment.remove_value("PATH")?; } else { - environment.set_string("PATH", &new_path)?; + environment.set_string("PATH", new_path)?; } // Tell other processes to update their environment @@ -515,22 +515,22 @@ fn get_windows_path_var() -> Result> { // Returns None if the existing old_path does not need changing, otherwise // prepends the path_str to old_path, handling empty old_path appropriately. -fn _add_to_path(old_path: String, path_str: String) -> Option { +fn _add_to_path(old_path: &str, path_str: &str) -> Option { if old_path.is_empty() { - Some(path_str) - } else if old_path.contains(&path_str) { + Some(path_str.to_owned()) + } else if old_path.contains(path_str) { None } else { - let mut new_path = path_str; + let mut new_path = path_str.to_owned(); new_path.push(';'); - new_path += &old_path; + new_path += old_path; Some(new_path) } } // Returns None if the existing old_path does not need changing -fn _remove_from_path(old_path: String, path_str: String) -> Option { - let idx = old_path.find(&path_str)?; +fn _remove_from_path(old_path: &str, path_str: &str) -> Option { + let idx = old_path.find(path_str)?; // If there's a trailing semicolon (likely, since we probably added one // during install), include that in the substring to remove. We don't search // for that to find the string, because if it's the last string in the path, @@ -552,17 +552,18 @@ fn _remove_from_path(old_path: String, path_str: String) -> Option { fn _with_path_cargo_home_bin(f: F, process: &Process) -> Result> where - F: FnOnce(String, String) -> Option, + F: FnOnce(&str, &str) -> Option, { - let windows_path = get_windows_path_var()?; + let path = get_windows_path_var()?; + let windows_path = path.as_deref(); let mut path_str = process.cargo_home()?; path_str.push("bin"); - Ok(windows_path.and_then(|old_path| f(old_path, path_str.to_string_lossy().to_string()))) + Ok(windows_path.and_then(|old_path| f(old_path, &path_str.to_string_lossy()))) } pub(crate) fn do_remove_from_path(process: &Process) -> Result<()> { let new_path = _with_path_cargo_home_bin(_remove_from_path, process)?; - _apply_new_path(new_path)?; + _apply_new_path(new_path.as_deref())?; do_remove_from_programs() } @@ -760,8 +761,8 @@ mod tests { assert_eq!( None, super::_add_to_path( - r"c:\users\example\.cargo\bin;foo".to_string(), - r"c:\users\example\.cargo\bin".to_string() + r"c:\users\example\.cargo\bin;foo", + r"c:\users\example\.cargo\bin" ) ); } @@ -796,7 +797,7 @@ mod tests { { // Can't compare the Results as Eq isn't derived; thanks error-chain. #![allow(clippy::unit_cmp)] - assert_eq!((), super::_apply_new_path(Some("foo".to_string())).unwrap()); + assert_eq!((), super::_apply_new_path(Some("foo")).unwrap()); } let environment = CURRENT_USER.create("Environment").unwrap(); let path = environment.get_string("PATH").unwrap(); @@ -815,7 +816,7 @@ mod tests { { // Can't compare the Results as Eq isn't derived; thanks error-chain. #![allow(clippy::unit_cmp)] - assert_eq!((), super::_apply_new_path(Some(String::new())).unwrap()); + assert_eq!((), super::_apply_new_path(Some("")).unwrap()); } let reg_value = environment.get_string("PATH"); match reg_value { @@ -868,8 +869,8 @@ mod tests { assert_eq!( "foo", super::_remove_from_path( - r"c:\users\example\.cargo\bin;foo".to_string(), - r"c:\users\example\.cargo\bin".to_string(), + r"c:\users\example\.cargo\bin;foo", + r"c:\users\example\.cargo\bin", ) .unwrap() ) @@ -880,8 +881,8 @@ mod tests { assert_eq!( "foo", super::_remove_from_path( - r"foo;c:\users\example\.cargo\bin".to_string(), - r"c:\users\example\.cargo\bin".to_string(), + r"foo;c:\users\example\.cargo\bin", + r"c:\users\example\.cargo\bin", ) .unwrap() )