@@ -288,14 +288,11 @@ pub enum DebugAsHex {
288
288
#[ derive( Copy , Clone , Debug , PartialEq , Eq , Default ) ]
289
289
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
290
290
pub struct FormattingOptions {
291
- sign : Option < Sign > ,
292
- sign_aware_zero_pad : bool ,
293
- alternate : bool ,
291
+ flags : u32 ,
294
292
fill : char ,
295
293
align : Option < Alignment > ,
296
294
width : Option < usize > ,
297
295
precision : Option < usize > ,
298
- debug_as_hex : Option < DebugAsHex > ,
299
296
}
300
297
301
298
impl FormattingOptions {
@@ -312,14 +309,11 @@ impl FormattingOptions {
312
309
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
313
310
pub const fn new ( ) -> Self {
314
311
Self {
315
- sign : None ,
316
- sign_aware_zero_pad : false ,
317
- alternate : false ,
312
+ flags : 0 ,
318
313
fill : ' ' ,
319
314
align : None ,
320
315
width : None ,
321
316
precision : None ,
322
- debug_as_hex : None ,
323
317
}
324
318
}
325
319
@@ -333,15 +327,24 @@ impl FormattingOptions {
333
327
/// - `-`: Currently not used
334
328
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
335
329
pub const fn sign ( & mut self , sign : Option < Sign > ) -> & mut Self {
336
- self . sign = sign;
330
+ self . flags = self . flags & !( 1 << rt:: Flag :: SignMinus as u32 | 1 << rt:: Flag :: SignPlus as u32 ) ;
331
+ match sign {
332
+ None => { } ,
333
+ Some ( Sign :: Plus ) => self . flags |= 1 << rt:: Flag :: SignPlus as u32 ,
334
+ Some ( Sign :: Minus ) => self . flags |= 1 << rt:: Flag :: SignMinus as u32 ,
335
+ }
337
336
self
338
337
}
339
338
/// Sets or unsets the `0` flag.
340
339
///
341
340
/// 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
342
341
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
343
342
pub const fn sign_aware_zero_pad ( & mut self , sign_aware_zero_pad : bool ) -> & mut Self {
344
- self . sign_aware_zero_pad = sign_aware_zero_pad;
343
+ if sign_aware_zero_pad {
344
+ self . flags |= 1 << rt:: Flag :: SignAwareZeroPad as u32
345
+ } else {
346
+ self . flags &= !( 1 << rt:: Flag :: SignAwareZeroPad as u32 )
347
+ }
345
348
self
346
349
}
347
350
/// Sets or unsets the `#` flag.
@@ -354,7 +357,11 @@ impl FormattingOptions {
354
357
/// - [`Binary`] - precedes the argument with a `0o`
355
358
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
356
359
pub const fn alternate ( & mut self , alternate : bool ) -> & mut Self {
357
- self . alternate = alternate;
360
+ if alternate {
361
+ self . flags |= 1 << rt:: Flag :: Alternate as u32
362
+ } else {
363
+ self . flags &= !( 1 << rt:: Flag :: Alternate as u32 )
364
+ }
358
365
self
359
366
}
360
367
/// Sets the fill character.
@@ -406,24 +413,36 @@ impl FormattingOptions {
406
413
/// hexadecimal or normal integers
407
414
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
408
415
pub const fn debug_as_hex ( & mut self , debug_as_hex : Option < DebugAsHex > ) -> & mut Self {
409
- self . debug_as_hex = debug_as_hex;
416
+ self . flags = self . flags & !( 1 << rt:: Flag :: DebugUpperHex as u32 | 1 << rt:: Flag :: DebugLowerHex as u32 ) ;
417
+ match debug_as_hex {
418
+ None => { } ,
419
+ Some ( DebugAsHex :: Upper ) => self . flags |= 1 << rt:: Flag :: DebugUpperHex as u32 ,
420
+ Some ( DebugAsHex :: Lower ) => self . flags |= 1 << rt:: Flag :: DebugLowerHex as u32 ,
421
+ }
410
422
self
411
423
}
412
424
413
425
/// Returns the current sign (the `+` or the `-` flag).
414
426
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
415
427
pub const fn get_sign ( & self ) -> Option < Sign > {
416
- self . sign
428
+ const SIGN_PLUS_BITFIELD : u32 = 1 << rt:: Flag :: SignPlus as u32 ;
429
+ const SIGN_MINUS_BITFIELD : u32 = 1 << rt:: Flag :: SignMinus as u32 ;
430
+ match self . flags & ( ( 1 << rt:: Flag :: SignPlus as u32 ) | ( 1 << rt:: Flag :: SignMinus as u32 ) ) {
431
+ SIGN_PLUS_BITFIELD => Some ( Sign :: Plus ) ,
432
+ SIGN_MINUS_BITFIELD => Some ( Sign :: Minus ) ,
433
+ 0 => None ,
434
+ _ => panic ! ( "Invalid sign bits set in flags" ) ,
435
+ }
417
436
}
418
437
/// Returns the current `0` flag.
419
438
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
420
439
pub const fn get_sign_aware_zero_pad ( & self ) -> bool {
421
- self . sign_aware_zero_pad
440
+ self . flags & ( 1 << rt :: Flag :: SignAwareZeroPad as u32 ) != 0
422
441
}
423
442
/// Returns the current `#` flag.
424
443
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
425
444
pub const fn get_alternate ( & self ) -> bool {
426
- self . alternate
445
+ self . flags & ( 1 << rt :: Flag :: Alternate as u32 ) != 0
427
446
}
428
447
/// Returns the current fill character.
429
448
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
@@ -448,7 +467,14 @@ impl FormattingOptions {
448
467
/// Returns the current precision.
449
468
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
450
469
pub const fn get_debug_as_hex ( & self ) -> Option < DebugAsHex > {
451
- self . debug_as_hex
470
+ const DEBUG_UPPER_BITFIELD : u32 = 1 << rt:: Flag :: DebugUpperHex as u32 ;
471
+ const DEBUG_LOWER_BITFIELD : u32 = 1 << rt:: Flag :: DebugLowerHex as u32 ;
472
+ match self . flags & ( ( 1 << rt:: Flag :: DebugUpperHex as u32 ) | ( 1 << rt:: Flag :: DebugLowerHex as u32 ) ) {
473
+ DEBUG_UPPER_BITFIELD => Some ( DebugAsHex :: Upper ) ,
474
+ DEBUG_LOWER_BITFIELD => Some ( DebugAsHex :: Lower ) ,
475
+ 0 => None ,
476
+ _ => panic ! ( "Invalid hex debug bits set in flags" ) ,
477
+ }
452
478
}
453
479
454
480
/// Creates a [`Formatter`] that writes its output to the given [`Write`] trait.
@@ -463,37 +489,13 @@ impl FormattingOptions {
463
489
#[ unstable( feature = "fmt_internals" , reason = "internal to standard library" , issue = "none" ) ]
464
490
/// Flags for formatting
465
491
pub fn flags ( & mut self , flags : u32 ) {
466
- self . sign = if flags & ( 1 << rt:: Flag :: SignPlus as u32 ) != 0 {
467
- Some ( Sign :: Plus )
468
- } else if flags & ( 1 << rt:: Flag :: SignMinus as u32 ) != 0 {
469
- Some ( Sign :: Minus )
470
- } else {
471
- None
472
- } ;
473
- self . alternate = ( flags & ( 1 << rt:: Flag :: Alternate as u32 ) ) != 0 ;
474
- self . sign_aware_zero_pad = ( flags & ( 1 << rt:: Flag :: SignAwareZeroPad as u32 ) ) != 0 ;
475
- self . debug_as_hex = if flags & ( 1 << rt:: Flag :: DebugLowerHex as u32 ) != 0 {
476
- Some ( DebugAsHex :: Lower )
477
- } else if flags & ( 1 << rt:: Flag :: DebugUpperHex as u32 ) != 0 {
478
- Some ( DebugAsHex :: Upper )
479
- } else {
480
- None
481
- } ;
492
+ self . flags = flags
482
493
}
483
494
#[ doc( hidden) ]
484
495
#[ unstable( feature = "fmt_internals" , reason = "internal to standard library" , issue = "none" ) ]
485
496
/// Flags for formatting
486
497
pub fn get_flags ( & self ) -> u32 {
487
- <bool as Into < u32 > >:: into ( self . get_sign ( ) == Some ( Sign :: Plus ) ) << rt:: Flag :: SignPlus as u32
488
- | <bool as Into < u32 > >:: into ( self . get_sign ( ) == Some ( Sign :: Minus ) )
489
- << rt:: Flag :: SignMinus as u32
490
- | <bool as Into < u32 > >:: into ( self . get_alternate ( ) ) << rt:: Flag :: Alternate as u32
491
- | <bool as Into < u32 > >:: into ( self . get_sign_aware_zero_pad ( ) )
492
- << rt:: Flag :: SignAwareZeroPad as u32
493
- | <bool as Into < u32 > >:: into ( self . debug_as_hex == Some ( DebugAsHex :: Lower ) )
494
- << rt:: Flag :: DebugLowerHex as u32
495
- | <bool as Into < u32 > >:: into ( self . debug_as_hex == Some ( DebugAsHex :: Upper ) )
496
- << rt:: Flag :: DebugUpperHex as u32
498
+ self . flags
497
499
}
498
500
}
499
501
@@ -2161,11 +2163,11 @@ impl<'a> Formatter<'a> {
2161
2163
// FIXME: Decide what public API we want for these two flags.
2162
2164
// https://github.com/rust-lang/rust/issues/48584
2163
2165
fn debug_lower_hex ( & self ) -> bool {
2164
- self . options . debug_as_hex == Some ( DebugAsHex :: Lower )
2166
+ self . options . flags & ( 1 << rt :: Flag :: DebugLowerHex as u32 ) != 0
2165
2167
}
2166
2168
2167
2169
fn debug_upper_hex ( & self ) -> bool {
2168
- self . options . debug_as_hex == Some ( DebugAsHex :: Upper )
2170
+ self . options . flags & ( 1 << rt :: Flag :: DebugUpperHex as u32 ) != 0
2169
2171
}
2170
2172
2171
2173
/// Creates a [`DebugStruct`] builder designed to assist with creation of
0 commit comments