mirror of
https://gitee.com/openharmony/third_party_rust_bitflags
synced 2024-11-23 07:10:11 +00:00
Merge pull request #24 from tamird/associated-constants
Convert to associated constants
This commit is contained in:
commit
b4d1048d66
@ -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;
|
||||
}
|
||||
}
|
||||
|
269
src/lib.rs
269
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 = "<pcwalton> 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 = "<strcat> 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>(), Flags::empty());
|
||||
assert_eq!([FlagA, FlagB].iter().cloned().collect::<Flags>(), FlagA | FlagB);
|
||||
assert_eq!([FlagA, FlagABC].iter().cloned().collect::<Flags>(), FlagABC);
|
||||
assert_eq!([Flags::FLAG_A, Flags::FLAG_B].iter().cloned().collect::<Flags>(),
|
||||
Flags::FLAG_A | Flags::FLAG_B);
|
||||
assert_eq!([Flags::FLAG_A, Flags::FLAG_ABC].iter().cloned().collect::<Flags>(),
|
||||
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]
|
||||
|
@ -1,4 +1,3 @@
|
||||
#![allow(dead_code)]
|
||||
#![no_std]
|
||||
|
||||
#[macro_use]
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user