@@ -291,21 +291,28 @@ impl Default for Cmd {
291
291
}
292
292
293
293
impl Cmd {
294
- pub fn add_arg ( mut self , arg : OsString ) -> Self {
295
- let arg_str = arg. to_string_lossy ( ) . to_string ( ) ;
294
+ pub fn add_arg < O > ( mut self , arg : O ) -> Self
295
+ where
296
+ O : AsRef < OsStr > ,
297
+ {
298
+ let arg_str = arg. as_ref ( ) . to_string_lossy ( ) . to_string ( ) ;
296
299
if arg_str != IGNORE_CMD && !self . args . iter ( ) . any ( |cmd| * cmd != IGNORE_CMD ) {
297
300
let v: Vec < & str > = arg_str. split ( '=' ) . collect ( ) ;
298
301
if v. len ( ) == 2 && v[ 0 ] . chars ( ) . all ( |c| c. is_ascii_alphanumeric ( ) || c == '_' ) {
299
302
self . vars . insert ( v[ 0 ] . into ( ) , v[ 1 ] . into ( ) ) ;
300
303
return self ;
301
304
}
302
- self . in_cmd_map = CMD_MAP . lock ( ) . unwrap ( ) . contains_key ( & arg) ;
305
+ self . in_cmd_map = CMD_MAP . lock ( ) . unwrap ( ) . contains_key ( arg. as_ref ( ) ) ;
303
306
}
304
- self . args . push ( arg) ;
307
+ self . args . push ( arg. as_ref ( ) . to_os_string ( ) ) ;
305
308
self
306
309
}
307
310
308
- pub fn add_args ( mut self , args : Vec < OsString > ) -> Self {
311
+ pub fn add_args < I , O > ( mut self , args : I ) -> Self
312
+ where
313
+ I : IntoIterator < Item = O > ,
314
+ O : AsRef < OsStr > ,
315
+ {
309
316
for arg in args {
310
317
self = self . add_arg ( arg) ;
311
318
}
@@ -583,6 +590,12 @@ impl CmdString {
583
590
}
584
591
}
585
592
593
+ impl AsRef < OsStr > for CmdString {
594
+ fn as_ref ( & self ) -> & OsStr {
595
+ self . 0 . as_ref ( )
596
+ }
597
+ }
598
+
586
599
impl < T : ?Sized + AsRef < OsStr > > From < & T > for CmdString {
587
600
fn from ( s : & T ) -> Self {
588
601
Self ( s. as_ref ( ) . into ( ) )
@@ -603,8 +616,8 @@ mod tests {
603
616
fn test_run_piped_cmds ( ) {
604
617
let mut current_dir = PathBuf :: new ( ) ;
605
618
assert ! ( Cmds :: default ( )
606
- . pipe( Cmd :: default ( ) . add_args( vec![ "echo" . into ( ) , "rust" . into ( ) ] ) )
607
- . pipe( Cmd :: default ( ) . add_args( vec![ "wc" . into ( ) ] ) )
619
+ . pipe( Cmd :: default ( ) . add_args( vec![ "echo" , "rust" ] ) )
620
+ . pipe( Cmd :: default ( ) . add_args( vec![ "wc" ] ) )
608
621
. run_cmd( & mut current_dir)
609
622
. is_ok( ) ) ;
610
623
}
@@ -614,16 +627,16 @@ mod tests {
614
627
let mut current_dir = PathBuf :: new ( ) ;
615
628
assert_eq ! (
616
629
Cmds :: default ( )
617
- . pipe( Cmd :: default ( ) . add_args( vec![ "echo" . into ( ) , "rust" . into ( ) ] ) )
630
+ . pipe( Cmd :: default ( ) . add_args( vec![ "echo" , "rust" ] ) )
618
631
. run_fun( & mut current_dir)
619
632
. unwrap( ) ,
620
633
"rust"
621
634
) ;
622
635
623
636
assert_eq ! (
624
637
Cmds :: default ( )
625
- . pipe( Cmd :: default ( ) . add_args( vec![ "echo" . into ( ) , "rust" . into ( ) ] ) )
626
- . pipe( Cmd :: default ( ) . add_args( vec![ "wc" . into ( ) , "-c" . into ( ) ] ) )
638
+ . pipe( Cmd :: default ( ) . add_args( vec![ "echo" , "rust" ] ) )
639
+ . pipe( Cmd :: default ( ) . add_args( vec![ "wc" , "-c" ] ) )
627
640
. run_fun( & mut current_dir)
628
641
. unwrap( )
629
642
. trim( ) ,
@@ -635,14 +648,14 @@ mod tests {
635
648
fn test_stdout_redirect ( ) {
636
649
let mut current_dir = PathBuf :: new ( ) ;
637
650
let tmp_file = "/tmp/file_echo_rust" ;
638
- let mut write_cmd = Cmd :: default ( ) . add_args ( vec ! [ "echo" . into ( ) , "rust" . into ( ) ] ) ;
651
+ let mut write_cmd = Cmd :: default ( ) . add_args ( vec ! [ "echo" , "rust" ] ) ;
639
652
write_cmd = write_cmd. add_redirect ( Redirect :: StdoutToFile ( PathBuf :: from ( tmp_file) , false ) ) ;
640
653
assert ! ( Cmds :: default ( )
641
654
. pipe( write_cmd)
642
655
. run_cmd( & mut current_dir)
643
656
. is_ok( ) ) ;
644
657
645
- let read_cmd = Cmd :: default ( ) . add_args ( vec ! [ "cat" . into ( ) , tmp_file. into ( ) ] ) ;
658
+ let read_cmd = Cmd :: default ( ) . add_args ( vec ! [ "cat" , tmp_file] ) ;
646
659
assert_eq ! (
647
660
Cmds :: default ( )
648
661
. pipe( read_cmd)
@@ -651,7 +664,7 @@ mod tests {
651
664
"rust"
652
665
) ;
653
666
654
- let cleanup_cmd = Cmd :: default ( ) . add_args ( vec ! [ "rm" . into ( ) , tmp_file. into ( ) ] ) ;
667
+ let cleanup_cmd = Cmd :: default ( ) . add_args ( vec ! [ "rm" , tmp_file] ) ;
655
668
assert ! ( Cmds :: default ( )
656
669
. pipe( cleanup_cmd)
657
670
. run_cmd( & mut current_dir)
0 commit comments