Skip to content

Instantly share code, notes, and snippets.

@tamird
Created April 28, 2015 22:24
Show Gist options
  • Save tamird/3f84ed69294958a22790 to your computer and use it in GitHub Desktop.
Save tamird/3f84ed69294958a22790 to your computer and use it in GitHub Desktop.
diff --git a/src/librustc_bitflags/lib.rs b/src/librustc_bitflags/lib.rs
index 93a2a5d..f40d730 100644
--- a/src/librustc_bitflags/lib.rs
+++ b/src/librustc_bitflags/lib.rs
@@ -12,6 +12,7 @@
// Do not remove on snapshot creation. Needed for bootstrap. (Issue #22364)
#![cfg_attr(stage0, feature(custom_attribute))]
#![crate_name = "rustc_bitflags"]
+#![feature(associated_consts)]
#![feature(staged_api)]
#![staged_api]
#![crate_type = "rlib"]
@@ -34,6 +35,7 @@
///
/// ```{.rust}
/// # #![feature(rustc_private)]
+/// # #![feature(associated_consts)]
/// #[macro_use] extern crate rustc_bitflags;
///
/// bitflags! {
@@ -41,19 +43,19 @@
/// const FLAG_A = 0b00000001,
/// const FLAG_B = 0b00000010,
/// const FLAG_C = 0b00000100,
-/// const FLAG_ABC = FLAG_A.bits
-/// | FLAG_B.bits
-/// | FLAG_C.bits,
+/// const FLAG_ABC = Flags::FLAG_A.bits
+/// | Flags::FLAG_B.bits
+/// | Flags::FLAG_C.bits,
/// }
/// }
///
/// fn main() {
-/// let e1 = FLAG_A | FLAG_C;
-/// let e2 = FLAG_B | FLAG_C;
-/// assert!((e1 | e2) == FLAG_ABC); // union
-/// assert!((e1 & e2) == FLAG_C); // intersection
-/// assert!((e1 - e2) == FLAG_A); // set difference
-/// assert!(!e2 == FLAG_A); // set complement
+/// let e1 = Flags::FLAG_A | Flags::FLAG_C;
+/// let e2 = Flags::FLAG_B | Flags::FLAG_C;
+/// assert!((e1 | e2) == Flags::FLAG_ABC); // union
+/// assert!((e1 & e2) == Flags::FLAG_C); // intersection
+/// assert!((e1 - e2) == Flags::FLAG_A); // set difference
+/// assert!(!e2 == Flags::FLAG_A); // set complement
/// }
/// ```
///
@@ -86,7 +88,7 @@
/// }
///
/// fn main() {
-/// let mut flags = FLAG_A | FLAG_B;
+/// let mut flags = Flags::FLAG_A | Flags::FLAG_B;
/// flags.clear();
/// assert!(flags.is_empty());
/// assert_eq!(format!("{:?}", flags), "hi!");
@@ -144,9 +146,9 @@ macro_rules! bitflags {
bits: $T,
}
- $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+
-
impl $BitFlags {
+ $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+
+
/// Returns an empty set of flags.
#[inline]
pub fn empty() -> $BitFlags {
@@ -314,9 +316,9 @@ mod tests {
#[doc = "* cmr bed"]
#[doc = "* strcat table"]
#[doc = "<strcat> wait what?"]
- const FlagABC = FlagA.bits
- | FlagB.bits
- | FlagC.bits,
+ const FlagABC = Flags::FlagA.bits
+ | Flags::FlagB.bits
+ | Flags::FlagC.bits,
}
}
@@ -329,32 +331,32 @@ mod tests {
#[test]
fn test_bits(){
assert_eq!(Flags::empty().bits(), 0b00000000);
- assert_eq!(FlagA.bits(), 0b00000001);
- assert_eq!(FlagABC.bits(), 0b00000111);
+ assert_eq!(Flags::FlagA.bits(), 0b00000001);
+ assert_eq!(Flags::FlagABC.bits(), 0b00000111);
assert_eq!(AnotherSetOfFlags::empty().bits(), 0b00);
- assert_eq!(AnotherFlag.bits(), !0);
+ assert_eq!(AnotherSetOfFlags::AnotherFlag.bits(), !0);
}
#[test]
fn test_from_bits() {
assert!(Flags::from_bits(0) == Some(Flags::empty()));
- assert!(Flags::from_bits(0b1) == Some(FlagA));
- assert!(Flags::from_bits(0b10) == Some(FlagB));
- assert!(Flags::from_bits(0b11) == Some(FlagA | FlagB));
+ assert!(Flags::from_bits(0b1) == Some(Flags::FlagA));
+ assert!(Flags::from_bits(0b10) == Some(Flags::FlagB));
+ assert!(Flags::from_bits(0b11) == Some(Flags::FlagA | Flags::FlagB));
assert!(Flags::from_bits(0b1000) == None);
- assert!(AnotherSetOfFlags::from_bits(!0) == Some(AnotherFlag));
+ assert!(AnotherSetOfFlags::from_bits(!0) == Some(AnotherSetOfFlags::AnotherFlag));
}
#[test]
fn test_from_bits_truncate() {
assert!(Flags::from_bits_truncate(0) == Flags::empty());
- assert!(Flags::from_bits_truncate(0b1) == FlagA);
- assert!(Flags::from_bits_truncate(0b10) == FlagB);
- assert!(Flags::from_bits_truncate(0b11) == (FlagA | FlagB));
+ assert!(Flags::from_bits_truncate(0b1) == Flags::FlagA);
+ assert!(Flags::from_bits_truncate(0b10) == Flags::FlagB);
+ assert!(Flags::from_bits_truncate(0b11) == (Flags::FlagA | Flags::FlagB));
assert!(Flags::from_bits_truncate(0b1000) == Flags::empty());
- assert!(Flags::from_bits_truncate(0b1001) == FlagA);
+ assert!(Flags::from_bits_truncate(0b1001) == Flags::FlagA);
assert!(AnotherSetOfFlags::from_bits_truncate(0) == AnotherSetOfFlags::empty());
}
@@ -362,19 +364,19 @@ mod tests {
#[test]
fn test_is_empty(){
assert!(Flags::empty().is_empty());
- assert!(!FlagA.is_empty());
- assert!(!FlagABC.is_empty());
+ assert!(!Flags::FlagA.is_empty());
+ assert!(!Flags::FlagABC.is_empty());
- assert!(!AnotherFlag.is_empty());
+ assert!(!AnotherSetOfFlags::AnotherFlag.is_empty());
}
#[test]
fn test_is_all() {
assert!(Flags::all().is_all());
- assert!(!FlagA.is_all());
- assert!(FlagABC.is_all());
+ assert!(!Flags::FlagA.is_all());
+ assert!(Flags::FlagABC.is_all());
- assert!(AnotherFlag.is_all());
+ assert!(AnotherSetOfFlags::AnotherFlag.is_all());
}
#[test]
@@ -383,77 +385,77 @@ mod tests {
let e2 = Flags::empty();
assert!(!e1.intersects(e2));
- assert!(AnotherFlag.intersects(AnotherFlag));
+ assert!(AnotherSetOfFlags::AnotherFlag.intersects(AnotherSetOfFlags::AnotherFlag));
}
#[test]
fn test_empty_does_not_intersect_with_full() {
let e1 = Flags::empty();
- let e2 = FlagABC;
+ let e2 = Flags::FlagABC;
assert!(!e1.intersects(e2));
}
#[test]
fn test_disjoint_intersects() {
- let e1 = FlagA;
- let e2 = FlagB;
+ let e1 = Flags::FlagA;
+ let e2 = Flags::FlagB;
assert!(!e1.intersects(e2));
}
#[test]
fn test_overlapping_intersects() {
- let e1 = FlagA;
- let e2 = FlagA | FlagB;
+ let e1 = Flags::FlagA;
+ let e2 = Flags::FlagA | Flags::FlagB;
assert!(e1.intersects(e2));
}
#[test]
fn test_contains() {
- let e1 = FlagA;
- let e2 = FlagA | FlagB;
+ let e1 = Flags::FlagA;
+ let e2 = Flags::FlagA | Flags::FlagB;
assert!(!e1.contains(e2));
assert!(e2.contains(e1));
- assert!(FlagABC.contains(e2));
+ assert!(Flags::FlagABC.contains(e2));
- assert!(AnotherFlag.contains(AnotherFlag));
+ assert!(AnotherSetOfFlags::AnotherFlag.contains(AnotherSetOfFlags::AnotherFlag));
}
#[test]
fn test_insert(){
- let mut e1 = FlagA;
- let e2 = FlagA | FlagB;
+ let mut e1 = Flags::FlagA;
+ let e2 = Flags::FlagA | Flags::FlagB;
e1.insert(e2);
assert!(e1 == e2);
let mut e3 = AnotherSetOfFlags::empty();
- e3.insert(AnotherFlag);
- assert!(e3 == AnotherFlag);
+ e3.insert(AnotherSetOfFlags::AnotherFlag);
+ assert!(e3 == AnotherSetOfFlags::AnotherFlag);
}
#[test]
fn test_remove(){
- let mut e1 = FlagA | FlagB;
- let e2 = FlagA | FlagC;
+ let mut e1 = Flags::FlagA | Flags::FlagB;
+ let e2 = Flags::FlagA | Flags::FlagC;
e1.remove(e2);
- assert!(e1 == FlagB);
+ assert!(e1 == Flags::FlagB);
- let mut e3 = AnotherFlag;
- e3.remove(AnotherFlag);
+ let mut e3 = AnotherSetOfFlags::AnotherFlag;
+ e3.remove(AnotherSetOfFlags::AnotherFlag);
assert!(e3 == AnotherSetOfFlags::empty());
}
#[test]
fn test_operators() {
- let e1 = FlagA | FlagC;
- let e2 = FlagB | FlagC;
- assert!((e1 | e2) == FlagABC); // union
- assert!((e1 & e2) == FlagC); // intersection
- assert!((e1 - e2) == FlagA); // set difference
- assert!(!e2 == FlagA); // set complement
- assert!(e1 ^ e2 == FlagA | FlagB); // toggle
+ let e1 = Flags::FlagA | Flags::FlagC;
+ let e2 = Flags::FlagB | Flags::FlagC;
+ assert!((e1 | e2) == Flags::FlagABC); // union
+ assert!((e1 & e2) == Flags::FlagC); // intersection
+ assert!((e1 - e2) == Flags::FlagA); // set difference
+ assert!(!e2 == Flags::FlagA); // set complement
+ assert!(e1 ^ e2 == Flags::FlagA | Flags::FlagB); // toggle
let mut e3 = e1;
e3.toggle(e2);
- assert!(e3 == FlagA | FlagB);
+ assert!(e3 == Flags::FlagA | Flags::FlagB);
let mut m4 = AnotherSetOfFlags::empty();
m4.toggle(AnotherSetOfFlags::empty());
@@ -466,11 +468,11 @@ mod tests {
let mut b = Flags::empty();
assert!(!(a < b) && !(b < a));
- b = FlagB;
+ b = Flags::FlagB;
assert!(a < b);
- a = FlagC;
+ a = Flags::FlagC;
assert!(!(a < b) && b < a);
- b = FlagC | FlagB;
+ b = Flags::FlagC | Flags::FlagB;
assert!(a < b);
}
@@ -480,10 +482,10 @@ mod tests {
let mut b = Flags::empty();
assert!(a <= b && a >= b);
- a = FlagA;
+ a = Flags::FlagA;
assert!(a > b && a >= b);
assert!(b < a && b <= a);
- b = FlagB;
+ b = Flags::FlagB;
assert!(b > a && b >= a);
assert!(a < b && a <= b);
}
@@ -494,7 +496,7 @@ mod tests {
let mut y = Flags::empty();
assert!(hash::hash::<Flags, SipHasher>(&x) == hash::hash::<Flags, SipHasher>(&y));
x = Flags::all();
- y = FlagABC;
+ y = Flags::FlagABC;
assert!(hash::hash::<Flags, SipHasher>(&x) == hash::hash::<Flags, SipHasher>(&y));
}
}
$ rustc --test /Users/tamird/src/rust/src/librustc_bitflags/lib.rs
$ echo $?
0
$ rustdoc --test /Users/tamird/src/rust/src/librustc_bitflags/lib.rs
running 2 tests
test bitflags!_0 ... FAILED
test bitflags!_1 ... FAILED
failures:
---- bitflags!_0 stdout ----
<anon>:10:30: 10:43 error: type `Flags` does not implement any method in scope named `FLAG_A`
<anon>:10 const FLAG_ABC = Flags::FLAG_A.bits
^~~~~~~~~~~~~
<rustc_bitflags macros>:1:1: 72:67 note: in expansion of bitflags!
<rustc_bitflags macros>:70:1: 72:63 note: expansion site
<rustc_bitflags macros>:1:1: 72:67 note: in expansion of bitflags!
<anon>:5:1: 14:2 note: expansion site
<anon>:11:30: 11:43 error: type `Flags` does not implement any method in scope named `FLAG_B`
<anon>:11 | Flags::FLAG_B.bits
^~~~~~~~~~~~~
<rustc_bitflags macros>:1:1: 72:67 note: in expansion of bitflags!
<rustc_bitflags macros>:70:1: 72:63 note: expansion site
<rustc_bitflags macros>:1:1: 72:67 note: in expansion of bitflags!
<anon>:5:1: 14:2 note: expansion site
<anon>:12:30: 12:43 error: type `Flags` does not implement any method in scope named `FLAG_C`
<anon>:12 | Flags::FLAG_C.bits,
^~~~~~~~~~~~~
<rustc_bitflags macros>:1:1: 72:67 note: in expansion of bitflags!
<rustc_bitflags macros>:70:1: 72:63 note: expansion site
<rustc_bitflags macros>:1:1: 72:67 note: in expansion of bitflags!
<anon>:5:1: 14:2 note: expansion site
error: aborting due to 3 previous errors
thread 'bitflags!_0' panicked at 'Box<Any>', /Users/tamird/src/rust/src/libsyntax/diagnostic.rs:192
---- bitflags!_1 stdout ----
<anon>:27:21: 27:34 error: type `Flags` does not implement any method in scope named `FLAG_A`
<anon>:27 let mut flags = Flags::FLAG_A | Flags::FLAG_B;
^~~~~~~~~~~~~
<anon>:27:37: 27:50 error: type `Flags` does not implement any method in scope named `FLAG_B`
<anon>:27 let mut flags = Flags::FLAG_A | Flags::FLAG_B;
^~~~~~~~~~~~~
error: aborting due to 2 previous errors
thread 'bitflags!_1' panicked at 'Box<Any>', /Users/tamird/src/rust/src/libsyntax/diagnostic.rs:192
failures:
bitflags!_0
bitflags!_1
test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured
thread '<unnamed>' panicked at 'Some tests failed', /Users/tamird/src/rust/src/libtest/lib.rs:253
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Any', /Users/tamird/src/rust/src/libcore/result.rs:729
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment