Skip to content

Use the bitflags! macro to implement FlagV1 #24697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,16 @@ impl<'a> ArgumentV1<'a> {
}

// flags available in the v1 format of format_args
#[derive(Copy, Clone)]
#[allow(dead_code)] // SignMinus isn't currently used
enum FlagV1 { SignPlus, SignMinus, Alternate, SignAwareZeroPad, }
bitflags! {
#[derive(Copy, Clone)]
#[allow(dead_code)] // SignMinus isn't currently used
flags FlagV1 : u32 {
const SignPlus = 1 << 0,
const SignMinus = 1 << 1,
const Alternate = 1 << 2,
const SignAwareZeroPad = 1 << 3,
}
}

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

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

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

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

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

Expand Down
2 changes: 2 additions & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
#![feature(reflect)]
#![feature(custom_attribute)]

#[macro_use] #[no_link] extern crate rustc_bitflags;

#[macro_use]
mod macros;

Expand Down