Skip to content

Commit 61bad30

Browse files
committed
Auto merge of #44920 - vi:rustdoc_implementors_src_link, r=GuillaumeGomez
rustdoc: Render [src] links for trait implementors Should close #43893. <s>No tests [yet].</s> r? @QuietMisdreavus
2 parents 67ed489 + acef039 commit 61bad30

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

src/librustdoc/html/render.rs

+29-14
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,18 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22532253
document(w, cx, it)
22542254
}
22552255

2256+
fn implementor2item<'a>(cache: &'a Cache, imp : &Implementor) -> Option<&'a clean::Item> {
2257+
if let Some(t_did) = imp.impl_.for_.def_id() {
2258+
if let Some(impl_item) = cache.impls.get(&t_did).and_then(|i| i.iter()
2259+
.find(|i| i.impl_item.def_id == imp.def_id))
2260+
{
2261+
let i = &impl_item.impl_item;
2262+
return Some(i);
2263+
}
2264+
}
2265+
None
2266+
}
2267+
22562268
fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22572269
t: &clean::Trait) -> fmt::Result {
22582270
let mut bounds = String::new();
@@ -2463,27 +2475,30 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
24632475
")?;
24642476

24652477
for implementor in foreign {
2466-
// need to get from a clean::Impl to a clean::Item so i can use render_impl
2467-
if let Some(t_did) = implementor.impl_.for_.def_id() {
2468-
if let Some(impl_item) = cache.impls.get(&t_did).and_then(|i| i.iter()
2469-
.find(|i| i.impl_item.def_id == implementor.def_id))
2470-
{
2471-
let i = &impl_item.impl_item;
2472-
let impl_ = Impl { impl_item: i.clone() };
2473-
let assoc_link = AssocItemLink::GotoSource(
2474-
i.def_id, &implementor.impl_.provided_trait_methods
2475-
);
2476-
render_impl(w, cx, &impl_, assoc_link,
2477-
RenderMode::Normal, i.stable_since(), false)?;
2478-
}
2478+
if let Some(i) = implementor2item(&cache, implementor) {
2479+
let impl_ = Impl { impl_item: i.clone() };
2480+
let assoc_link = AssocItemLink::GotoSource(
2481+
i.def_id, &implementor.impl_.provided_trait_methods
2482+
);
2483+
render_impl(w, cx, &impl_, assoc_link,
2484+
RenderMode::Normal, i.stable_since(), false)?;
24792485
}
24802486
}
24812487
}
24822488

24832489
write!(w, "{}", impl_header)?;
24842490

24852491
for implementor in local {
2486-
write!(w, "<li><code>")?;
2492+
write!(w, "<li>")?;
2493+
if let Some(item) = implementor2item(&cache, implementor) {
2494+
if let Some(l) = (Item { cx, item }).src_href() {
2495+
write!(w, "<div class='out-of-band'>")?;
2496+
write!(w, "<a class='srclink' href='{}' title='{}'>[src]</a>",
2497+
l, "goto source code")?;
2498+
write!(w, "</div>")?;
2499+
}
2500+
}
2501+
write!(w, "<code>")?;
24872502
// If there's already another implementor that has the same abbridged name, use the
24882503
// full path, for example in `std::iter::ExactSizeIterator`
24892504
let use_absolute = match implementor.impl_.for_ {

src/librustdoc/html/static/rustdoc.css

+8-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ h3.impl, h3.method, h4.method, h3.type, h4.type, h4.associatedconstant {
111111
h3.impl, h3.method, h3.type {
112112
margin-top: 15px;
113113
}
114-
h1, h2, h3, h4, .sidebar, a.source, .search-input, .content table :not(code)>a, .collapse-toggle {
114+
115+
h1, h2, h3, h4,
116+
.sidebar, a.source, .search-input, .content table :not(code)>a,
117+
.collapse-toggle, ul.item-list > li > .out-of-band {
115118
font-family: "Fira Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
116119
}
117120

@@ -310,6 +313,10 @@ h4.method > .out-of-band {
310313
font-size: 19px;
311314
}
312315

316+
ul.item-list > li > .out-of-band {
317+
font-size: 19px;
318+
}
319+
313320
h4 > code, h3 > code, .invisible > code {
314321
position: inherit;
315322
}

src/test/rustdoc/issue-43893.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2016 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+
// ignore-cross-compile
12+
13+
#![crate_name = "foo"]
14+
15+
pub trait SomeTrait {}
16+
pub struct SomeStruct;
17+
18+
// @has foo/trait.SomeTrait.html '//a/@href' '../src/foo/issue-43893.rs.html#19'
19+
impl SomeTrait for usize {}
20+
21+
// @has foo/trait.SomeTrait.html '//a/@href' '../src/foo/issue-43893.rs.html#22-24'
22+
impl SomeTrait for SomeStruct {
23+
// deliberately multi-line impl
24+
}

0 commit comments

Comments
 (0)