Skip to content

Commit 4e45df7

Browse files
Minimize expansion on NeedsDisplay
This uses #[unstable] and #[allow_internal_unstable] to reduce the need for expansion of the struct in every macro invocation.
1 parent e0cc6ac commit 4e45df7

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

library/alloc/src/macros.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ macro_rules! vec {
6464
($($x:expr,)*) => (vec![$($x),*])
6565
}
6666

67+
// HACK(jubilee): Shim for specializing format! It is possible to manually
68+
// implement ToString, bypassing Display. "{}" normally formats in terms of
69+
// Display. NeedsDisplay enforces equally strict type boundarie.
70+
#[unstable(feature = "display_type_guard", issue = "none")]
71+
#[allow(dead_code)]
72+
struct NeedsDisplay<T: crate::fmt::Display> {
73+
inner: T,
74+
}
75+
6776
/// Creates a `String` using interpolation of runtime expressions.
6877
///
6978
/// The first argument `format!` receives is a format string. This must be a string
@@ -99,17 +108,12 @@ macro_rules! vec {
99108
/// format!("hello {}", "world!");
100109
/// format!("x = {}, y = {y}", 10, y = 30);
101110
/// ```
111+
#[allow_internal_unstable(display_type_guard)]
102112
#[macro_export]
103113
#[stable(feature = "rust1", since = "1.0.0")]
104114
macro_rules! format {
105115
// A faster path for simple format! calls.
106116
("{}", $($arg:tt,?)+) => {{
107-
// It is possible to implement ToString manually, bypassing Display.
108-
// However, "{}" formats in terms of Display. This wrapper enforces
109-
// equally strict type boundaries even though we are calling ToString.
110-
struct NeedsDisplay<T: $crate::fmt::Display> {
111-
inner: T,
112-
}
113117
let fast = |t: NeedsDisplay<_> | $crate::string::ToString::to_string(t.inner);
114118
// This TokenTree must resolve to a Displayable value to be valid.
115119
fast(NeedsDisplay { inner: &{$($arg)*} })

0 commit comments

Comments
 (0)