Skip to content

Commit fc31fce

Browse files
committed
rustdoc: use serde, which can escape strings more quickly
This means we don't gain as much as we did from using single-quotes, since serde_json can only produce double-quoted strings, but it's still a win.
1 parent dddc2fd commit fc31fce

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed

Diff for: src/librustdoc/html/render/write_shared.rs

+15-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::ffi::OsStr;
2-
use std::fmt;
32
use std::fs::{self, File};
43
use std::io::prelude::*;
54
use std::io::{self, BufReader};
@@ -10,6 +9,8 @@ use std::sync::LazyLock as Lazy;
109
use itertools::Itertools;
1110
use rustc_data_structures::flock;
1211
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
12+
use serde::ser::SerializeSeq;
13+
use serde::{Serialize, Serializer};
1314

1415
use super::{collect_paths_for_type, ensure_trailing_slash, Context, BASIC_KEYWORDS};
1516
use crate::clean::Crate;
@@ -563,36 +564,18 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
563564
types: Vec<String>,
564565
}
565566

566-
impl Implementor {
567-
fn to_js_string(&self) -> impl fmt::Display + '_ {
568-
fn single_quote_string(s: &str) -> String {
569-
let mut result = String::with_capacity(s.len() + 2);
570-
result.push_str("'");
571-
for c in s.chars() {
572-
if c == '"' {
573-
result.push_str("\"");
574-
} else {
575-
result.extend(c.escape_default());
576-
}
577-
}
578-
result.push_str("'");
579-
result
567+
impl Serialize for Implementor {
568+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
569+
where
570+
S: Serializer,
571+
{
572+
let mut seq = serializer.serialize_seq(None)?;
573+
seq.serialize_element(&self.text)?;
574+
if self.synthetic {
575+
seq.serialize_element(&1)?;
576+
seq.serialize_element(&self.types)?;
580577
}
581-
crate::html::format::display_fn(|f| {
582-
let text_esc = single_quote_string(&self.text);
583-
if self.synthetic {
584-
let types = crate::html::format::comma_sep(
585-
self.types.iter().map(|type_| single_quote_string(type_)),
586-
false,
587-
);
588-
// use `1` to represent a synthetic, because it's fewer bytes than `true`
589-
write!(f, "[{text_esc},1,[{types}]]")
590-
} else {
591-
// The types list is only used for synthetic impls.
592-
// If this changes, `main.js` and `write_shared.rs` both need changed.
593-
write!(f, "[{text_esc}]")
594-
}
595-
})
578+
seq.end()
596579
}
597580
}
598581

@@ -626,12 +609,9 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
626609
}
627610

628611
let implementors = format!(
629-
r#""{}":[{}]"#,
612+
r#""{}":{}"#,
630613
krate.name(cx.tcx()),
631-
crate::html::format::comma_sep(
632-
implementors.iter().map(Implementor::to_js_string),
633-
false
634-
)
614+
serde_json::to_string(&implementors).expect("failed serde conversion"),
635615
);
636616

637617
let mut mydst = dst.clone();

0 commit comments

Comments
 (0)