Skip to content

Use the bitflags! macro to implement FlagV1 #45079

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 4 commits 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
2 changes: 2 additions & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/libcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ path = "lib.rs"
test = false
bench = false

[dependencies]
bitflags = "1.0"
rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }

[dev-dependencies]
rand = { path = "../librand" }

Expand Down
10 changes: 5 additions & 5 deletions src/libcore/fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use fmt::{self, FlagV1};
use fmt;

struct PadAdapter<'a, 'b: 'a> {
fmt: &'a mut fmt::Formatter<'b>,
Expand Down Expand Up @@ -140,7 +140,7 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
}

fn is_pretty(&self) -> bool {
self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
self.fmt.alternate()
}
}

Expand Down Expand Up @@ -233,7 +233,7 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
}

fn is_pretty(&self) -> bool {
self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
self.fmt.alternate()
}
}

Expand Down Expand Up @@ -277,7 +277,7 @@ impl<'a, 'b: 'a> DebugInner<'a, 'b> {
}

fn is_pretty(&self) -> bool {
self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
self.fmt.alternate()
}
}

Expand Down Expand Up @@ -519,6 +519,6 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> {
}

fn is_pretty(&self) -> bool {
self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
self.fmt.alternate()
}
}
35 changes: 20 additions & 15 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W {
#[allow(missing_debug_implementations)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Formatter<'a> {
flags: u32,
flags: FlagV1,
fill: char,
align: rt::v1::Alignment,
width: Option<usize>,
Expand Down Expand Up @@ -320,10 +320,15 @@ 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! {
// flags available in the v1 format of format_args
struct FlagV1: u32 {
const SIGN_PLUS = 1 << 0;
const SIGN_MINUS = 1 << 1;
const ALTERNATE = 1 << 2;
const SIGN_AWARE_ZERO_PAD = 1 << 3;
}
}

impl<'a> Arguments<'a> {
/// When using the format_args!() macro, this function is used to generate the
Expand Down Expand Up @@ -948,7 +953,7 @@ pub trait UpperExp {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn write(output: &mut Write, args: Arguments) -> Result {
let mut formatter = Formatter {
flags: 0,
flags: FlagV1::empty(),
width: None,
precision: None,
buf: output,
Expand Down Expand Up @@ -994,7 +999,7 @@ impl<'a> Formatter<'a> {
// Fill in the format parameters into the formatter
self.fill = arg.format.fill;
self.align = arg.format.align;
self.flags = arg.format.flags;
self.flags = FlagV1::from_bits_truncate(arg.format.flags);
self.width = self.getcount(&arg.format.width);
self.precision = self.getcount(&arg.format.precision);

Expand Down Expand Up @@ -1276,9 +1281,9 @@ impl<'a> Formatter<'a> {
write(self.buf, fmt)
}

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

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

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

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

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

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

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

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

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

Expand Down
7 changes: 7 additions & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@
#![cfg_attr(not(stage0), feature(const_cell_new))]
#![cfg_attr(not(stage0), feature(const_nonzero_new))]

// See librustc_cratesio_shim/Cargo.toml for a comment explaining this.
#[allow(unused_extern_crates)]
extern crate rustc_cratesio_shim;

#[macro_use]
extern crate bitflags;

#[prelude_import]
#[allow(unused)]
use prelude::v1::*;
Expand Down