diff --git a/src/Cargo.lock b/src/Cargo.lock index 77e33855f2344..9166c606c3332 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -323,7 +323,9 @@ dependencies = [ name = "core" version = "0.0.0" dependencies = [ + "bitflags 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.0.0", + "rustc_cratesio_shim 0.0.0", ] [[package]] diff --git a/src/libcore/Cargo.toml b/src/libcore/Cargo.toml index 178df02ccdde3..2299e8b0f2a46 100644 --- a/src/libcore/Cargo.toml +++ b/src/libcore/Cargo.toml @@ -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" } diff --git a/src/libcore/fmt/builders.rs b/src/libcore/fmt/builders.rs index b594c886b64f5..60b9eeb1283cd 100644 --- a/src/libcore/fmt/builders.rs +++ b/src/libcore/fmt/builders.rs @@ -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>, @@ -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() } } @@ -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() } } @@ -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() } } @@ -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() } } diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 6c251b9eb0924..a971aa5b58341 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -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, @@ -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 @@ -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, @@ -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); @@ -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")] @@ -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 @@ -1585,13 +1590,13 @@ impl 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::() * 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); diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 69612bd2a32a4..137b7ee7a4f59 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -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::*;