Skip to content

Commit f5a5042

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

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/libcore/fmt/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,16 @@ impl<'a> ArgumentV1<'a> {
198198
}
199199

200200
// 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, }
201+
bitflags! {
202+
#[derive(Copy, Clone)]
203+
#[allow(dead_code)] // SignMinus isn't currently used
204+
flags FlagV1 : u32 {
205+
const SignPlus = 1 << 0,
206+
const SignMinus = 1 << 1,
207+
const Alternate = 1 << 2,
208+
const SignAwareZeroPad = 1 << 3,
209+
}
210+
}
204211

205212
impl<'a> Arguments<'a> {
206213
/// When using the format_args!() macro, this function is used to generate the
@@ -472,12 +479,12 @@ impl<'a> Formatter<'a> {
472479
let mut sign = None;
473480
if !is_positive {
474481
sign = Some('-'); width += 1;
475-
} else if self.flags & (1 << (FlagV1::SignPlus as u32)) != 0 {
482+
} else if self.flags.contains(FlagV1::SignPlus) {
476483
sign = Some('+'); width += 1;
477484
}
478485

479486
let mut prefixed = false;
480-
if self.flags & (1 << (FlagV1::Alternate as u32)) != 0 {
487+
if self.flags.contains(FlagV1::Alternate) {
481488
prefixed = true; width += prefix.char_len();
482489
}
483490

@@ -507,7 +514,7 @@ impl<'a> Formatter<'a> {
507514
}
508515
// The sign and prefix goes before the padding if the fill character
509516
// is zero
510-
Some(min) if self.flags & (1 << (FlagV1::SignAwareZeroPad as u32)) != 0 => {
517+
Some(min) if self.flags.contains(FlagV1::SignAwareZeroPad) => {
511518
self.fill = '0';
512519
try!(write_prefix(self));
513520
self.with_padding(min - width, Alignment::Right, |f| {
@@ -873,8 +880,8 @@ impl<T> Pointer for *const T {
873880
// it denotes whether to prefix with 0x. We use it to work out whether
874881
// or not to zero extend, and then unconditionally set it to get the
875882
// prefix.
876-
if f.flags & 1 << (FlagV1::Alternate as u32) > 0 {
877-
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
883+
if f.flags.contains(FlagV1::Alternate) {
884+
f.flags.insert(FlagV1::SignAwareZeroPad);
878885

879886
if let None = f.width {
880887
// The formats need two extra bytes, for the 0x
@@ -885,7 +892,7 @@ impl<T> Pointer for *const T {
885892
}
886893
}
887894
}
888-
f.flags |= 1 << (FlagV1::Alternate as u32);
895+
f.flags.insert(FlagV1::Alternate);
889896

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

src/libcore/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
#![feature(reflect)]
7676
#![feature(custom_attribute)]
7777

78+
#[macro_use] #[no_link] extern crate rustc_bitflags;
79+
7880
#[macro_use]
7981
mod macros;
8082

0 commit comments

Comments
 (0)