Skip to content

Commit 8f70c24

Browse files
committed
Use the bitflags! macro to implement FlagV1
Closes #15738.
1 parent 41b105b commit 8f70c24

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

src/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/libcore/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ path = "lib.rs"
99
test = false
1010
bench = false
1111

12+
[dependencies]
13+
bitflags = "1.0"
14+
rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
15+
1216
[dev-dependencies]
1317
rand = { path = "../librand" }
1418

src/libcore/fmt/mod.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W {
245245
#[allow(missing_debug_implementations)]
246246
#[stable(feature = "rust1", since = "1.0.0")]
247247
pub struct Formatter<'a> {
248-
flags: u32,
248+
flags: FlagV1,
249249
fill: char,
250250
align: rt::v1::Alignment,
251251
width: Option<usize>,
@@ -320,9 +320,15 @@ impl<'a> ArgumentV1<'a> {
320320
}
321321
}
322322

323-
// flags available in the v1 format of format_args
324-
#[derive(Copy, Clone)]
325-
enum FlagV1 { SignPlus, SignMinus, Alternate, SignAwareZeroPad, }
323+
bitflags! {
324+
// flags available in the v1 format of format_args
325+
struct FlagV1: u32 {
326+
const SIGN_PLUS = 1 << 0;
327+
const SIGN_MINUS = 1 << 1;
328+
const ALTERNATE = 1 << 2;
329+
const SIGN_AWARE_ZERO_PAD = 1 << 3;
330+
}
331+
}
326332

327333
impl<'a> Arguments<'a> {
328334
/// When using the format_args!() macro, this function is used to generate the
@@ -947,7 +953,7 @@ pub trait UpperExp {
947953
#[stable(feature = "rust1", since = "1.0.0")]
948954
pub fn write(output: &mut Write, args: Arguments) -> Result {
949955
let mut formatter = Formatter {
950-
flags: 0,
956+
flags: FlagV1::empty(),
951957
width: None,
952958
precision: None,
953959
buf: output,
@@ -993,7 +999,7 @@ impl<'a> Formatter<'a> {
993999
// Fill in the format parameters into the formatter
9941000
self.fill = arg.format.fill;
9951001
self.align = arg.format.align;
996-
self.flags = arg.format.flags;
1002+
self.flags = FlagV1::from_bits_truncate(arg.format.flags);
9971003
self.width = self.getcount(&arg.format.width);
9981004
self.precision = self.getcount(&arg.format.precision);
9991005

@@ -1277,7 +1283,7 @@ impl<'a> Formatter<'a> {
12771283

12781284
/// Flags for formatting
12791285
#[stable(feature = "rust1", since = "1.0.0")]
1280-
pub fn flags(&self) -> u32 { self.flags }
1286+
pub fn flags(&self) -> u32 { self.flags.bits }
12811287

12821288
/// Character used as 'fill' whenever there is alignment
12831289
#[stable(feature = "fmt_flags", since = "1.5.0")]
@@ -1305,20 +1311,20 @@ impl<'a> Formatter<'a> {
13051311

13061312
/// Determines if the `+` flag was specified.
13071313
#[stable(feature = "fmt_flags", since = "1.5.0")]
1308-
pub fn sign_plus(&self) -> bool { self.flags & (1 << FlagV1::SignPlus as u32) != 0 }
1314+
pub fn sign_plus(&self) -> bool { self.flags.intersects(FlagV1::SIGN_PLUS) }
13091315

13101316
/// Determines if the `-` flag was specified.
13111317
#[stable(feature = "fmt_flags", since = "1.5.0")]
1312-
pub fn sign_minus(&self) -> bool { self.flags & (1 << FlagV1::SignMinus as u32) != 0 }
1318+
pub fn sign_minus(&self) -> bool { f.flags.intersects(FlagV1::SIGN_MINUS) }
13131319

13141320
/// Determines if the `#` flag was specified.
13151321
#[stable(feature = "fmt_flags", since = "1.5.0")]
1316-
pub fn alternate(&self) -> bool { self.flags & (1 << FlagV1::Alternate as u32) != 0 }
1322+
pub fn alternate(&self) -> bool { f.flags.intersects(FlagV1::ALTERNATE) }
13171323

13181324
/// Determines if the `0` flag was specified.
13191325
#[stable(feature = "fmt_flags", since = "1.5.0")]
13201326
pub fn sign_aware_zero_pad(&self) -> bool {
1321-
self.flags & (1 << FlagV1::SignAwareZeroPad as u32) != 0
1327+
self.flags.intersects(FlagV1::SIGN_AWARE_ZERO_PAD)
13221328
}
13231329

13241330
/// Creates a [`DebugStruct`] builder designed to assist with creation of
@@ -1584,13 +1590,13 @@ impl<T: ?Sized> Pointer for *const T {
15841590
// or not to zero extend, and then unconditionally set it to get the
15851591
// prefix.
15861592
if f.alternate() {
1587-
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
1593+
f.flags.insert(FlagV1::SIGN_AWARE_ZERO_PAD);
15881594

15891595
if let None = f.width {
15901596
f.width = Some(((mem::size_of::<usize>() * 8) / 4) + 2);
15911597
}
15921598
}
1593-
f.flags |= 1 << (FlagV1::Alternate as u32);
1599+
f.flags.insert(FlagV1::ALTERNATE);
15941600

15951601
let ret = LowerHex::fmt(&(*self as *const () as usize), f);
15961602

src/libcore/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@
109109
#![cfg_attr(not(stage0), feature(const_cell_new))]
110110
#![cfg_attr(not(stage0), feature(const_nonzero_new))]
111111

112+
// See librustc_cratesio_shim/Cargo.toml for a comment explaining this.
113+
#[allow(unused_extern_crates)]
114+
extern crate rustc_cratesio_shim;
115+
116+
#[macro_use]
117+
extern crate bitflags;
118+
112119
#[prelude_import]
113120
#[allow(unused)]
114121
use prelude::v1::*;

0 commit comments

Comments
 (0)