Skip to content

Commit 9cb57c6

Browse files
committed
macro -> fn
1 parent 1f6faa1 commit 9cb57c6

File tree

4 files changed

+451
-386
lines changed

4 files changed

+451
-386
lines changed

src/librustdoc/html/format.rs

+10-34
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,15 @@ use crate::html::render::Context;
3737
use crate::joined::Joined as _;
3838
use crate::passes::collect_intra_doc_links::UrlFragment;
3939

40-
/// This macro is the same as [`std::write!`] for [`String`]s, but swallows the returned `Result`,
41-
/// since writing into a `String` can never fail.
42-
macro_rules! write_str {
43-
($dst:expr, $($arg:tt)*) => {{
44-
// make sure $dst is a `String` (or `&mut String`)
45-
trait AssertString {
46-
fn assert_string(&mut self) {}
47-
}
48-
impl AssertString for ::std::string::String {}
49-
$dst.assert_string();
50-
51-
let _ = $dst.write_fmt(::std::format_args!($($arg)*));
52-
}};
40+
pub(crate) fn write_str(s: &mut String, f: fmt::Arguments<'_>) {
41+
s.write_fmt(f).unwrap();
5342
}
54-
/// This macro is the same as [`std::writeln!`] for [`String`]s, but swallows the returned `Result`,
55-
/// since writing into a `String` can never fail.
56-
macro_rules! writeln_str {
57-
($dst:expr, $($arg:tt)*) => {{
58-
// make sure $dst is a `String` (or `&mut String`)
59-
trait AssertString {
60-
fn assert_string(&mut self) {}
61-
}
62-
impl AssertString for ::std::string::String {}
63-
$dst.assert_string();
6443

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

69-
pub(crate) use {write_str, writeln_str};
70-
7149
pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
7250
bounds: &'a [clean::GenericBound],
7351
cx: &'a Context<'tcx>,
@@ -689,8 +667,6 @@ pub(crate) fn href_relative_parts<'fqp>(
689667
}
690668

691669
pub(crate) fn link_tooltip(did: DefId, fragment: &Option<UrlFragment>, cx: &Context<'_>) -> String {
692-
use write_str as write;
693-
694670
let cache = cx.cache();
695671
let Some((fqp, shortty)) = cache.paths.get(&did).or_else(|| cache.external_paths.get(&did))
696672
else {
@@ -704,16 +680,16 @@ pub(crate) fn link_tooltip(did: DefId, fragment: &Option<UrlFragment>, cx: &Cont
704680
fqp
705681
};
706682
if let &Some(UrlFragment::Item(id)) = fragment {
707-
write!(buf, "{} ", cx.tcx().def_descr(id));
683+
write_str(&mut buf, format_args!("{} ", cx.tcx().def_descr(id)));
708684
for component in fqp {
709-
write!(buf, "{component}::");
685+
write_str(&mut buf, format_args!("{component}::"));
710686
}
711-
write!(buf, "{}", cx.tcx().item_name(id));
687+
write_str(&mut buf, format_args!("{}", cx.tcx().item_name(id)));
712688
} else if !fqp.is_empty() {
713689
let mut fqp_it = fqp.iter();
714-
write!(buf, "{shortty} {}", fqp_it.next().unwrap());
690+
write_str(&mut buf, format_args!("{shortty} {}", fqp_it.next().unwrap()));
715691
for component in fqp_it {
716-
write!(buf, "::{component}");
692+
write_str(&mut buf, format_args!("::{component}"));
717693
}
718694
}
719695
buf

src/librustdoc/html/highlight.rs

+39-34
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_span::edition::Edition;
1414
use rustc_span::symbol::Symbol;
1515
use rustc_span::{BytePos, DUMMY_SP, Span};
1616

17-
use super::format;
17+
use super::format::{self, write_str, writeln_str};
1818
use crate::clean::PrimitiveType;
1919
use crate::html::escape::EscapeBodyText;
2020
use crate::html::render::{Context, LinkFromSrc};
@@ -65,57 +65,63 @@ fn write_header(
6565
tooltip: Tooltip,
6666
extra_classes: &[String],
6767
) {
68-
use super::format::write_str as write;
69-
70-
write!(
68+
write_str(
7169
out,
72-
"<div class=\"example-wrap{}\">",
73-
match tooltip {
74-
Tooltip::Ignore => " ignore",
75-
Tooltip::CompileFail => " compile_fail",
76-
Tooltip::ShouldPanic => " should_panic",
77-
Tooltip::Edition(_) => " edition",
78-
Tooltip::None => "",
79-
},
70+
format_args!(
71+
"<div class=\"example-wrap{}\">",
72+
match tooltip {
73+
Tooltip::Ignore => " ignore",
74+
Tooltip::CompileFail => " compile_fail",
75+
Tooltip::ShouldPanic => " should_panic",
76+
Tooltip::Edition(_) => " edition",
77+
Tooltip::None => "",
78+
}
79+
),
8080
);
8181

8282
if tooltip != Tooltip::None {
8383
let edition_code;
84-
write!(
84+
write_str(
8585
out,
86-
"<a href=\"#\" class=\"tooltip\" title=\"{}\">ⓘ</a>",
87-
match tooltip {
88-
Tooltip::Ignore => "This example is not tested",
89-
Tooltip::CompileFail => "This example deliberately fails to compile",
90-
Tooltip::ShouldPanic => "This example panics",
91-
Tooltip::Edition(edition) => {
92-
edition_code = format!("This example runs with edition {edition}");
93-
&edition_code
86+
format_args!(
87+
"<a href=\"#\" class=\"tooltip\" title=\"{}\">ⓘ</a>",
88+
match tooltip {
89+
Tooltip::Ignore => "This example is not tested",
90+
Tooltip::CompileFail => "This example deliberately fails to compile",
91+
Tooltip::ShouldPanic => "This example panics",
92+
Tooltip::Edition(edition) => {
93+
edition_code = format!("This example runs with edition {edition}");
94+
&edition_code
95+
}
96+
Tooltip::None => unreachable!(),
9497
}
95-
Tooltip::None => unreachable!(),
96-
},
98+
),
9799
);
98100
}
99101

100102
if let Some(extra) = extra_content {
101103
out.push_str(&extra);
102104
}
103105
if class.is_empty() {
104-
write!(
106+
write_str(
105107
out,
106-
"<pre class=\"rust{}{}\">",
107-
if extra_classes.is_empty() { "" } else { " " },
108-
extra_classes.join(" "),
108+
format_args!(
109+
"<pre class=\"rust{}{}\">",
110+
if extra_classes.is_empty() { "" } else { " " },
111+
extra_classes.join(" ")
112+
),
109113
);
110114
} else {
111-
write!(
115+
write_str(
112116
out,
113-
"<pre class=\"rust {class}{}{}\">",
114-
if extra_classes.is_empty() { "" } else { " " },
115-
extra_classes.join(" "),
117+
format_args!(
118+
"<pre class=\"rust {class}{}{}\">",
119+
if extra_classes.is_empty() { "" } else { " " },
120+
extra_classes.join(" ")
121+
),
116122
);
117123
}
118-
write!(out, "<code>");
124+
write_str(out, format_args!("<code>"));
119125
}
120126

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

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

332337
/// How a span of text is classified. Mostly corresponds to token kinds.

0 commit comments

Comments
 (0)