1
Fork 0

Remove usage of global variable "inlined_types"

This commit is contained in:
Guillaume Gomez 2020-01-13 23:28:34 +01:00
parent 1b117d7546
commit 12882a8392
2 changed files with 50 additions and 25 deletions

View file

@ -992,13 +992,11 @@ themePicker.onblur = handleThemeButtonsBlur;
writeln!(v, "{}", *implementor).unwrap(); writeln!(v, "{}", *implementor).unwrap();
} }
v.push_str( v.push_str(
r" "if (window.register_implementors) {\
if (window.register_implementors) { window.register_implementors(implementors);\
window.register_implementors(implementors); } else {\
} else { window.pending_implementors = implementors;\
window.pending_implementors = implementors; }",
}
",
); );
v.push_str("})()"); v.push_str("})()");
cx.shared.fs.write(&mydst, &v)?; cx.shared.fs.write(&mydst, &v)?;
@ -2353,6 +2351,7 @@ fn render_implementor(
implementor: &Impl, implementor: &Impl,
w: &mut Buffer, w: &mut Buffer,
implementor_dups: &FxHashMap<&str, (DefId, bool)>, implementor_dups: &FxHashMap<&str, (DefId, bool)>,
aliases: &[String],
) { ) {
// If there's already another implementor that has the same abbridged name, use the // If there's already another implementor that has the same abbridged name, use the
// full path, for example in `std::iter::ExactSizeIterator` // full path, for example in `std::iter::ExactSizeIterator`
@ -2375,6 +2374,7 @@ fn render_implementor(
Some(use_absolute), Some(use_absolute),
false, false,
false, false,
aliases,
); );
} }
@ -2396,6 +2396,7 @@ fn render_impls(cx: &Context, w: &mut Buffer, traits: &[&&Impl], containing_item
None, None,
false, false,
true, true,
&[],
); );
buffer.into_inner() buffer.into_inner()
}) })
@ -2597,8 +2598,6 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait)
// If there are methods directly on this trait object, render them here. // If there are methods directly on this trait object, render them here.
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All); render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All);
let mut synthetic_types = Vec::new();
if let Some(implementors) = cx.cache.implementors.get(&it.def_id) { if let Some(implementors) = cx.cache.implementors.get(&it.def_id) {
// The DefId is for the first Type found with that name. The bool is // The DefId is for the first Type found with that name. The bool is
// if any Types with the same name but different DefId have been found. // if any Types with the same name but different DefId have been found.
@ -2649,6 +2648,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait)
None, None,
true, true,
false, false,
&[],
); );
} }
write_loading_content(w, ""); write_loading_content(w, "");
@ -2661,7 +2661,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait)
"<div class='item-list' id='implementors-list'>", "<div class='item-list' id='implementors-list'>",
); );
for implementor in concrete { for implementor in concrete {
render_implementor(cx, implementor, w, &implementor_dups); render_implementor(cx, implementor, w, &implementor_dups, &[]);
} }
write_loading_content(w, "</div>"); write_loading_content(w, "</div>");
@ -2673,9 +2673,13 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait)
"<div class='item-list' id='synthetic-implementors-list'>", "<div class='item-list' id='synthetic-implementors-list'>",
); );
for implementor in synthetic { for implementor in synthetic {
synthetic_types render_implementor(
.extend(collect_paths_for_type(implementor.inner_impl().for_.clone())); cx,
render_implementor(cx, implementor, w, &implementor_dups); implementor,
w,
&implementor_dups,
&collect_paths_for_type(implementor.inner_impl().for_.clone()),
);
} }
write_loading_content(w, "</div>"); write_loading_content(w, "</div>");
} }
@ -2700,17 +2704,12 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait)
write_loading_content(w, "</div>"); write_loading_content(w, "</div>");
} }
} }
write!(
w,
r#"<script type="text/javascript">window.inlined_types=new Set({});</script>"#,
serde_json::to_string(&synthetic_types).unwrap(),
);
write!( write!(
w, w,
r#"<script type="text/javascript" async "<script type=\"text/javascript\" \
src="{root_path}/implementors/{path}/{ty}.{name}.js"> src=\"{root_path}/implementors/{path}/{ty}.{name}.js\" async>\
</script>"#, </script>",
root_path = vec![".."; cx.current.len()].join("/"), root_path = vec![".."; cx.current.len()].join("/"),
path = if it.def_id.is_local() { path = if it.def_id.is_local() {
cx.current.join("/") cx.current.join("/")
@ -3392,6 +3391,7 @@ fn render_assoc_items(
None, None,
false, false,
true, true,
&[],
); );
} }
} }
@ -3602,6 +3602,9 @@ fn render_impl(
use_absolute: Option<bool>, use_absolute: Option<bool>,
is_on_foreign_type: bool, is_on_foreign_type: bool,
show_default_items: bool, show_default_items: bool,
// This argument is used to reference same type with different pathes to avoid duplication
// in documentation pages for trait with automatic implementations like "Send" and "Sync".
aliases: &[String],
) { ) {
if render_mode == RenderMode::Normal { if render_mode == RenderMode::Normal {
let id = cx.derive_id(match i.inner_impl().trait_ { let id = cx.derive_id(match i.inner_impl().trait_ {
@ -3614,8 +3617,13 @@ fn render_impl(
} }
None => "impl".to_string(), None => "impl".to_string(),
}); });
let aliases = if aliases.is_empty() {
String::new()
} else {
format!(" aliases=\"{}\"", aliases.join(","))
};
if let Some(use_absolute) = use_absolute { if let Some(use_absolute) = use_absolute {
write!(w, "<h3 id='{}' class='impl'><code class='in-band'>", id); write!(w, "<h3 id='{}' class='impl'{}><code class='in-band'>", id, aliases);
fmt_impl_for_trait_page(&i.inner_impl(), w, use_absolute); fmt_impl_for_trait_page(&i.inner_impl(), w, use_absolute);
if show_def_docs { if show_def_docs {
for it in &i.inner_impl().items { for it in &i.inner_impl().items {
@ -3637,8 +3645,9 @@ fn render_impl(
} else { } else {
write!( write!(
w, w,
"<h3 id='{}' class='impl'><code class='in-band'>{}</code>", "<h3 id='{}' class='impl'{}><code class='in-band'>{}</code>",
id, id,
aliases,
i.inner_impl().print() i.inner_impl().print()
); );
} }

View file

@ -1895,6 +1895,22 @@ function getSearchElement() {
var implementors = document.getElementById("implementors-list"); var implementors = document.getElementById("implementors-list");
var synthetic_implementors = document.getElementById("synthetic-implementors-list"); var synthetic_implementors = document.getElementById("synthetic-implementors-list");
// This `inlined_types` variable is used to avoid having the same implementation showing
// up twice. For example "String" in the "Sync" doc page.
//
// By the way, this is only used by and useful for traits implemented automatically (like
// "Send" and "Sync").
var inlined_types = new Set();
onEachLazy(synthetic_implementors.getElementsByClassName("impl"), function(el) {
var aliases = el.getAttribute("aliases");
if (!aliases) {
return;
}
aliases.split(",").forEach(function(alias) {
inlined_types.add(alias);
});
});
var libs = Object.getOwnPropertyNames(imp); var libs = Object.getOwnPropertyNames(imp);
var llength = libs.length; var llength = libs.length;
for (var i = 0; i < llength; ++i) { for (var i = 0; i < llength; ++i) {
@ -1911,10 +1927,10 @@ function getSearchElement() {
if (struct.synthetic) { if (struct.synthetic) {
var stlength = struct.types.length; var stlength = struct.types.length;
for (var k = 0; k < stlength; k++) { for (var k = 0; k < stlength; k++) {
if (window.inlined_types.has(struct.types[k])) { if (inlined_types.has(struct.types[k])) {
continue struct_loop; continue struct_loop;
} }
window.inlined_types.add(struct.types[k]); inlined_types.add(struct.types[k]);
} }
} }