Skip to content

Commit d5cac19

Browse files
committed
Fewer temporary strings
1 parent e2fa4e0 commit d5cac19

File tree

6 files changed

+126
-97
lines changed

6 files changed

+126
-97
lines changed

src/librustdoc/formats/item_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,6 @@ impl ItemType {
158158

159159
impl fmt::Display for ItemType {
160160
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161-
write!(f, "{}", self.as_str())
161+
f.write_str(self.as_str())
162162
}
163163
}

src/librustdoc/html/highlight.rs

+21-23
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@
77
88
use crate::html::escape::Escape;
99

10-
use std::fmt::{Display, Write};
10+
use std::fmt::Display;
1111
use std::iter::Peekable;
1212

1313
use rustc_lexer::{LiteralKind, TokenKind};
1414
use rustc_span::edition::Edition;
1515
use rustc_span::symbol::Symbol;
1616
use rustc_span::with_default_session_globals;
1717

18+
use super::format::Buffer;
19+
1820
/// Highlights `src`, returning the HTML output.
1921
crate fn render_with_highlighting(
2022
src: &str,
23+
out: &mut Buffer,
2124
class: Option<&str>,
2225
playground_button: Option<&str>,
2326
tooltip: Option<(Option<Edition>, &str)>,
2427
edition: Edition,
25-
) -> String {
28+
) {
2629
debug!("highlighting: ================\n{}\n==============", src);
27-
let mut out = String::with_capacity(src.len());
2830
if let Some((edition_info, class)) = tooltip {
2931
write!(
3032
out,
@@ -35,23 +37,19 @@ crate fn render_with_highlighting(
3537
} else {
3638
String::new()
3739
},
38-
)
39-
.unwrap();
40+
);
4041
}
4142

42-
write_header(&mut out, class);
43-
write_code(&mut out, &src, edition);
44-
write_footer(&mut out, playground_button);
45-
46-
out
43+
write_header(out, class);
44+
write_code(out, &src, edition);
45+
write_footer(out, playground_button);
4746
}
4847

49-
fn write_header(out: &mut String, class: Option<&str>) {
50-
write!(out, "<div class=\"example-wrap\"><pre class=\"rust {}\">\n", class.unwrap_or_default())
51-
.unwrap()
48+
fn write_header(out: &mut Buffer, class: Option<&str>) {
49+
write!(out, "<div class=\"example-wrap\"><pre class=\"rust {}\">\n", class.unwrap_or_default());
5250
}
5351

54-
fn write_code(out: &mut String, src: &str, edition: Edition) {
52+
fn write_code(out: &mut Buffer, src: &str, edition: Edition) {
5553
// This replace allows to fix how the code source with DOS backline characters is displayed.
5654
let src = src.replace("\r\n", "\n");
5755
Classifier::new(&src, edition).highlight(&mut |highlight| {
@@ -63,8 +61,8 @@ fn write_code(out: &mut String, src: &str, edition: Edition) {
6361
});
6462
}
6563

66-
fn write_footer(out: &mut String, playground_button: Option<&str>) {
67-
write!(out, "</pre>{}</div>\n", playground_button.unwrap_or_default()).unwrap()
64+
fn write_footer(out: &mut Buffer, playground_button: Option<&str>) {
65+
write!(out, "</pre>{}</div>\n", playground_button.unwrap_or_default());
6866
}
6967

7068
/// How a span of text is classified. Mostly corresponds to token kinds.
@@ -331,13 +329,13 @@ impl<'a> Classifier<'a> {
331329

332330
/// Called when we start processing a span of text that should be highlighted.
333331
/// The `Class` argument specifies how it should be highlighted.
334-
fn enter_span(out: &mut String, klass: Class) {
335-
write!(out, "<span class=\"{}\">", klass.as_html()).unwrap()
332+
fn enter_span(out: &mut Buffer, klass: Class) {
333+
write!(out, "<span class=\"{}\">", klass.as_html());
336334
}
337335

338336
/// Called at the end of a span of highlighted text.
339-
fn exit_span(out: &mut String) {
340-
write!(out, "</span>").unwrap()
337+
fn exit_span(out: &mut Buffer) {
338+
out.write_str("</span>");
341339
}
342340

343341
/// Called for a span of text. If the text should be highlighted differently
@@ -351,10 +349,10 @@ fn exit_span(out: &mut String) {
351349
/// ```
352350
/// The latter can be thought of as a shorthand for the former, which is more
353351
/// flexible.
354-
fn string<T: Display>(out: &mut String, text: T, klass: Option<Class>) {
352+
fn string<T: Display>(out: &mut Buffer, text: T, klass: Option<Class>) {
355353
match klass {
356-
None => write!(out, "{}", text).unwrap(),
357-
Some(klass) => write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text).unwrap(),
354+
None => write!(out, "{}", text),
355+
Some(klass) => write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text),
358356
}
359357
}
360358

src/librustdoc/html/highlight/tests.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::write_code;
2+
use crate::html::format::Buffer;
23
use expect_test::expect_file;
34
use rustc_span::edition::Edition;
45

@@ -18,9 +19,9 @@ const STYLE: &str = r#"
1819
fn test_html_highlighting() {
1920
let src = include_str!("fixtures/sample.rs");
2021
let html = {
21-
let mut out = String::new();
22+
let mut out = Buffer::new();
2223
write_code(&mut out, src, Edition::Edition2018);
23-
format!("{}<pre><code>{}</code></pre>\n", STYLE, out)
24+
format!("{}<pre><code>{}</code></pre>\n", STYLE, out.into_inner())
2425
};
2526
expect_file!["fixtures/sample.html"].assert_eq(&html);
2627
}
@@ -30,7 +31,7 @@ fn test_dos_backline() {
3031
let src = "pub fn foo() {\r\n\
3132
println!(\"foo\");\r\n\
3233
}\r\n";
33-
let mut html = String::new();
34+
let mut html = Buffer::new();
3435
write_code(&mut html, src, Edition::Edition2018);
35-
expect_file!["fixtures/dos_line.html"].assert_eq(&html);
36+
expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
3637
}

src/librustdoc/html/markdown.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ use pulldown_cmark::{
4141
html, BrokenLink, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag,
4242
};
4343

44+
use super::format::Buffer;
45+
4446
#[cfg(test)]
4547
mod tests;
4648

@@ -235,9 +237,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
235237
}
236238
let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
237239
let text = lines.collect::<Vec<Cow<'_, str>>>().join("\n");
238-
// insert newline to clearly separate it from the
239-
// previous block so we can shorten the html output
240-
let mut s = String::from("\n");
240+
241241
let playground_button = self.playground.as_ref().and_then(|playground| {
242242
let krate = &playground.crate_name;
243243
let url = &playground.url;
@@ -298,17 +298,22 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
298298
None
299299
};
300300

301-
s.push_str(&highlight::render_with_highlighting(
301+
// insert newline to clearly separate it from the
302+
// previous block so we can shorten the html output
303+
let mut s = Buffer::new();
304+
s.push_str("\n");
305+
highlight::render_with_highlighting(
302306
&text,
307+
&mut s,
303308
Some(&format!(
304309
"rust-example-rendered{}",
305310
if let Some((_, class)) = tooltip { format!(" {}", class) } else { String::new() }
306311
)),
307312
playground_button.as_deref(),
308313
tooltip,
309314
edition,
310-
));
311-
Some(Event::Html(s.into()))
315+
);
316+
Some(Event::Html(s.into_inner().into()))
312317
}
313318
}
314319

0 commit comments

Comments
 (0)