Mercurial > lbo > hg > localmr
changeset 16:2b2676d774b1
Remove unnecessary type parameters in Lines reader
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 31 Jan 2016 12:52:22 +0000 |
parents | 8a15fbf8589f |
children | 5a7f556c223d |
files | src/formats/lines.rs |
diffstat | 1 files changed, 35 insertions(+), 42 deletions(-) [+] |
line wrap: on
line diff
--- a/src/formats/lines.rs Sun Jan 31 11:40:57 2016 +0000 +++ b/src/formats/lines.rs Sun Jan 31 12:52:22 2016 +0000 @@ -13,53 +13,47 @@ src: Box<LinesIterator<Src>>, } -/// Read lines from text files or other inputs. -/// When reading from files, all reads are buffered by a BufReader with the -/// default capacity. -// bogus type parameter -impl LinesReader<fs::File> { - /// Returns a LinesReader reading lines from stdin. - pub fn new_from_stdin() -> LinesReader<io::Stdin> { - LinesReader { src: Box::new(io::BufReader::new(io::stdin()).lines()) } - } +/// Returns a LinesReader reading lines from stdin. +pub fn new_from_stdin() -> LinesReader<io::Stdin> { + LinesReader { src: Box::new(io::BufReader::new(io::stdin()).lines()) } +} - /// Returns a LinesReader reading from the given file. If you have several - /// files, you can easily use the chain() method to chain several readers. - pub fn new_from_file(path: &String) -> io::Result<LinesReader<fs::File>> { - fs::OpenOptions::new() - .read(true) - .open(path) - .map(move |f| LinesReader { src: Box::new(io::BufReader::new(f).lines()) }) - } +/// Returns a LinesReader reading from the given file. If you have several +/// files, you can easily use the chain() method to chain several readers. +pub fn new_from_file(path: &String) -> io::Result<LinesReader<fs::File>> { + fs::OpenOptions::new() + .read(true) + .open(path) + .map(move |f| LinesReader { src: Box::new(io::BufReader::new(f).lines()) }) +} - /// Returns a LinesReader reading from all files in the given directory that have - /// a given suffix. (This needs to use dynamic dispatch internally, because otherwise - /// the type would need to represent the number of files that are used; the overhead however - /// is low compared to disk accesses). - pub fn new_from_dir(path: &String, with_suffix: &String) -> io::Result<LinesReader<Box<Read>>> { - let mut reader: Box<Read> = Box::new(io::empty()); - let dir = try!(fs::read_dir(path)); +/// Returns a LinesReader reading from all files in the given directory that have +/// a given suffix. (This needs to use dynamic dispatch internally, because otherwise +/// the type would need to represent the number of files that are used; the overhead however +/// is low compared to disk accesses). +pub fn new_from_dir(path: &String, with_suffix: &String) -> io::Result<LinesReader<Box<Read>>> { + let mut reader: Box<Read> = Box::new(io::empty()); + let dir = try!(fs::read_dir(path)); - for entry in dir { - let name; - match entry { - Err(e) => { - println!("Could not read file from {:?}: {}", path, e); - continue; - } - Ok(direntry) => name = direntry.path(), + for entry in dir { + let name; + match entry { + Err(e) => { + println!("Could not read file from {:?}: {}", path, e); + continue; } + Ok(direntry) => name = direntry.path(), + } - // ugh - if String::from(&*name.to_string_lossy()).ends_with(with_suffix) { - match fs::OpenOptions::new().read(true).open(name.clone()) { - Err(e) => println!("Could not open file {:?}: {}", name, e), - Ok(f) => reader = Box::new(reader.chain(f)), - } + // ugh + if String::from(&*name.to_string_lossy()).ends_with(with_suffix) { + match fs::OpenOptions::new().read(true).open(name.clone()) { + Err(e) => println!("Could not open file {:?}: {}", name, e), + Ok(f) => reader = Box::new(reader.chain(f)), } } - Ok(LinesReader { src: Box::new(io::BufReader::new(reader).lines()) }) } + Ok(LinesReader { src: Box::new(io::BufReader::new(reader).lines()) }) } /// Iterate over the lines from a LinesReader. @@ -79,13 +73,12 @@ #[cfg(test)] mod test { use formats::lines; - use std::fs; #[test] fn test_read_file() { let file = "Cargo.toml"; let it; - match lines::LinesReader::<fs::File>::new_from_file(&String::from(file)) { + match lines::new_from_file(&String::from(file)) { Err(e) => panic!("{}", e), Ok(r) => it = r, } @@ -100,7 +93,7 @@ let path = String::from("src/"); let suffix = String::from(".rs"); let it; - match lines::LinesReader::<fs::File>::new_from_dir(&path, &suffix) { + match lines::new_from_dir(&path, &suffix) { Err(e) => panic!("{}", e), Ok(r) => it = r, }