Skip to content

Commit c1e0bce

Browse files
committed
Use the bitflags! macro to implement FlagV1
Closes rust-lang#15738.
1 parent 3dbfa74 commit c1e0bce

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/libcore/fmt/mod.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
1313
#![stable(feature = "rust1", since = "1.0.0")]
1414

15+
#[macro_use] #[no_link] extern crate rustc_bitflags;
16+
1517
use prelude::*;
1618

1719
use cell::{Cell, RefCell, Ref, RefMut, BorrowState};
@@ -198,9 +200,16 @@ impl<'a> ArgumentV1<'a> {
198200
}
199201

200202
// flags available in the v1 format of format_args
201-
#[derive(Copy, Clone)]
202-
#[allow(dead_code)] // SignMinus isn't currently used
203-
enum FlagV1 { SignPlus, SignMinus, Alternate, SignAwareZeroPad, }
203+
bitflags! {
204+
#[derive(Copy, Clone)]
205+
#[allow(dead_code)] // SignMinus isn't currently used
206+
flags FlagV1 : u32 {
207+
const SignPlus = 1 << 0,
208+
const SignMinus = 1 << 1,
209+
const Alternate = 1 << 2,
210+
const SignAwareZeroPad = 1 << 3,
211+
}
212+
}
204213

205214
impl<'a> Arguments<'a> {
206215
/// When using the format_args!() macro, this function is used to generate the
@@ -472,12 +481,12 @@ impl<'a> Formatter<'a> {
472481
let mut sign = None;
473482
if !is_positive {
474483
sign = Some('-'); width += 1;
475-
} else if self.flags & (1 << (FlagV1::SignPlus as u32)) != 0 {
484+
} else if self.flags.contains(FlagV1::SignPlus) {
476485
sign = Some('+'); width += 1;
477486
}
478487

479488
let mut prefixed = false;
480-
if self.flags & (1 << (FlagV1::Alternate as u32)) != 0 {
489+
if self.flags.contains(FlagV1::Alternate) {
481490
prefixed = true; width += prefix.char_len();
482491
}
483492

@@ -507,7 +516,7 @@ impl<'a> Formatter<'a> {
507516
}
508517
// The sign and prefix goes before the padding if the fill character
509518
// is zero
510-
Some(min) if self.flags & (1 << (FlagV1::SignAwareZeroPad as u32)) != 0 => {
519+
Some(min) if self.flags.contains(FlagV1::SignAwareZeroPad) => {
511520
self.fill = '0';
512521
try!(write_prefix(self));
513522
self.with_padding(min - width, Alignment::Right, |f| {
@@ -873,8 +882,8 @@ impl<T> Pointer for *const T {
873882
// it denotes whether to prefix with 0x. We use it to work out whether
874883
// or not to zero extend, and then unconditionally set it to get the
875884
// prefix.
876-
if f.flags & 1 << (FlagV1::Alternate as u32) > 0 {
877-
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
885+
if f.flags.contains(FlagV1::Alternate) {
886+
f.flags.insert(FlagV1::SignAwareZeroPad);
878887

879888
if let None = f.width {
880889
// The formats need two extra bytes, for the 0x
@@ -885,7 +894,7 @@ impl<T> Pointer for *const T {
885894
}
886895
}
887896
}
888-
f.flags |= 1 << (FlagV1::Alternate as u32);
897+
f.flags.insert(FlagV1::Alternate);
889898

890899
let ret = LowerHex::fmt(&(*self as usize), f);
891900

0 commit comments

Comments
 (0)