mirror of
https://github.com/denisidoro/navi
synced 2024-11-13 23:37:10 +00:00
Fix unnecessary allocations when reading file.
This commit is contained in:
parent
8c4100abea
commit
53c69aa72c
2 changed files with 9 additions and 9 deletions
|
@ -9,17 +9,14 @@ use std::fs::File;
|
|||
use std::io::{self, BufRead};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub fn read_lines<P>(filename: P) -> Result<Vec<String>, Error>
|
||||
pub fn read_lines<P>(filename: P) -> Result<impl Iterator<Item = Result<String, Error>>, Error>
|
||||
where
|
||||
P: AsRef<Path> + Display,
|
||||
P: AsRef<Path> + Display + Copy,
|
||||
{
|
||||
let error_string = format!("Failed to read lines from `{}`", filename);
|
||||
let file = File::open(filename)?;
|
||||
io::BufReader::new(file)
|
||||
let file = File::open(filename).with_context(|| format!("Failed to open file {}", filename))?;
|
||||
Ok(io::BufReader::new(file)
|
||||
.lines()
|
||||
.map(|line| line.map_err(Error::from))
|
||||
.collect::<Result<Vec<String>, Error>>()
|
||||
.with_context(|| error_string)
|
||||
.map(|line| line.map_err(Error::from)))
|
||||
}
|
||||
|
||||
pub fn pathbuf_to_string(pathbuf: PathBuf) -> Result<String, Error> {
|
||||
|
|
|
@ -145,7 +145,10 @@ fn read_file(
|
|||
|
||||
let (tag_width, comment_width) = *display::WIDTHS;
|
||||
|
||||
for (line_nr, line) in filesystem::read_lines(path)?.into_iter().enumerate() {
|
||||
for (line_nr, line_result) in filesystem::read_lines(path)?.enumerate() {
|
||||
let line = line_result
|
||||
.with_context(|| format!("Failed to read line nr.{} from `{}`", line_nr, path))?;
|
||||
|
||||
if should_break {
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue