Skip to content

Commit 43c70e6

Browse files
nbdd0121jwnrt
authored andcommitted
Remove most of to_string calls
Changed to code to take `impl Display` as input rather than string, so most to_string calls and format calls can be removed. Signed-off-by: Gary Guo <[email protected]>
1 parent 5f52c61 commit 43c70e6

File tree

2 files changed

+67
-63
lines changed

2 files changed

+67
-63
lines changed

src/json.rs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl JsonEmitter {
234234

235235
fn emit_bytes<W: fmt::Write>(&mut self, w: &mut W, bytes: &[u8]) -> Result<()> {
236236
self.level += 1;
237-
self.writeln(w, &self.color.aggregate.paint("[").to_string())?;
237+
self.writeln(w, self.color.aggregate.paint("["))?;
238238
self.emit_indent(w)?;
239239
for (i, value) in bytes.iter().enumerate() {
240240
if i > 0 {
@@ -250,14 +250,14 @@ impl JsonEmitter {
250250
self.writeln(w, "")?;
251251
self.level -= 1;
252252
self.emit_indent(w)?;
253-
write!(w, "{}", &self.color.aggregate.paint("]"))?;
253+
write!(w, "{}", self.color.aggregate.paint("]"))?;
254254
Ok(())
255255
}
256256

257257
// TODO: Can this function be rewritten to be less complex?
258258
fn emit_sequence<W: fmt::Write>(&mut self, w: &mut W, sequence: &[Document]) -> Result<()> {
259259
self.level += 1;
260-
self.writeln(w, &self.color.aggregate.paint("[").to_string())?;
260+
self.writeln(w, self.color.aggregate.paint("["))?;
261261
if !sequence.is_empty() {
262262
self.emit_indent(w)?;
263263
}
@@ -289,7 +289,7 @@ impl JsonEmitter {
289289
if !val_done {
290290
self.emit_node(w, node)?;
291291
if i != last {
292-
write!(w, "{}", &self.color.punctuation.paint(","))?;
292+
write!(w, "{}", self.color.punctuation.paint(","))?;
293293
}
294294
val_done = true;
295295
need_eol = true;
@@ -300,7 +300,7 @@ impl JsonEmitter {
300300
} else {
301301
self.emit_node(w, value)?;
302302
if i != last {
303-
write!(w, "{}", &self.color.punctuation.paint(","))?;
303+
write!(w, "{}", self.color.punctuation.paint(","))?;
304304
}
305305
need_eol = true;
306306
}
@@ -310,7 +310,7 @@ impl JsonEmitter {
310310
}
311311
self.level -= 1;
312312
self.emit_indent(w)?;
313-
write!(w, "{}", &self.color.aggregate.paint("]"))?;
313+
write!(w, "{}", self.color.aggregate.paint("]"))?;
314314
Ok(())
315315
}
316316

@@ -332,7 +332,7 @@ impl JsonEmitter {
332332
// TODO: Can this function be rewritten to be less complex?
333333
fn emit_mapping<W: fmt::Write>(&mut self, w: &mut W, mapping: &[Document]) -> Result<()> {
334334
self.level += 1;
335-
self.writeln(w, &self.color.aggregate.paint("{").to_string())?;
335+
self.writeln(w, self.color.aggregate.paint("{"))?;
336336
if !mapping.is_empty() {
337337
self.emit_indent(w)?;
338338
}
@@ -370,21 +370,21 @@ impl JsonEmitter {
370370
w,
371371
"{}{}{}",
372372
self.color.punctuation.paint("\""),
373-
self.color.key.paint(format!("{}", v)),
373+
self.color.key.paint(v),
374374
self.color.punctuation.paint("\"")
375375
)?,
376376
Document::Int(v) => write!(
377377
w,
378378
"{}{}{}",
379379
self.color.punctuation.paint("\""),
380-
self.color.key.paint(format!("{}", v)),
380+
self.color.key.paint(v),
381381
self.color.punctuation.paint("\"")
382382
)?,
383383
Document::Float(v) => write!(
384384
w,
385385
"{}{}{}",
386386
self.color.punctuation.paint("\""),
387-
self.color.key.paint(format!("{}", v)),
387+
self.color.key.paint(v),
388388
self.color.punctuation.paint("\"")
389389
)?,
390390
Document::Comment(_, _) => return Err(Error::KeyTypeError("comment")),
@@ -395,12 +395,12 @@ impl JsonEmitter {
395395
Document::Fragment(_) => return Err(Error::KeyTypeError("fragment")),
396396
Document::Null => return Err(Error::KeyTypeError("null")),
397397
};
398-
write!(w, "{}", &self.color.punctuation.paint(": "))?;
398+
write!(w, "{}", self.color.punctuation.paint(": "))?;
399399
key_done = true;
400400
} else if !val_done {
401401
self.emit_node(w, node)?;
402402
if i != last {
403-
write!(w, "{}", &self.color.punctuation.paint(","))?;
403+
write!(w, "{}", self.color.punctuation.paint(","))?;
404404
}
405405
val_done = true;
406406
need_eol = true;
@@ -412,7 +412,7 @@ impl JsonEmitter {
412412
}
413413
self.level -= 1;
414414
self.emit_indent(w)?;
415-
write!(w, "{}", &self.color.aggregate.paint("}"))?;
415+
write!(w, "{}", self.color.aggregate.paint("}"))?;
416416
Ok(())
417417
}
418418

@@ -459,7 +459,9 @@ impl JsonEmitter {
459459
write!(
460460
w,
461461
"{}",
462-
self.color.comment.paint(format!("{} {}", leader, line))
462+
self.color
463+
.comment
464+
.paint(format_args!("{} {}", leader, line))
463465
)?;
464466
}
465467
}
@@ -479,7 +481,7 @@ impl JsonEmitter {
479481
}
480482

481483
fn emit_string_strict<W: fmt::Write>(&mut self, w: &mut W, value: &str) -> Result<()> {
482-
write!(w, "{}", &self.color.punctuation.paint("\""))?;
484+
write!(w, "{}", self.color.punctuation.paint("\""))?;
483485
let bytes = value.as_bytes();
484486
let mut start = 0;
485487
for (i, &byte) in bytes.iter().enumerate() {
@@ -488,26 +490,28 @@ impl JsonEmitter {
488490
continue;
489491
}
490492
if start < i {
491-
write!(w, "{}", &self.color.string.paint(&value[start..i]))?;
493+
write!(w, "{}", self.color.string.paint(&value[start..i]))?;
492494
}
493495
match escape {
494496
UU => write!(
495497
w,
496498
"{}",
497-
&self.color.escape.paint(format!("\\u{:04x}", byte))
499+
self.color.escape.paint(format_args!("\\u{:04x}", byte))
498500
)?,
499501
_ => write!(
500502
w,
501503
"{}",
502-
&self.color.escape.paint(format!("\\{}", escape as char))
504+
self.color
505+
.escape
506+
.paint(format_args!("\\{}", escape as char))
503507
)?,
504508
};
505509
start = i + 1;
506510
}
507511
if start != bytes.len() {
508-
write!(w, "{}", &self.color.string.paint(&value[start..]))?;
512+
write!(w, "{}", self.color.string.paint(&value[start..]))?;
509513
}
510-
write!(w, "{}", &self.color.punctuation.paint("\""))?;
514+
write!(w, "{}", self.color.punctuation.paint("\""))?;
511515
Ok(())
512516
}
513517

@@ -516,10 +520,10 @@ impl JsonEmitter {
516520
writeln!(w)?;
517521
self.level += 1;
518522
self.emit_indent(w)?;
519-
self.writeln(w, &self.color.punctuation.paint("'''").to_string())?;
523+
self.writeln(w, self.color.punctuation.paint("'''"))?;
520524
self.emit_indent(w)?;
521525
} else {
522-
write!(w, "{}", &self.color.punctuation.paint("\""))?;
526+
write!(w, "{}", self.color.punctuation.paint("\""))?;
523527
}
524528
let bytes = value.as_bytes();
525529
let mut start = 0;
@@ -529,19 +533,21 @@ impl JsonEmitter {
529533
continue;
530534
}
531535
if start < i {
532-
write!(w, "{}", &self.color.string.paint(&value[start..i]))?;
536+
write!(w, "{}", self.color.string.paint(&value[start..i]))?;
533537
}
534538
match escape {
535539
UU => write!(
536540
w,
537541
"{}",
538-
&self.color.escape.paint(format!("\\u{:04x}", byte))
542+
self.color.escape.paint(format_args!("\\u{:04x}", byte))
539543
)?,
540544
NN => match self.multiline {
541545
Multiline::None => write!(
542546
w,
543547
"{}",
544-
&self.color.escape.paint(format!("\\{}", escape as char))
548+
self.color
549+
.escape
550+
.paint(format_args!("\\{}", escape as char))
545551
)?,
546552
Multiline::Json5 => writeln!(w, "{}", self.color.escape.paint("\\"))?,
547553
Multiline::Hjson => {
@@ -552,30 +558,32 @@ impl JsonEmitter {
552558
_ => write!(
553559
w,
554560
"{}",
555-
&self.color.escape.paint(format!("\\{}", escape as char))
561+
self.color
562+
.escape
563+
.paint(format_args!("\\{}", escape as char))
556564
)?,
557565
};
558566
start = i + 1;
559567
}
560568
if start != bytes.len() {
561-
write!(w, "{}", &self.color.string.paint(&value[start..]))?;
569+
write!(w, "{}", self.color.string.paint(&value[start..]))?;
562570
}
563571
if self.multiline == Multiline::Hjson {
564572
writeln!(w)?;
565573
self.emit_indent(w)?;
566-
write!(w, "{}", &self.color.punctuation.paint("'''"))?;
574+
write!(w, "{}", self.color.punctuation.paint("'''"))?;
567575
self.level -= 1;
568576
} else {
569-
write!(w, "{}", &self.color.punctuation.paint("\""))?;
577+
write!(w, "{}", self.color.punctuation.paint("\""))?;
570578
}
571579
Ok(())
572580
}
573581

574582
fn emit_boolean<W: fmt::Write>(&mut self, w: &mut W, b: bool) -> Result<()> {
575583
if b {
576-
write!(w, "{}", &self.color.boolean.paint("true"))?;
584+
write!(w, "{}", self.color.boolean.paint("true"))?;
577585
} else {
578-
write!(w, "{}", &self.color.boolean.paint("false"))?;
586+
write!(w, "{}", self.color.boolean.paint("false"))?;
579587
}
580588
Ok(())
581589
}
@@ -594,18 +602,18 @@ impl JsonEmitter {
594602
self.color.punctuation.paint("\"")
595603
)?;
596604
} else {
597-
write!(w, "{}", &self.color.integer.paint(s))?;
605+
write!(w, "{}", self.color.integer.paint(s))?;
598606
}
599607
Ok(())
600608
}
601609

602610
fn emit_float<W: fmt::Write>(&mut self, w: &mut W, f: f64) -> Result<()> {
603-
write!(w, "{}", &self.color.float.paint(format!("{}", f)))?;
611+
write!(w, "{}", self.color.float.paint(f))?;
604612
Ok(())
605613
}
606614

607615
fn emit_null<W: fmt::Write>(&mut self, w: &mut W) -> Result<()> {
608-
write!(w, "{}", &self.color.null.paint("null"))?;
616+
write!(w, "{}", self.color.null.paint("null"))?;
609617
Ok(())
610618
}
611619

0 commit comments

Comments
 (0)