use serde_json's to_writer

This commit is contained in:
D. Scott Boggs 2023-01-03 06:58:54 -05:00 committed by Scott Boggs
parent dbbdb8e5c0
commit 1e4b2e0adb
1 changed files with 1 additions and 7 deletions

View File

@ -46,13 +46,7 @@ pub fn to_vec(data: &Data) -> Result<Vec<u8>> {
/// Attempts to serialize a Data struct to something that implements the /// Attempts to serialize a Data struct to something that implements the
/// std::io::Write trait /// std::io::Write trait
pub fn to_writer<W: Write>(data: &Data, writer: W) -> Result<()> { pub fn to_writer<W: Write>(data: &Data, writer: W) -> Result<()> {
let mut buf_writer = BufWriter::new(writer); Ok(serde_json::to_writer(data, writer)?)
let vec = to_vec(data)?;
if vec.len() != buf_writer.write(&vec)? {
Err(crate::Error::NotAllBytesWritten)
} else {
Ok(())
}
} }
/// Attempts to serialize a Data struct to a file /// Attempts to serialize a Data struct to a file