@@ -64,6 +64,15 @@ macro_rules! vec {
64
64
( $( $x: expr, ) * ) => ( vec![ $( $x) ,* ] )
65
65
}
66
66
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
+
67
76
/// Creates a `String` using interpolation of runtime expressions.
68
77
///
69
78
/// The first argument `format!` receives is a format string. This must be a string
@@ -99,17 +108,12 @@ macro_rules! vec {
99
108
/// format!("hello {}", "world!");
100
109
/// format!("x = {}, y = {y}", 10, y = 30);
101
110
/// ```
111
+ #[ allow_internal_unstable( display_type_guard) ]
102
112
#[ macro_export]
103
113
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
104
114
macro_rules! format {
105
115
// A faster path for simple format! calls.
106
116
( "{}" , $( $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
- }
113
117
let fast = |t: NeedsDisplay <_> | $crate:: string:: ToString :: to_string( t. inner) ;
114
118
// This TokenTree must resolve to a Displayable value to be valid.
115
119
fast( NeedsDisplay { inner: & { $( $arg) * } } )
0 commit comments