Skip to content

Commit 0339b96

Browse files
Rawkytmimi
authored andcommitted
Avoid allocation in JsonEmitter::emit_footer
Write directly to the output instead of creating an intermediate `String`.
1 parent b23b699 commit 0339b96

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Diff for: src/emitter/json.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22
use crate::rustfmt_diff::{DiffLine, Mismatch, make_diff};
33
use serde::Serialize;
4-
use serde_json::to_string as to_json_string;
4+
use serde_json::to_writer as to_json_writer;
55

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

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

3233
fn emit_formatted_file(
@@ -252,7 +253,7 @@ mod tests {
252253
)
253254
.unwrap();
254255
let _ = emitter.emit_footer(&mut writer);
255-
let exp_json = to_json_string(&vec![MismatchedFile {
256+
let exp_json = serde_json::to_string(&vec![MismatchedFile {
256257
name: String::from(file_name),
257258
mismatches: vec![
258259
MismatchedBlock {
@@ -338,7 +339,7 @@ mod tests {
338339
}],
339340
};
340341

341-
let exp_json = to_json_string(&vec![exp_bin, exp_lib]).unwrap();
342+
let exp_json = serde_json::to_string(&vec![exp_bin, exp_lib]).unwrap();
342343
assert_eq!(&writer[..], format!("{exp_json}\n").as_bytes());
343344
}
344345
}

0 commit comments

Comments
 (0)