Skip to content

Commit

Permalink
macro -> fn
Browse files Browse the repository at this point in the history
  • Loading branch information
yotamofek committed Feb 11, 2025
1 parent 1f6faa1 commit 9cb57c6
Show file tree
Hide file tree
Showing 4 changed files with 451 additions and 386 deletions.
44 changes: 10 additions & 34 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,15 @@ use crate::html::render::Context;
use crate::joined::Joined as _;
use crate::passes::collect_intra_doc_links::UrlFragment;

/// This macro is the same as [`std::write!`] for [`String`]s, but swallows the returned `Result`,
/// since writing into a `String` can never fail.
macro_rules! write_str {
($dst:expr, $($arg:tt)*) => {{
// make sure $dst is a `String` (or `&mut String`)
trait AssertString {
fn assert_string(&mut self) {}
}
impl AssertString for ::std::string::String {}
$dst.assert_string();

let _ = $dst.write_fmt(::std::format_args!($($arg)*));
}};
pub(crate) fn write_str(s: &mut String, f: fmt::Arguments<'_>) {
s.write_fmt(f).unwrap();
}
/// This macro is the same as [`std::writeln!`] for [`String`]s, but swallows the returned `Result`,
/// since writing into a `String` can never fail.
macro_rules! writeln_str {
($dst:expr, $($arg:tt)*) => {{
// make sure $dst is a `String` (or `&mut String`)
trait AssertString {
fn assert_string(&mut self) {}
}
impl AssertString for ::std::string::String {}
$dst.assert_string();

let _ = $dst.write_fmt(::std::format_args_nl!($($arg)*));
}};
pub(crate) fn writeln_str(s: &mut String, f: fmt::Arguments<'_>) {
s.write_fmt(f).unwrap();
s.push('\n');
}

pub(crate) use {write_str, writeln_str};

pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
bounds: &'a [clean::GenericBound],
cx: &'a Context<'tcx>,
Expand Down Expand Up @@ -689,8 +667,6 @@ pub(crate) fn href_relative_parts<'fqp>(
}

pub(crate) fn link_tooltip(did: DefId, fragment: &Option<UrlFragment>, cx: &Context<'_>) -> String {
use write_str as write;

let cache = cx.cache();
let Some((fqp, shortty)) = cache.paths.get(&did).or_else(|| cache.external_paths.get(&did))
else {
Expand All @@ -704,16 +680,16 @@ pub(crate) fn link_tooltip(did: DefId, fragment: &Option<UrlFragment>, cx: &Cont
fqp
};
if let &Some(UrlFragment::Item(id)) = fragment {
write!(buf, "{} ", cx.tcx().def_descr(id));
write_str(&mut buf, format_args!("{} ", cx.tcx().def_descr(id)));
for component in fqp {
write!(buf, "{component}::");
write_str(&mut buf, format_args!("{component}::"));
}
write!(buf, "{}", cx.tcx().item_name(id));
write_str(&mut buf, format_args!("{}", cx.tcx().item_name(id)));
} else if !fqp.is_empty() {
let mut fqp_it = fqp.iter();
write!(buf, "{shortty} {}", fqp_it.next().unwrap());
write_str(&mut buf, format_args!("{shortty} {}", fqp_it.next().unwrap()));
for component in fqp_it {
write!(buf, "::{component}");
write_str(&mut buf, format_args!("::{component}"));
}
}
buf
Expand Down
73 changes: 39 additions & 34 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_span::edition::Edition;
use rustc_span::symbol::Symbol;
use rustc_span::{BytePos, DUMMY_SP, Span};

use super::format;
use super::format::{self, write_str, writeln_str};
use crate::clean::PrimitiveType;
use crate::html::escape::EscapeBodyText;
use crate::html::render::{Context, LinkFromSrc};
Expand Down Expand Up @@ -65,57 +65,63 @@ fn write_header(
tooltip: Tooltip,
extra_classes: &[String],
) {
use super::format::write_str as write;

write!(
write_str(
out,
"<div class=\"example-wrap{}\">",
match tooltip {
Tooltip::Ignore => " ignore",
Tooltip::CompileFail => " compile_fail",
Tooltip::ShouldPanic => " should_panic",
Tooltip::Edition(_) => " edition",
Tooltip::None => "",
},
format_args!(
"<div class=\"example-wrap{}\">",
match tooltip {
Tooltip::Ignore => " ignore",
Tooltip::CompileFail => " compile_fail",
Tooltip::ShouldPanic => " should_panic",
Tooltip::Edition(_) => " edition",
Tooltip::None => "",
}
),
);

if tooltip != Tooltip::None {
let edition_code;
write!(
write_str(
out,
"<a href=\"#\" class=\"tooltip\" title=\"{}\">ⓘ</a>",
match tooltip {
Tooltip::Ignore => "This example is not tested",
Tooltip::CompileFail => "This example deliberately fails to compile",
Tooltip::ShouldPanic => "This example panics",
Tooltip::Edition(edition) => {
edition_code = format!("This example runs with edition {edition}");
&edition_code
format_args!(
"<a href=\"#\" class=\"tooltip\" title=\"{}\">ⓘ</a>",
match tooltip {
Tooltip::Ignore => "This example is not tested",
Tooltip::CompileFail => "This example deliberately fails to compile",
Tooltip::ShouldPanic => "This example panics",
Tooltip::Edition(edition) => {
edition_code = format!("This example runs with edition {edition}");
&edition_code
}
Tooltip::None => unreachable!(),
}
Tooltip::None => unreachable!(),
},
),
);
}

if let Some(extra) = extra_content {
out.push_str(&extra);
}
if class.is_empty() {
write!(
write_str(
out,
"<pre class=\"rust{}{}\">",
if extra_classes.is_empty() { "" } else { " " },
extra_classes.join(" "),
format_args!(
"<pre class=\"rust{}{}\">",
if extra_classes.is_empty() { "" } else { " " },
extra_classes.join(" ")
),
);
} else {
write!(
write_str(
out,
"<pre class=\"rust {class}{}{}\">",
if extra_classes.is_empty() { "" } else { " " },
extra_classes.join(" "),
format_args!(
"<pre class=\"rust {class}{}{}\">",
if extra_classes.is_empty() { "" } else { " " },
extra_classes.join(" ")
),
);
}
write!(out, "<code>");
write_str(out, format_args!("<code>"));
}

/// Check if two `Class` can be merged together. In the following rules, "unclassified" means `None`
Expand Down Expand Up @@ -325,8 +331,7 @@ pub(super) fn write_code(
}

fn write_footer(out: &mut String, playground_button: Option<&str>) {
use super::format::writeln_str as writeln;
writeln!(out, "</code></pre>{}</div>", playground_button.unwrap_or_default());
writeln_str(out, format_args!("</code></pre>{}</div>", playground_button.unwrap_or_default()));
}

/// How a span of text is classified. Mostly corresponds to token kinds.
Expand Down
Loading

0 comments on commit 9cb57c6

Please sign in to comment.