Skip to content

Commit 3f144e1

Browse files
Move Toc printing from fmt::Display
1 parent 8a9dab3 commit 3f144e1

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ impl MarkdownWithToc<'_> {
752752
html::push_html(&mut s, p);
753753
}
754754

755-
format!("<nav id=\"TOC\">{}</nav>{}", toc.into_toc(), s)
755+
format!("<nav id=\"TOC\">{}</nav>{}", toc.into_toc().print(), s)
756756
}
757757
}
758758

src/librustdoc/html/toc.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
//! Table-of-contents creation.
22
3-
use std::fmt;
4-
use std::string::String;
5-
63
/// A (recursive) table of contents
7-
#[derive(PartialEq)]
4+
#[derive(Debug, PartialEq)]
85
pub struct Toc {
96
/// The levels are strictly decreasing, i.e.
107
///
@@ -28,7 +25,7 @@ impl Toc {
2825
}
2926
}
3027

31-
#[derive(PartialEq)]
28+
#[derive(Debug, PartialEq)]
3229
pub struct TocEntry {
3330
level: u32,
3431
sec_number: String,
@@ -165,25 +162,23 @@ impl TocBuilder {
165162
}
166163
}
167164

168-
impl fmt::Debug for Toc {
169-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170-
fmt::Display::fmt(self, f)
171-
}
172-
}
173-
174-
impl fmt::Display for Toc {
175-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
176-
write!(fmt, "<ul>")?;
165+
impl Toc {
166+
fn print_inner(&self, v: &mut String) {
167+
v.push_str("<ul>");
177168
for entry in &self.entries {
178-
// recursively format this table of contents (the
179-
// `{children}` is the key).
180-
write!(fmt,
181-
"\n<li><a href=\"#{id}\">{num} {name}</a>{children}</li>",
169+
// recursively format this table of contents
170+
v.push_str(&format!("\n<li><a href=\"#{id}\">{num} {name}</a>",
182171
id = entry.id,
183-
num = entry.sec_number, name = entry.name,
184-
children = entry.children)?
172+
num = entry.sec_number, name = entry.name));
173+
entry.children.print_inner(&mut *v);
174+
v.push_str("</li>");
185175
}
186-
write!(fmt, "</ul>")
176+
v.push_str("</ul>");
177+
}
178+
crate fn print(&self) -> String {
179+
let mut v = String::new();
180+
self.print_inner(&mut v);
181+
v
187182
}
188183
}
189184

0 commit comments

Comments
 (0)