Avoid cloning the file names vector

By taking the file names as a mutable vector, we can avoid having to allocate a new one when it’s empty. The recent changes to Options::getopts have made it more obvious that we could move the same vector out of getopts’s matches, instead of cloning it there.
This commit is contained in:
Ben S 2015-11-15 15:52:55 +00:00
parent 2efaf7ec45
commit ca65c981f1
2 changed files with 8 additions and 11 deletions

View File

@ -42,10 +42,14 @@ struct Exa {
}
impl Exa {
fn run(&mut self, args_file_names: &[String]) {
fn run(&mut self, mut args_file_names: Vec<String>) {
let mut files = Vec::new();
let mut dirs = Vec::new();
if args_file_names.is_empty() {
args_file_names.push(".".to_owned());
}
for file_name in args_file_names.iter() {
match File::from_path(Path::new(&file_name), None) {
Err(e) => {
@ -145,7 +149,7 @@ fn main() {
match Options::getopts(&args) {
Ok((options, paths)) => {
let mut exa = Exa { options: options };
exa.run(&paths);
exa.run(paths);
},
Err(e) => {
println!("{}", e);

View File

@ -83,15 +83,8 @@ impl Options {
return Err(Misfire::Version);
}
let path_strs = if matches.free.is_empty() {
vec![ ".".to_string() ]
}
else {
matches.free.clone()
};
let options = try!(Options::deduce(&matches));
Ok((options, path_strs))
Ok((options, matches.free))
}
/// Whether the View specified in this set of options includes a Git
@ -611,7 +604,7 @@ mod test {
#[test]
fn no_args() {
let args = Options::getopts(&[]).unwrap().1;
assert_eq!(args, vec![ ".".to_string() ])
assert!(args.is_empty()); // Listing the `.` directory is done in main.rs
}
#[test]