@@ -272,14 +272,11 @@ pub enum DebugAsHex {
272
272
#[ derive( Copy , Clone , Debug , PartialEq , Eq , Default ) ]
273
273
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
274
274
pub struct FormattingOptions {
275
- sign : Option < Sign > ,
276
- sign_aware_zero_pad : bool ,
277
- alternate : bool ,
275
+ flags : u32 ,
278
276
fill : char ,
279
277
align : Option < Alignment > ,
280
278
width : Option < usize > ,
281
279
precision : Option < usize > ,
282
- debug_as_hex : Option < DebugAsHex > ,
283
280
}
284
281
285
282
impl FormattingOptions {
@@ -296,14 +293,11 @@ impl FormattingOptions {
296
293
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
297
294
pub const fn new ( ) -> Self {
298
295
Self {
299
- sign : None ,
300
- sign_aware_zero_pad : false ,
301
- alternate : false ,
296
+ flags : 0 ,
302
297
fill : ' ' ,
303
298
align : None ,
304
299
width : None ,
305
300
precision : None ,
306
- debug_as_hex : None ,
307
301
}
308
302
}
309
303
@@ -317,15 +311,24 @@ impl FormattingOptions {
317
311
/// - `-`: Currently not used
318
312
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
319
313
pub const fn sign ( & mut self , sign : Option < Sign > ) -> & mut Self {
320
- self . sign = sign;
314
+ self . flags = self . flags & !( 1 << rt:: Flag :: SignMinus as u32 | 1 << rt:: Flag :: SignPlus as u32 ) ;
315
+ match sign {
316
+ None => { } ,
317
+ Some ( Sign :: Plus ) => self . flags |= 1 << rt:: Flag :: SignPlus as u32 ,
318
+ Some ( Sign :: Minus ) => self . flags |= 1 << rt:: Flag :: SignMinus as u32 ,
319
+ }
321
320
self
322
321
}
323
322
/// Sets or unsets the `0` flag.
324
323
///
325
324
/// This is used to indicate for integer formats that the padding to width should both be done with a 0 character as well as be sign-aware
326
325
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
327
326
pub const fn sign_aware_zero_pad ( & mut self , sign_aware_zero_pad : bool ) -> & mut Self {
328
- self . sign_aware_zero_pad = sign_aware_zero_pad;
327
+ if sign_aware_zero_pad {
328
+ self . flags |= 1 << rt:: Flag :: SignAwareZeroPad as u32
329
+ } else {
330
+ self . flags &= !( 1 << rt:: Flag :: SignAwareZeroPad as u32 )
331
+ }
329
332
self
330
333
}
331
334
/// Sets or unsets the `#` flag.
@@ -338,7 +341,11 @@ impl FormattingOptions {
338
341
/// - [`Binary`] - precedes the argument with a `0o`
339
342
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
340
343
pub const fn alternate ( & mut self , alternate : bool ) -> & mut Self {
341
- self . alternate = alternate;
344
+ if alternate {
345
+ self . flags |= 1 << rt:: Flag :: Alternate as u32
346
+ } else {
347
+ self . flags &= !( 1 << rt:: Flag :: Alternate as u32 )
348
+ }
342
349
self
343
350
}
344
351
/// Sets the fill character.
@@ -390,24 +397,36 @@ impl FormattingOptions {
390
397
/// hexadecimal or normal integers
391
398
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
392
399
pub const fn debug_as_hex ( & mut self , debug_as_hex : Option < DebugAsHex > ) -> & mut Self {
393
- self . debug_as_hex = debug_as_hex;
400
+ self . flags = self . flags & !( 1 << rt:: Flag :: DebugUpperHex as u32 | 1 << rt:: Flag :: DebugLowerHex as u32 ) ;
401
+ match debug_as_hex {
402
+ None => { } ,
403
+ Some ( DebugAsHex :: Upper ) => self . flags |= 1 << rt:: Flag :: DebugUpperHex as u32 ,
404
+ Some ( DebugAsHex :: Lower ) => self . flags |= 1 << rt:: Flag :: DebugLowerHex as u32 ,
405
+ }
394
406
self
395
407
}
396
408
397
409
/// Returns the current sign (the `+` or the `-` flag).
398
410
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
399
411
pub const fn get_sign ( & self ) -> Option < Sign > {
400
- self . sign
412
+ const SIGN_PLUS_BITFIELD : u32 = 1 << rt:: Flag :: SignPlus as u32 ;
413
+ const SIGN_MINUS_BITFIELD : u32 = 1 << rt:: Flag :: SignMinus as u32 ;
414
+ match self . flags & ( ( 1 << rt:: Flag :: SignPlus as u32 ) | ( 1 << rt:: Flag :: SignMinus as u32 ) ) {
415
+ SIGN_PLUS_BITFIELD => Some ( Sign :: Plus ) ,
416
+ SIGN_MINUS_BITFIELD => Some ( Sign :: Minus ) ,
417
+ 0 => None ,
418
+ _ => panic ! ( "Invalid sign bits set in flags" ) ,
419
+ }
401
420
}
402
421
/// Returns the current `0` flag.
403
422
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
404
423
pub const fn get_sign_aware_zero_pad ( & self ) -> bool {
405
- self . sign_aware_zero_pad
424
+ self . flags & ( 1 << rt :: Flag :: SignAwareZeroPad as u32 ) != 0
406
425
}
407
426
/// Returns the current `#` flag.
408
427
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
409
428
pub const fn get_alternate ( & self ) -> bool {
410
- self . alternate
429
+ self . flags & ( 1 << rt :: Flag :: Alternate as u32 ) != 0
411
430
}
412
431
/// Returns the current fill character.
413
432
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
@@ -432,7 +451,14 @@ impl FormattingOptions {
432
451
/// Returns the current precision.
433
452
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
434
453
pub const fn get_debug_as_hex ( & self ) -> Option < DebugAsHex > {
435
- self . debug_as_hex
454
+ const DEBUG_UPPER_BITFIELD : u32 = 1 << rt:: Flag :: DebugUpperHex as u32 ;
455
+ const DEBUG_LOWER_BITFIELD : u32 = 1 << rt:: Flag :: DebugLowerHex as u32 ;
456
+ match self . flags & ( ( 1 << rt:: Flag :: DebugUpperHex as u32 ) | ( 1 << rt:: Flag :: DebugLowerHex as u32 ) ) {
457
+ DEBUG_UPPER_BITFIELD => Some ( DebugAsHex :: Upper ) ,
458
+ DEBUG_LOWER_BITFIELD => Some ( DebugAsHex :: Lower ) ,
459
+ 0 => None ,
460
+ _ => panic ! ( "Invalid hex debug bits set in flags" ) ,
461
+ }
436
462
}
437
463
438
464
/// Creates a [`Formatter`] that writes its output to the given [`Write`] trait.
@@ -447,37 +473,13 @@ impl FormattingOptions {
447
473
#[ unstable( feature = "fmt_internals" , reason = "internal to standard library" , issue = "none" ) ]
448
474
/// Flags for formatting
449
475
pub fn flags ( & mut self , flags : u32 ) {
450
- self . sign = if flags & ( 1 << rt:: Flag :: SignPlus as u32 ) != 0 {
451
- Some ( Sign :: Plus )
452
- } else if flags & ( 1 << rt:: Flag :: SignMinus as u32 ) != 0 {
453
- Some ( Sign :: Minus )
454
- } else {
455
- None
456
- } ;
457
- self . alternate = ( flags & ( 1 << rt:: Flag :: Alternate as u32 ) ) != 0 ;
458
- self . sign_aware_zero_pad = ( flags & ( 1 << rt:: Flag :: SignAwareZeroPad as u32 ) ) != 0 ;
459
- self . debug_as_hex = if flags & ( 1 << rt:: Flag :: DebugLowerHex as u32 ) != 0 {
460
- Some ( DebugAsHex :: Lower )
461
- } else if flags & ( 1 << rt:: Flag :: DebugUpperHex as u32 ) != 0 {
462
- Some ( DebugAsHex :: Upper )
463
- } else {
464
- None
465
- } ;
476
+ self . flags = flags
466
477
}
467
478
#[ doc( hidden) ]
468
479
#[ unstable( feature = "fmt_internals" , reason = "internal to standard library" , issue = "none" ) ]
469
480
/// Flags for formatting
470
481
pub fn get_flags ( & self ) -> u32 {
471
- <bool as Into < u32 > >:: into ( self . get_sign ( ) == Some ( Sign :: Plus ) ) << rt:: Flag :: SignPlus as u32
472
- | <bool as Into < u32 > >:: into ( self . get_sign ( ) == Some ( Sign :: Minus ) )
473
- << rt:: Flag :: SignMinus as u32
474
- | <bool as Into < u32 > >:: into ( self . get_alternate ( ) ) << rt:: Flag :: Alternate as u32
475
- | <bool as Into < u32 > >:: into ( self . get_sign_aware_zero_pad ( ) )
476
- << rt:: Flag :: SignAwareZeroPad as u32
477
- | <bool as Into < u32 > >:: into ( self . debug_as_hex == Some ( DebugAsHex :: Lower ) )
478
- << rt:: Flag :: DebugLowerHex as u32
479
- | <bool as Into < u32 > >:: into ( self . debug_as_hex == Some ( DebugAsHex :: Upper ) )
480
- << rt:: Flag :: DebugUpperHex as u32
482
+ self . flags
481
483
}
482
484
}
483
485
@@ -2081,11 +2083,11 @@ impl<'a> Formatter<'a> {
2081
2083
// FIXME: Decide what public API we want for these two flags.
2082
2084
// https://github.com/rust-lang/rust/issues/48584
2083
2085
fn debug_lower_hex ( & self ) -> bool {
2084
- self . options . debug_as_hex == Some ( DebugAsHex :: Lower )
2086
+ self . options . flags & ( 1 << rt :: Flag :: DebugLowerHex as u32 ) != 0
2085
2087
}
2086
2088
2087
2089
fn debug_upper_hex ( & self ) -> bool {
2088
- self . options . debug_as_hex == Some ( DebugAsHex :: Upper )
2090
+ self . options . flags & ( 1 << rt :: Flag :: DebugUpperHex as u32 ) != 0
2089
2091
}
2090
2092
2091
2093
/// Creates a [`DebugStruct`] builder designed to assist with creation of
0 commit comments