Avoid unnecessary allocations.

This commit is contained in:
Stephen Chung 2022-05-07 16:29:20 +08:00
parent 4194e2c048
commit b4fea634b0
2 changed files with 10 additions and 4 deletions

View File

@ -156,11 +156,13 @@ pub fn format_map_as_json(map: &Map) -> String {
let mut result = String::from('{'); let mut result = String::from('{');
for (key, value) in map { for (key, value) in map {
use std::fmt::Write;
if result.len() > 1 { if result.len() > 1 {
result.push(','); result.push(',');
} }
result.push_str(&format!("{:?}", key)); write!(result, "{:?}", key).unwrap();
result.push(':'); result.push(':');
if let Some(val) = value.read_lock::<Map>() { if let Some(val) = value.read_lock::<Map>() {
@ -171,7 +173,7 @@ pub fn format_map_as_json(map: &Map) -> String {
if value.is::<()>() { if value.is::<()>() {
result.push_str("null"); result.push_str("null");
} else { } else {
result.push_str(&format!("{:?}", value)); write!(result, "{:?}", value).unwrap();
} }
} }

View File

@ -195,12 +195,16 @@ mod print_debug_functions {
result.push_str("#{"); result.push_str("#{");
map.iter_mut().enumerate().for_each(|(i, (k, v))| { map.iter_mut().enumerate().for_each(|(i, (k, v))| {
result.push_str(&format!( use std::fmt::Write;
write!(
result,
"{:?}: {}{}", "{:?}: {}{}",
k, k,
&print_with_func(FUNC_TO_DEBUG, &ctx, v), &print_with_func(FUNC_TO_DEBUG, &ctx, v),
if i < len - 1 { ", " } else { "" } if i < len - 1 { ", " } else { "" }
)); )
.unwrap();
}); });
result.push('}'); result.push('}');