Skip to content

Commit 060d2eb

Browse files
committed
Make Escape and EscapeBodyText support lazy processing
1 parent f7423f0 commit 060d2eb

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

src/librustdoc/html/escape.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::fmt;
77

88
use unicode_segmentation::UnicodeSegmentation;
99

10-
#[inline(always)]
10+
#[inline]
1111
fn escape(s: &str, mut w: impl fmt::Write, escape_quotes: bool) -> fmt::Result {
1212
// Because the internet is always right, turns out there's not that many
1313
// characters to escape: http://stackoverflow.com/questions/7381974
@@ -35,13 +35,30 @@ fn escape(s: &str, mut w: impl fmt::Write, escape_quotes: bool) -> fmt::Result {
3535
Ok(())
3636
}
3737

38+
struct WriteEscaped<W: fmt::Write> {
39+
writer: W,
40+
escape_quotes: bool,
41+
}
42+
43+
impl<W: fmt::Write> fmt::Write for WriteEscaped<W> {
44+
#[inline]
45+
fn write_str(&mut self, s: &str) -> fmt::Result {
46+
escape(s, &mut self.writer, self.escape_quotes)
47+
}
48+
}
49+
3850
/// Wrapper struct which will emit the HTML-escaped version of the contained
3951
/// string when passed to a format string.
40-
pub(crate) struct Escape<'a>(pub &'a str);
52+
pub(crate) struct Escape<T>(pub T);
4153

42-
impl fmt::Display for Escape<'_> {
54+
impl<T: fmt::Display> fmt::Display for Escape<T> {
55+
#[inline]
4356
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
44-
escape(self.0, fmt, true)
57+
self.0.fmt(
58+
&mut fmt
59+
.options()
60+
.create_formatter(&mut WriteEscaped { writer: fmt, escape_quotes: true }),
61+
)
4562
}
4663
}
4764

@@ -51,11 +68,15 @@ impl fmt::Display for Escape<'_> {
5168
/// This is only safe to use for text nodes. If you need your output to be
5269
/// safely contained in an attribute, use [`Escape`]. If you don't know the
5370
/// difference, use [`Escape`].
54-
pub(crate) struct EscapeBodyText<'a>(pub &'a str);
71+
pub(crate) struct EscapeBodyText<T>(pub T);
5572

56-
impl fmt::Display for EscapeBodyText<'_> {
73+
impl<T: fmt::Display> fmt::Display for EscapeBodyText<T> {
5774
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
58-
escape(self.0, fmt, false)
75+
self.0.fmt(
76+
&mut fmt
77+
.options()
78+
.create_formatter(&mut WriteEscaped { writer: fmt, escape_quotes: false }),
79+
)
5980
}
6081
}
6182

src/librustdoc/html/escape/tests.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
use std::iter;
2+
3+
#[test]
4+
fn escape() {
5+
use super::Escape as E;
6+
assert_eq!(format!("<Hello> {}", E("<World>")), "<Hello> &lt;World&gt;");
7+
}
8+
19
// basic examples
210
#[test]
311
fn escape_body_text_with_wbr() {

src/librustdoc/html/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ pub(crate) fn anchor<'a: 'cx, 'cx>(
853853
f,
854854
r#"<a class="{short_ty}" href="{url}" title="{short_ty} {path}">{text}</a>"#,
855855
path = join_with_double_colon(&fqp),
856-
text = EscapeBodyText(text.as_str()),
856+
text = EscapeBodyText(text),
857857
)
858858
} else {
859859
f.write_str(text.as_str())

src/librustdoc/html/render/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ fn short_item_info(
720720
}
721721
DeprecatedSince::Future => String::from("Deprecating in a future version"),
722722
DeprecatedSince::NonStandard(since) => {
723-
format!("Deprecated since {}", Escape(since.as_str()))
723+
format!("Deprecated since {}", Escape(since))
724724
}
725725
DeprecatedSince::Unspecified | DeprecatedSince::Err => String::from("Deprecated"),
726726
};
@@ -1519,7 +1519,7 @@ pub(crate) fn notable_traits_button<'a, 'tcx>(
15191519
write!(
15201520
f,
15211521
" <a href=\"#\" class=\"tooltip\" data-notable-ty=\"{ty}\">ⓘ</a>",
1522-
ty = Escape(&format!("{:#}", ty.print(cx))),
1522+
ty = Escape(ty.print(cx)),
15231523
)
15241524
})
15251525
})

src/librustdoc/html/render/print_item.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -534,12 +534,16 @@ fn extra_info_tags<'a, 'tcx: 'a>(
534534
import_def_id: Option<DefId>,
535535
) -> impl Display + 'a + Captures<'tcx> {
536536
fmt::from_fn(move |f| {
537-
fn tag_html<'a>(class: &'a str, title: &'a str, contents: &'a str) -> impl Display + 'a {
537+
fn tag_html<'a>(
538+
class: impl fmt::Display + 'a,
539+
title: impl fmt::Display + 'a,
540+
contents: impl fmt::Display + 'a,
541+
) -> impl Display + 'a {
538542
fmt::from_fn(move |f| {
539543
write!(
540544
f,
541545
r#"<wbr><span class="stab {class}" title="{title}">{contents}</span>"#,
542-
title = Escape(title),
546+
title = Escape(&title),
543547
)
544548
})
545549
}

src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![feature(debug_closure_helpers)]
99
#![feature(file_buffered)]
1010
#![feature(format_args_nl)]
11+
#![feature(formatting_options)]
1112
#![feature(if_let_guard)]
1213
#![feature(impl_trait_in_assoc_type)]
1314
#![feature(iter_intersperse)]

0 commit comments

Comments
 (0)