1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-07 08:27:46 +00:00

Add a BufWriter for writes out to temp files

This commit is contained in:
electricboogie 2021-04-25 15:42:36 -05:00
parent 733949b2e7
commit 5fb7014c2b

View file

@ -4,7 +4,7 @@ use std::collections::VecDeque;
use std::error::Error; use std::error::Error;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::SeekFrom::Start; use std::io::SeekFrom::Start;
use std::io::{BufRead, BufReader, Seek, Write}; use std::io::{BufRead, BufReader, BufWriter, Seek, Write};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::path::PathBuf; use std::path::PathBuf;
@ -232,12 +232,14 @@ where
} }
fn write_chunk(&self, file: &PathBuf, chunk: &mut Vec<Line>) -> Result<(), Box<dyn Error>> { fn write_chunk(&self, file: &PathBuf, chunk: &mut Vec<Line>) -> Result<(), Box<dyn Error>> {
let mut new_file = OpenOptions::new().create(true).append(true).open(file)?; let new_file = OpenOptions::new().create(true).append(true).open(file)?;
let mut buf_write = Box::new(BufWriter::new(new_file)) as Box<dyn Write>;
for s in chunk { for s in chunk {
let mut serialized = serde_json::to_string(&s).expect("JSON write error: "); let mut serialized = serde_json::to_string(&s).expect("JSON write error: ");
serialized.push_str("\n"); serialized.push_str("\n");
new_file.write_all(serialized.as_bytes())?; buf_write.write(serialized.as_bytes())?;
} }
buf_write.flush()?;
Ok(()) Ok(())
} }