From ee9eddd85169035ed3410774f884ee2e9d148b69 Mon Sep 17 00:00:00 2001 From: Andrew Houts <16907671+ahouts@users.noreply.github.com> Date: Sun, 5 Dec 2021 12:38:58 -0600 Subject: [PATCH] avoid unnecessary allocation (#4178) --- crates/nu-json/src/ser.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/nu-json/src/ser.rs b/crates/nu-json/src/ser.rs index 01ea63e86..200b90f1e 100644 --- a/crates/nu-json/src/ser.rs +++ b/crates/nu-json/src/ser.rs @@ -925,11 +925,8 @@ fn escape_char(wr: &mut W, value: char) -> Result<()> where W: io::Write, { - // FIXME: this allocation is required in order to be compatible with stable - // rust, which doesn't support encoding a `char` into a stack buffer. - let mut s = String::new(); - s.push(value); - escape_bytes(wr, s.as_bytes()) + let mut scratch = [0_u8; 4]; + escape_bytes(wr, value.encode_utf8(&mut scratch).as_bytes()) } fn fmt_f32_or_null(wr: &mut W, value: f32) -> Result<()>