Skip to content

Commit 75528f2

Browse files
Replace writeln!/write! with push_str
1 parent 57243b7 commit 75528f2

File tree

1 file changed

+69
-81
lines changed

1 file changed

+69
-81
lines changed

src/librustdoc/html/render.rs

Lines changed: 69 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use crate::docfs::{DocFS, ErrorStorage, PathError};
6565
use crate::doctree;
6666
use crate::fold::DocFolder;
6767
use crate::html::escape::Escape;
68-
use crate::html::format::{AsyncSpace, ConstnessSpace};
68+
use crate::html::format::{Buffer, AsyncSpace, ConstnessSpace};
6969
use crate::html::format::{GenericBounds, WhereClause, href, AbiSpace, DefaultSpace};
7070
use crate::html::format::{VisSpace, Function, UnsafetySpace, MutableSpace};
7171
use crate::html::format::fmt_impl_for_trait_page;
@@ -1025,12 +1025,12 @@ themePicker.onblur = handleThemeButtonsBlur;
10251025
}
10261026
all_aliases.push(format!("ALIASES[\"{}\"] = {{{}}};", krate.name, output));
10271027
all_aliases.sort();
1028-
let mut v = Vec::new();
1029-
try_err!(writeln!(&mut v, "var ALIASES = {{}};"), &dst);
1028+
let mut v = Buffer::html();
1029+
writeln!(&mut v, "var ALIASES = {{}};");
10301030
for aliases in &all_aliases {
1031-
try_err!(writeln!(&mut v, "{}", aliases), &dst);
1031+
writeln!(&mut v, "{}", aliases);
10321032
}
1033-
cx.shared.fs.write(&dst, &v)?;
1033+
cx.shared.fs.write(&dst, v.into_inner().into_bytes())?;
10341034
}
10351035

10361036
use std::ffi::OsString;
@@ -1114,12 +1114,9 @@ themePicker.onblur = handleThemeButtonsBlur;
11141114
&krate.name,
11151115
hierarchy.to_json_string()));
11161116
all_sources.sort();
1117-
let mut v = Vec::new();
1118-
try_err!(writeln!(&mut v,
1119-
"var N = null;var sourcesIndex = {{}};\n{}\ncreateSourceSidebar();",
1120-
all_sources.join("\n")),
1121-
&dst);
1122-
cx.shared.fs.write(&dst, &v)?;
1117+
let v = format!("var N = null;var sourcesIndex = {{}};\n{}\ncreateSourceSidebar();\n",
1118+
all_sources.join("\n"));
1119+
cx.shared.fs.write(&dst, v.as_bytes())?;
11231120
}
11241121

11251122
// Update the search index
@@ -1134,14 +1131,11 @@ themePicker.onblur = handleThemeButtonsBlur;
11341131
// with rustdoc running in parallel.
11351132
all_indexes.sort();
11361133
{
1137-
let mut v = Vec::new();
1138-
try_err!(writeln!(&mut v, "var N=null,E=\"\",T=\"t\",U=\"u\",searchIndex={{}};"), &dst);
1139-
try_err!(write_minify_replacer(
1140-
&mut v,
1134+
let mut v = String::from("var N=null,E=\"\",T=\"t\",U=\"u\",searchIndex={};\n");
1135+
v.push_str(&minify_replacer(
11411136
&format!("{}\n{}", variables.join(""), all_indexes.join("\n")),
1142-
options.enable_minification),
1143-
&dst);
1144-
try_err!(write!(&mut v, "initSearch(searchIndex);addSearchOptions(searchIndex);"), &dst);
1137+
options.enable_minification));
1138+
v.push_str("initSearch(searchIndex);addSearchOptions(searchIndex);");
11451139
cx.shared.fs.write(&dst, &v)?;
11461140
}
11471141
if options.enable_index_page {
@@ -1247,19 +1241,18 @@ themePicker.onblur = handleThemeButtonsBlur;
12471241
// identically even with rustdoc running in parallel.
12481242
all_implementors.sort();
12491243

1250-
let mut v = Vec::new();
1251-
try_err!(writeln!(&mut v, "(function() {{var implementors = {{}};"), &mydst);
1244+
let mut v = String::from("(function() {var implementors = {}};\n");
12521245
for implementor in &all_implementors {
1253-
try_err!(writeln!(&mut v, "{}", *implementor), &mydst);
1246+
v.push_str(&format!("{}", *implementor));
12541247
}
1255-
try_err!(writeln!(&mut v, "{}", r"
1248+
v.push_str(r"
12561249
if (window.register_implementors) {
12571250
window.register_implementors(implementors);
12581251
} else {
12591252
window.pending_implementors = implementors;
12601253
}
1261-
"), &mydst);
1262-
try_err!(writeln!(&mut v, r"}})()"), &mydst);
1254+
\n");
1255+
v.push_str("})()");
12631256
cx.shared.fs.write(&mydst, &v)?;
12641257
}
12651258
Ok(())
@@ -1279,68 +1272,65 @@ fn write_minify(fs:&DocFS, dst: PathBuf, contents: &str, enable_minification: bo
12791272
}
12801273
}
12811274

1282-
fn write_minify_replacer<W: Write>(
1283-
dst: &mut W,
1275+
fn minify_replacer(
12841276
contents: &str,
12851277
enable_minification: bool,
1286-
) -> io::Result<()> {
1278+
) -> String {
12871279
use minifier::js::{simple_minify, Keyword, ReservedChar, Token, Tokens};
12881280

12891281
if enable_minification {
1290-
writeln!(dst, "{}",
1291-
{
1292-
let tokens: Tokens<'_> = simple_minify(contents)
1293-
.into_iter()
1294-
.filter(|(f, next)| {
1295-
// We keep backlines.
1296-
minifier::js::clean_token_except(f, next, &|c: &Token<'_>| {
1297-
c.get_char() != Some(ReservedChar::Backline)
1298-
})
1299-
})
1300-
.map(|(f, _)| {
1301-
minifier::js::replace_token_with(f, &|t: &Token<'_>| {
1302-
match *t {
1303-
Token::Keyword(Keyword::Null) => Some(Token::Other("N")),
1304-
Token::String(s) => {
1305-
let s = &s[1..s.len() -1]; // The quotes are included
1306-
if s.is_empty() {
1307-
Some(Token::Other("E"))
1308-
} else if s == "t" {
1309-
Some(Token::Other("T"))
1310-
} else if s == "u" {
1311-
Some(Token::Other("U"))
1312-
} else {
1313-
None
1314-
}
1315-
}
1316-
_ => None,
1317-
}
1318-
})
1319-
})
1320-
.collect::<Vec<_>>()
1321-
.into();
1322-
tokens.apply(|f| {
1323-
// We add a backline after the newly created variables.
1324-
minifier::js::aggregate_strings_into_array_with_separation_filter(
1325-
f,
1326-
"R",
1327-
Token::Char(ReservedChar::Backline),
1328-
// This closure prevents crates' names from being aggregated.
1329-
//
1330-
// The point here is to check if the string is preceded by '[' and
1331-
// "searchIndex". If so, it means this is a crate name and that it
1332-
// shouldn't be aggregated.
1333-
|tokens, pos| {
1334-
pos < 2 ||
1335-
!tokens[pos - 1].eq_char(ReservedChar::OpenBracket) ||
1336-
tokens[pos - 2].get_other() != Some("searchIndex")
1282+
let tokens: Tokens<'_> = simple_minify(contents)
1283+
.into_iter()
1284+
.filter(|(f, next)| {
1285+
// We keep backlines.
1286+
minifier::js::clean_token_except(f, next, &|c: &Token<'_>| {
1287+
c.get_char() != Some(ReservedChar::Backline)
1288+
})
1289+
})
1290+
.map(|(f, _)| {
1291+
minifier::js::replace_token_with(f, &|t: &Token<'_>| {
1292+
match *t {
1293+
Token::Keyword(Keyword::Null) => Some(Token::Other("N")),
1294+
Token::String(s) => {
1295+
let s = &s[1..s.len() -1]; // The quotes are included
1296+
if s.is_empty() {
1297+
Some(Token::Other("E"))
1298+
} else if s == "t" {
1299+
Some(Token::Other("T"))
1300+
} else if s == "u" {
1301+
Some(Token::Other("U"))
1302+
} else {
1303+
None
13371304
}
1338-
)
1339-
})
1340-
.to_string()
1305+
}
1306+
_ => None,
1307+
}
13411308
})
1309+
})
1310+
.collect::<Vec<_>>()
1311+
.into();
1312+
let o = tokens.apply(|f| {
1313+
// We add a backline after the newly created variables.
1314+
minifier::js::aggregate_strings_into_array_with_separation_filter(
1315+
f,
1316+
"R",
1317+
Token::Char(ReservedChar::Backline),
1318+
// This closure prevents crates' names from being aggregated.
1319+
//
1320+
// The point here is to check if the string is preceded by '[' and
1321+
// "searchIndex". If so, it means this is a crate name and that it
1322+
// shouldn't be aggregated.
1323+
|tokens, pos| {
1324+
pos < 2 ||
1325+
!tokens[pos - 1].eq_char(ReservedChar::OpenBracket) ||
1326+
tokens[pos - 2].get_other() != Some("searchIndex")
1327+
}
1328+
)
1329+
})
1330+
.to_string();
1331+
format!("{}\n", o)
13421332
} else {
1343-
writeln!(dst, "{}", contents)
1333+
format!("{}\n", contents)
13441334
}
13451335
}
13461336

@@ -2073,9 +2063,7 @@ impl Context {
20732063
if !self.render_redirect_pages {
20742064
let items = self.build_sidebar_items(&m);
20752065
let js_dst = self.dst.join("sidebar-items.js");
2076-
let mut v = Vec::new();
2077-
try_err!(write!(&mut v, "initSidebarItems({});",
2078-
as_json(&items)), &js_dst);
2066+
let v = format!("initSidebarItems({});", as_json(&items));
20792067
scx.fs.write(&js_dst, &v)?;
20802068
}
20812069

0 commit comments

Comments
 (0)