Skip to content

Commit 72d5675

Browse files
committed
Address review comments
1 parent 5c01cf4 commit 72d5675

File tree

2 files changed

+43
-49
lines changed

2 files changed

+43
-49
lines changed

src/librustdoc/html/markdown.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use std::fmt;
3535
use std::slice;
3636
use std::str;
3737

38-
use html::render::with_unique_id;
38+
use html::render::derive_id;
3939
use html::toc::TocBuilder;
4040
use html::highlight;
4141
use html::escape::Escape;
@@ -307,17 +307,17 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
307307
let opaque = unsafe { (*data).opaque as *mut hoedown_html_renderer_state };
308308
let opaque = unsafe { &mut *((*opaque).opaque as *mut MyOpaque) };
309309

310-
let text = with_unique_id(id, |id| {
311-
let sec = opaque.toc_builder.as_mut().map_or("".to_owned(), |builder| {
312-
format!("{} ", builder.push(level as u32, s.clone(), id.to_owned()))
313-
});
310+
let id = derive_id(id);
314311

315-
// Render the HTML
316-
format!("<h{lvl} id='{id}' class='section-header'>\
317-
<a href='#{id}'>{sec}{}</a></h{lvl}>",
318-
s, lvl = level, id = id, sec = sec)
312+
let sec = opaque.toc_builder.as_mut().map_or("".to_owned(), |builder| {
313+
format!("{} ", builder.push(level as u32, s.clone(), id.clone()))
319314
});
320315

316+
// Render the HTML
317+
let text = format!("<h{lvl} id='{id}' class='section-header'>\
318+
<a href='#{id}'>{sec}{}</a></h{lvl}>",
319+
s, lvl = level, id = id, sec = sec);
320+
321321
let text = CString::new(text).unwrap();
322322
unsafe { hoedown_buffer_puts(ob, text.as_ptr()) }
323323
}

src/librustdoc/html/render.rs

+34-40
Original file line numberDiff line numberDiff line change
@@ -372,23 +372,19 @@ pub fn reset_ids() {
372372
USED_ID_MAP.with(|s| *s.borrow_mut() = init_ids());
373373
}
374374

375-
pub fn with_unique_id<T, F: FnOnce(&str) -> T>(candidate: String, f: F) -> T {
375+
pub fn derive_id(candidate: String) -> String {
376376
USED_ID_MAP.with(|map| {
377-
let (id, ret) = match map.borrow_mut().get_mut(&candidate) {
378-
None => {
379-
let ret = f(&candidate);
380-
(candidate, ret)
381-
},
377+
let id = match map.borrow_mut().get_mut(&candidate) {
378+
None => candidate,
382379
Some(a) => {
383380
let id = format!("{}-{}", candidate, *a);
384-
let ret = f(&id);
385381
*a += 1;
386-
(id, ret)
382+
id
387383
}
388384
};
389385

390-
map.borrow_mut().insert(id, 1);
391-
ret
386+
map.borrow_mut().insert(id.clone(), 1);
387+
id
392388
})
393389
}
394390

@@ -1745,10 +1741,9 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
17451741
ItemType::AssociatedType => ("associated-types", "Associated Types"),
17461742
ItemType::AssociatedConst => ("associated-consts", "Associated Constants"),
17471743
};
1748-
try!(with_unique_id(short.to_owned(), |id|
1749-
write!(w, "<h2 id='{id}' class='section-header'>\
1750-
<a href=\"#{id}\">{name}</a></h2>\n<table>",
1751-
id = id, name = name)));
1744+
try!(write!(w, "<h2 id='{id}' class='section-header'>\
1745+
<a href=\"#{id}\">{name}</a></h2>\n<table>",
1746+
id = derive_id(short.to_owned()), name = name));
17521747
}
17531748

17541749
match myitem.inner {
@@ -1970,10 +1965,10 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
19701965
fn trait_item(w: &mut fmt::Formatter, cx: &Context, m: &clean::Item)
19711966
-> fmt::Result {
19721967
let name = m.name.as_ref().unwrap();
1973-
try!(with_unique_id(format!("{}.{}", shortty(m), name), |id|
1974-
write!(w, "<h3 id='{id}' class='method stab {stab}'><code>",
1968+
let id = derive_id(format!("{}.{}", shortty(m), name));
1969+
try!(write!(w, "<h3 id='{id}' class='method stab {stab}'><code>",
19751970
id = id,
1976-
stab = m.stability_class())));
1971+
stab = m.stability_class()));
19771972
try!(render_assoc_item(w, m, AssocItemLink::Anchor));
19781973
try!(write!(w, "</code></h3>"));
19791974
try!(document(w, cx, m));
@@ -2162,12 +2157,11 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
21622157
if fields.peek().is_some() {
21632158
try!(write!(w, "<h2 class='fields'>Fields</h2>\n<table>"));
21642159
for field in fields {
2165-
let name = field.name.as_ref().unwrap();
2166-
try!(with_unique_id(format!("structfield.{}", name), |id|
2167-
write!(w, "<tr class='stab {}'><td id='{}'><code>{}</code></td><td>",
2168-
field.stability_class(),
2169-
id,
2170-
name)));
2160+
try!(write!(w, "<tr class='stab {stab}'>
2161+
<td id='structfield.{name}'>\
2162+
<code>{name}</code></td><td>",
2163+
stab = field.stability_class(),
2164+
name = field.name.as_ref().unwrap()));
21712165
try!(document(w, cx, field));
21722166
try!(write!(w, "</td></tr>"));
21732167
}
@@ -2234,9 +2228,8 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22342228
if !e.variants.is_empty() {
22352229
try!(write!(w, "<h2 class='variants'>Variants</h2>\n<table>"));
22362230
for variant in &e.variants {
2237-
let name = variant.name.as_ref().unwrap();
2238-
try!(with_unique_id(format!("variant.{}", name), |id|
2239-
write!(w, "<tr><td id='{}'><code>{}</code></td><td>", id, name)));
2231+
try!(write!(w, "<tr><td id='variant.{name}'><code>{name}</code></td><td>",
2232+
name = variant.name.as_ref().unwrap()));
22402233
try!(document(w, cx, variant));
22412234
match variant.inner {
22422235
clean::VariantItem(ref var) => {
@@ -2254,10 +2247,11 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22542247
try!(write!(w, "<h3 class='fields'>Fields</h3>\n
22552248
<table>"));
22562249
for field in fields {
2257-
let v = variant.name.as_ref().unwrap();
2258-
let f = field.name.as_ref().unwrap();
2259-
try!(with_unique_id(format!("variant.{}.field.{}", v, f), |id|
2260-
write!(w, "<tr><td id='{}'><code>{}</code></td><td>", id, f)));
2250+
try!(write!(w, "<tr><td \
2251+
id='variant.{v}.field.{f}'>\
2252+
<code>{f}</code></td><td>",
2253+
v = variant.name.as_ref().unwrap(),
2254+
f = field.name.as_ref().unwrap()));
22612255
try!(document(w, cx, field));
22622256
try!(write!(w, "</td></tr>"));
22632257
}
@@ -2474,33 +2468,33 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
24742468
clean::MethodItem(..) | clean::TyMethodItem(..) => {
24752469
// Only render when the method is not static or we allow static methods
24762470
if !is_static_method(item) || render_static {
2477-
try!(with_unique_id(format!("method.{}", name), |id|
2478-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
2471+
let id = derive_id(format!("method.{}", name));
2472+
try!(write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item)));
24792473
try!(render_assoc_item(w, item, link));
24802474
try!(write!(w, "</code></h4>\n"));
24812475
}
24822476
}
24832477
clean::TypedefItem(ref tydef, _) => {
2484-
try!(with_unique_id(format!("assoc_type.{}", name), |id|
2485-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
2478+
let id = derive_id(format!("assoc_type.{}", name));
2479+
try!(write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item)));
24862480
try!(write!(w, "type {} = {}", name, tydef.type_));
24872481
try!(write!(w, "</code></h4>\n"));
24882482
}
24892483
clean::AssociatedConstItem(ref ty, ref default) => {
2490-
try!(with_unique_id(format!("assoc_const.{}", name), |id|
2491-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
2484+
let id = derive_id(format!("assoc_const.{}", name));
2485+
try!(write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item)));
24922486
try!(assoc_const(w, item, ty, default.as_ref()));
24932487
try!(write!(w, "</code></h4>\n"));
24942488
}
24952489
clean::ConstantItem(ref c) => {
2496-
try!(with_unique_id(format!("assoc_const.{}", name), |id|
2497-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
2490+
let id = derive_id(format!("assoc_const.{}", name));
2491+
try!(write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item)));
24982492
try!(assoc_const(w, item, &c.type_, Some(&c.expr)));
24992493
try!(write!(w, "</code></h4>\n"));
25002494
}
25012495
clean::AssociatedTypeItem(ref bounds, ref default) => {
2502-
try!(with_unique_id(format!("assoc_type.{}", name), |id|
2503-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))));
2496+
let id = derive_id(format!("assoc_type.{}", name));
2497+
try!(write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item)));
25042498
try!(assoc_type(w, item, bounds, default));
25052499
try!(write!(w, "</code></h4>\n"));
25062500
}

0 commit comments

Comments
 (0)