Skip to content

Commit c250b5f

Browse files
Remove fmt::Display impls on Markdown structs
These impls prevent ergonomic use of the config (e.g., forcing us to use RefCell) despite all usecases for these structs only using their Display impls once.
1 parent dbad77f commit c250b5f

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

src/librustdoc/externalfiles.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl ExternalHtml {
3636
load_external_files(md_before_content, diag)
3737
.map(|m_bc| (ih,
3838
format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map),
39-
codes, edition, playground))))
39+
codes, edition, playground).to_string())))
4040
)
4141
.and_then(|(ih, bc)|
4242
load_external_files(after_content, diag)
@@ -46,7 +46,7 @@ impl ExternalHtml {
4646
load_external_files(md_after_content, diag)
4747
.map(|m_ac| (ih, bc,
4848
format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map),
49-
codes, edition, playground))))
49+
codes, edition, playground).to_string())))
5050
)
5151
.map(|(ih, bc, ac)|
5252
ExternalHtml {

src/librustdoc/html/markdown.rs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! Markdown formatting for rustdoc.
22
//!
3-
//! This module implements markdown formatting through the pulldown-cmark
4-
//! rust-library. This module exposes all of the
5-
//! functionality through a unit struct, `Markdown`, which has an implementation
6-
//! of `fmt::Display`. Example usage:
3+
//! This module implements markdown formatting through the pulldown-cmark library.
74
//!
85
//! ```
96
//! #![feature(rustc_private)]
@@ -16,8 +13,9 @@
1613
//!
1714
//! let s = "My *markdown* _text_";
1815
//! let mut id_map = IdMap::new();
19-
//! let html = format!("{}", Markdown(s, &[], RefCell::new(&mut id_map),
20-
//! ErrorCodes::Yes, Edition::Edition2015, None));
16+
//! let md = Markdown(s, &[], RefCell::new(&mut id_map),
17+
//! ErrorCodes::Yes, Edition::Edition2015, None);
18+
//! let html = md.to_string();
2119
//! // ... something using html
2220
//! ```
2321
@@ -27,7 +25,7 @@ use rustc_data_structures::fx::FxHashMap;
2725
use std::cell::RefCell;
2826
use std::collections::VecDeque;
2927
use std::default::Default;
30-
use std::fmt::{self, Write};
28+
use std::fmt::Write;
3129
use std::borrow::Cow;
3230
use std::ops::Range;
3331
use std::str;
@@ -46,9 +44,8 @@ fn opts() -> Options {
4644
Options::ENABLE_TABLES | Options::ENABLE_FOOTNOTES
4745
}
4846

49-
/// A tuple struct that has the `fmt::Display` trait implemented.
50-
/// When formatted, this struct will emit the HTML corresponding to the rendered
51-
/// version of the contained markdown string.
47+
/// When `to_string` is called, this struct will emit the HTML corresponding to
48+
/// the rendered version of the contained markdown string.
5249
pub struct Markdown<'a>(
5350
pub &'a str,
5451
/// A list of link replacements.
@@ -691,13 +688,13 @@ impl LangString {
691688
}
692689
}
693690

694-
impl<'a> fmt::Display for Markdown<'a> {
695-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
696-
let Markdown(md, links, ref ids, codes, edition, playground) = *self;
691+
impl Markdown<'_> {
692+
pub fn to_string(self) -> String {
693+
let Markdown(md, links, ids, codes, edition, playground) = self;
697694
let mut ids = ids.borrow_mut();
698695

699696
// This is actually common enough to special-case
700-
if md.is_empty() { return Ok(()) }
697+
if md.is_empty() { return String::new(); }
701698
let replacer = |_: &str, s: &str| {
702699
if let Some(&(_, ref replace)) = links.into_iter().find(|link| &*link.0 == s) {
703700
Some((replace.clone(), s.to_owned()))
@@ -716,13 +713,13 @@ impl<'a> fmt::Display for Markdown<'a> {
716713
let p = Footnotes::new(p);
717714
html::push_html(&mut s, p);
718715

719-
fmt.write_str(&s)
716+
s
720717
}
721718
}
722719

723-
impl<'a> fmt::Display for MarkdownWithToc<'a> {
724-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
725-
let MarkdownWithToc(md, ref ids, codes, edition, playground) = *self;
720+
impl MarkdownWithToc<'_> {
721+
pub fn to_string(self) -> String {
722+
let MarkdownWithToc(md, ref ids, codes, edition, playground) = self;
726723
let mut ids = ids.borrow_mut();
727724

728725
let p = Parser::new_ext(md, opts());
@@ -738,19 +735,17 @@ impl<'a> fmt::Display for MarkdownWithToc<'a> {
738735
html::push_html(&mut s, p);
739736
}
740737

741-
write!(fmt, "<nav id=\"TOC\">{}</nav>", toc.into_toc())?;
742-
743-
fmt.write_str(&s)
738+
format!("<nav id=\"TOC\">{}</nav>{}", toc.into_toc(), s)
744739
}
745740
}
746741

747-
impl<'a> fmt::Display for MarkdownHtml<'a> {
748-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
749-
let MarkdownHtml(md, ref ids, codes, edition, playground) = *self;
742+
impl MarkdownHtml<'_> {
743+
pub fn to_string(self) -> String {
744+
let MarkdownHtml(md, ref ids, codes, edition, playground) = self;
750745
let mut ids = ids.borrow_mut();
751746

752747
// This is actually common enough to special-case
753-
if md.is_empty() { return Ok(()) }
748+
if md.is_empty() { return String::new(); }
754749
let p = Parser::new_ext(md, opts());
755750

756751
// Treat inline HTML as plain text.
@@ -766,15 +761,15 @@ impl<'a> fmt::Display for MarkdownHtml<'a> {
766761
let p = Footnotes::new(p);
767762
html::push_html(&mut s, p);
768763

769-
fmt.write_str(&s)
764+
s
770765
}
771766
}
772767

773-
impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
774-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
775-
let MarkdownSummaryLine(md, links) = *self;
768+
impl MarkdownSummaryLine<'_> {
769+
pub fn to_string(self) -> String {
770+
let MarkdownSummaryLine(md, links) = self;
776771
// This is actually common enough to special-case
777-
if md.is_empty() { return Ok(()) }
772+
if md.is_empty() { return String::new(); }
778773

779774
let replacer = |_: &str, s: &str| {
780775
if let Some(&(_, ref replace)) = links.into_iter().find(|link| &*link.0 == s) {
@@ -790,7 +785,7 @@ impl<'a> fmt::Display for MarkdownSummaryLine<'a> {
790785

791786
html::push_html(&mut s, LinkReplacer::new(SummaryLine::new(p), links));
792787

793-
fmt.write_str(&s)
788+
s
794789
}
795790
}
796791

src/librustdoc/html/render.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,7 +2596,7 @@ fn render_markdown(w: &mut fmt::Formatter<'_>,
25962596
if is_hidden { " hidden" } else { "" },
25972597
prefix,
25982598
Markdown(md_text, &links, RefCell::new(&mut ids),
2599-
cx.codes, cx.edition, &cx.playground))
2599+
cx.codes, cx.edition, &cx.playground).to_string())
26002600
}
26012601

26022602
fn document_short(
@@ -2866,7 +2866,7 @@ fn item_module(w: &mut fmt::Formatter<'_>, cx: &Context,
28662866
</tr>",
28672867
name = *myitem.name.as_ref().unwrap(),
28682868
stab_tags = stability_tags(myitem),
2869-
docs = MarkdownSummaryLine(doc_value, &myitem.links()),
2869+
docs = MarkdownSummaryLine(doc_value, &myitem.links()).to_string(),
28702870
class = myitem.type_(),
28712871
add = add,
28722872
stab = stab.unwrap_or_else(|| String::new()),
@@ -2963,7 +2963,7 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec<String> {
29632963
let mut ids = cx.id_map.borrow_mut();
29642964
let html = MarkdownHtml(
29652965
&note, RefCell::new(&mut ids), error_codes, cx.edition, &cx.playground);
2966-
message.push_str(&format!(": {}", html));
2966+
message.push_str(&format!(": {}", html.to_string()));
29672967
}
29682968
stability.push(format!("<div class='stab deprecated'>{}</div>", message));
29692969
}
@@ -3017,7 +3017,7 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec<String> {
30173017
error_codes,
30183018
cx.edition,
30193019
&cx.playground,
3020-
)
3020+
).to_string()
30213021
);
30223022
}
30233023

@@ -4248,7 +4248,7 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt
42484248
let mut ids = cx.id_map.borrow_mut();
42494249
write!(w, "<div class='docblock'>{}</div>",
42504250
Markdown(&*dox, &i.impl_item.links(), RefCell::new(&mut ids),
4251-
cx.codes, cx.edition, &cx.playground))?;
4251+
cx.codes, cx.edition, &cx.playground).to_string())?;
42524252
}
42534253
}
42544254

src/tools/error_index_generator/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl Formatter for HTMLFormatter {
101101
};
102102
write!(output, "{}",
103103
Markdown(desc, &[], RefCell::new(&mut id_map),
104-
ErrorCodes::Yes, DEFAULT_EDITION, &Some(playground)))?
104+
ErrorCodes::Yes, DEFAULT_EDITION, &Some(playground)).to_string())?
105105
},
106106
None => write!(output, "<p>No description.</p>\n")?,
107107
}

0 commit comments

Comments
 (0)