Skip to content

Commit b34427d

Browse files
committed
rustdoc: Render associated types on traits and impls
1 parent b825b34 commit b34427d

File tree

6 files changed

+97
-40
lines changed

6 files changed

+97
-40
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ pub enum ItemEnum {
336336
ForeignStaticItem(Static),
337337
MacroItem(Macro),
338338
PrimitiveItem(PrimitiveType),
339-
AssociatedTypeItem,
339+
AssociatedTypeItem(TyParam),
340340
}
341341

342342
#[deriving(Clone, Encodable, Decodable)]
@@ -982,6 +982,8 @@ impl Clean<Type> for ast::PolyTraitRef {
982982
}
983983
}
984984

985+
/// An item belonging to a trait, whether a method or associated. Could be named
986+
/// TraitItem except that's already taken by an exported enum variant.
985987
#[deriving(Clone, Encodable, Decodable)]
986988
pub enum TraitMethod {
987989
RequiredMethod(Item),
@@ -1002,6 +1004,12 @@ impl TraitMethod {
10021004
_ => false,
10031005
}
10041006
}
1007+
pub fn is_type(&self) -> bool {
1008+
match self {
1009+
&TypeTraitItem(..) => true,
1010+
_ => false,
1011+
}
1012+
}
10051013
pub fn item<'a>(&'a self) -> &'a Item {
10061014
match *self {
10071015
RequiredMethod(ref item) => item,
@@ -2211,7 +2219,7 @@ impl Clean<Item> for ast::AssociatedType {
22112219
source: self.ty_param.span.clean(cx),
22122220
name: Some(self.ty_param.ident.clean(cx)),
22132221
attrs: self.attrs.clean(cx),
2214-
inner: AssociatedTypeItem,
2222+
inner: AssociatedTypeItem(self.ty_param.clean(cx)),
22152223
visibility: None,
22162224
def_id: ast_util::local_def(self.ty_param.id),
22172225
stability: None,
@@ -2225,7 +2233,17 @@ impl Clean<Item> for ty::AssociatedType {
22252233
source: DUMMY_SP.clean(cx),
22262234
name: Some(self.name.clean(cx)),
22272235
attrs: Vec::new(),
2228-
inner: AssociatedTypeItem,
2236+
// FIXME(#18048): this is wrong, but cross-crate associated types are broken
2237+
// anyway, for the time being.
2238+
inner: AssociatedTypeItem(TyParam {
2239+
name: self.name.clean(cx),
2240+
did: ast::DefId {
2241+
krate: 0,
2242+
node: ast::DUMMY_NODE_ID
2243+
},
2244+
bounds: vec![],
2245+
default: None
2246+
}),
22292247
visibility: None,
22302248
def_id: self.def_id,
22312249
stability: None,

src/librustdoc/html/format.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ pub struct Stability<'a>(pub &'a Option<clean::Stability>);
4646
pub struct ConciseStability<'a>(pub &'a Option<clean::Stability>);
4747
/// Wrapper struct for emitting a where clause from Generics.
4848
pub struct WhereClause<'a>(pub &'a clean::Generics);
49-
5049
/// Wrapper struct for emitting type parameter bounds.
51-
struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
50+
pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
5251

5352
impl VisSpace {
5453
pub fn get(&self) -> Option<ast::Visibility> {

src/librustdoc/html/item_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn shortty(item: &clean::Item) -> ItemType {
9595
clean::ForeignStaticItem(..) => ForeignStatic,
9696
clean::MacroItem(..) => Macro,
9797
clean::PrimitiveItem(..) => Primitive,
98-
clean::AssociatedTypeItem => AssociatedType,
98+
clean::AssociatedTypeItem(..) => AssociatedType,
9999
}
100100
}
101101

src/librustdoc/html/render.rs

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use clean;
5757
use doctree;
5858
use fold::DocFolder;
5959
use html::format::{VisSpace, Method, FnStyleSpace, MutableSpace, Stability};
60-
use html::format::{ConciseStability, WhereClause};
60+
use html::format::{ConciseStability, TyParamBounds, WhereClause};
6161
use html::highlight;
6262
use html::item_type::{ItemType, shortty};
6363
use html::item_type;
@@ -1654,27 +1654,23 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
16541654
t.generics,
16551655
bounds,
16561656
WhereClause(&t.generics)));
1657-
let required = t.items.iter()
1658-
.filter(|m| {
1659-
match **m {
1660-
clean::RequiredMethod(_) => true,
1661-
_ => false,
1662-
}
1663-
})
1664-
.collect::<Vec<&clean::TraitMethod>>();
1665-
let provided = t.items.iter()
1666-
.filter(|m| {
1667-
match **m {
1668-
clean::ProvidedMethod(_) => true,
1669-
_ => false,
1670-
}
1671-
})
1672-
.collect::<Vec<&clean::TraitMethod>>();
1657+
1658+
let types = t.items.iter().filter(|m| m.is_type()).collect::<Vec<_>>();
1659+
let required = t.items.iter().filter(|m| m.is_req()).collect::<Vec<_>>();
1660+
let provided = t.items.iter().filter(|m| m.is_def()).collect::<Vec<_>>();
16731661

16741662
if t.items.len() == 0 {
16751663
try!(write!(w, "{{ }}"));
16761664
} else {
16771665
try!(write!(w, "{{\n"));
1666+
for t in types.iter() {
1667+
try!(write!(w, " "));
1668+
try!(render_method(w, t.item()));
1669+
try!(write!(w, ";\n"));
1670+
}
1671+
if types.len() > 0 && required.len() > 0 {
1672+
try!(w.write("\n".as_bytes()));
1673+
}
16781674
for m in required.iter() {
16791675
try!(write!(w, " "));
16801676
try!(render_method(w, m.item()));
@@ -1707,6 +1703,17 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
17071703
Ok(())
17081704
}
17091705

1706+
if types.len() > 0 {
1707+
try!(write!(w, "
1708+
<h2 id='associated-types'>Associated Types</h2>
1709+
<div class='methods'>
1710+
"));
1711+
for t in types.iter() {
1712+
try!(trait_item(w, *t));
1713+
}
1714+
try!(write!(w, "</div>"));
1715+
}
1716+
17101717
// Output the documentation for each function individually
17111718
if required.len() > 0 {
17121719
try!(write!(w, "
@@ -1761,7 +1768,7 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
17611768
}
17621769

17631770
fn render_method(w: &mut fmt::Formatter, meth: &clean::Item) -> fmt::Result {
1764-
fn fun(w: &mut fmt::Formatter, it: &clean::Item, fn_style: ast::FnStyle,
1771+
fn method(w: &mut fmt::Formatter, it: &clean::Item, fn_style: ast::FnStyle,
17651772
g: &clean::Generics, selfty: &clean::SelfTy,
17661773
d: &clean::FnDecl) -> fmt::Result {
17671774
write!(w, "{}fn <a href='#{ty}.{name}' class='fnname'>{name}</a>\
@@ -1776,14 +1783,28 @@ fn render_method(w: &mut fmt::Formatter, meth: &clean::Item) -> fmt::Result {
17761783
decl = Method(selfty, d),
17771784
where_clause = WhereClause(g))
17781785
}
1786+
fn assoc_type(w: &mut fmt::Formatter, it: &clean::Item,
1787+
typ: &clean::TyParam) -> fmt::Result {
1788+
try!(write!(w, "type {}", it.name.as_ref().unwrap()));
1789+
if typ.bounds.len() > 0 {
1790+
try!(write!(w, ": {}", TyParamBounds(&*typ.bounds)))
1791+
}
1792+
if let Some(ref default) = typ.default {
1793+
try!(write!(w, " = {}", default));
1794+
}
1795+
Ok(())
1796+
}
17791797
match meth.inner {
17801798
clean::TyMethodItem(ref m) => {
1781-
fun(w, meth, m.fn_style, &m.generics, &m.self_, &m.decl)
1799+
method(w, meth, m.fn_style, &m.generics, &m.self_, &m.decl)
17821800
}
17831801
clean::MethodItem(ref m) => {
1784-
fun(w, meth, m.fn_style, &m.generics, &m.self_, &m.decl)
1802+
method(w, meth, m.fn_style, &m.generics, &m.self_, &m.decl)
17851803
}
1786-
_ => unreachable!()
1804+
clean::AssociatedTypeItem(ref typ) => {
1805+
assoc_type(w, meth, typ)
1806+
}
1807+
_ => panic!("render_method called on non-method")
17871808
}
17881809
}
17891810

@@ -2040,11 +2061,26 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
20402061

20412062
fn doctraititem(w: &mut fmt::Formatter, item: &clean::Item, dox: bool)
20422063
-> fmt::Result {
2043-
try!(write!(w, "<h4 id='method.{}' class='method'>{}<code>",
2044-
*item.name.as_ref().unwrap(),
2045-
ConciseStability(&item.stability)));
2046-
try!(render_method(w, item));
2047-
try!(write!(w, "</code></h4>\n"));
2064+
match item.inner {
2065+
clean::MethodItem(..) => {
2066+
try!(write!(w, "<h4 id='method.{}' class='{}'>{}<code>",
2067+
*item.name.as_ref().unwrap(),
2068+
shortty(item),
2069+
ConciseStability(&item.stability)));
2070+
try!(render_method(w, item));
2071+
try!(write!(w, "</code></h4>\n"));
2072+
}
2073+
clean::TypedefItem(ref tydef) => {
2074+
let name = item.name.as_ref().unwrap();
2075+
try!(write!(w, "<h4 id='assoc_type.{}' class='{}'>{}<code>",
2076+
*name,
2077+
shortty(item),
2078+
ConciseStability(&item.stability)));
2079+
try!(write!(w, "type {} = {}", name, tydef.type_));
2080+
try!(write!(w, "</code></h4>\n"));
2081+
}
2082+
_ => panic!("unknown trait item with name {}", item.name)
2083+
}
20482084
match item.doc_value() {
20492085
Some(s) if dox => {
20502086
try!(write!(w, "<div class='docblock'>{}</div>", Markdown(s)));
@@ -2054,7 +2090,7 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
20542090
}
20552091
}
20562092

2057-
try!(write!(w, "<div class='impl-methods'>"));
2093+
try!(write!(w, "<div class='impl-items'>"));
20582094
for trait_item in i.impl_.items.iter() {
20592095
try!(doctraititem(w, trait_item, true));
20602096
}
@@ -2076,6 +2112,8 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
20762112

20772113
// If we've implemented a trait, then also emit documentation for all
20782114
// default methods which weren't overridden in the implementation block.
2115+
// FIXME: this also needs to be done for associated types, whenever defaults
2116+
// for them work.
20792117
match i.impl_.trait_ {
20802118
Some(clean::ResolvedPath { did, .. }) => {
20812119
try!({

src/librustdoc/html/static/main.css

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ h2 {
8484
h3 {
8585
font-size: 1.3em;
8686
}
87-
h1, h2, h3:not(.impl):not(.method), h4:not(.method) {
87+
h1, h2, h3:not(.impl):not(.method):not(.type), h4:not(.method):not(.type) {
8888
color: black;
8989
font-weight: 500;
9090
margin: 20px 0 15px 0;
@@ -94,15 +94,15 @@ h1.fqn {
9494
border-bottom: 1px dashed #D5D5D5;
9595
margin-top: 0;
9696
}
97-
h2, h3:not(.impl):not(.method), h4:not(.method) {
97+
h2, h3:not(.impl):not(.method):not(.type), h4:not(.method):not(.type) {
9898
border-bottom: 1px solid #DDDDDD;
9999
}
100-
h3.impl, h3.method, h4.method {
100+
h3.impl, h3.method, h4.method, h3.type, h4.type {
101101
font-weight: 600;
102102
margin-top: 10px;
103103
margin-bottom: 10px;
104104
}
105-
h3.impl, h3.method {
105+
h3.impl, h3.method, h3.type {
106106
margin-top: 15px;
107107
}
108108
h1, h2, h3, h4, section.sidebar, a.source, .search-input, .content table :not(code)>a, .collapse-toggle {
@@ -234,6 +234,7 @@ nav.sub {
234234
.content .highlighted.struct { background-color: #e7b1a0; }
235235
.content .highlighted.fn { background-color: #c6afb3; }
236236
.content .highlighted.method { background-color: #c6afb3; }
237+
.content .highlighted.type { background-color: #c6afb3; }
237238
.content .highlighted.ffi { background-color: #c6afb3; }
238239

239240
.docblock.short.nowrap {
@@ -306,7 +307,7 @@ nav.sub {
306307
}
307308
.content .methods .docblock { margin-left: 40px; }
308309

309-
.content .impl-methods .docblock { margin-left: 40px; }
310+
.content .impl-items .docblock { margin-left: 40px; }
310311

311312
nav {
312313
border-bottom: 1px solid #e0e0e0;
@@ -440,7 +441,7 @@ h1 .stability {
440441
padding: 4px 10px;
441442
}
442443

443-
.impl-methods .stability, .methods .stability {
444+
.impl-items .stability, .methods .stability {
444445
margin-right: 20px;
445446
}
446447

src/librustdoc/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
#![crate_type = "rlib"]
1717

1818
#![allow(unknown_features)]
19-
#![feature(globs, struct_variant, macro_rules, phase, slicing_syntax, tuple_indexing)]
19+
#![feature(globs, macro_rules, phase, slicing_syntax, tuple_indexing)]
20+
#![feature(if_let)]
2021

2122
extern crate arena;
2223
extern crate getopts;

0 commit comments

Comments
 (0)