Skip to content

Commit 8552f2c

Browse files
committed
make render_stability_since_raw_with_extra return Option<impl fmt::Display>
1 parent ea829ee commit 8552f2c

File tree

2 files changed

+34
-48
lines changed

2 files changed

+34
-48
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ fn assoc_method<'a, 'tcx>(
10191019
/// example, if the item became stable at 1.0.0, and const-stable at 1.45.0, this function would
10201020
/// write a span containing "1.0.0 (const: 1.45.0)".
10211021
///
1022-
/// Returns `true` if a stability annotation was rendered.
1022+
/// Returns `None` if there is no stability annotation to be rendered.
10231023
///
10241024
/// Stability and const-stability are considered separately. If the item is unstable, no version
10251025
/// will be written. If the item is const-unstable, "const: unstable" will be appended to the
@@ -1030,11 +1030,10 @@ fn assoc_method<'a, 'tcx>(
10301030
/// will include the const-stable version, but no stable version will be emitted, as a natural
10311031
/// consequence of the above rules.
10321032
fn render_stability_since_raw_with_extra(
1033-
w: &mut String,
10341033
stable_version: Option<StableSince>,
10351034
const_stability: Option<ConstStability>,
10361035
extra_class: &str,
1037-
) -> bool {
1036+
) -> Option<impl fmt::Display + '_> {
10381037
let mut title = String::new();
10391038
let mut stability = String::new();
10401039

@@ -1084,14 +1083,9 @@ fn render_stability_since_raw_with_extra(
10841083
}
10851084
}
10861085

1087-
if !stability.is_empty() {
1088-
write_str(
1089-
w,
1090-
format_args!(r#"<span class="since{extra_class}" title="{title}">{stability}</span>"#),
1091-
);
1092-
}
1093-
1094-
!stability.is_empty()
1086+
(!stability.is_empty()).then_some(fmt::from_fn(move |w| {
1087+
write!(w, r#"<span class="since{extra_class}" title="{title}">{stability}</span>"#)
1088+
}))
10951089
}
10961090

10971091
fn since_to_string(since: &StableSince) -> Option<String> {
@@ -1104,11 +1098,10 @@ fn since_to_string(since: &StableSince) -> Option<String> {
11041098

11051099
#[inline]
11061100
fn render_stability_since_raw(
1107-
w: &mut String,
11081101
ver: Option<StableSince>,
11091102
const_stability: Option<ConstStability>,
1110-
) -> bool {
1111-
render_stability_since_raw_with_extra(w, ver, const_stability, "")
1103+
) -> Option<impl fmt::Display> {
1104+
render_stability_since_raw_with_extra(ver, const_stability, "")
11121105
}
11131106

11141107
fn render_assoc_item<'a, 'tcx>(
@@ -2114,30 +2107,27 @@ fn render_rightside(w: &mut String, cx: &Context<'_>, item: &clean::Item, render
21142107
let src_href = cx.src_href(item);
21152108
let has_src_ref = src_href.is_some();
21162109

2117-
let mut rightside = String::new();
2118-
let has_stability = render_stability_since_raw_with_extra(
2119-
&mut rightside,
2110+
let stability = render_stability_since_raw_with_extra(
21202111
item.stable_since(tcx),
21212112
const_stability,
21222113
if has_src_ref { "" } else { " rightside" },
21232114
);
2124-
if let Some(link) = src_href {
2125-
if has_stability {
2126-
write_str(
2127-
&mut rightside,
2128-
format_args!(" · <a class=\"src\" href=\"{link}\">Source</a>"),
2129-
);
2130-
} else {
2115+
match (stability, src_href) {
2116+
(Some(stability), Some(link)) => {
21312117
write_str(
2132-
&mut rightside,
2133-
format_args!("<a class=\"src rightside\" href=\"{link}\">Source</a>"),
2118+
w,
2119+
format_args!(
2120+
"<span class=\"rightside\">{stability} · <a class=\"src\" href=\"{link}\">Source</a></span>"
2121+
),
21342122
);
21352123
}
2136-
}
2137-
if has_stability && has_src_ref {
2138-
write_str(w, format_args!("<span class=\"rightside\">{rightside}</span>"));
2139-
} else {
2140-
w.push_str(&rightside);
2124+
(Some(stability), None) => {
2125+
write_str(w, format_args!("{stability}"));
2126+
}
2127+
(None, Some(link)) => {
2128+
write_str(w, format_args!("<a class=\"src rightside\" href=\"{link}\">Source</a>"));
2129+
}
2130+
(None, None) => {}
21412131
}
21422132
}
21432133

src/librustdoc/html/render/print_item.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,10 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut String)
206206
unreachable!();
207207
}
208208
};
209-
let stability_since_raw = {
210-
let mut buf = String::new();
211-
render_stability_since_raw(
212-
&mut buf,
213-
item.stable_since(cx.tcx()),
214-
item.const_stability(cx.tcx()),
215-
);
216-
buf
217-
};
209+
let stability_since_raw =
210+
render_stability_since_raw(item.stable_since(cx.tcx()), item.const_stability(cx.tcx()))
211+
.maybe_display()
212+
.to_string();
218213

219214
// Write source tag
220215
//
@@ -1693,16 +1688,17 @@ fn item_variants(
16931688
w,
16941689
format_args!(
16951690
"<section id=\"{id}\" class=\"variant\">\
1696-
<a href=\"#{id}\" class=\"anchor\">§</a>"
1691+
<a href=\"#{id}\" class=\"anchor\">§</a>\
1692+
{}\
1693+
<h3 class=\"code-header\">",
1694+
render_stability_since_raw_with_extra(
1695+
variant.stable_since(tcx),
1696+
variant.const_stability(tcx),
1697+
" rightside",
1698+
)
1699+
.maybe_display()
16971700
),
16981701
);
1699-
render_stability_since_raw_with_extra(
1700-
w,
1701-
variant.stable_since(tcx),
1702-
variant.const_stability(tcx),
1703-
" rightside",
1704-
);
1705-
w.push_str("<h3 class=\"code-header\">");
17061702
if let clean::VariantItem(ref var) = variant.kind
17071703
&& let clean::VariantKind::CLike = var.kind
17081704
{

0 commit comments

Comments
 (0)