@@ -22,26 +22,24 @@ pub fn expand_parsed_format_args(ecx: &mut ExtCtxt<'_>, fmt: FormatArgs) -> P<as
22
22
}
23
23
24
24
let args = Ident :: new ( sym:: _args, macsp) ;
25
- let f = Ident :: new ( sym:: f , macsp) ;
25
+ let w = Ident :: new ( sym:: w , macsp) ;
26
26
27
27
let arguments = fmt. arguments . into_vec ( ) ;
28
28
29
29
let mut statements = Vec :: new ( ) ;
30
30
31
- let mut default_options = true ;
32
-
33
31
for piece in fmt. template {
34
32
match piece {
35
33
FormatArgsPiece :: Literal ( s) => {
36
34
// Generate:
37
- // f .write_str("…")?;
35
+ // w .write_str("…")?;
38
36
statements. push ( ecx. stmt_expr ( ecx. expr (
39
37
macsp,
40
38
ast:: ExprKind :: Try ( ecx. expr (
41
39
macsp,
42
40
ast:: ExprKind :: MethodCall (
43
41
ast:: PathSegment :: from_ident ( Ident :: new ( sym:: write_str, macsp) ) ,
44
- ecx. expr_ident ( macsp, f ) ,
42
+ ecx. expr_ident ( macsp, w ) ,
45
43
vec ! [ ecx. expr_str( macsp, s) ] ,
46
44
macsp,
47
45
) ,
@@ -51,16 +49,23 @@ pub fn expand_parsed_format_args(ecx: &mut ExtCtxt<'_>, fmt: FormatArgs) -> P<as
51
49
FormatArgsPiece :: Placeholder ( p) => {
52
50
// Don't set options if they're still set to defaults
53
51
// and this placeholder also uses default options.
54
- let d = p. format_options == FormatOptions :: default ( ) ;
55
- if !default_options || !d {
56
- default_options = d;
57
- // Generate:
58
- // f.set_options(…);
59
- statements. push ( ecx. stmt_expr ( ecx. expr (
52
+
53
+ // Generate:
54
+ // ::core::fmt::Formatter::new(w)
55
+ let mut formatter = ecx. expr_call_global (
56
+ macsp,
57
+ ecx. std_path ( & [ sym:: fmt, sym:: Formatter , sym:: new] ) ,
58
+ vec ! [ ecx. expr_ident( macsp, w) ] ,
59
+ ) ;
60
+
61
+ if p. format_options != FormatOptions :: default ( ) {
62
+ // Add:
63
+ // .with_options(…)
64
+ formatter = ecx. expr (
60
65
macsp,
61
66
ast:: ExprKind :: MethodCall (
62
- ast:: PathSegment :: from_ident ( Ident :: new ( sym:: set_options , macsp) ) ,
63
- ecx . expr_ident ( macsp , f ) ,
67
+ ast:: PathSegment :: from_ident ( Ident :: new ( sym:: with_options , macsp) ) ,
68
+ formatter ,
64
69
vec ! [
65
70
ecx. expr_u32( macsp, p. format_options. flags) ,
66
71
ecx. expr_char( macsp, p. format_options. fill. unwrap_or( ' ' ) ) ,
@@ -90,10 +95,12 @@ pub fn expand_parsed_format_args(ecx: &mut ExtCtxt<'_>, fmt: FormatArgs) -> P<as
90
95
] ,
91
96
macsp,
92
97
) ,
93
- ) ) ) ;
98
+ ) ;
94
99
}
100
+
95
101
// Generate:
96
- // ::core::fmt::Display::fmt(arg.0, f)?;
102
+ // ::core::fmt::Display::fmt(arg.0, &mut formatter)?;
103
+
97
104
let arg = if let Ok ( i) = p. argument . index {
98
105
ecx. expr_field (
99
106
arguments[ i] . expr . span . with_ctxt ( macsp. ctxt ( ) ) ,
@@ -119,7 +126,17 @@ pub fn expand_parsed_format_args(ecx: &mut ExtCtxt<'_>, fmt: FormatArgs) -> P<as
119
126
ast:: ExprKind :: Try ( ecx. expr_call_global (
120
127
arg. span ,
121
128
ecx. std_path ( & [ sym:: fmt, fmt_trait, sym:: fmt] ) ,
122
- vec ! [ arg, ecx. expr_ident( macsp, f) ] ,
129
+ vec ! [
130
+ arg,
131
+ ecx. expr(
132
+ macsp,
133
+ ast:: ExprKind :: AddrOf (
134
+ ast:: BorrowKind :: Ref ,
135
+ ast:: Mutability :: Mut ,
136
+ formatter,
137
+ ) ,
138
+ ) ,
139
+ ] ,
123
140
) ) ,
124
141
) ) ) ;
125
142
}
@@ -131,7 +148,7 @@ pub fn expand_parsed_format_args(ecx: &mut ExtCtxt<'_>, fmt: FormatArgs) -> P<as
131
148
statements. push ( ecx. stmt_expr ( ecx. expr_ok ( macsp, ecx. expr_tuple ( macsp, Vec :: new ( ) ) ) ) ) ;
132
149
133
150
// Generate:
134
- // |f : &mut ::core::fmt::Formatter | -> ::core::fmt::Result {
151
+ // |w : &mut dyn ::core::fmt::Write | -> ::core::fmt::Result {
135
152
// … // statements
136
153
// }
137
154
let closure = ecx. expr (
@@ -144,18 +161,26 @@ pub fn expand_parsed_format_args(ecx: &mut ExtCtxt<'_>, fmt: FormatArgs) -> P<as
144
161
ecx. fn_decl (
145
162
vec ! [ ecx. param(
146
163
macsp,
147
- f ,
164
+ w ,
148
165
ecx. ty_rptr(
149
166
macsp,
150
- ecx. ty_path ( ecx . path_all (
167
+ ecx. ty (
151
168
macsp,
152
- true ,
153
- ecx. std_path( & [ sym:: fmt, sym:: Formatter ] ) ,
154
- vec![ ast:: GenericArg :: Lifetime ( ast:: Lifetime {
155
- id: ast:: DUMMY_NODE_ID ,
156
- ident: Ident :: new( kw:: UnderscoreLifetime , macsp) ,
157
- } ) ] ,
158
- ) ) ,
169
+ ast:: TyKind :: TraitObject (
170
+ vec![ ast:: GenericBound :: Trait (
171
+ ast:: PolyTraitRef :: new(
172
+ vec![ ] ,
173
+ ecx. path_global(
174
+ macsp,
175
+ ecx. std_path( & [ sym:: fmt, sym:: Write ] ) ,
176
+ ) ,
177
+ macsp,
178
+ ) ,
179
+ ast:: TraitBoundModifier :: None ,
180
+ ) ] ,
181
+ ast:: TraitObjectSyntax :: Dyn ,
182
+ ) ,
183
+ ) ,
159
184
None ,
160
185
ast:: Mutability :: Mut ,
161
186
) ,
0 commit comments