fix(bench): propagate I/O errors in append_lines instead of silently defaulting to 0 (closes #45)

This commit is contained in:
dailz
2026-06-07 09:13:37 +08:00
parent 97a2c6a925
commit f6081b9fe9

View File

@@ -83,11 +83,11 @@ pub fn generate_growable_file(dir: &Path) -> std::io::Result<PathBuf> {
/// Append `count` lines to the file /// Append `count` lines to the file
pub fn append_lines(path: &Path, count: usize) -> std::io::Result<()> { pub fn append_lines(path: &Path, count: usize) -> std::io::Result<()> {
let existing_lines = count_existing_lines(path)?;
let mut file = BufWriter::with_capacity( let mut file = BufWriter::with_capacity(
64 * 1024, 64 * 1024,
fs::OpenOptions::new().append(true).open(path)?, fs::OpenOptions::new().append(true).open(path)?,
); );
let existing_lines = count_existing_lines(path).unwrap_or(0);
for i in 0..count { for i in 0..count {
writeln!( writeln!(
file, file,
@@ -117,7 +117,12 @@ pub fn rotate_file(path: &Path) -> std::io::Result<PathBuf> {
fn count_existing_lines(path: &Path) -> std::io::Result<u64> { fn count_existing_lines(path: &Path) -> std::io::Result<u64> {
let file = fs::File::open(path)?; let file = fs::File::open(path)?;
let reader = BufReader::new(file); let reader = BufReader::new(file);
Ok(reader.lines().count() as u64) let mut count = 0u64;
for line in reader.lines() {
line?;
count += 1;
}
Ok(count)
} }
#[cfg(test)] #[cfg(test)]