@@ -2,7 +2,7 @@ use std::env;
22use std:: error;
33use std:: fmt;
44use std:: fs;
5- use std:: io:: { self , BufRead } ;
5+ use std:: io;
66use std:: str;
77use std:: sync:: atomic:: { AtomicBool , Ordering } ;
88
@@ -15,36 +15,14 @@ pub fn used_unstable_argsfile() -> bool {
1515 USED_ARGSFILE_FEATURE . load ( Ordering :: Relaxed )
1616}
1717
18- struct FileArgs {
19- path : String ,
20- input : Vec < u8 > ,
21- }
22-
23- impl FileArgs {
24- fn new ( path : String , input : Vec < u8 > ) -> Self {
25- FileArgs { path, input }
26- }
27-
28- fn lines ( self ) -> impl Iterator < Item = Result < String , Error > > {
29- let Self { input, path } = self ;
30- io:: Cursor :: new ( input) . lines ( ) . map ( move |res| {
31- let path = path. clone ( ) ;
32- res. map_err ( move |err| match err. kind ( ) {
33- io:: ErrorKind :: InvalidData => Error :: Utf8Error ( Some ( path) ) ,
34- _ => Error :: IOError ( path, err) ,
35- } )
36- } )
37- }
38- }
39-
4018pub struct ArgsIter {
4119 base : env:: ArgsOs ,
42- file : Option < Box < dyn Iterator < Item = Result < String , Error > > > > ,
20+ file : std :: vec :: IntoIter < String > ,
4321}
4422
4523impl ArgsIter {
4624 pub fn new ( ) -> Self {
47- ArgsIter { base : env:: args_os ( ) , file : None }
25+ ArgsIter { base : env:: args_os ( ) , file : vec ! [ ] . into_iter ( ) }
4826 }
4927}
5028
@@ -53,11 +31,8 @@ impl Iterator for ArgsIter {
5331
5432 fn next ( & mut self ) -> Option < Self :: Item > {
5533 loop {
56- if let Some ( ref mut file) = & mut self . file {
57- match file. next ( ) {
58- Some ( res) => return Some ( res. map_err ( From :: from) ) ,
59- None => self . file = None ,
60- }
34+ if let Some ( line) = self . file . next ( ) {
35+ return Some ( Ok ( line) ) ;
6136 }
6237
6338 let arg =
@@ -66,14 +41,18 @@ impl Iterator for ArgsIter {
6641 Some ( Err ( err) ) => return Some ( Err ( err) ) ,
6742 Some ( Ok ( ref arg) ) if arg. starts_with ( "@" ) => {
6843 let path = & arg[ 1 ..] ;
69- let lines = match fs:: read ( path) {
44+ let file = match fs:: read_to_string ( path) {
7045 Ok ( file) => {
7146 USED_ARGSFILE_FEATURE . store ( true , Ordering :: Relaxed ) ;
72- FileArgs :: new ( path. to_string ( ) , file) . lines ( )
47+ file
48+ }
49+ Err ( ref err) if err. kind ( ) == io:: ErrorKind :: InvalidData => {
50+ return Some ( Err ( Error :: Utf8Error ( Some ( path. to_string ( ) ) ) ) ) ;
7351 }
7452 Err ( err) => return Some ( Err ( Error :: IOError ( path. to_string ( ) , err) ) ) ,
7553 } ;
76- self . file = Some ( Box :: new ( lines) ) ;
54+ self . file =
55+ file. lines ( ) . map ( ToString :: to_string) . collect :: < Vec < _ > > ( ) . into_iter ( ) ;
7756 }
7857 Some ( Ok ( arg) ) => return Some ( Ok ( arg) ) ,
7958 None => return None ,
0 commit comments