Skip to content

Commit 8f6855c

Browse files
committed
rustdoc: Render methods/impls for bare traits
This renders a "Methods" and "Trait Implementations" section for each item implemented for a bare trait itself. Closes #19055
1 parent ba40231 commit 8f6855c

File tree

2 files changed

+65
-32
lines changed

2 files changed

+65
-32
lines changed

src/librustdoc/html/render.rs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,10 @@ impl DocFolder for Cache {
920920
let path = match self.paths.get(&did) {
921921
Some(&(_, ItemType::Trait)) =>
922922
Some(&self.stack[..self.stack.len() - 1]),
923-
// The current stack not necessarily has correlation for
924-
// where the type was defined. On the other hand,
925-
// `paths` always has the right information if present.
923+
// The current stack not necessarily has correlation
924+
// for where the type was defined. On the other
925+
// hand, `paths` always has the right
926+
// information if present.
926927
Some(&(ref fqp, ItemType::Struct)) |
927928
Some(&(ref fqp, ItemType::Enum)) =>
928929
Some(&fqp[..fqp.len() - 1]),
@@ -1861,6 +1862,9 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
18611862
try!(write!(w, "</div>"));
18621863
}
18631864

1865+
// If there are methods directly on this trait object, render them here.
1866+
try!(render_methods(w, it));
1867+
18641868
let cache = cache();
18651869
try!(write!(w, "
18661870
<h2 id='implementors'>Implementors</h2>
@@ -2179,37 +2183,36 @@ enum MethodLink {
21792183
}
21802184

21812185
fn render_methods(w: &mut fmt::Formatter, it: &clean::Item) -> fmt::Result {
2182-
match cache().impls.get(&it.def_id) {
2183-
Some(v) => {
2184-
let (non_trait, traits): (Vec<_>, _) = v.iter().cloned()
2185-
.partition(|i| i.impl_.trait_.is_none());
2186-
if non_trait.len() > 0 {
2187-
try!(write!(w, "<h2 id='methods'>Methods</h2>"));
2188-
for i in &non_trait {
2189-
try!(render_impl(w, i, MethodLink::Anchor));
2190-
}
2191-
}
2192-
if traits.len() > 0 {
2193-
try!(write!(w, "<h2 id='implementations'>Trait \
2194-
Implementations</h2>"));
2195-
let (derived, manual): (Vec<_>, _) = traits.into_iter()
2196-
.partition(|i| i.impl_.derived);
2197-
for i in &manual {
2198-
let did = i.trait_did().unwrap();
2199-
try!(render_impl(w, i, MethodLink::GotoSource(did)));
2200-
}
2201-
if derived.len() > 0 {
2202-
try!(write!(w, "<h3 id='derived_implementations'>\
2203-
Derived Implementations \
2204-
</h3>"));
2205-
for i in &derived {
2206-
let did = i.trait_did().unwrap();
2207-
try!(render_impl(w, i, MethodLink::GotoSource(did)));
2208-
}
2209-
}
2186+
let v = match cache().impls.get(&it.def_id) {
2187+
Some(v) => v.clone(),
2188+
None => return Ok(()),
2189+
};
2190+
let (non_trait, traits): (Vec<_>, _) = v.into_iter()
2191+
.partition(|i| i.impl_.trait_.is_none());
2192+
if non_trait.len() > 0 {
2193+
try!(write!(w, "<h2 id='methods'>Methods</h2>"));
2194+
for i in &non_trait {
2195+
try!(render_impl(w, i, MethodLink::Anchor));
2196+
}
2197+
}
2198+
if traits.len() > 0 {
2199+
try!(write!(w, "<h2 id='implementations'>Trait \
2200+
Implementations</h2>"));
2201+
let (derived, manual): (Vec<_>, _) = traits.into_iter()
2202+
.partition(|i| i.impl_.derived);
2203+
for i in &manual {
2204+
let did = i.trait_did().unwrap();
2205+
try!(render_impl(w, i, MethodLink::GotoSource(did)));
2206+
}
2207+
if derived.len() > 0 {
2208+
try!(write!(w, "<h3 id='derived_implementations'>\
2209+
Derived Implementations \
2210+
</h3>"));
2211+
for i in &derived {
2212+
let did = i.trait_did().unwrap();
2213+
try!(render_impl(w, i, MethodLink::GotoSource(did)));
22102214
}
22112215
}
2212-
None => {}
22132216
}
22142217
Ok(())
22152218
}

src/test/rustdoc/issue-19055.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// @has issue_19055/trait.Any.html
12+
pub trait Any {}
13+
14+
impl<'any> Any + 'any {
15+
// @has - '//*[@id="method.is"]' 'fn is'
16+
pub fn is<T: 'static>(&self) -> bool { loop {} }
17+
18+
// @has - '//*[@id="method.downcast_ref"]' 'fn downcast_ref'
19+
pub fn downcast_ref<T: 'static>(&self) -> Option<&T> { loop {} }
20+
21+
// @has - '//*[@id="method.downcast_mut"]' 'fn downcast_mut'
22+
pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> { loop {} }
23+
}
24+
25+
pub trait Foo {
26+
fn foo(&self) {}
27+
}
28+
29+
// @has - '//*[@id="method.foo"]' 'fn foo'
30+
impl Foo for Any {}

0 commit comments

Comments
 (0)