@@ -2,6 +2,7 @@ use std::env;
2
2
use std:: ffi:: OsString ;
3
3
use std:: fs:: { self , File } ;
4
4
use std:: io:: { self , BufRead , BufReader , BufWriter , Write } ;
5
+ use std:: iter:: TakeWhile ;
5
6
use std:: ops:: Not ;
6
7
use std:: path:: { Path , PathBuf } ;
7
8
use std:: process:: Command ;
@@ -89,31 +90,49 @@ fn has_arg_flag(name: &str) -> bool {
89
90
args. any ( |val| val == name)
90
91
}
91
92
92
- /// Gets the value of a `--flag`.
93
- fn get_arg_flag_value ( name : & str ) -> Option < String > {
94
- // Stop searching at `--`.
95
- let mut args = std :: env :: args ( ) . take_while ( |val| val != "--" ) ;
96
- loop {
97
- let arg = match args . next ( ) {
98
- Some ( arg ) => arg ,
99
- None => return None ,
100
- } ;
101
- if !arg . starts_with ( name ) {
102
- continue ;
93
+ struct ArgFlagValueIter < ' a > {
94
+ args : TakeWhile < env :: Args , fn ( & String ) -> bool > ,
95
+ name : & ' a str ,
96
+ }
97
+
98
+ impl < ' a > ArgFlagValueIter < ' a > {
99
+ fn new ( name : & ' a str ) -> Self {
100
+ Self {
101
+ // Stop searching at `--`.
102
+ args : env :: args ( ) . take_while ( |val| val != "--" ) ,
103
+ name ,
103
104
}
104
- // Strip leading `name`.
105
- let suffix = & arg[ name. len ( ) ..] ;
106
- if suffix. is_empty ( ) {
107
- // This argument is exactly `name`; the next one is the value.
108
- return args. next ( ) ;
109
- } else if suffix. starts_with ( '=' ) {
110
- // This argument is `name=value`; get the value.
111
- // Strip leading `=`.
112
- return Some ( suffix[ 1 ..] . to_owned ( ) ) ;
105
+ }
106
+ }
107
+
108
+ impl Iterator for ArgFlagValueIter < ' _ > {
109
+ type Item = String ;
110
+
111
+ fn next ( & mut self ) -> Option < Self :: Item > {
112
+ loop {
113
+ let arg = self . args . next ( ) ?;
114
+ if !arg. starts_with ( self . name ) {
115
+ continue ;
116
+ }
117
+ // Strip leading `name`.
118
+ let suffix = & arg[ self . name . len ( ) ..] ;
119
+ if suffix. is_empty ( ) {
120
+ // This argument is exactly `name`; the next one is the value.
121
+ return self . args . next ( ) ;
122
+ } else if suffix. starts_with ( '=' ) {
123
+ // This argument is `name=value`; get the value.
124
+ // Strip leading `=`.
125
+ return Some ( suffix[ 1 ..] . to_owned ( ) ) ;
126
+ }
113
127
}
114
128
}
115
129
}
116
130
131
+ /// Gets the value of a `--flag`.
132
+ fn get_arg_flag_value ( name : & str ) -> Option < String > {
133
+ ArgFlagValueIter :: new ( name) . next ( )
134
+ }
135
+
117
136
/// Returns the path to the `miri` binary
118
137
fn find_miri ( ) -> PathBuf {
119
138
if let Some ( path) = env:: var_os ( "MIRI" ) {
0 commit comments