Skip to content

Commit cbe4ac3

Browse files
QuietMisdreavusGuillaumeGomez
authored andcommitted
spotlight Iterator/Read/Write impls on function return types
1 parent aabfed5 commit cbe4ac3

File tree

7 files changed

+99
-15
lines changed

7 files changed

+99
-15
lines changed

src/libcore/iter/iterator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fn _assert_is_object_safe(_: &Iterator<Item=()>) {}
3030
#[stable(feature = "rust1", since = "1.0.0")]
3131
#[rustc_on_unimplemented = "`{Self}` is not an iterator; maybe try calling \
3232
`.iter()` or a similar method"]
33+
#[doc(spotlight)]
3334
pub trait Iterator {
3435
/// The type of the elements being iterated over.
3536
#[stable(feature = "rust1", since = "1.0.0")]

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,13 @@ pub fn build_external_trait(cx: &DocContext, did: DefId) -> clean::Trait {
140140
let generics = (cx.tcx.generics_of(did), &predicates).clean(cx);
141141
let generics = filter_non_trait_generics(did, generics);
142142
let (generics, supertrait_bounds) = separate_supertrait_bounds(generics);
143+
let is_spotlight = load_attrs(cx, did).has_doc_flag("spotlight");
143144
clean::Trait {
144145
unsafety: cx.tcx.trait_def(did).unsafety,
145146
generics,
146147
items: trait_items,
147148
bounds: supertrait_bounds,
149+
is_spotlight,
148150
}
149151
}
150152

src/librustdoc/clean/mod.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
151151
match module.inner {
152152
ModuleItem(ref module) => {
153153
for it in &module.items {
154-
if it.is_extern_crate() && it.attrs.has_doc_masked() {
154+
if it.is_extern_crate() && it.attrs.has_doc_flag("masked") {
155155
masked_crates.insert(it.def_id.krate);
156156
}
157157
}
@@ -596,12 +596,12 @@ impl Attributes {
596596
None
597597
}
598598

599-
pub fn has_doc_masked(&self) -> bool {
599+
pub fn has_doc_flag(&self, flag: &str) -> bool {
600600
for attr in &self.other_attrs {
601601
if !attr.check_name("doc") { continue; }
602602

603603
if let Some(items) = attr.meta_item_list() {
604-
if items.iter().filter_map(|i| i.meta_item()).any(|it| it.check_name("masked")) {
604+
if items.iter().filter_map(|i| i.meta_item()).any(|it| it.check_name(flag)) {
605605
return true;
606606
}
607607
}
@@ -1331,19 +1331,31 @@ impl Clean<FunctionRetTy> for hir::FunctionRetTy {
13311331
}
13321332
}
13331333

1334+
impl GetDefId for FunctionRetTy {
1335+
fn def_id(&self) -> Option<DefId> {
1336+
match *self {
1337+
Return(ref ty) => ty.def_id(),
1338+
DefaultReturn => None,
1339+
}
1340+
}
1341+
}
1342+
13341343
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
13351344
pub struct Trait {
13361345
pub unsafety: hir::Unsafety,
13371346
pub items: Vec<Item>,
13381347
pub generics: Generics,
13391348
pub bounds: Vec<TyParamBound>,
1349+
pub is_spotlight: bool,
13401350
}
13411351

13421352
impl Clean<Item> for doctree::Trait {
13431353
fn clean(&self, cx: &DocContext) -> Item {
1354+
let attrs = self.attrs.clean(cx);
1355+
let is_spotlight = attrs.has_doc_flag("spotlight");
13441356
Item {
13451357
name: Some(self.name.clean(cx)),
1346-
attrs: self.attrs.clean(cx),
1358+
attrs: attrs,
13471359
source: self.whence.clean(cx),
13481360
def_id: cx.tcx.hir.local_def_id(self.id),
13491361
visibility: self.vis.clean(cx),
@@ -1354,6 +1366,7 @@ impl Clean<Item> for doctree::Trait {
13541366
items: self.items.clean(cx),
13551367
generics: self.generics.clean(cx),
13561368
bounds: self.bounds.clean(cx),
1369+
is_spotlight: is_spotlight,
13571370
}),
13581371
}
13591372
}

src/librustdoc/html/render.rs

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,7 +1816,8 @@ fn plain_summary_line(s: Option<&str>) -> String {
18161816

18171817
fn document(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item) -> fmt::Result {
18181818
document_stability(w, cx, item)?;
1819-
let prefix = render_assoc_const_value(item);
1819+
let mut prefix = render_assoc_const_value(item);
1820+
prefix.push_str(&render_spotlight_traits(item)?);
18201821
document_full(w, item, cx, &prefix)?;
18211822
Ok(())
18221823
}
@@ -2603,10 +2604,10 @@ fn assoc_const(w: &mut fmt::Formatter,
26032604
Ok(())
26042605
}
26052606

2606-
fn assoc_type(w: &mut fmt::Formatter, it: &clean::Item,
2607-
bounds: &Vec<clean::TyParamBound>,
2608-
default: Option<&clean::Type>,
2609-
link: AssocItemLink) -> fmt::Result {
2607+
fn assoc_type<W: fmt::Write>(w: &mut W, it: &clean::Item,
2608+
bounds: &Vec<clean::TyParamBound>,
2609+
default: Option<&clean::Type>,
2610+
link: AssocItemLink) -> fmt::Result {
26102611
write!(w, "type <a href='{}' class=\"type\">{}</a>",
26112612
naive_assoc_href(it, link),
26122613
it.name.as_ref().unwrap())?;
@@ -3236,6 +3237,62 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool) -> bool {
32363237
}
32373238
}
32383239

3240+
fn render_spotlight_traits(item: &clean::Item) -> Result<String, fmt::Error> {
3241+
let mut out = String::new();
3242+
3243+
match item.inner {
3244+
clean::FunctionItem(clean::Function { ref decl, .. }) |
3245+
clean::TyMethodItem(clean::TyMethod { ref decl, .. }) |
3246+
clean::MethodItem(clean::Method { ref decl, .. }) |
3247+
clean::ForeignFunctionItem(clean::Function { ref decl, .. }) => {
3248+
out = spotlight_decl(decl)?;
3249+
}
3250+
_ => {}
3251+
}
3252+
3253+
Ok(out)
3254+
}
3255+
3256+
fn spotlight_decl(decl: &clean::FnDecl) -> Result<String, fmt::Error> {
3257+
let mut out = String::new();
3258+
3259+
if let Some(did) = decl.output.def_id() {
3260+
let c = cache();
3261+
if let Some(impls) = c.impls.get(&did) {
3262+
for i in impls {
3263+
let impl_ = i.inner_impl();
3264+
if impl_.trait_.def_id().and_then(|d| c.traits.get(&d))
3265+
.map_or(false, |t| t.is_spotlight) {
3266+
if out.is_empty() {
3267+
out.push_str("<span class=\"docblock autohide\">");
3268+
out.push_str(&format!("<h3>Important traits for {}</h3>", impl_.for_));
3269+
out.push_str("<code class=\"spotlight\">");
3270+
}
3271+
3272+
//use the "where" class here to make it small
3273+
out.push_str(&format!("<span class=\"where fmt-newline\">{}</span>", impl_));
3274+
let t_did = impl_.trait_.def_id().unwrap();
3275+
for it in &impl_.items {
3276+
if let clean::TypedefItem(ref tydef, _) = it.inner {
3277+
out.push_str("<span class=\"where fmt-newline\"> ");
3278+
assoc_type(&mut out, it, &vec![],
3279+
Some(&tydef.type_),
3280+
AssocItemLink::GotoSource(t_did, &FxHashSet()))?;
3281+
out.push_str(";</span>");
3282+
}
3283+
}
3284+
}
3285+
}
3286+
}
3287+
}
3288+
3289+
if !out.is_empty() {
3290+
out.push_str("</code></span>");
3291+
}
3292+
3293+
Ok(out)
3294+
}
3295+
32393296
fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLink,
32403297
render_mode: RenderMode, outer_version: Option<&str>,
32413298
show_def_docs: bool) -> fmt::Result {
@@ -3270,14 +3327,16 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
32703327
trait_: Option<&clean::Trait>, show_def_docs: bool) -> fmt::Result {
32713328
let item_type = item.type_();
32723329
let name = item.name.as_ref().unwrap();
3330+
let mut method_prefix: Option<String> = None;
32733331

32743332
let render_method_item: bool = match render_mode {
32753333
RenderMode::Normal => true,
32763334
RenderMode::ForDeref { mut_: deref_mut_ } => should_render_item(&item, deref_mut_),
32773335
};
32783336

32793337
match item.inner {
3280-
clean::MethodItem(..) | clean::TyMethodItem(..) => {
3338+
clean::MethodItem(clean::Method { ref decl, .. }) |
3339+
clean::TyMethodItem(clean::TyMethod{ ref decl, .. }) => {
32813340
// Only render when the method is not static or we allow static methods
32823341
if render_method_item {
32833342
let id = derive_id(format!("{}.{}", item_type, name));
@@ -3297,6 +3356,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
32973356
render_stability_since_raw(w, item.stable_since(), outer_version)?;
32983357
}
32993358
write!(w, "</span></h4>\n")?;
3359+
method_prefix = Some(spotlight_decl(decl)?);
33003360
}
33013361
}
33023362
clean::TypedefItem(ref tydef, _) => {
@@ -3328,7 +3388,12 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
33283388
}
33293389

33303390
if render_method_item || render_mode == RenderMode::Normal {
3331-
let prefix = render_assoc_const_value(item);
3391+
let mut prefix = render_assoc_const_value(item);
3392+
3393+
if let Some(method_prefix) = method_prefix {
3394+
prefix.push_str(&method_prefix);
3395+
}
3396+
33323397
if !is_default_item {
33333398
if let Some(t) = trait_ {
33343399
// The trait item may have been stripped so we might not

src/librustdoc/html/static/main.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,9 +1624,7 @@
16241624
}
16251625

16261626
onEach(document.getElementById('main').getElementsByClassName('docblock'), function(e) {
1627-
if (e.parentNode.id === "main") {
1628-
e.parentNode.insertBefore(createToggle(), e);
1629-
}
1627+
e.parentNode.insertBefore(createToggle(), e);
16301628
});
16311629

16321630
onEach(document.getElementsByClassName('docblock'), function(e) {

src/librustdoc/html/static/rustdoc.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,12 @@ code, pre {
141141
border-radius: 3px;
142142
padding: 0 0.2em;
143143
}
144-
.docblock pre code, .docblock-short pre code {
144+
.docblock pre code, .docblock-short pre code, .docblock code.spotlight {
145145
padding: 0;
146146
}
147+
.docblock code.spotlight :last-child {
148+
padding-bottom: 0.6em;
149+
}
147150
pre {
148151
padding: 14px;
149152
}

src/libstd/io/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize>
450450
/// # }
451451
/// ```
452452
#[stable(feature = "rust1", since = "1.0.0")]
453+
#[doc(spotlight)]
453454
pub trait Read {
454455
/// Pull some bytes from this source into the specified buffer, returning
455456
/// how many bytes were read.
@@ -968,6 +969,7 @@ impl Initializer {
968969
/// # }
969970
/// ```
970971
#[stable(feature = "rust1", since = "1.0.0")]
972+
#[doc(spotlight)]
971973
pub trait Write {
972974
/// Write a buffer into this object, returning how many bytes were written.
973975
///

0 commit comments

Comments
 (0)