From 537b5b38cccda20fb60bf54377ffd695bab5d885 Mon Sep 17 00:00:00 2001 From: Jeremy Fitzhardinge Date: Sat, 9 Sep 2023 11:30:23 -0700 Subject: [PATCH] Use serde_json::to_writer for JsonEmitter::emit Avoids an unnecessary intermediate string. --- compiler/rustc_errors/src/json.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index 7b92ce95f830..2f7145ceadc4 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -141,11 +141,12 @@ impl JsonEmitter { fn emit(&mut self, val: EmitTyped<'_>) -> io::Result<()> { if self.pretty { - writeln!(self.dst, "{}", serde_json::to_string_pretty(&val).unwrap()) + serde_json::to_writer_pretty(&mut *self.dst, &val)? } else { - writeln!(self.dst, "{}", serde_json::to_string(&val).unwrap()) - } - .and_then(|_| self.dst.flush()) + serde_json::to_writer(&mut *self.dst, &val)? + }; + self.dst.write_all(b"\n")?; + self.dst.flush() } }