Skip to content

Commit 34392ad

Browse files
committed
rustdoc: Add type bounds to impls
1 parent 0a0d3c7 commit 34392ad

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/librustdoc/doc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub struct MethodDoc {
135135
#[deriving(Eq)]
136136
pub struct ImplDoc {
137137
item: ItemDoc,
138+
bounds_str: Option<~str>,
138139
trait_types: ~[~str],
139140
self_ty: Option<~str>,
140141
methods: ~[MethodDoc]

src/librustdoc/extract.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ fn impldoc_from_impl(
277277
) -> doc::ImplDoc {
278278
doc::ImplDoc {
279279
item: itemdoc,
280+
bounds_str: None,
280281
trait_types: ~[],
281282
self_ty: None,
282283
methods: do vec::map(methods) |method| {

src/librustdoc/markdown_pass.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ pub fn header_name(doc: doc::ItemTag) -> ~str {
249249
}
250250
&doc::ImplTag(ref doc) => {
251251
fail_unless!(doc.self_ty.is_some());
252+
let bounds = if (&doc.bounds_str).is_some() {
253+
fmt!(" where %s", (&doc.bounds_str).get())
254+
} else {
255+
~""
256+
};
252257
let self_ty = (&doc.self_ty).get();
253258
let mut trait_part = ~"";
254259
for doc.trait_types.eachi |i, trait_type| {
@@ -259,7 +264,7 @@ pub fn header_name(doc: doc::ItemTag) -> ~str {
259264
}
260265
trait_part += *trait_type;
261266
}
262-
fmt!("%s for %s", trait_part, self_ty)
267+
fmt!("%s for %s%s", trait_part, self_ty, bounds)
263268
}
264269
_ => {
265270
doc.name()
@@ -271,11 +276,18 @@ pub fn header_text(doc: doc::ItemTag) -> ~str {
271276
match &doc {
272277
&doc::ImplTag(ref ImplDoc) => {
273278
let header_kind = header_kind(copy doc);
279+
let bounds = if (&ImplDoc.bounds_str).is_some() {
280+
fmt!(" where `%s`", (&ImplDoc.bounds_str).get())
281+
} else {
282+
~""
283+
};
274284
let desc = if ImplDoc.trait_types.is_empty() {
275-
fmt!("for `%s`", (&ImplDoc.self_ty).get())
285+
fmt!("for `%s`%s", (&ImplDoc.self_ty).get(), bounds)
276286
} else {
277-
fmt!("of `%s` for `%s`", ImplDoc.trait_types[0],
278-
(&ImplDoc.self_ty).get())
287+
fmt!("of `%s` for `%s`%s",
288+
ImplDoc.trait_types[0],
289+
(&ImplDoc.self_ty).get(),
290+
bounds)
279291
};
280292
return fmt!("%s %s", header_kind, desc);
281293
}
@@ -749,6 +761,12 @@ fn should_write_impl_header() {
749761
fail_unless!(str::contains(markdown, ~"## Implementation for `int`"));
750762
}
751763

764+
#[test]
765+
fn should_write_impl_header_with_bounds() {
766+
let markdown = test::render(~"impl <T> int<T> { }");
767+
fail_unless!(str::contains(markdown, ~"## Implementation for `int<T>` where `<T>`"));
768+
}
769+
752770
#[test]
753771
fn should_write_impl_header_with_trait() {
754772
let markdown = test::render(~"impl j for int { fn a() { } }");

src/librustdoc/tystr_pass.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,20 @@ fn fold_impl(
271271
272272
let srv = fold.ctxt.clone();
273273
274-
let (trait_types, self_ty) = {
274+
let (bounds, trait_types, self_ty) = {
275275
let doc = copy doc;
276276
do astsrv::exec(srv) |ctxt| {
277277
match ctxt.ast_map.get(&doc.id()) {
278278
ast_map::node_item(@ast::item {
279-
node: ast::item_impl(_, opt_trait_type, self_ty, _), _
279+
node: ast::item_impl(ref generics, opt_trait_type, self_ty, _), _
280280
}, _) => {
281+
let bounds = pprust::generics_to_str(generics, extract::interner());
282+
let bounds = if bounds.is_empty() { None } else { Some(bounds) };
281283
let trait_types = opt_trait_type.map_default(~[], |p| {
282284
~[pprust::path_to_str(p.path, extract::interner())]
283285
});
284-
(trait_types,
286+
(bounds,
287+
trait_types,
285288
Some(pprust::ty_to_str(
286289
self_ty, extract::interner())))
287290
}
@@ -291,13 +294,20 @@ fn fold_impl(
291294
};
292295
293296
doc::ImplDoc {
297+
bounds_str: bounds,
294298
trait_types: trait_types,
295299
self_ty: self_ty,
296300
methods: merge_methods(fold.ctxt.clone(), doc.id(), copy doc.methods),
297301
.. doc
298302
}
299303
}
300304
305+
#[test]
306+
fn should_add_impl_bounds() {
307+
let doc = test::mk_doc(~"impl<T, U: Copy, V: Copy + Clone> Option<T, U, V> { }");
308+
fail_unless!(doc.cratemod().impls()[0].bounds_str == Some(~"<T, U: Copy, V: Copy + Clone>"));
309+
}
310+
301311
#[test]
302312
fn should_add_impl_trait_types() {
303313
let doc = test::mk_doc(~"impl j for int { fn a<T>() { } }");

0 commit comments

Comments
 (0)