Remove global derive_id and reset_ids functions
Previously these functions relied on TLS but we can instead thread the relevant state through explicitly.
This commit is contained in:
parent
2216db9de7
commit
7bea518d3a
6 changed files with 190 additions and 153 deletions
|
@ -50,6 +50,7 @@ use std::mem;
|
|||
use std::path::{PathBuf, Path, Component};
|
||||
use std::str;
|
||||
use std::sync::Arc;
|
||||
use std::rc::Rc;
|
||||
|
||||
use externalfiles::ExternalHtml;
|
||||
|
||||
|
@ -73,7 +74,7 @@ use html::format::{GenericBounds, WhereClause, href, AbiSpace};
|
|||
use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace};
|
||||
use html::format::fmt_impl_for_trait_page;
|
||||
use html::item_type::ItemType;
|
||||
use html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine, ErrorCodes};
|
||||
use html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine, ErrorCodes, IdMap};
|
||||
use html::{highlight, layout};
|
||||
|
||||
use minifier;
|
||||
|
@ -89,7 +90,7 @@ pub type NameDoc = (String, Option<String>);
|
|||
/// easily cloned because it is cloned per work-job (about once per item in the
|
||||
/// rustdoc tree).
|
||||
#[derive(Clone)]
|
||||
pub struct Context {
|
||||
struct Context {
|
||||
/// Current hierarchy of components leading down to what's currently being
|
||||
/// rendered
|
||||
pub current: Vec<String>,
|
||||
|
@ -101,10 +102,12 @@ pub struct Context {
|
|||
/// publicly reused items to redirect to the right location.
|
||||
pub render_redirect_pages: bool,
|
||||
pub codes: ErrorCodes,
|
||||
/// The map used to ensure all generated 'id=' attributes are unique.
|
||||
id_map: Rc<RefCell<IdMap>>,
|
||||
pub shared: Arc<SharedContext>,
|
||||
}
|
||||
|
||||
pub struct SharedContext {
|
||||
struct SharedContext {
|
||||
/// The path to the crate root source minus the file name.
|
||||
/// Used for simplifying paths to the highlighted source code files.
|
||||
pub src_root: PathBuf,
|
||||
|
@ -452,9 +455,8 @@ impl ToJson for IndexItemFunctionType {
|
|||
|
||||
thread_local!(static CACHE_KEY: RefCell<Arc<Cache>> = Default::default());
|
||||
thread_local!(pub static CURRENT_LOCATION_KEY: RefCell<Vec<String>> = RefCell::new(Vec::new()));
|
||||
thread_local!(pub static USED_ID_MAP: RefCell<FxHashMap<String, usize>> = RefCell::new(init_ids()));
|
||||
|
||||
fn init_ids() -> FxHashMap<String, usize> {
|
||||
pub fn initial_ids() -> Vec<String> {
|
||||
[
|
||||
"main",
|
||||
"search",
|
||||
|
@ -472,36 +474,7 @@ fn init_ids() -> FxHashMap<String, usize> {
|
|||
"methods",
|
||||
"deref-methods",
|
||||
"implementations",
|
||||
].into_iter().map(|id| (String::from(*id), 1)).collect()
|
||||
}
|
||||
|
||||
/// This method resets the local table of used ID attributes. This is typically
|
||||
/// used at the beginning of rendering an entire HTML page to reset from the
|
||||
/// previous state (if any).
|
||||
pub fn reset_ids(embedded: bool) {
|
||||
USED_ID_MAP.with(|s| {
|
||||
*s.borrow_mut() = if embedded {
|
||||
init_ids()
|
||||
} else {
|
||||
FxHashMap()
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
pub fn derive_id(candidate: String) -> String {
|
||||
USED_ID_MAP.with(|map| {
|
||||
let id = match map.borrow_mut().get_mut(&candidate) {
|
||||
None => candidate,
|
||||
Some(a) => {
|
||||
let id = format!("{}-{}", candidate, *a);
|
||||
*a += 1;
|
||||
id
|
||||
}
|
||||
};
|
||||
|
||||
map.borrow_mut().insert(id.clone(), 1);
|
||||
id
|
||||
})
|
||||
].into_iter().map(|id| (String::from(*id))).collect()
|
||||
}
|
||||
|
||||
/// Generates the documentation for `crate` into the directory `dst`
|
||||
|
@ -515,7 +488,8 @@ pub fn run(mut krate: clean::Crate,
|
|||
renderinfo: RenderInfo,
|
||||
sort_modules_alphabetically: bool,
|
||||
themes: Vec<PathBuf>,
|
||||
enable_minification: bool) -> Result<(), Error> {
|
||||
enable_minification: bool,
|
||||
id_map: IdMap) -> Result<(), Error> {
|
||||
let src_root = match krate.src {
|
||||
FileName::Real(ref p) => match p.parent() {
|
||||
Some(p) => p.to_path_buf(),
|
||||
|
@ -584,6 +558,7 @@ pub fn run(mut krate: clean::Crate,
|
|||
dst,
|
||||
render_redirect_pages: false,
|
||||
codes: ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build()),
|
||||
id_map: Rc::new(RefCell::new(id_map)),
|
||||
shared: Arc::new(scx),
|
||||
};
|
||||
|
||||
|
@ -1711,6 +1686,11 @@ impl<'a> fmt::Display for Settings<'a> {
|
|||
}
|
||||
|
||||
impl Context {
|
||||
fn derive_id(&self, id: String) -> String {
|
||||
let mut map = self.id_map.borrow_mut();
|
||||
map.derive(id)
|
||||
}
|
||||
|
||||
/// String representation of how to get back to the root path of the 'doc/'
|
||||
/// folder in terms of a relative URL.
|
||||
fn root_path(&self) -> String {
|
||||
|
@ -1865,7 +1845,10 @@ impl Context {
|
|||
resource_suffix: &self.shared.resource_suffix,
|
||||
};
|
||||
|
||||
reset_ids(true);
|
||||
{
|
||||
self.id_map.borrow_mut().reset();
|
||||
self.id_map.borrow_mut().populate(initial_ids());
|
||||
}
|
||||
|
||||
if !self.render_redirect_pages {
|
||||
layout::render(writer, &self.shared.layout, &page,
|
||||
|
@ -2222,16 +2205,18 @@ fn document(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item) -> fmt::Re
|
|||
|
||||
/// Render md_text as markdown.
|
||||
fn render_markdown(w: &mut fmt::Formatter,
|
||||
cx: &Context,
|
||||
md_text: &str,
|
||||
links: Vec<(String, String)>,
|
||||
prefix: &str,
|
||||
codes: ErrorCodes)
|
||||
prefix: &str)
|
||||
-> fmt::Result {
|
||||
write!(w, "<div class='docblock'>{}{}</div>", prefix, Markdown(md_text, &links, codes))
|
||||
let mut ids = cx.id_map.borrow_mut();
|
||||
write!(w, "<div class='docblock'>{}{}</div>",
|
||||
prefix, Markdown(md_text, &links, RefCell::new(&mut ids), cx.codes))
|
||||
}
|
||||
|
||||
fn document_short(w: &mut fmt::Formatter, item: &clean::Item, link: AssocItemLink,
|
||||
prefix: &str, codes: ErrorCodes) -> fmt::Result {
|
||||
fn document_short(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item, link: AssocItemLink,
|
||||
prefix: &str) -> fmt::Result {
|
||||
if let Some(s) = item.doc_value() {
|
||||
let markdown = if s.contains('\n') {
|
||||
format!("{} [Read more]({})",
|
||||
|
@ -2239,7 +2224,7 @@ fn document_short(w: &mut fmt::Formatter, item: &clean::Item, link: AssocItemLin
|
|||
} else {
|
||||
plain_summary_line(Some(s)).to_string()
|
||||
};
|
||||
render_markdown(w, &markdown, item.links(), prefix, codes)?;
|
||||
render_markdown(w, cx, &markdown, item.links(), prefix)?;
|
||||
} else if !prefix.is_empty() {
|
||||
write!(w, "<div class='docblock'>{}</div>", prefix)?;
|
||||
}
|
||||
|
@ -2265,7 +2250,7 @@ fn document_full(w: &mut fmt::Formatter, item: &clean::Item,
|
|||
cx: &Context, prefix: &str) -> fmt::Result {
|
||||
if let Some(s) = cx.shared.maybe_collapsed_doc_value(item) {
|
||||
debug!("Doc block: =====\n{}\n=====", s);
|
||||
render_markdown(w, &*s, item.links(), prefix, cx.codes)?;
|
||||
render_markdown(w, cx, &*s, item.links(), prefix)?;
|
||||
} else if !prefix.is_empty() {
|
||||
write!(w, "<div class='docblock'>{}</div>", prefix)?;
|
||||
}
|
||||
|
@ -2431,7 +2416,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
|
|||
let (short, name) = item_ty_to_strs(&myty.unwrap());
|
||||
write!(w, "<h2 id='{id}' class='section-header'>\
|
||||
<a href=\"#{id}\">{name}</a></h2>\n<table>",
|
||||
id = derive_id(short.to_owned()), name = name)?;
|
||||
id = cx.derive_id(short.to_owned()), name = name)?;
|
||||
}
|
||||
|
||||
match myitem.inner {
|
||||
|
@ -2526,7 +2511,8 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
|
|||
} else {
|
||||
String::new()
|
||||
};
|
||||
let html = MarkdownHtml(&deprecated_reason, error_codes);
|
||||
let mut ids = cx.id_map.borrow_mut();
|
||||
let html = MarkdownHtml(&deprecated_reason, RefCell::new(&mut ids), error_codes);
|
||||
let text = if stability::deprecation_in_effect(&stab.deprecated_since) {
|
||||
format!("Deprecated{}{}", since, html)
|
||||
} else {
|
||||
|
@ -2557,12 +2543,14 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
|
|||
</div>",
|
||||
unstable_extra));
|
||||
} else {
|
||||
let mut ids = cx.id_map.borrow_mut();
|
||||
let text = format!("<summary><span class=microscope>🔬</span> \
|
||||
This is a nightly-only experimental API. {}\
|
||||
</summary>{}",
|
||||
unstable_extra,
|
||||
MarkdownHtml(
|
||||
&stab.unstable_reason,
|
||||
RefCell::new(&mut ids),
|
||||
error_codes));
|
||||
stability.push(format!("<div class='stab unstable'><details>{}</details></div>",
|
||||
text));
|
||||
|
@ -2583,14 +2571,15 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<S
|
|||
String::new()
|
||||
};
|
||||
|
||||
let mut ids = cx.id_map.borrow_mut();
|
||||
let text = if stability::deprecation_in_effect(&depr.since) {
|
||||
format!("Deprecated{}{}",
|
||||
since,
|
||||
MarkdownHtml(¬e, error_codes))
|
||||
MarkdownHtml(¬e, RefCell::new(&mut ids), error_codes))
|
||||
} else {
|
||||
format!("Deprecating in {}{}",
|
||||
Escape(&depr.since),
|
||||
MarkdownHtml(¬e, error_codes))
|
||||
MarkdownHtml(¬e, RefCell::new(&mut ids), error_codes))
|
||||
};
|
||||
stability.push(format!("<div class='stab deprecated'>{}</div>", text))
|
||||
}
|
||||
|
@ -2831,8 +2820,8 @@ fn item_trait(
|
|||
-> fmt::Result {
|
||||
let name = m.name.as_ref().unwrap();
|
||||
let item_type = m.type_();
|
||||
let id = derive_id(format!("{}.{}", item_type, name));
|
||||
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||
let id = cx.derive_id(format!("{}.{}", item_type, name));
|
||||
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||
write!(w, "{extra}<h3 id='{id}' class='method'>\
|
||||
<span id='{ns_id}' class='invisible'><code>",
|
||||
extra = render_spotlight_traits(m)?,
|
||||
|
@ -3187,10 +3176,10 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
|||
document_non_exhaustive_header(it))?;
|
||||
document_non_exhaustive(w, it)?;
|
||||
for (field, ty) in fields {
|
||||
let id = derive_id(format!("{}.{}",
|
||||
let id = cx.derive_id(format!("{}.{}",
|
||||
ItemType::StructField,
|
||||
field.name.as_ref().unwrap()));
|
||||
let ns_id = derive_id(format!("{}.{}",
|
||||
let ns_id = cx.derive_id(format!("{}.{}",
|
||||
field.name.as_ref().unwrap(),
|
||||
ItemType::StructField.name_space()));
|
||||
write!(w, "<span id=\"{id}\" class=\"{item_type} small-section-header\">
|
||||
|
@ -3321,10 +3310,10 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
|||
document_non_exhaustive_header(it))?;
|
||||
document_non_exhaustive(w, it)?;
|
||||
for variant in &e.variants {
|
||||
let id = derive_id(format!("{}.{}",
|
||||
let id = cx.derive_id(format!("{}.{}",
|
||||
ItemType::Variant,
|
||||
variant.name.as_ref().unwrap()));
|
||||
let ns_id = derive_id(format!("{}.{}",
|
||||
let ns_id = cx.derive_id(format!("{}.{}",
|
||||
variant.name.as_ref().unwrap(),
|
||||
ItemType::Variant.name_space()));
|
||||
write!(w, "<span id=\"{id}\" class=\"variant small-section-header\">\
|
||||
|
@ -3352,7 +3341,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
|||
if let clean::VariantItem(Variant {
|
||||
kind: VariantKind::Struct(ref s)
|
||||
}) = variant.inner {
|
||||
let variant_id = derive_id(format!("{}.{}.fields",
|
||||
let variant_id = cx.derive_id(format!("{}.{}.fields",
|
||||
ItemType::Variant,
|
||||
variant.name.as_ref().unwrap()));
|
||||
write!(w, "<span class='docblock autohide sub-variant' id='{id}'>",
|
||||
|
@ -3362,10 +3351,10 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
|||
for field in &s.fields {
|
||||
use clean::StructFieldItem;
|
||||
if let StructFieldItem(ref ty) = field.inner {
|
||||
let id = derive_id(format!("variant.{}.field.{}",
|
||||
let id = cx.derive_id(format!("variant.{}.field.{}",
|
||||
variant.name.as_ref().unwrap(),
|
||||
field.name.as_ref().unwrap()));
|
||||
let ns_id = derive_id(format!("{}.{}.{}.{}",
|
||||
let ns_id = cx.derive_id(format!("{}.{}.{}.{}",
|
||||
variant.name.as_ref().unwrap(),
|
||||
ItemType::Variant.name_space(),
|
||||
field.name.as_ref().unwrap(),
|
||||
|
@ -3794,7 +3783,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
|||
render_mode: RenderMode, outer_version: Option<&str>,
|
||||
show_def_docs: bool) -> fmt::Result {
|
||||
if render_mode == RenderMode::Normal {
|
||||
let id = derive_id(match i.inner_impl().trait_ {
|
||||
let id = cx.derive_id(match i.inner_impl().trait_ {
|
||||
Some(ref t) => format!("impl-{}", small_url_encode(&format!("{:#}", t))),
|
||||
None => "impl".to_string(),
|
||||
});
|
||||
|
@ -3814,8 +3803,9 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
|||
}
|
||||
write!(w, "</span></td></tr></tbody></table></h3>")?;
|
||||
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
|
||||
let mut ids = cx.id_map.borrow_mut();
|
||||
write!(w, "<div class='docblock'>{}</div>",
|
||||
Markdown(&*dox, &i.impl_item.links(), cx.codes))?;
|
||||
Markdown(&*dox, &i.impl_item.links(), RefCell::new(&mut ids), cx.codes))?;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3836,8 +3826,8 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
|||
clean::TyMethodItem(clean::TyMethod{ ref decl, .. }) => {
|
||||
// Only render when the method is not static or we allow static methods
|
||||
if render_method_item {
|
||||
let id = derive_id(format!("{}.{}", item_type, name));
|
||||
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||
let id = cx.derive_id(format!("{}.{}", item_type, name));
|
||||
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
||||
write!(w, "{}", spotlight_decl(decl)?)?;
|
||||
write!(w, "<span id='{}' class='invisible'>", ns_id)?;
|
||||
|
@ -3858,24 +3848,24 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
|||
}
|
||||
}
|
||||
clean::TypedefItem(ref tydef, _) => {
|
||||
let id = derive_id(format!("{}.{}", ItemType::AssociatedType, name));
|
||||
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||
let id = cx.derive_id(format!("{}.{}", ItemType::AssociatedType, name));
|
||||
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
||||
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
|
||||
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?;
|
||||
write!(w, "</code></span></h4>\n")?;
|
||||
}
|
||||
clean::AssociatedConstItem(ref ty, ref default) => {
|
||||
let id = derive_id(format!("{}.{}", item_type, name));
|
||||
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||
let id = cx.derive_id(format!("{}.{}", item_type, name));
|
||||
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
||||
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
|
||||
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?;
|
||||
write!(w, "</code></span></h4>\n")?;
|
||||
}
|
||||
clean::AssociatedTypeItem(ref bounds, ref default) => {
|
||||
let id = derive_id(format!("{}.{}", item_type, name));
|
||||
let ns_id = derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||
let id = cx.derive_id(format!("{}.{}", item_type, name));
|
||||
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
|
||||
write!(w, "<h4 id='{}' class=\"{}\">", id, item_type)?;
|
||||
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
|
||||
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?;
|
||||
|
@ -3901,7 +3891,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
|||
} else if show_def_docs {
|
||||
// In case the item isn't documented,
|
||||
// provide short documentation from the trait.
|
||||
document_short(w, it, link, &prefix, cx.codes)?;
|
||||
document_short(w, cx, it, link, &prefix)?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -3913,7 +3903,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
|
|||
} else {
|
||||
document_stability(w, cx, item)?;
|
||||
if show_def_docs {
|
||||
document_short(w, item, link, &prefix, cx.codes)?;
|
||||
document_short(w, cx, item, link, &prefix)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4719,25 +4709,6 @@ pub fn cache() -> Arc<Cache> {
|
|||
CACHE_KEY.with(|c| c.borrow().clone())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[test]
|
||||
fn test_unique_id() {
|
||||
let input = ["foo", "examples", "examples", "method.into_iter","examples",
|
||||
"method.into_iter", "foo", "main", "search", "methods",
|
||||
"examples", "method.into_iter", "assoc_type.Item", "assoc_type.Item"];
|
||||
let expected = ["foo", "examples", "examples-1", "method.into_iter", "examples-2",
|
||||
"method.into_iter-1", "foo-1", "main-1", "search-1", "methods-1",
|
||||
"examples-3", "method.into_iter-2", "assoc_type.Item", "assoc_type.Item-1"];
|
||||
|
||||
let test = || {
|
||||
let actual: Vec<String> = input.iter().map(|s| derive_id(s.to_string())).collect();
|
||||
assert_eq!(&actual[..], expected);
|
||||
};
|
||||
test();
|
||||
reset_ids(true);
|
||||
test();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[test]
|
||||
fn test_name_key() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue