Skip to content

Commit b5404f0

Browse files
committed
Simplify by removing FileArgs
1 parent 892ae41 commit b5404f0

File tree

3 files changed

+21
-35
lines changed

3 files changed

+21
-35
lines changed

src/librustc_driver/args/mod.rs

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22
use std::error;
33
use std::fmt;
44
use std::fs;
5-
use std::io::{self, BufRead};
5+
use std::io;
66
use std::str;
77
use 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-
4018
pub struct ArgsIter {
4119
base: env::ArgsOs,
42-
file: Option<Box<dyn Iterator<Item = Result<String, Error>>>>,
20+
file: std::vec::IntoIter<String>,
4321
}
4422

4523
impl 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,

src/librustc_driver/args/tests.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
use super::*;
22

3+
use std::str;
4+
35
fn want_args(v: impl IntoIterator<Item = &'static str>) -> Vec<String> {
46
v.into_iter().map(String::from).collect()
57
}
68

79
fn got_args(file: &[u8]) -> Result<Vec<String>, Error> {
8-
FileArgs::new(String::new(), file.to_vec()).lines().collect()
10+
let ret = str::from_utf8(file)
11+
.map_err(|_| Error::Utf8Error(None))?
12+
.lines()
13+
.map(ToString::to_string)
14+
.collect::<Vec<_>>();
15+
Ok(ret)
916
}
1017

1118
#[test]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: Argument 19 is not valid: Utf8 error in $DIR/commandline-argfile-badutf8.args
1+
error: Argument 18 is not valid: Utf8 error in $DIR/commandline-argfile-badutf8.args
22

0 commit comments

Comments
 (0)