@@ -457,11 +457,11 @@ pub(crate) fn wait_for_parent() -> Result<()> {
457
457
458
458
pub ( crate ) fn do_add_to_path ( process : & Process ) -> Result < ( ) > {
459
459
let new_path = _with_path_cargo_home_bin ( _add_to_path, process) ?;
460
- _apply_new_path ( new_path) ?;
460
+ _apply_new_path ( new_path. as_deref ( ) ) ?;
461
461
do_add_to_programs ( process)
462
462
}
463
463
464
- fn _apply_new_path ( new_path : Option < String > ) -> Result < ( ) > {
464
+ fn _apply_new_path ( new_path : Option < & str > ) -> Result < ( ) > {
465
465
use std:: ptr;
466
466
use windows_sys:: Win32 :: Foundation :: * ;
467
467
use windows_sys:: Win32 :: UI :: WindowsAndMessaging :: {
@@ -478,7 +478,7 @@ fn _apply_new_path(new_path: Option<String>) -> Result<()> {
478
478
if new_path. is_empty ( ) {
479
479
environment. remove_value ( "PATH" ) ?;
480
480
} else {
481
- environment. set_string ( "PATH" , & new_path) ?;
481
+ environment. set_string ( "PATH" , new_path) ?;
482
482
}
483
483
484
484
// Tell other processes to update their environment
@@ -515,22 +515,22 @@ fn get_windows_path_var() -> Result<Option<String>> {
515
515
516
516
// Returns None if the existing old_path does not need changing, otherwise
517
517
// prepends the path_str to old_path, handling empty old_path appropriately.
518
- fn _add_to_path ( old_path : String , path_str : String ) -> Option < String > {
518
+ fn _add_to_path ( old_path : & str , path_str : & str ) -> Option < String > {
519
519
if old_path. is_empty ( ) {
520
- Some ( path_str)
521
- } else if old_path. contains ( & path_str) {
520
+ Some ( path_str. to_owned ( ) )
521
+ } else if old_path. contains ( path_str) {
522
522
None
523
523
} else {
524
- let mut new_path = path_str;
524
+ let mut new_path = path_str. to_owned ( ) ;
525
525
new_path. push ( ';' ) ;
526
- new_path += & old_path;
526
+ new_path += old_path;
527
527
Some ( new_path)
528
528
}
529
529
}
530
530
531
531
// Returns None if the existing old_path does not need changing
532
- fn _remove_from_path ( old_path : String , path_str : String ) -> Option < String > {
533
- let idx = old_path. find ( & path_str) ?;
532
+ fn _remove_from_path ( old_path : & str , path_str : & str ) -> Option < String > {
533
+ let idx = old_path. find ( path_str) ?;
534
534
// If there's a trailing semicolon (likely, since we probably added one
535
535
// during install), include that in the substring to remove. We don't search
536
536
// 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<String> {
552
552
553
553
fn _with_path_cargo_home_bin < F > ( f : F , process : & Process ) -> Result < Option < String > >
554
554
where
555
- F : FnOnce ( String , String ) -> Option < String > ,
555
+ F : FnOnce ( & str , & str ) -> Option < String > ,
556
556
{
557
- let windows_path = get_windows_path_var ( ) ?;
557
+ let path = get_windows_path_var ( ) ?;
558
+ let windows_path = path. as_deref ( ) ;
558
559
let mut path_str = process. cargo_home ( ) ?;
559
560
path_str. push ( "bin" ) ;
560
- Ok ( windows_path. and_then ( |old_path| f ( old_path, path_str. to_string_lossy ( ) . to_string ( ) ) ) )
561
+ Ok ( windows_path. and_then ( |old_path| f ( old_path, & path_str. to_string_lossy ( ) ) ) )
561
562
}
562
563
563
564
pub ( crate ) fn do_remove_from_path ( process : & Process ) -> Result < ( ) > {
564
565
let new_path = _with_path_cargo_home_bin ( _remove_from_path, process) ?;
565
- _apply_new_path ( new_path) ?;
566
+ _apply_new_path ( new_path. as_deref ( ) ) ?;
566
567
do_remove_from_programs ( )
567
568
}
568
569
@@ -760,8 +761,8 @@ mod tests {
760
761
assert_eq ! (
761
762
None ,
762
763
super :: _add_to_path(
763
- r"c:\users\example\.cargo\bin;foo" . to_string ( ) ,
764
- r"c:\users\example\.cargo\bin" . to_string ( )
764
+ r"c:\users\example\.cargo\bin;foo" ,
765
+ r"c:\users\example\.cargo\bin"
765
766
)
766
767
) ;
767
768
}
@@ -796,7 +797,7 @@ mod tests {
796
797
{
797
798
// Can't compare the Results as Eq isn't derived; thanks error-chain.
798
799
#![ allow( clippy:: unit_cmp) ]
799
- assert_eq ! ( ( ) , super :: _apply_new_path( Some ( "foo" . to_string ( ) ) ) . unwrap( ) ) ;
800
+ assert_eq ! ( ( ) , super :: _apply_new_path( Some ( "foo" ) ) . unwrap( ) ) ;
800
801
}
801
802
let environment = CURRENT_USER . create ( "Environment" ) . unwrap ( ) ;
802
803
let path = environment. get_string ( "PATH" ) . unwrap ( ) ;
@@ -815,7 +816,7 @@ mod tests {
815
816
{
816
817
// Can't compare the Results as Eq isn't derived; thanks error-chain.
817
818
#![ allow( clippy:: unit_cmp) ]
818
- assert_eq ! ( ( ) , super :: _apply_new_path( Some ( String :: new ( ) ) ) . unwrap( ) ) ;
819
+ assert_eq ! ( ( ) , super :: _apply_new_path( Some ( "" ) ) . unwrap( ) ) ;
819
820
}
820
821
let reg_value = environment. get_string ( "PATH" ) ;
821
822
match reg_value {
@@ -868,8 +869,8 @@ mod tests {
868
869
assert_eq ! (
869
870
"foo" ,
870
871
super :: _remove_from_path(
871
- r"c:\users\example\.cargo\bin;foo" . to_string ( ) ,
872
- r"c:\users\example\.cargo\bin" . to_string ( ) ,
872
+ r"c:\users\example\.cargo\bin;foo" ,
873
+ r"c:\users\example\.cargo\bin" ,
873
874
)
874
875
. unwrap( )
875
876
)
@@ -880,8 +881,8 @@ mod tests {
880
881
assert_eq ! (
881
882
"foo" ,
882
883
super :: _remove_from_path(
883
- r"foo;c:\users\example\.cargo\bin" . to_string ( ) ,
884
- r"c:\users\example\.cargo\bin" . to_string ( ) ,
884
+ r"foo;c:\users\example\.cargo\bin" ,
885
+ r"c:\users\example\.cargo\bin" ,
885
886
)
886
887
. unwrap( )
887
888
)
0 commit comments