Skip to content

Commit 135916c

Browse files
committed
Use the bitflags! macro to implement FlagV1
Closes rust-lang#15738.
1 parent 9560754 commit 135916c

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

mk/crates.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ HOST_CRATES := syntax $(RUSTC_CRATES) rustdoc fmt_macros
6060
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
6161
TOOLS := compiletest rustdoc rustc rustbook error-index-generator
6262

63-
DEPS_core :=
63+
DEPS_rustc_bitflags :=
64+
DEPS_core := rustc_bitflags
6465
DEPS_libc := core
6566
DEPS_rustc_unicode := core
6667
DEPS_alloc := core libc native:jemalloc
@@ -87,7 +88,6 @@ DEPS_rustc_back := std syntax rustc_llvm flate log libc
8788
DEPS_rustc_data_structures := std log serialize
8889
DEPS_rustdoc := rustc rustc_driver native:hoedown serialize getopts \
8990
test rustc_lint
90-
DEPS_rustc_bitflags := core
9191
DEPS_flate := std native:miniz
9292
DEPS_arena := std
9393
DEPS_graphviz := std

src/libcore/fmt/builders.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
110110
}
111111

112112
fn is_pretty(&self) -> bool {
113-
self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
113+
self.fmt.flags().intersects(FlagV1::ALTERNATE)
114114
}
115115
}
116116

@@ -173,7 +173,7 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
173173
}
174174

175175
fn is_pretty(&self) -> bool {
176-
self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
176+
self.fmt.flags().intersects(FlagV1::ALTERNATE)
177177
}
178178
}
179179

@@ -205,7 +205,7 @@ impl<'a, 'b: 'a> DebugInner<'a, 'b> {
205205
}
206206

207207
fn is_pretty(&self) -> bool {
208-
self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
208+
self.fmt.flags().intersects(FlagV1::ALTERNATE)
209209
}
210210
}
211211

@@ -328,6 +328,6 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
328328
}
329329

330330
fn is_pretty(&self) -> bool {
331-
self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
331+
self.fmt.flags().intersects(FlagV1::ALTERNATE)
332332
}
333333
}

src/libcore/fmt/mod.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub trait Write {
126126
/// traits.
127127
#[stable(feature = "rust1", since = "1.0.0")]
128128
pub struct Formatter<'a> {
129-
flags: u32,
129+
flags: FlagV1,
130130
fill: char,
131131
align: rt::v1::Alignment,
132132
width: Option<usize>,
@@ -193,10 +193,21 @@ impl<'a> ArgumentV1<'a> {
193193
}
194194
}
195195

196-
// flags available in the v1 format of format_args
197-
#[derive(Copy, Clone)]
198-
#[allow(dead_code)] // SignMinus isn't currently used
199-
enum FlagV1 { SignPlus, SignMinus, Alternate, SignAwareZeroPad, }
196+
bitflags! {
197+
// flags available in the v1 format of format_args
198+
#[doc(hidden)]
199+
flags FlagV1 : u32 {
200+
#[doc(hidden)]
201+
const SIGNPLUS = 1 << 0,
202+
#[doc(hidden)]
203+
#[allow(dead_code)] // SIGNMINUS isn't currently used
204+
const SIGNMINUS = 1 << 1,
205+
#[doc(hidden)]
206+
const ALTERNATE = 1 << 2,
207+
#[doc(hidden)]
208+
const SIGNAWAREZEROPAD = 1 << 3,
209+
}
210+
}
200211

201212
impl<'a> Arguments<'a> {
202213
/// When using the format_args!() macro, this function is used to generate the
@@ -360,7 +371,7 @@ pub trait UpperExp {
360371
#[stable(feature = "rust1", since = "1.0.0")]
361372
pub fn write(output: &mut Write, args: Arguments) -> Result {
362373
let mut formatter = Formatter {
363-
flags: 0,
374+
flags: FlagV1::empty(),
364375
width: None,
365376
precision: None,
366377
buf: output,
@@ -410,7 +421,7 @@ impl<'a> Formatter<'a> {
410421
// Fill in the format parameters into the formatter
411422
self.fill = arg.format.fill;
412423
self.align = arg.format.align;
413-
self.flags = arg.format.flags;
424+
self.flags = FlagV1::from_bits_truncate(arg.format.flags);
414425
self.width = self.getcount(&arg.format.width);
415426
self.precision = self.getcount(&arg.format.precision);
416427

@@ -466,12 +477,12 @@ impl<'a> Formatter<'a> {
466477
let mut sign = None;
467478
if !is_positive {
468479
sign = Some('-'); width += 1;
469-
} else if self.flags & (1 << (FlagV1::SignPlus as u32)) != 0 {
480+
} else if self.flags.intersects(FlagV1::SIGNPLUS) {
470481
sign = Some('+'); width += 1;
471482
}
472483

473484
let mut prefixed = false;
474-
if self.flags & (1 << (FlagV1::Alternate as u32)) != 0 {
485+
if self.flags.intersects(FlagV1::ALTERNATE) {
475486
prefixed = true; width += prefix.char_len();
476487
}
477488

@@ -501,7 +512,7 @@ impl<'a> Formatter<'a> {
501512
}
502513
// The sign and prefix goes before the padding if the fill character
503514
// is zero
504-
Some(min) if self.flags & (1 << (FlagV1::SignAwareZeroPad as u32)) != 0 => {
515+
Some(min) if self.flags.intersects(FlagV1::SIGNAWAREZEROPAD) => {
505516
self.fill = '0';
506517
try!(write_prefix(self));
507518
self.with_padding(min - width, Alignment::Right, |f| {
@@ -619,7 +630,7 @@ impl<'a> Formatter<'a> {
619630

620631
/// Flags for formatting (packed version of rt::Flag)
621632
#[stable(feature = "rust1", since = "1.0.0")]
622-
pub fn flags(&self) -> u32 { self.flags }
633+
pub fn flags(&self) -> FlagV1 { self.flags }
623634

624635
/// Character used as 'fill' whenever there is alignment
625636
#[unstable(feature = "core", reason = "method was just created")]
@@ -867,8 +878,8 @@ impl<T> Pointer for *const T {
867878
// it denotes whether to prefix with 0x. We use it to work out whether
868879
// or not to zero extend, and then unconditionally set it to get the
869880
// prefix.
870-
if f.flags & 1 << (FlagV1::Alternate as u32) > 0 {
871-
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
881+
if f.flags.intersects(FlagV1::ALTERNATE) {
882+
f.flags.insert(FlagV1::SIGNAWAREZEROPAD);
872883

873884
if let None = f.width {
874885
// The formats need two extra bytes, for the 0x
@@ -879,7 +890,7 @@ impl<T> Pointer for *const T {
879890
}
880891
}
881892
}
882-
f.flags |= 1 << (FlagV1::Alternate as u32);
893+
f.flags.insert(FlagV1::ALTERNATE);
883894

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

src/libcore/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,20 @@
6363
#![allow(raw_pointer_derive)]
6464
#![deny(missing_docs)]
6565

66+
#![feature(associated_consts)]
67+
#![feature(concat_idents)]
68+
#![feature(custom_attribute)]
69+
#![feature(fundamental)]
6670
#![feature(intrinsics, lang_items)]
6771
#![feature(on_unimplemented)]
72+
#![feature(optin_builtin_traits)]
73+
#![feature(reflect)]
74+
#![feature(rustc_attrs)]
6875
#![feature(simd)]
6976
#![feature(staged_api)]
7077
#![feature(unboxed_closures)]
71-
#![feature(rustc_attrs)]
72-
#![feature(optin_builtin_traits)]
73-
#![feature(fundamental)]
74-
#![feature(concat_idents)]
75-
#![feature(reflect)]
76-
#![feature(custom_attribute)]
78+
79+
#[macro_use] #[no_link] extern crate rustc_bitflags;
7780

7881
#[macro_use]
7982
mod macros;
@@ -173,4 +176,6 @@ mod core {
173176
mod std {
174177
// range syntax
175178
pub use ops;
179+
// bitflags!{}
180+
pub use option;
176181
}

0 commit comments

Comments
 (0)