Skip to content

Commit

Permalink
Drop RefCell from IdMap in markdown rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed Aug 11, 2019
1 parent c250b5f commit 1aa0964
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 deletions.
6 changes: 2 additions & 4 deletions src/librustdoc/externalfiles.rs
Expand Up @@ -6,8 +6,6 @@ use crate::syntax::feature_gate::UnstableFeatures;
use crate::syntax::edition::Edition;
use crate::html::markdown::{IdMap, ErrorCodes, Markdown, Playground};

use std::cell::RefCell;

#[derive(Clone, Debug)]
pub struct ExternalHtml {
/// Content that will be included inline in the <head> section of a
Expand Down Expand Up @@ -35,7 +33,7 @@ impl ExternalHtml {
.and_then(|(ih, bc)|
load_external_files(md_before_content, diag)
.map(|m_bc| (ih,
format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map),
format!("{}{}", bc, Markdown(&m_bc, &[], id_map,
codes, edition, playground).to_string())))
)
.and_then(|(ih, bc)|
Expand All @@ -45,7 +43,7 @@ impl ExternalHtml {
.and_then(|(ih, bc, ac)|
load_external_files(md_after_content, diag)
.map(|m_ac| (ih, bc,
format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map),
format!("{}{}", ac, Markdown(&m_ac, &[], id_map,
codes, edition, playground).to_string())))
)
.map(|(ih, bc, ac)|
Expand Down
19 changes: 7 additions & 12 deletions src/librustdoc/html/markdown.rs
Expand Up @@ -9,12 +9,10 @@
//!
//! use syntax::edition::Edition;
//! use rustdoc::html::markdown::{IdMap, Markdown, ErrorCodes};
//! use std::cell::RefCell;
//!
//! let s = "My *markdown* _text_";
//! let mut id_map = IdMap::new();
//! let md = Markdown(s, &[], RefCell::new(&mut id_map),
//! ErrorCodes::Yes, Edition::Edition2015, None);
//! let md = Markdown(s, &[], &mut id_map, ErrorCodes::Yes, Edition::Edition2015, &None);
//! let html = md.to_string();
//! // ... something using html
//! ```
Expand Down Expand Up @@ -51,7 +49,7 @@ pub struct Markdown<'a>(
/// A list of link replacements.
pub &'a [(String, String)],
/// The current list of used header IDs.
pub RefCell<&'a mut IdMap>,
pub &'a mut IdMap,
/// Whether to allow the use of explicit error codes in doctest lang strings.
pub ErrorCodes,
/// Default edition to use when parsing doctests (to add a `fn main`).
Expand All @@ -61,15 +59,15 @@ pub struct Markdown<'a>(
/// A tuple struct like `Markdown` that renders the markdown with a table of contents.
pub struct MarkdownWithToc<'a>(
pub &'a str,
pub RefCell<&'a mut IdMap>,
pub &'a mut IdMap,
pub ErrorCodes,
pub Edition,
pub &'a Option<Playground>,
);
/// A tuple struct like `Markdown` that renders the markdown escaping HTML tags.
pub struct MarkdownHtml<'a>(
pub &'a str,
pub RefCell<&'a mut IdMap>,
pub &'a mut IdMap,
pub ErrorCodes,
pub Edition,
pub &'a Option<Playground>,
Expand Down Expand Up @@ -690,8 +688,7 @@ impl LangString {

impl Markdown<'_> {
pub fn to_string(self) -> String {
let Markdown(md, links, ids, codes, edition, playground) = self;
let mut ids = ids.borrow_mut();
let Markdown(md, links, mut ids, codes, edition, playground) = self;

// This is actually common enough to special-case
if md.is_empty() { return String::new(); }
Expand Down Expand Up @@ -719,8 +716,7 @@ impl Markdown<'_> {

impl MarkdownWithToc<'_> {
pub fn to_string(self) -> String {
let MarkdownWithToc(md, ref ids, codes, edition, playground) = self;
let mut ids = ids.borrow_mut();
let MarkdownWithToc(md, mut ids, codes, edition, playground) = self;

let p = Parser::new_ext(md, opts());

Expand All @@ -741,8 +737,7 @@ impl MarkdownWithToc<'_> {

impl MarkdownHtml<'_> {
pub fn to_string(self) -> String {
let MarkdownHtml(md, ref ids, codes, edition, playground) = self;
let mut ids = ids.borrow_mut();
let MarkdownHtml(md, mut ids, codes, edition, playground) = self;

// This is actually common enough to special-case
if md.is_empty() { return String::new(); }
Expand Down
12 changes: 6 additions & 6 deletions src/librustdoc/html/markdown/tests.rs
Expand Up @@ -73,8 +73,8 @@ fn test_lang_string_parse() {
fn test_header() {
fn t(input: &str, expect: &str) {
let mut map = IdMap::new();
let output = Markdown(input, &[], RefCell::new(&mut map),
ErrorCodes::Yes, DEFAULT_EDITION).to_string();
let output = Markdown(
input, &[], &mut map, ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string();
assert_eq!(output, expect, "original: {}", input);
}

Expand All @@ -96,8 +96,8 @@ fn test_header() {
fn test_header_ids_multiple_blocks() {
let mut map = IdMap::new();
fn t(map: &mut IdMap, input: &str, expect: &str) {
let output = Markdown(input, &[], RefCell::new(map),
ErrorCodes::Yes, DEFAULT_EDITION).to_string();
let output = Markdown(input, &[], map,
ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string();
assert_eq!(output, expect, "original: {}", input);
}

Expand Down Expand Up @@ -134,8 +134,8 @@ fn test_plain_summary_line() {
fn test_markdown_html_escape() {
fn t(input: &str, expect: &str) {
let mut idmap = IdMap::new();
let output = MarkdownHtml(input, RefCell::new(&mut idmap),
ErrorCodes::Yes, DEFAULT_EDITION).to_string();
let output = MarkdownHtml(input, &mut idmap,
ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string();
assert_eq!(output, expect, "original: {}", input);
}

Expand Down
9 changes: 4 additions & 5 deletions src/librustdoc/html/render.rs
Expand Up @@ -2595,7 +2595,7 @@ fn render_markdown(w: &mut fmt::Formatter<'_>,
write!(w, "<div class='docblock{}'>{}{}</div>",
if is_hidden { " hidden" } else { "" },
prefix,
Markdown(md_text, &links, RefCell::new(&mut ids),
Markdown(md_text, &links, &mut ids,
cx.codes, cx.edition, &cx.playground).to_string())
}

Expand Down Expand Up @@ -2961,8 +2961,7 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec<String> {

if let Some(note) = note {
let mut ids = cx.id_map.borrow_mut();
let html = MarkdownHtml(
&note, RefCell::new(&mut ids), error_codes, cx.edition, &cx.playground);
let html = MarkdownHtml(&note, &mut ids, error_codes, cx.edition, &cx.playground);
message.push_str(&format!(": {}", html.to_string()));
}
stability.push(format!("<div class='stab deprecated'>{}</div>", message));
Expand Down Expand Up @@ -3013,7 +3012,7 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec<String> {
message,
MarkdownHtml(
&unstable_reason,
RefCell::new(&mut ids),
&mut ids,
error_codes,
cx.edition,
&cx.playground,
Expand Down Expand Up @@ -4247,7 +4246,7 @@ fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocIt
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
let mut ids = cx.id_map.borrow_mut();
write!(w, "<div class='docblock'>{}</div>",
Markdown(&*dox, &i.impl_item.links(), RefCell::new(&mut ids),
Markdown(&*dox, &i.impl_item.links(), &mut ids,
cx.codes, cx.edition, &cx.playground).to_string())?;
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/librustdoc/markdown.rs
@@ -1,7 +1,6 @@
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::cell::RefCell;

use errors;
use testing;
Expand Down Expand Up @@ -83,9 +82,9 @@ pub fn render(
let mut ids = IdMap::new();
let error_codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
let text = if !options.markdown_no_toc {
MarkdownWithToc(text, RefCell::new(&mut ids), error_codes, edition, &playground).to_string()
MarkdownWithToc(text, &mut ids, error_codes, edition, &playground).to_string()
} else {
Markdown(text, &[], RefCell::new(&mut ids), error_codes, edition, &playground).to_string()
Markdown(text, &[], &mut ids, error_codes, edition, &playground).to_string()
};

let err = write!(
Expand Down
2 changes: 1 addition & 1 deletion src/tools/error_index_generator/main.rs
Expand Up @@ -100,7 +100,7 @@ impl Formatter for HTMLFormatter {
url: String::from("https://play.rust-lang.org/"),
};
write!(output, "{}",
Markdown(desc, &[], RefCell::new(&mut id_map),
Markdown(desc, &[], &mut id_map,
ErrorCodes::Yes, DEFAULT_EDITION, &Some(playground)).to_string())?
},
None => write!(output, "<p>No description.</p>\n")?,
Expand Down

0 comments on commit 1aa0964

Please sign in to comment.