From c603839d5f8050cf050b18d7e2c14f48e78d2382 Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Sat, 11 Feb 2017 04:16:13 -0800 Subject: [PATCH 1/3] rustdoc: Only include a stability span if needed. --- src/librustdoc/clean/mod.rs | 30 ++++++++++++++++++++---------- src/librustdoc/html/render.rs | 17 ++++++++++++----- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 291fc8dfa9680..4a6e862246010 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -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 { + match self.stability { + 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> { diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 6234d89024441..fc1597469b1cd 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -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))?; @@ -2369,13 +2369,16 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, write!(w, " ", + ", 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, "", + stab = stability_class)?; + } document(w, cx, field)?; } } @@ -2406,11 +2409,15 @@ fn item_union(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, write!(w, "

Fields

")?; for (field, ty) in fields { write!(w, "{name}: {ty} - ", + ", 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, "", + stab = stability_class)?; + } + write!(w, "")?; document(w, cx, field)?; } } From bd14c7f333d55e7b84ab56e4fe6fef2e5325b1be Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Sat, 11 Feb 2017 07:10:03 -0800 Subject: [PATCH 2/3] Remove extra closing span element. --- src/librustdoc/html/render.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index fc1597469b1cd..fd83711f1f446 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2417,7 +2417,6 @@ fn item_union(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, write!(w, "", stab = stability_class)?; } - write!(w, "")?; document(w, cx, field)?; } } From 1fa9dbc00e8d5eff8f698097afb112c142bb8da0 Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Sat, 11 Feb 2017 10:00:56 -0800 Subject: [PATCH 3/3] Use functional transformations on the option instead of matching. --- src/librustdoc/clean/mod.rs | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 4a6e862246010..751ed7d443d29 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -328,26 +328,23 @@ impl Item { } pub fn stability_class(&self) -> Option { - match self.stability { - Some(ref s) => { - let mut classes = Vec::with_capacity(2); + self.stability.as_ref().and_then(|ref s| { + let mut classes = Vec::with_capacity(2); - if s.level == stability::Unstable { - classes.push("unstable"); - } + if s.level == stability::Unstable { + classes.push("unstable"); + } - if !s.deprecated_since.is_empty() { - classes.push("deprecated"); - } + if !s.deprecated_since.is_empty() { + classes.push("deprecated"); + } - if classes.len() != 0 { - Some(classes.join(" ")) - } else { - None - } + if classes.len() != 0 { + Some(classes.join(" ")) + } else { + None } - None => None, - } + }) } pub fn stable_since(&self) -> Option<&str> {