Sfoglia il codice sorgente

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.
Ben S 10 anni fa
parent
commit
ca65c981f1
2 ha cambiato i file con 8 aggiunte e 11 eliminazioni
  1. 6 2
      src/main.rs
  2. 2 9
      src/options.rs

+ 6 - 2
src/main.rs

@@ -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);

+ 2 - 9
src/options.rs

@@ -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]