Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid allocation in JsonEmitter::emit_footer #6335

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/emitter/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::rustfmt_diff::{DiffLine, Mismatch, make_diff};
use serde::Serialize;
use serde_json::to_string as to_json_string;
use serde_json::to_writer as to_json_writer;

#[derive(Debug, Default)]
pub(crate) struct JsonEmitter {
Expand All @@ -26,7 +26,8 @@ struct MismatchedFile {

impl Emitter for JsonEmitter {
fn emit_footer(&self, output: &mut dyn Write) -> Result<(), io::Error> {
writeln!(output, "{}", &to_json_string(&self.mismatched_files)?)
to_json_writer(&mut *output, &self.mismatched_files)?;
writeln!(output)
}

fn emit_formatted_file(
Expand Down Expand Up @@ -252,7 +253,7 @@ mod tests {
)
.unwrap();
let _ = emitter.emit_footer(&mut writer);
let exp_json = to_json_string(&vec![MismatchedFile {
let exp_json = serde_json::to_string(&vec![MismatchedFile {
name: String::from(file_name),
mismatches: vec![
MismatchedBlock {
Expand Down Expand Up @@ -338,7 +339,7 @@ mod tests {
}],
};

let exp_json = to_json_string(&vec![exp_bin, exp_lib]).unwrap();
let exp_json = serde_json::to_string(&vec![exp_bin, exp_lib]).unwrap();
assert_eq!(&writer[..], format!("{exp_json}\n").as_bytes());
}
}
Loading