@@ -177,64 +177,107 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
177
177
}
178
178
}
179
179
180
+ struct DebugInner < ' a , ' b : ' a > {
181
+ fmt : & ' a mut fmt:: Formatter < ' b > ,
182
+ result : fmt:: Result ,
183
+ has_fields : bool ,
184
+ }
185
+
186
+ impl < ' a , ' b : ' a > DebugInner < ' a , ' b > {
187
+ fn entry ( & mut self , entry : & fmt:: Debug ) {
188
+ self . result = self . result . and_then ( |_| {
189
+ if self . is_pretty ( ) {
190
+ let mut writer = PadAdapter :: new ( self . fmt ) ;
191
+ let prefix = if self . has_fields { "," } else { "" } ;
192
+ fmt:: write ( & mut writer, format_args ! ( "{}\n {:#?}" , prefix, entry) )
193
+ } else {
194
+ let prefix = if self . has_fields { ", " } else { "" } ;
195
+ write ! ( self . fmt, "{}{:?}" , prefix, entry)
196
+ }
197
+ } ) ;
198
+
199
+ self . has_fields = true ;
200
+ }
201
+
202
+ pub fn finish ( & mut self ) {
203
+ let prefix = if self . is_pretty ( ) && self . has_fields { "\n " } else { "" } ;
204
+ self . result = self . result . and_then ( |_| self . fmt . write_str ( prefix) ) ;
205
+ }
206
+
207
+ fn is_pretty ( & self ) -> bool {
208
+ self . fmt . flags ( ) & ( 1 << ( FlagV1 :: Alternate as usize ) ) != 0
209
+ }
210
+ }
211
+
180
212
/// A struct to help with `fmt::Debug` implementations.
181
213
///
182
214
/// Constructed by the `Formatter::debug_set` method.
183
215
#[ must_use]
184
216
pub struct DebugSet < ' a , ' b : ' a > {
185
- fmt : & ' a mut fmt:: Formatter < ' b > ,
186
- result : fmt:: Result ,
187
- has_fields : bool ,
217
+ inner : DebugInner < ' a , ' b > ,
188
218
}
189
219
190
- pub fn debug_set_new < ' a , ' b > ( fmt : & ' a mut fmt:: Formatter < ' b > , name : & str ) -> DebugSet < ' a , ' b > {
191
- let result = write ! ( fmt, "{} {{" , name ) ;
220
+ pub fn debug_set_new < ' a , ' b > ( fmt : & ' a mut fmt:: Formatter < ' b > ) -> DebugSet < ' a , ' b > {
221
+ let result = write ! ( fmt, "{{" ) ;
192
222
DebugSet {
193
- fmt : fmt,
194
- result : result,
195
- has_fields : false ,
223
+ inner : DebugInner {
224
+ fmt : fmt,
225
+ result : result,
226
+ has_fields : false ,
227
+ }
196
228
}
197
229
}
198
230
199
231
impl < ' a , ' b : ' a > DebugSet < ' a , ' b > {
200
232
/// Adds a new entry to the set output.
201
233
#[ unstable( feature = "debug_builders" , reason = "method was just created" ) ]
202
234
pub fn entry ( mut self , entry : & fmt:: Debug ) -> DebugSet < ' a , ' b > {
203
- self . result = self . result . and_then ( |_| {
204
- let prefix = if self . has_fields {
205
- ","
206
- } else {
207
- ""
208
- } ;
209
-
210
- if self . is_pretty ( ) {
211
- let mut writer = PadAdapter :: new ( self . fmt ) ;
212
- fmt:: write ( & mut writer, format_args ! ( "{}\n {:#?}" , prefix, entry) )
213
- } else {
214
- write ! ( self . fmt, "{} {:?}" , prefix, entry)
215
- }
216
- } ) ;
217
-
218
- self . has_fields = true ;
235
+ self . inner . entry ( entry) ;
219
236
self
220
237
}
221
238
222
239
/// Consumes the `DebugSet`, finishing output and returning any error
223
240
/// encountered.
224
241
#[ unstable( feature = "debug_builders" , reason = "method was just created" ) ]
225
- pub fn finish ( self ) -> fmt:: Result {
226
- self . result . and_then ( |_| {
227
- let end = match ( self . has_fields , self . is_pretty ( ) ) {
228
- ( false , _) => "}" ,
229
- ( true , false ) => " }" ,
230
- ( true , true ) => "\n }" ,
231
- } ;
232
- self . fmt . write_str ( end)
233
- } )
242
+ pub fn finish ( mut self ) -> fmt:: Result {
243
+ self . inner . finish ( ) ;
244
+ self . inner . result . and_then ( |_| self . inner . fmt . write_str ( "}" ) )
234
245
}
246
+ }
235
247
236
- fn is_pretty ( & self ) -> bool {
237
- self . fmt . flags ( ) & ( 1 << ( FlagV1 :: Alternate as usize ) ) != 0
248
+ /// A struct to help with `fmt::Debug` implementations.
249
+ ///
250
+ /// Constructed by the `Formatter::debug_list` method.
251
+ #[ must_use]
252
+ pub struct DebugList < ' a , ' b : ' a > {
253
+ inner : DebugInner < ' a , ' b > ,
254
+ }
255
+
256
+ pub fn debug_list_new < ' a , ' b > ( fmt : & ' a mut fmt:: Formatter < ' b > ) -> DebugList < ' a , ' b > {
257
+ let result = write ! ( fmt, "[" ) ;
258
+ DebugList {
259
+ inner : DebugInner {
260
+ fmt : fmt,
261
+ result : result,
262
+ has_fields : false ,
263
+ }
264
+ }
265
+ }
266
+
267
+ impl < ' a , ' b : ' a > DebugList < ' a , ' b > {
268
+ /// Adds a new entry to the set output.
269
+ #[ unstable( feature = "debug_builders" , reason = "method was just created" ) ]
270
+ pub fn entry ( mut self , entry : & fmt:: Debug ) -> DebugList < ' a , ' b > {
271
+ self . inner . entry ( entry) ;
272
+ self
273
+ }
274
+
275
+ /// Consumes the `DebugSet`, finishing output and returning any error
276
+ /// encountered.
277
+ #[ unstable( feature = "debug_builders" , reason = "method was just created" ) ]
278
+ pub fn finish ( mut self ) -> fmt:: Result {
279
+ self . inner . finish ( ) ;
280
+ self . inner . result . and_then ( |_| self . inner . fmt . write_str ( "]" ) )
238
281
}
239
282
}
240
283
@@ -248,8 +291,8 @@ pub struct DebugMap<'a, 'b: 'a> {
248
291
has_fields : bool ,
249
292
}
250
293
251
- pub fn debug_map_new < ' a , ' b > ( fmt : & ' a mut fmt:: Formatter < ' b > , name : & str ) -> DebugMap < ' a , ' b > {
252
- let result = write ! ( fmt, "{} {{" , name ) ;
294
+ pub fn debug_map_new < ' a , ' b > ( fmt : & ' a mut fmt:: Formatter < ' b > ) -> DebugMap < ' a , ' b > {
295
+ let result = write ! ( fmt, "{{" ) ;
253
296
DebugMap {
254
297
fmt : fmt,
255
298
result : result,
@@ -262,37 +305,26 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
262
305
#[ unstable( feature = "debug_builders" , reason = "method was just created" ) ]
263
306
pub fn entry ( mut self , key : & fmt:: Debug , value : & fmt:: Debug ) -> DebugMap < ' a , ' b > {
264
307
self . result = self . result . and_then ( |_| {
265
- let prefix = if self . has_fields {
266
- ","
267
- } else {
268
- ""
269
- } ;
270
-
271
308
if self . is_pretty ( ) {
272
309
let mut writer = PadAdapter :: new ( self . fmt ) ;
310
+ let prefix = if self . has_fields { "," } else { "" } ;
273
311
fmt:: write ( & mut writer, format_args ! ( "{}\n {:#?}: {:#?}" , prefix, key, value) )
274
312
} else {
275
- write ! ( self . fmt, "{} {:?}: {:?}" , prefix, key, value)
313
+ let prefix = if self . has_fields { ", " } else { "" } ;
314
+ write ! ( self . fmt, "{}{:?}: {:?}" , prefix, key, value)
276
315
}
277
316
} ) ;
278
317
279
318
self . has_fields = true ;
280
-
281
319
self
282
320
}
283
321
284
322
/// Consumes the `DebugMap`, finishing output and returning any error
285
323
/// encountered.
286
324
#[ unstable( feature = "debug_builders" , reason = "method was just created" ) ]
287
325
pub fn finish ( self ) -> fmt:: Result {
288
- self . result . and_then ( |_| {
289
- let end = match ( self . has_fields , self . is_pretty ( ) ) {
290
- ( false , _) => "}" ,
291
- ( true , false ) => " }" ,
292
- ( true , true ) => "\n }" ,
293
- } ;
294
- self . fmt . write_str ( end)
295
- } )
326
+ let prefix = if self . is_pretty ( ) && self . has_fields { "\n " } else { "" } ;
327
+ self . result . and_then ( |_| write ! ( self . fmt, "{}}}" , prefix) )
296
328
}
297
329
298
330
fn is_pretty ( & self ) -> bool {
0 commit comments