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.
This commit is contained in:
Michael Howell 2022-08-05 16:36:47 -07:00
parent dddc2fd6de
commit fc31fce670

View File

@ -1,5 +1,4 @@
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fmt;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::prelude::*; use std::io::prelude::*;
use std::io::{self, BufReader}; use std::io::{self, BufReader};
@ -10,6 +9,8 @@ use std::sync::LazyLock as Lazy;
use itertools::Itertools; use itertools::Itertools;
use rustc_data_structures::flock; use rustc_data_structures::flock;
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer};
use super::{collect_paths_for_type, ensure_trailing_slash, Context, BASIC_KEYWORDS}; use super::{collect_paths_for_type, ensure_trailing_slash, Context, BASIC_KEYWORDS};
use crate::clean::Crate; use crate::clean::Crate;
@ -563,36 +564,18 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
types: Vec<String>, types: Vec<String>,
} }
impl Implementor { impl Serialize for Implementor {
fn to_js_string(&self) -> impl fmt::Display + '_ { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
fn single_quote_string(s: &str) -> String { where
let mut result = String::with_capacity(s.len() + 2); S: Serializer,
result.push_str("'"); {
for c in s.chars() { let mut seq = serializer.serialize_seq(None)?;
if c == '"' { seq.serialize_element(&self.text)?;
result.push_str("\""); if self.synthetic {
} else { seq.serialize_element(&1)?;
result.extend(c.escape_default()); seq.serialize_element(&self.types)?;
}
}
result.push_str("'");
result
} }
crate::html::format::display_fn(|f| { seq.end()
let text_esc = single_quote_string(&self.text);
if self.synthetic {
let types = crate::html::format::comma_sep(
self.types.iter().map(|type_| single_quote_string(type_)),
false,
);
// use `1` to represent a synthetic, because it's fewer bytes than `true`
write!(f, "[{text_esc},1,[{types}]]")
} else {
// The types list is only used for synthetic impls.
// If this changes, `main.js` and `write_shared.rs` both need changed.
write!(f, "[{text_esc}]")
}
})
} }
} }
@ -626,12 +609,9 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
} }
let implementors = format!( let implementors = format!(
r#""{}":[{}]"#, r#""{}":{}"#,
krate.name(cx.tcx()), krate.name(cx.tcx()),
crate::html::format::comma_sep( serde_json::to_string(&implementors).expect("failed serde conversion"),
implementors.iter().map(Implementor::to_js_string),
false
)
); );
let mut mydst = dst.clone(); let mut mydst = dst.clone();