Optimize Crates2Json::write_to_path: Use to_writer

which avoids allocating a `Vec<u8>` just to hold serialized data.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-21 22:18:08 +10:00
parent d7ae1f242b
commit c2ce265afa
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -60,7 +60,8 @@ impl Crates2Json {
} }
pub fn write_to_path(&self, path: impl AsRef<Path>) -> Result<(), Crates2JsonParseError> { pub fn write_to_path(&self, path: impl AsRef<Path>) -> Result<(), Crates2JsonParseError> {
fs::write(path, &serde_json::to_vec(&self)?)?; let file = fs::File::create(path.as_ref())?;
serde_json::to_writer(file, &self)?;
Ok(()) Ok(())
} }