Skip to content

Commit 34ef4d2

Browse files
committed
rustdoc: Show all implementors of traits
When inlining documentation across crates, primitive implementors of traits were not shown. This commit tweaks the infrastructure to treat primitive and Path-like impls the same way, displaying all implementors everywhere. cc rust-lang#14462
1 parent 9e0db2d commit 34ef4d2

File tree

2 files changed

+43
-63
lines changed

2 files changed

+43
-63
lines changed

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

+37-55
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,11 @@ pub enum ExternalLocation {
102102
Unknown,
103103
}
104104

105-
/// Different ways an implementor of a trait can be rendered.
106-
pub enum Implementor {
107-
/// Paths are displayed specially by omitting the `impl XX for` cruft
108-
PathType(clean::Type),
109-
/// This is the generic representation of a trait implementor, used for
110-
/// primitive types and otherwise non-path types.
111-
OtherType(clean::Generics, /* trait */ clean::Type, /* for */ clean::Type),
105+
/// Metadata about an implementor of a trait.
106+
pub struct Implementor {
107+
generics: clean::Generics,
108+
trait_: clean::Type,
109+
for_: clean::Type,
112110
}
113111

114112
/// This cache is used to store information about the `clean::Crate` being
@@ -312,6 +310,7 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> {
312310
// for future parallelization opportunities
313311
let cache = Arc::new(cache);
314312
cache_key.replace(Some(cache.clone()));
313+
current_location_key.replace(Some(Vec::new()));
315314

316315
try!(write_shared(&cx, &krate, &*cache, index));
317316
let krate = try!(render_sources(&mut cx, krate));
@@ -476,7 +475,6 @@ fn write_shared(cx: &Context,
476475
let dst = cx.dst.join("implementors");
477476
try!(mkdir(&dst));
478477
for (&did, imps) in cache.implementors.iter() {
479-
if ast_util::is_local(did) { continue }
480478
let &(ref remote_path, remote_item_type) = cache.paths.get(&did);
481479

482480
let mut mydst = dst.clone();
@@ -495,25 +493,15 @@ fn write_shared(cx: &Context,
495493
try!(writeln!(&mut f, r"(function() \{var implementors = \{\};"));
496494

497495
for implementor in all_implementors.iter() {
498-
try!(writeln!(&mut f, "{}", *implementor));
496+
try!(write!(&mut f, "{}", *implementor));
499497
}
500498

501-
try!(write!(&mut f, r"implementors['{}'] = \{", krate.name));
499+
try!(write!(&mut f, r"implementors['{}'] = [", krate.name));
502500
for imp in imps.iter() {
503-
let &(ref path, item_type) = match *imp {
504-
PathType(clean::ResolvedPath { did, .. }) => {
505-
cache.paths.get(&did)
506-
}
507-
PathType(..) | OtherType(..) => continue,
508-
};
509-
try!(write!(&mut f, r#"{}:"#, *path.get(path.len() - 1)));
510-
try!(write!(&mut f, r#""{}"#,
511-
path.slice_to(path.len() - 1).connect("/")));
512-
try!(write!(&mut f, r#"/{}.{}.html","#,
513-
item_type.to_static_str(),
514-
*path.get(path.len() - 1)));
501+
try!(write!(&mut f, r#""impl{} {} for {}","#,
502+
imp.generics, imp.trait_, imp.for_));
515503
}
516-
try!(writeln!(&mut f, r"\};"));
504+
try!(writeln!(&mut f, r"];"));
517505
try!(writeln!(&mut f, "{}", r"
518506
if (window.register_implementors) {
519507
window.register_implementors(implementors);
@@ -737,16 +725,11 @@ impl DocFolder for Cache {
737725
let v = self.implementors.find_or_insert_with(did, |_| {
738726
Vec::new()
739727
});
740-
match i.for_ {
741-
clean::ResolvedPath{..} => {
742-
v.unshift(PathType(i.for_.clone()));
743-
}
744-
_ => {
745-
v.push(OtherType(i.generics.clone(),
746-
i.trait_.get_ref().clone(),
747-
i.for_.clone()));
748-
}
749-
}
728+
v.push(Implementor {
729+
generics: i.generics.clone(),
730+
trait_: i.trait_.get_ref().clone(),
731+
for_: i.for_.clone(),
732+
});
750733
}
751734
Some(..) | None => {}
752735
}
@@ -1489,34 +1472,33 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
14891472
try!(write!(w, "</div>"));
14901473
}
14911474

1492-
match cache_key.get().unwrap().implementors.find(&it.def_id) {
1475+
let cache = cache_key.get().unwrap();
1476+
try!(write!(w, "
1477+
<h2 id='implementors'>Implementors</h2>
1478+
<ul class='item-list' id='implementors-list'>
1479+
"));
1480+
match cache.implementors.find(&it.def_id) {
14931481
Some(implementors) => {
1494-
try!(write!(w, "
1495-
<h2 id='implementors'>Implementors</h2>
1496-
<ul class='item-list' id='implementors-list'>
1497-
"));
14981482
for i in implementors.iter() {
1499-
match *i {
1500-
PathType(ref ty) => {
1501-
try!(write!(w, "<li><code>{}</code></li>", *ty));
1502-
}
1503-
OtherType(ref generics, ref trait_, ref for_) => {
1504-
try!(write!(w, "<li><code>impl{} {} for {}</code></li>",
1505-
*generics, *trait_, *for_));
1506-
}
1507-
}
1483+
try!(writeln!(w, "<li><code>impl{} {} for {}</code></li>",
1484+
i.generics, i.trait_, i.for_));
15081485
}
1509-
try!(write!(w, "</ul>"));
1510-
try!(write!(w, r#"<script type="text/javascript" async
1511-
src="{}/implementors/{}/{}.{}.js"></script>"#,
1512-
cx.current.iter().map(|_| "..")
1513-
.collect::<Vec<&str>>().connect("/"),
1514-
cx.current.connect("/"),
1515-
shortty(it).to_static_str(),
1516-
*it.name.get_ref()));
15171486
}
15181487
None => {}
15191488
}
1489+
try!(write!(w, "</ul>"));
1490+
try!(write!(w, r#"<script type="text/javascript" async
1491+
src="{root_path}/implementors/{path}/\
1492+
{ty}.{name}.js"></script>"#,
1493+
root_path = Vec::from_elem(cx.current.len(), "..").connect("/"),
1494+
path = if ast_util::is_local(it.def_id) {
1495+
cx.current.connect("/")
1496+
} else {
1497+
let path = cache.external_paths.get(&it.def_id);
1498+
path.slice_to(path.len() - 1).connect("/")
1499+
},
1500+
ty = shortty(it).to_static_str(),
1501+
name = *it.name.get_ref()));
15201502
Ok(())
15211503
}
15221504

Diff for: src/librustdoc/html/static/main.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -658,15 +658,13 @@
658658
var list = $('#implementors-list');
659659
var libs = Object.getOwnPropertyNames(imp);
660660
for (var i = 0; i < libs.length; i++) {
661-
var structs = Object.getOwnPropertyNames(imp[libs[i]]);
661+
if (libs[i] == currentCrate) continue;
662+
var structs = imp[libs[i]];
662663
for (var j = 0; j < structs.length; j++) {
663-
console.log(i, structs[j]);
664-
var path = rootPath + imp[libs[i]][structs[j]];
665-
var klass = path.contains("type.") ? "type" : "struct";
666-
var link = $('<a>').text(structs[j])
667-
.attr('href', path)
668-
.attr('class', klass);
669-
var code = $('<code>').append(link);
664+
var code = $('<code>').append(structs[j]);
665+
$.each(code.find('a'), function(idx, a) {
666+
$(a).attr('href', rootPath + $(a).attr('href'));
667+
});
670668
var li = $('<li>').append(code);
671669
list.append(li);
672670
}

0 commit comments

Comments
 (0)