Skip to content

Commit 41f6e97

Browse files
committed
rustdoc: Render default methods for impls as well
This does not work for cross-crate implementations of traits. Cross-crate implementations are a separate issue that should be addressed separately. Basically when an implementation of an external trait is detected, the trait would have to be loaded at that time (or possibly sooner...). Rustdoc currently doesn't have the proper infrastructure for adding this. Closes #9985 cc #9999
1 parent bf6bb01 commit 41f6e97

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

src/librustdoc/html/render.rs

+56-29
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,9 @@ pub struct Cache {
149149

150150
/// This map contains information about all known traits of this crate.
151151
/// Implementations of a crate should inherit the documentation of the
152-
/// parent trait if no extra documentation is specified, and this map is
153-
/// keyed on trait id with a value of a 'method name => documentation'
154-
/// mapping.
155-
traits: HashMap<ast::NodeId, HashMap<~str, ~str>>,
152+
/// parent trait if no extra documentation is specified, and default methods
153+
/// should show up in documentation about trait implementations.
154+
traits: HashMap<ast::NodeId, clean::Trait>,
156155

157156
/// When rendering traits, it's often useful to be able to list all
158157
/// implementors of the trait, and this mapping is exactly, that: a mapping
@@ -488,18 +487,7 @@ impl DocFolder for Cache {
488487
// trait
489488
match item.inner {
490489
clean::TraitItem(ref t) => {
491-
let mut dox = HashMap::new();
492-
for meth in t.methods.iter() {
493-
let it = meth.item();
494-
match it.doc_value() {
495-
None => {}
496-
Some(s) => {
497-
dox.insert(it.name.get_ref().to_owned(),
498-
s.to_owned());
499-
}
500-
}
501-
}
502-
self.traits.insert(item.id, dox);
490+
self.traits.insert(item.id, t.clone());
503491
}
504492
_ => {}
505493
}
@@ -1480,18 +1468,25 @@ fn render_impl(w: &mut io::Writer, i: &clean::Impl, dox: &Option<~str>) {
14801468
}
14811469
None => {}
14821470
}
1483-
write!(w, "<div class='methods'>");
1484-
for meth in i.methods.iter() {
1471+
1472+
fn docmeth(w: &mut io::Writer, item: &clean::Item) -> bool {
14851473
write!(w, "<h4 id='method.{}' class='method'><code>",
1486-
*meth.name.get_ref());
1487-
render_method(w, meth, false);
1474+
*item.name.get_ref());
1475+
render_method(w, item, false);
14881476
write!(w, "</code></h4>\n");
1489-
match meth.doc_value() {
1477+
match item.doc_value() {
14901478
Some(s) => {
14911479
write!(w, "<div class='docblock'>{}</div>", Markdown(s));
1492-
continue
1480+
true
14931481
}
1494-
None => {}
1482+
None => false
1483+
}
1484+
}
1485+
1486+
write!(w, "<div class='methods'>");
1487+
for meth in i.methods.iter() {
1488+
if docmeth(w, meth) {
1489+
continue
14951490
}
14961491

14971492
// No documentation? Attempt to slurp in the trait's documentation
@@ -1501,13 +1496,19 @@ fn render_impl(w: &mut io::Writer, i: &clean::Impl, dox: &Option<~str>) {
15011496
};
15021497
do local_data::get(cache_key) |cache| {
15031498
do cache.unwrap().read |cache| {
1504-
let name = meth.name.get_ref().as_slice();
15051499
match cache.traits.find(&trait_id) {
1506-
Some(m) => {
1507-
match m.find_equiv(&name) {
1508-
Some(s) => {
1509-
write!(w, "<div class='docblock'>{}</div>",
1510-
Markdown(s.as_slice()));
1500+
Some(t) => {
1501+
let name = meth.name.clone();
1502+
match t.methods.iter().find(|t| t.item().name == name) {
1503+
Some(method) => {
1504+
match method.item().doc_value() {
1505+
Some(s) => {
1506+
write!(w,
1507+
"<div class='docblock'>{}</div>",
1508+
Markdown(s));
1509+
}
1510+
None => {}
1511+
}
15111512
}
15121513
None => {}
15131514
}
@@ -1517,6 +1518,32 @@ fn render_impl(w: &mut io::Writer, i: &clean::Impl, dox: &Option<~str>) {
15171518
}
15181519
}
15191520
}
1521+
1522+
// If we've implemented a trait, then also emit documentation for all
1523+
// default methods which weren't overridden in the implementation block.
1524+
match trait_id {
1525+
None => {}
1526+
Some(id) => {
1527+
do local_data::get(cache_key) |cache| {
1528+
do cache.unwrap().read |cache| {
1529+
match cache.traits.find(&id) {
1530+
Some(t) => {
1531+
for method in t.methods.iter() {
1532+
let n = method.item().name.clone();
1533+
match i.methods.iter().find(|m| m.name == n) {
1534+
Some(*) => continue,
1535+
None => {}
1536+
}
1537+
1538+
docmeth(w, method.item());
1539+
}
1540+
}
1541+
None => {}
1542+
}
1543+
}
1544+
}
1545+
}
1546+
}
15201547
write!(w, "</div>");
15211548
}
15221549

0 commit comments

Comments
 (0)