Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: Only include a stability span if needed. #39740

Merged
merged 3 commits into from
Feb 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,27 @@ impl Item {
}
}

pub fn stability_class(&self) -> String {
self.stability.as_ref().map(|ref s| {
let mut base = match s.level {
stability::Unstable => "unstable".to_string(),
stability::Stable => String::new(),
};
if !s.deprecated_since.is_empty() {
base.push_str(" deprecated");
pub fn stability_class(&self) -> Option<String> {
match self.stability {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe (I'm no expert either) this would be more idiomatic:

self.stability.as_ref().and_then(|ref s| {
// ...
})

one level less of indentation, no need to map None to None. I made a tiny playground script to make sure they both mean the same thing if you wanna take a look https://is.gd/5fSbZf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, you're right. It was originally written that way, but I changed it because I got confused between Option::map and Option::and_then, thinking the closure couldn't return an option. I'll switch it back.

Some(ref s) => {
let mut classes = Vec::with_capacity(2);

if s.level == stability::Unstable {
classes.push("unstable");
}

if !s.deprecated_since.is_empty() {
classes.push("deprecated");
}

if classes.len() != 0 {
Some(classes.join(" "))
} else {
None
}
}
base
}).unwrap_or(String::new())
None => None,
}
}

pub fn stable_since(&self) -> Option<&str> {
Expand Down
17 changes: 12 additions & 5 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
stab_docs = stab_docs,
docs = shorter(Some(&Markdown(doc_value).to_string())),
class = myitem.type_(),
stab = myitem.stability_class(),
stab = myitem.stability_class().unwrap_or("".to_string()),
unsafety_flag = unsafety_flag,
href = item_path(myitem.type_(), myitem.name.as_ref().unwrap()),
title = full_path(cx, myitem))?;
Expand Down Expand Up @@ -2369,13 +2369,16 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
write!(w, "<span id='{id}' class='{item_type}'>
<span id='{ns_id}' class='invisible'>
<code>{name}: {ty}</code>
</span></span><span class='stab {stab}'></span>",
</span></span>",
item_type = ItemType::StructField,
id = id,
ns_id = ns_id,
stab = field.stability_class(),
name = field.name.as_ref().unwrap(),
ty = ty)?;
if let Some(stability_class) = field.stability_class() {
write!(w, "<span class='stab {stab}'></span>",
stab = stability_class)?;
}
document(w, cx, field)?;
}
}
Expand Down Expand Up @@ -2406,11 +2409,15 @@ fn item_union(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
write!(w, "<h2 class='fields'>Fields</h2>")?;
for (field, ty) in fields {
write!(w, "<span id='{shortty}.{name}' class='{shortty}'><code>{name}: {ty}</code>
</span><span class='stab {stab}'></span>",
</span>",
shortty = ItemType::StructField,
stab = field.stability_class(),
name = field.name.as_ref().unwrap(),
ty = ty)?;
if let Some(stability_class) = field.stability_class() {
write!(w, "<span class='stab {stab}'></span>",
stab = stability_class)?;
}
write!(w, "</span>")?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra close looks suspicious, and is likely the cause of the failing Travis build (which is failing on a rustdoc test).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

document(w, cx, field)?;
}
}
Expand Down