about summary refs log tree commit diff
path: root/src/librustdoc/display.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-09-22 11:15:49 +0000
committerbors <bors@rust-lang.org>2025-09-22 11:15:49 +0000
commit29005cb128e6d447e6bd9c110c9a684665f95985 (patch)
treea25020539ef1a6939c6298a4f77126720d2168d9 /src/librustdoc/display.rs
parent9f32ccf35fb877270bc44a86a126440f04d676d0 (diff)
parent8f80707bc5fa74992bdc2dc201a6461860769f28 (diff)
downloadrust-29005cb128e6d447e6bd9c110c9a684665f95985.tar.gz
rust-29005cb128e6d447e6bd9c110c9a684665f95985.zip
Auto merge of #146879 - Zalathar:rollup-vm97j8b, r=Zalathar
Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#145411 (regression test for Cow<[u8]> layout)
 - rust-lang/rust#146397 (std_detect on Darwin AArch64: update features)
 - rust-lang/rust#146791 (emit attribute for readonly non-pure inline assembly)
 - rust-lang/rust#146831 (Support ctr and lr as clobber-only registers in PowerPC inline assembly)
 - rust-lang/rust#146838 (Introduce "wrapper" helpers to rustdoc)
 - rust-lang/rust#146845 (Add self-profile events for target-machine creation)
 - rust-lang/rust#146846 (btree InternalNode::new safety comments)
 - rust-lang/rust#146858 (Make mips64el-unknown-linux-muslabi64 link dynamically)
 - rust-lang/rust#146878 (assert_unsafe_precondition: fix some incorrect check_language_ub)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'src/librustdoc/display.rs')
-rw-r--r--src/librustdoc/display.rs86
1 files changed, 85 insertions, 1 deletions
diff --git a/src/librustdoc/display.rs b/src/librustdoc/display.rs
index db868c5c9a8..d62ea4c3688 100644
--- a/src/librustdoc/display.rs
+++ b/src/librustdoc/display.rs
@@ -1,6 +1,6 @@
 //! Various utilities for working with [`fmt::Display`] implementations.
 
-use std::fmt::{self, Display, Formatter};
+use std::fmt::{self, Display, Formatter, FormattingOptions};
 
 pub(crate) trait Joined: IntoIterator {
     /// Takes an iterator over elements that implement [`Display`], and format them into `f`, separated by `sep`.
@@ -45,3 +45,87 @@ impl<T: Display> MaybeDisplay for Option<T> {
         })
     }
 }
+
+#[derive(Clone, Copy)]
+pub(crate) struct Wrapped<T> {
+    prefix: T,
+    suffix: T,
+}
+
+pub(crate) enum AngleBracket {
+    Open,
+    Close,
+}
+
+impl Display for AngleBracket {
+    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+        f.write_str(match (self, f.alternate()) {
+            (Self::Open, true) => "<",
+            (Self::Open, false) => "&lt;",
+            (Self::Close, true) => ">",
+            (Self::Close, false) => "&gt;",
+        })
+    }
+}
+
+impl Wrapped<AngleBracket> {
+    pub(crate) fn with_angle_brackets() -> Self {
+        Self { prefix: AngleBracket::Open, suffix: AngleBracket::Close }
+    }
+}
+
+impl Wrapped<char> {
+    pub(crate) fn with_parens() -> Self {
+        Self { prefix: '(', suffix: ')' }
+    }
+
+    pub(crate) fn with_square_brackets() -> Self {
+        Self { prefix: '[', suffix: ']' }
+    }
+}
+
+impl<T: Display> Wrapped<T> {
+    pub(crate) fn with(prefix: T, suffix: T) -> Self {
+        Self { prefix, suffix }
+    }
+
+    pub(crate) fn when(self, if_: bool) -> Wrapped<impl Display> {
+        Wrapped {
+            prefix: if_.then_some(self.prefix).maybe_display(),
+            suffix: if_.then_some(self.suffix).maybe_display(),
+        }
+    }
+
+    pub(crate) fn wrap_fn(
+        self,
+        content: impl Fn(&mut Formatter<'_>) -> fmt::Result,
+    ) -> impl Display {
+        fmt::from_fn(move |f| {
+            self.prefix.fmt(f)?;
+            content(f)?;
+            self.suffix.fmt(f)
+        })
+    }
+
+    pub(crate) fn wrap<C: Display>(self, content: C) -> impl Display {
+        self.wrap_fn(move |f| content.fmt(f))
+    }
+}
+
+#[derive(Clone, Copy)]
+pub(crate) struct WithOpts {
+    opts: FormattingOptions,
+}
+
+impl WithOpts {
+    pub(crate) fn from(f: &Formatter<'_>) -> Self {
+        Self { opts: f.options() }
+    }
+
+    pub(crate) fn display(self, t: impl Display) -> impl Display {
+        fmt::from_fn(move |f| {
+            let mut f = f.with_options(self.opts);
+            t.fmt(&mut f)
+        })
+    }
+}