From 956a077d7347b768a3d252f03f7e3de20949b2cd Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 25 May 2015 19:00:26 -0400 Subject: [PATCH 1/2] Use associated constants Fixes #20. Closes #21. --- Cargo.toml | 3 + src/example_generated.rs | 6 +- src/lib.rs | 269 ++++++++++++++-------------- tests/compile-fail/private_flags.rs | 20 +++ tests/conflicting_trait_impls.rs | 1 - tests/external.rs | 6 +- tests/external_no_std.rs | 5 +- tests/i128_bitflags.rs | 21 ++- tests/tests.rs | 19 ++ 9 files changed, 192 insertions(+), 158 deletions(-) create mode 100644 tests/compile-fail/private_flags.rs create mode 100644 tests/tests.rs diff --git a/Cargo.toml b/Cargo.toml index 261bdee..f9a4d36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,9 @@ description = """ A macro to generate structures which behave like bitflags. """ +[dev-dependencies] +compiletest_rs = "0.2" + [badges] travis-ci = { repository = "rust-lang-nursery/bitflags" } diff --git a/src/example_generated.rs b/src/example_generated.rs index 05b99e8..ee5b59b 100644 --- a/src/example_generated.rs +++ b/src/example_generated.rs @@ -9,8 +9,8 @@ bitflags! { 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 = Self::FLAG_A.bits + | Self::FLAG_B.bits + | Self::FLAG_C.bits; } } diff --git a/src/lib.rs b/src/lib.rs index 8d4f704..9c6c5ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,19 +26,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 = Self::FLAG_A.bits +//! | Self::FLAG_B.bits +//! | Self::FLAG_C.bits; //! } //! } //! //! fn main() { -//! let e1 = FLAG_A | FLAG_C; -//! let e2 = FLAG_B | FLAG_C; -//! assert_eq!((e1 | e2), FLAG_ABC); // union -//! assert_eq!((e1 & e2), FLAG_C); // intersection -//! assert_eq!((e1 - e2), FLAG_A); // set difference -//! assert_eq!(!e2, FLAG_A); // set complement +//! let e1 = Flags::FLAG_A | Flags::FLAG_C; +//! let e2 = Flags::FLAG_B | Flags::FLAG_C; +//! assert_eq!((e1 | e2), Flags::FLAG_ABC); // union +//! assert_eq!((e1 & e2), Flags::FLAG_C); // intersection +//! assert_eq!((e1 - e2), Flags::FLAG_A); // set difference +//! assert_eq!(!e2, Flags::FLAG_A); // set complement //! } //! ``` //! @@ -75,12 +75,12 @@ //! } //! //! 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!"); -//! assert_eq!(format!("{:?}", FLAG_A | FLAG_B), "FLAG_A | FLAG_B"); -//! assert_eq!(format!("{:?}", FLAG_B), "FLAG_B"); +//! assert_eq!(format!("{:?}", Flags::FLAG_A | Flags::FLAG_B), "FLAG_A | FLAG_B"); +//! assert_eq!(format!("{:?}", Flags::FLAG_B), "FLAG_B"); //! } //! ``` //! @@ -108,8 +108,8 @@ //! } //! //! fn main() { -//! let flag1 = example::FLAG_A; -//! let flag2 = example::FLAG_B; // error: const `FLAG_B` is private +//! let flag1 = example::Flags1::FLAG_A; +//! let flag2 = example::Flags2::FLAG_B; // error: const `FLAG_B` is private //! } //! ``` //! @@ -207,13 +207,13 @@ //! // explicit `Default` implementation //! impl Default for Flags { //! fn default() -> Flags { -//! FLAG_A | FLAG_C +//! Flags::FLAG_A | Flags::FLAG_C //! } //! } //! //! fn main() { //! let implemented_default: Flags = Default::default(); -//! assert_eq!(implemented_default, (FLAG_A | FLAG_C)); +//! assert_eq!(implemented_default, (Flags::FLAG_A | Flags::FLAG_C)); //! } //! ``` @@ -249,19 +249,19 @@ pub extern crate core as _core; /// 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 = Self::FLAG_A.bits +/// | Self::FLAG_B.bits +/// | Self::FLAG_C.bits; /// } /// } /// /// fn main() { -/// let e1 = FLAG_A | FLAG_C; -/// let e2 = FLAG_B | FLAG_C; -/// assert_eq!((e1 | e2), FLAG_ABC); // union -/// assert_eq!((e1 & e2), FLAG_C); // intersection -/// assert_eq!((e1 - e2), FLAG_A); // set difference -/// assert_eq!(!e2, FLAG_A); // set complement +/// let e1 = Flags::FLAG_A | Flags::FLAG_C; +/// let e2 = Flags::FLAG_B | Flags::FLAG_C; +/// assert_eq!((e1 | e2), Flags::FLAG_ABC); // union +/// assert_eq!((e1 & e2), Flags::FLAG_C); // intersection +/// assert_eq!((e1 - e2), Flags::FLAG_A); // set difference +/// assert_eq!(!e2, Flags::FLAG_A); // set complement /// } /// ``` /// @@ -295,12 +295,12 @@ pub extern crate core as _core; /// } /// /// 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!"); -/// assert_eq!(format!("{:?}", FLAG_A | FLAG_B), "FLAG_A | FLAG_B"); -/// assert_eq!(format!("{:?}", FLAG_B), "FLAG_B"); +/// assert_eq!(format!("{:?}", Flags::FLAG_A | Flags::FLAG_B), "FLAG_A | FLAG_B"); +/// assert_eq!(format!("{:?}", Flags::FLAG_B), "FLAG_B"); /// } /// ``` #[macro_export] @@ -320,11 +320,6 @@ macro_rules! bitflags { bits: $T, } - $( - $(#[$inner $($args)*])* - pub const $Flag: $BitFlags = $BitFlags { bits: $value }; - )+ - __impl_bitflags! { struct $BitFlags: $T { $( @@ -349,11 +344,6 @@ macro_rules! bitflags { bits: $T, } - $( - $(#[$inner $($args)*])* - const $Flag: $BitFlags = $BitFlags { bits: $value }; - )+ - __impl_bitflags! { struct $BitFlags: $T { $( @@ -401,7 +391,7 @@ macro_rules! __impl_bitflags { #[allow(deprecated)] $(? #[$attr $($args)*])* fn $Flag(&self) -> bool { - self.bits & $Flag.bits == $Flag.bits + self.bits & Self::$Flag.bits == Self::$Flag.bits } } )+ @@ -446,6 +436,11 @@ macro_rules! __impl_bitflags { #[allow(dead_code)] impl $BitFlags { + $( + $(#[$attr $($args)*])* + pub const $Flag: $BitFlags = $BitFlags { bits: $value }; + )+ + /// Returns an empty set of flags. #[inline] pub fn empty() -> $BitFlags { @@ -467,7 +462,7 @@ macro_rules! __impl_bitflags { __impl_bitflags! { #[allow(deprecated)] $(? #[$attr $($args)*])* - fn $Flag() -> $T { $Flag.bits } + fn $Flag() -> $T { Self::$Flag.bits } } )+ } @@ -712,7 +707,6 @@ macro_rules! __impl_bitflags { pub mod example_generated; #[cfg(test)] -#[allow(non_upper_case_globals, dead_code)] mod tests { use std::hash::{Hash, Hasher}; use std::collections::hash_map::DefaultHasher; @@ -723,71 +717,71 @@ mod tests { #[doc = "> "] #[doc = "> - Richard Feynman"] struct Flags: u32 { - const FlagA = 0b00000001; + const FLAG_A = 0b00000001; #[doc = " macros are way better at generating code than trans is"] - const FlagB = 0b00000010; - const FlagC = 0b00000100; + const FLAG_B = 0b00000010; + const FLAG_C = 0b00000100; #[doc = "* cmr bed"] #[doc = "* strcat table"] #[doc = " wait what?"] - const FlagABC = FlagA.bits - | FlagB.bits - | FlagC.bits; + const FLAG_ABC = Self::FLAG_A.bits + | Self::FLAG_B.bits + | Self::FLAG_C.bits; } } bitflags! { struct _CfgFlags: u32 { #[cfg(windows)] - const _CfgA = 0b01; + const _CFG_A = 0b01; #[cfg(unix)] - const _CfgB = 0b01; + const _CFG_B = 0b01; #[cfg(windows)] - const _CfgC = _CfgA.bits | 0b10; + const _CFG_C = _CFG_A.bits | 0b10; } } bitflags! { struct AnotherSetOfFlags: i8 { - const AnotherFlag = -1_i8; + const ANOTHER_FLAG = -1_i8; } } bitflags! { struct LongFlags: u32 { - const LongFlagA = 0b1111111111111111; + const LONG_FLAG_A = 0b1111111111111111; } } #[test] fn test_bits(){ assert_eq!(Flags::empty().bits(), 0b00000000); - assert_eq!(FlagA.bits(), 0b00000001); - assert_eq!(FlagABC.bits(), 0b00000111); + assert_eq!(Flags::FLAG_A.bits(), 0b00000001); + assert_eq!(Flags::FLAG_ABC.bits(), 0b00000111); assert_eq!(AnotherSetOfFlags::empty().bits(), 0b00); - assert_eq!(AnotherFlag.bits(), !0_i8); + assert_eq!(AnotherSetOfFlags::ANOTHER_FLAG.bits(), !0_i8); } #[test] fn test_from_bits() { assert_eq!(Flags::from_bits(0), Some(Flags::empty())); - assert_eq!(Flags::from_bits(0b1), Some(FlagA)); - assert_eq!(Flags::from_bits(0b10), Some(FlagB)); - assert_eq!(Flags::from_bits(0b11), Some(FlagA | FlagB)); + assert_eq!(Flags::from_bits(0b1), Some(Flags::FLAG_A)); + assert_eq!(Flags::from_bits(0b10), Some(Flags::FLAG_B)); + assert_eq!(Flags::from_bits(0b11), Some(Flags::FLAG_A | Flags::FLAG_B)); assert_eq!(Flags::from_bits(0b1000), None); - assert_eq!(AnotherSetOfFlags::from_bits(!0_i8), Some(AnotherFlag)); + assert_eq!(AnotherSetOfFlags::from_bits(!0_i8), Some(AnotherSetOfFlags::ANOTHER_FLAG)); } #[test] fn test_from_bits_truncate() { assert_eq!(Flags::from_bits_truncate(0), Flags::empty()); - assert_eq!(Flags::from_bits_truncate(0b1), FlagA); - assert_eq!(Flags::from_bits_truncate(0b10), FlagB); - assert_eq!(Flags::from_bits_truncate(0b11), (FlagA | FlagB)); + assert_eq!(Flags::from_bits_truncate(0b1), Flags::FLAG_A); + assert_eq!(Flags::from_bits_truncate(0b10), Flags::FLAG_B); + assert_eq!(Flags::from_bits_truncate(0b11), (Flags::FLAG_A | Flags::FLAG_B)); assert_eq!(Flags::from_bits_truncate(0b1000), Flags::empty()); - assert_eq!(Flags::from_bits_truncate(0b1001), FlagA); + assert_eq!(Flags::from_bits_truncate(0b1001), Flags::FLAG_A); assert_eq!(AnotherSetOfFlags::from_bits_truncate(0_i8), AnotherSetOfFlags::empty()); } @@ -795,19 +789,19 @@ mod tests { #[test] fn test_is_empty(){ assert!(Flags::empty().is_empty()); - assert!(!FlagA.is_empty()); - assert!(!FlagABC.is_empty()); + assert!(!Flags::FLAG_A.is_empty()); + assert!(!Flags::FLAG_ABC.is_empty()); - assert!(!AnotherFlag.is_empty()); + assert!(!AnotherSetOfFlags::ANOTHER_FLAG.is_empty()); } #[test] fn test_is_all() { assert!(Flags::all().is_all()); - assert!(!FlagA.is_all()); - assert!(FlagABC.is_all()); + assert!(!Flags::FLAG_A.is_all()); + assert!(Flags::FLAG_ABC.is_all()); - assert!(AnotherFlag.is_all()); + assert!(AnotherSetOfFlags::ANOTHER_FLAG.is_all()); } #[test] @@ -816,77 +810,77 @@ mod tests { let e2 = Flags::empty(); assert!(!e1.intersects(e2)); - assert!(AnotherFlag.intersects(AnotherFlag)); + assert!(AnotherSetOfFlags::ANOTHER_FLAG.intersects(AnotherSetOfFlags::ANOTHER_FLAG)); } #[test] fn test_empty_does_not_intersect_with_full() { let e1 = Flags::empty(); - let e2 = FlagABC; + let e2 = Flags::FLAG_ABC; assert!(!e1.intersects(e2)); } #[test] fn test_disjoint_intersects() { - let e1 = FlagA; - let e2 = FlagB; + let e1 = Flags::FLAG_A; + let e2 = Flags::FLAG_B; assert!(!e1.intersects(e2)); } #[test] fn test_overlapping_intersects() { - let e1 = FlagA; - let e2 = FlagA | FlagB; + let e1 = Flags::FLAG_A; + let e2 = Flags::FLAG_A | Flags::FLAG_B; assert!(e1.intersects(e2)); } #[test] fn test_contains() { - let e1 = FlagA; - let e2 = FlagA | FlagB; + let e1 = Flags::FLAG_A; + let e2 = Flags::FLAG_A | Flags::FLAG_B; assert!(!e1.contains(e2)); assert!(e2.contains(e1)); - assert!(FlagABC.contains(e2)); + assert!(Flags::FLAG_ABC.contains(e2)); - assert!(AnotherFlag.contains(AnotherFlag)); + assert!(AnotherSetOfFlags::ANOTHER_FLAG.contains(AnotherSetOfFlags::ANOTHER_FLAG)); } #[test] fn test_insert(){ - let mut e1 = FlagA; - let e2 = FlagA | FlagB; + let mut e1 = Flags::FLAG_A; + let e2 = Flags::FLAG_A | Flags::FLAG_B; e1.insert(e2); assert_eq!(e1, e2); let mut e3 = AnotherSetOfFlags::empty(); - e3.insert(AnotherFlag); - assert_eq!(e3, AnotherFlag); + e3.insert(AnotherSetOfFlags::ANOTHER_FLAG); + assert_eq!(e3, AnotherSetOfFlags::ANOTHER_FLAG); } #[test] fn test_remove(){ - let mut e1 = FlagA | FlagB; - let e2 = FlagA | FlagC; + let mut e1 = Flags::FLAG_A | Flags::FLAG_B; + let e2 = Flags::FLAG_A | Flags::FLAG_C; e1.remove(e2); - assert_eq!(e1, FlagB); + assert_eq!(e1, Flags::FLAG_B); - let mut e3 = AnotherFlag; - e3.remove(AnotherFlag); + let mut e3 = AnotherSetOfFlags::ANOTHER_FLAG; + e3.remove(AnotherSetOfFlags::ANOTHER_FLAG); assert_eq!(e3, AnotherSetOfFlags::empty()); } #[test] fn test_operators() { - let e1 = FlagA | FlagC; - let e2 = FlagB | FlagC; - assert_eq!((e1 | e2), FlagABC); // union - assert_eq!((e1 & e2), FlagC); // intersection - assert_eq!((e1 - e2), FlagA); // set difference - assert_eq!(!e2, FlagA); // set complement - assert_eq!(e1 ^ e2, FlagA | FlagB); // toggle + let e1 = Flags::FLAG_A | Flags::FLAG_C; + let e2 = Flags::FLAG_B | Flags::FLAG_C; + assert_eq!((e1 | e2), Flags::FLAG_ABC); // union + assert_eq!((e1 & e2), Flags::FLAG_C); // intersection + assert_eq!((e1 - e2), Flags::FLAG_A); // set difference + assert_eq!(!e2, Flags::FLAG_A); // set complement + assert_eq!(e1 ^ e2, Flags::FLAG_A | Flags::FLAG_B); // toggle let mut e3 = e1; e3.toggle(e2); - assert_eq!(e3, FlagA | FlagB); + assert_eq!(e3, Flags::FLAG_A | Flags::FLAG_B); let mut m4 = AnotherSetOfFlags::empty(); m4.toggle(AnotherSetOfFlags::empty()); @@ -895,23 +889,23 @@ mod tests { #[test] fn test_set() { - let mut e1 = FlagA | FlagC; - e1.set(FlagB, true); - e1.set(FlagC, false); + let mut e1 = Flags::FLAG_A | Flags::FLAG_C; + e1.set(Flags::FLAG_B, true); + e1.set(Flags::FLAG_C, false); - assert_eq!(e1, FlagA | FlagB); + assert_eq!(e1, Flags::FLAG_A | Flags::FLAG_B); } #[test] fn test_assignment_operators() { let mut m1 = Flags::empty(); - let e1 = FlagA | FlagC; + let e1 = Flags::FLAG_A | Flags::FLAG_C; // union - m1 |= FlagA; - assert_eq!(m1, FlagA); + m1 |= Flags::FLAG_A; + assert_eq!(m1, Flags::FLAG_A); // intersection m1 &= e1; - assert_eq!(m1, FlagA); + assert_eq!(m1, Flags::FLAG_A); // set difference m1 -= m1; assert_eq!(m1, Flags::empty()); @@ -929,23 +923,25 @@ mod tests { assert_eq!(flags, Flags::empty()); flags = Flags::empty(); - flags.extend([FlagA, FlagB].iter().cloned()); - assert_eq!(flags, FlagA | FlagB); + flags.extend([Flags::FLAG_A, Flags::FLAG_B].iter().cloned()); + assert_eq!(flags, Flags::FLAG_A | Flags::FLAG_B); - flags = FlagA; - flags.extend([FlagA, FlagB].iter().cloned()); - assert_eq!(flags, FlagA | FlagB); + flags = Flags::FLAG_A; + flags.extend([Flags::FLAG_A, Flags::FLAG_B].iter().cloned()); + assert_eq!(flags, Flags::FLAG_A | Flags::FLAG_B); - flags = FlagB; - flags.extend([FlagA, FlagABC].iter().cloned()); - assert_eq!(flags, FlagABC); + flags = Flags::FLAG_B; + flags.extend([Flags::FLAG_A, Flags::FLAG_ABC].iter().cloned()); + assert_eq!(flags, Flags::FLAG_ABC); } #[test] fn test_from_iterator() { assert_eq!([].iter().cloned().collect::(), Flags::empty()); - assert_eq!([FlagA, FlagB].iter().cloned().collect::(), FlagA | FlagB); - assert_eq!([FlagA, FlagABC].iter().cloned().collect::(), FlagABC); + assert_eq!([Flags::FLAG_A, Flags::FLAG_B].iter().cloned().collect::(), + Flags::FLAG_A | Flags::FLAG_B); + assert_eq!([Flags::FLAG_A, Flags::FLAG_ABC].iter().cloned().collect::(), + Flags::FLAG_ABC); } #[test] @@ -954,11 +950,11 @@ mod tests { let mut b = Flags::empty(); assert!(!(a < b) && !(b < a)); - b = FlagB; + b = Flags::FLAG_B; assert!(a < b); - a = FlagC; + a = Flags::FLAG_C; assert!(!(a < b) && b < a); - b = FlagC | FlagB; + b = Flags::FLAG_C | Flags::FLAG_B; assert!(a < b); } @@ -968,10 +964,10 @@ mod tests { let mut b = Flags::empty(); assert!(a <= b && a >= b); - a = FlagA; + a = Flags::FLAG_A; assert!(a > b && a >= b); assert!(b < a && b <= a); - b = FlagB; + b = Flags::FLAG_B; assert!(b > a && b >= a); assert!(a < b && a <= b); } @@ -988,62 +984,63 @@ mod tests { let mut y = Flags::empty(); assert_eq!(hash(&x), hash(&y)); x = Flags::all(); - y = FlagABC; + y = Flags::FLAG_ABC; assert_eq!(hash(&x), hash(&y)); } #[test] fn test_debug() { - assert_eq!(format!("{:?}", FlagA | FlagB), "FlagA | FlagB"); + assert_eq!(format!("{:?}", Flags::FLAG_A | Flags::FLAG_B), "FLAG_A | FLAG_B"); assert_eq!(format!("{:?}", Flags::empty()), "(empty)"); - assert_eq!(format!("{:?}", FlagABC), "FlagA | FlagB | FlagC | FlagABC"); + assert_eq!(format!("{:?}", Flags::FLAG_ABC), "FLAG_A | FLAG_B | FLAG_C | FLAG_ABC"); } #[test] fn test_binary() { - assert_eq!(format!("{:b}", FlagABC), "111"); - assert_eq!(format!("{:#b}", FlagABC), "0b111"); + assert_eq!(format!("{:b}", Flags::FLAG_ABC), "111"); + assert_eq!(format!("{:#b}", Flags::FLAG_ABC), "0b111"); } #[test] fn test_octal() { - assert_eq!(format!("{:o}", LongFlagA), "177777"); - assert_eq!(format!("{:#o}", LongFlagA), "0o177777"); + assert_eq!(format!("{:o}", LongFlags::LONG_FLAG_A), "177777"); + assert_eq!(format!("{:#o}", LongFlags::LONG_FLAG_A), "0o177777"); } #[test] fn test_lowerhex() { - assert_eq!(format!("{:x}", LongFlagA), "ffff"); - assert_eq!(format!("{:#x}", LongFlagA), "0xffff"); + assert_eq!(format!("{:x}", LongFlags::LONG_FLAG_A), "ffff"); + assert_eq!(format!("{:#x}", LongFlags::LONG_FLAG_A), "0xffff"); } #[test] fn test_upperhex() { - assert_eq!(format!("{:X}", LongFlagA), "FFFF"); - assert_eq!(format!("{:#X}", LongFlagA), "0xFFFF"); + assert_eq!(format!("{:X}", LongFlags::LONG_FLAG_A), "FFFF"); + assert_eq!(format!("{:#X}", LongFlags::LONG_FLAG_A), "0xFFFF"); } mod submodule { bitflags! { pub struct PublicFlags: i8 { - const FlagX = 0; + const FLAG_X = 0; } } bitflags! { struct PrivateFlags: i8 { - const FlagY = 0; + const FLAG_Y = 0; } } #[test] fn test_private() { - let _ = FlagY; + + let _ = PrivateFlags::FLAG_Y; } } #[test] fn test_public() { - let _ = submodule::FlagX; + let _ = submodule::PublicFlags::FLAG_X; } mod t1 { @@ -1072,8 +1069,8 @@ mod tests { const B = 2; } } - assert_eq!(Flags::all(), A); - assert_eq!(format!("{:?}", A), "A"); + assert_eq!(Flags::all(), Flags::A); + assert_eq!(format!("{:?}", Flags::A), "A"); } #[test] diff --git a/tests/compile-fail/private_flags.rs b/tests/compile-fail/private_flags.rs new file mode 100644 index 0000000..fc9bceb --- /dev/null +++ b/tests/compile-fail/private_flags.rs @@ -0,0 +1,20 @@ +#[macro_use] +extern crate bitflags; + +mod example { + bitflags! { + pub struct Flags1: u32 { + const FLAG_A = 0b00000001; + } + } + bitflags! { + struct Flags2: u32 { + const FLAG_B = 0b00000010; + } + } +} + +fn main() { + let flag1 = example::Flags1::FLAG_A; + let flag2 = example::Flags2::FLAG_B; //~ ERROR struct `Flags2` is private +} diff --git a/tests/conflicting_trait_impls.rs b/tests/conflicting_trait_impls.rs index 4704dfa..933e31c 100644 --- a/tests/conflicting_trait_impls.rs +++ b/tests/conflicting_trait_impls.rs @@ -1,4 +1,3 @@ -#![allow(dead_code)] #![no_std] #[macro_use] diff --git a/tests/external.rs b/tests/external.rs index fc1c346..b393778 100644 --- a/tests/external.rs +++ b/tests/external.rs @@ -1,5 +1,3 @@ -#![allow(dead_code)] - #[macro_use] extern crate bitflags; @@ -11,11 +9,11 @@ bitflags! { const B = 0b00000010; const C = 0b00000100; #[doc = "foo"] - const ABC = A.bits | B.bits | C.bits; + const ABC = Flags::A.bits | Flags::B.bits | Flags::C.bits; } } #[test] fn smoke() { - assert_eq!(ABC, A | B | C); + assert_eq!(Flags::ABC, Flags::A | Flags::B | Flags::C); } diff --git a/tests/external_no_std.rs b/tests/external_no_std.rs index 8b8c706..7c5c9e0 100644 --- a/tests/external_no_std.rs +++ b/tests/external_no_std.rs @@ -1,4 +1,3 @@ -#![allow(dead_code)] #![no_std] #[macro_use] @@ -12,11 +11,11 @@ bitflags! { const B = 0b00000010; const C = 0b00000100; #[doc = "foo"] - const ABC = A.bits | B.bits | C.bits; + const ABC = Flags::A.bits | Flags::B.bits | Flags::C.bits; } } #[test] fn smoke() { - assert_eq!(ABC, A | B | C); + assert_eq!(Flags::ABC, Flags::A | Flags::B | Flags::C); } diff --git a/tests/i128_bitflags.rs b/tests/i128_bitflags.rs index acbb927..47bd06b 100644 --- a/tests/i128_bitflags.rs +++ b/tests/i128_bitflags.rs @@ -1,6 +1,5 @@ #![cfg(feature = "unstable_testing")] -#![allow(dead_code, unused_imports)] #![feature(i128_type)] #[macro_use] @@ -12,19 +11,19 @@ bitflags! { const A = 0x0000_0000_0000_0000_0000_0000_0000_0001; const B = 0x0000_0000_0000_1000_0000_0000_0000_0000; const C = 0x8000_0000_0000_0000_0000_0000_0000_0000; - const ABC = A.bits | B.bits | C.bits; + const ABC = Self::A.bits | Self::B.bits | Self::C.bits; } } #[test] fn test_i128_bitflags() { - assert_eq!(ABC, A | B | C); - assert_eq!(A.bits, 0x0000_0000_0000_0000_0000_0000_0000_0001); - assert_eq!(B.bits, 0x0000_0000_0000_1000_0000_0000_0000_0000); - assert_eq!(C.bits, 0x8000_0000_0000_0000_0000_0000_0000_0000); - assert_eq!(ABC.bits, 0x8000_0000_0000_1000_0000_0000_0000_0001); - assert_eq!(format!("{:?}", A), "A"); - assert_eq!(format!("{:?}", B), "B"); - assert_eq!(format!("{:?}", C), "C"); - assert_eq!(format!("{:?}", ABC), "A | B | C | ABC"); + assert_eq!(Flags128::ABC, Flags128::A | Flags128::B | Flags128::C); + assert_eq!(Flags128::A.bits, 0x0000_0000_0000_0000_0000_0000_0000_0001); + assert_eq!(Flags128::B.bits, 0x0000_0000_0000_1000_0000_0000_0000_0000); + assert_eq!(Flags128::C.bits, 0x8000_0000_0000_0000_0000_0000_0000_0000); + assert_eq!(Flags128::ABC.bits, 0x8000_0000_0000_1000_0000_0000_0000_0001); + assert_eq!(format!("{:?}", Flags128::A), "A"); + assert_eq!(format!("{:?}", Flags128::B), "B"); + assert_eq!(format!("{:?}", Flags128::C), "C"); + assert_eq!(format!("{:?}", Flags128::ABC), "A | B | C | ABC"); } diff --git a/tests/tests.rs b/tests/tests.rs new file mode 100644 index 0000000..7d007a9 --- /dev/null +++ b/tests/tests.rs @@ -0,0 +1,19 @@ +extern crate compiletest_rs as compiletest; + +use std::path::PathBuf; + +fn run_mode(mode: &'static str) { + let mut config = compiletest::Config::default(); + let cfg_mode = mode.parse().ok().expect("Invalid mode"); + + config.mode = cfg_mode; + config.src_base = PathBuf::from(format!("tests/{}", mode)); + config.target_rustcflags = Some("--extern bitflags=target/debug/libbitflags.rlib".to_owned()); + + compiletest::run_tests(&config); +} + +#[test] +fn compile_test() { + run_mode("compile-fail"); +} From 2831edd7edaece9144d3631eedce2f1df209fa4c Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 31 Aug 2017 19:50:46 -0400 Subject: [PATCH 2/2] [TBS] remove compiletest --- Cargo.toml | 3 --- tests/compile-fail/private_flags.rs | 20 -------------------- tests/tests.rs | 19 ------------------- 3 files changed, 42 deletions(-) delete mode 100644 tests/compile-fail/private_flags.rs delete mode 100644 tests/tests.rs diff --git a/Cargo.toml b/Cargo.toml index f9a4d36..261bdee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,9 +17,6 @@ description = """ A macro to generate structures which behave like bitflags. """ -[dev-dependencies] -compiletest_rs = "0.2" - [badges] travis-ci = { repository = "rust-lang-nursery/bitflags" } diff --git a/tests/compile-fail/private_flags.rs b/tests/compile-fail/private_flags.rs deleted file mode 100644 index fc9bceb..0000000 --- a/tests/compile-fail/private_flags.rs +++ /dev/null @@ -1,20 +0,0 @@ -#[macro_use] -extern crate bitflags; - -mod example { - bitflags! { - pub struct Flags1: u32 { - const FLAG_A = 0b00000001; - } - } - bitflags! { - struct Flags2: u32 { - const FLAG_B = 0b00000010; - } - } -} - -fn main() { - let flag1 = example::Flags1::FLAG_A; - let flag2 = example::Flags2::FLAG_B; //~ ERROR struct `Flags2` is private -} diff --git a/tests/tests.rs b/tests/tests.rs deleted file mode 100644 index 7d007a9..0000000 --- a/tests/tests.rs +++ /dev/null @@ -1,19 +0,0 @@ -extern crate compiletest_rs as compiletest; - -use std::path::PathBuf; - -fn run_mode(mode: &'static str) { - let mut config = compiletest::Config::default(); - let cfg_mode = mode.parse().ok().expect("Invalid mode"); - - config.mode = cfg_mode; - config.src_base = PathBuf::from(format!("tests/{}", mode)); - config.target_rustcflags = Some("--extern bitflags=target/debug/libbitflags.rlib".to_owned()); - - compiletest::run_tests(&config); -} - -#[test] -fn compile_test() { - run_mode("compile-fail"); -}