Skip to content

Commit eae9569

Browse files
author
hyd-dev
committed
Add ArgFlagValueIter
1 parent 903bfd8 commit eae9569

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

cargo-miri/bin.rs

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::env;
22
use std::ffi::OsString;
33
use std::fs::{self, File};
44
use std::io::{self, BufRead, BufReader, BufWriter, Write};
5+
use std::iter::TakeWhile;
56
use std::ops::Not;
67
use std::path::{Path, PathBuf};
78
use std::process::Command;
@@ -89,31 +90,49 @@ fn has_arg_flag(name: &str) -> bool {
8990
args.any(|val| val == name)
9091
}
9192

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,
103104
}
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+
}
113127
}
114128
}
115129
}
116130

131+
/// Gets the value of a `--flag`.
132+
fn get_arg_flag_value(name: &str) -> Option<String> {
133+
ArgFlagValueIter::new(name).next()
134+
}
135+
117136
/// Returns the path to the `miri` binary
118137
fn find_miri() -> PathBuf {
119138
if let Some(path) = env::var_os("MIRI") {

0 commit comments

Comments
 (0)