Skip to content

Commit

Permalink
Merge pull request #18 from DCNick3/deprecate-all-and-full
Browse files Browse the repository at this point in the history
Revise the naming of `all` and `full` methods
  • Loading branch information
Lukas3674 authored May 17, 2024
2 parents 29843a8 + f3403dd commit 46c7f14
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 40 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn main() {

println!("{:?}", BitmaskVecDebug::none()); // BitmaskVecDebug[]
println!("{:?}", BitmaskVecDebug::Flag1); // BitmaskVecDebug[Flag1]
println!("{:?}", BitmaskVecDebug::full()); // BitmaskVecDebug[Flag1, Flag2]
println!("{:?}", BitmaskVecDebug::all_flags()); // BitmaskVecDebug[Flag1, Flag2]
}
```

Expand All @@ -138,15 +138,15 @@ const fn bits(&self) -> #type;
// Returns a bitmask that contains all values.
//
// This will include bits that do not have any flags.
// Use `::full()` if you only want to use flags.
const fn all() -> Self;
// This will include bits that do not have any associated flags.
// Use `::all_flags()` if you only want to use flags.
const fn all_bits() -> Self;
// Returns `true` if the bitmask contains all values.
//
// This will check for `bits == !0`,
// use `.is_full()` if you only want to check for all flags
const fn is_all(&self) -> bool;
// use `.is_all_flags()` if you only want to check for all flags
const fn is_all_bits(&self) -> bool;
// Returns a bitmask that does not contain any values.
const fn none() -> Self;
Expand All @@ -155,13 +155,13 @@ const fn none() -> Self;
const fn is_none(&self) -> bool;
// Returns a bitmask that contains all flags.
const fn full() -> Self;
const fn all_flags() -> Self;
// Returns `true` if the bitmask contains all flags.
//
// This will fail if any unused bit is set,
// consider using `.truncate()` first.
const fn is_full(&self) -> bool;
const fn is_all_flags(&self) -> bool;
// Returns a bitmask that only has bits corresponding to flags
const fn truncate(&self) -> Self;
Expand Down
64 changes: 51 additions & 13 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,52 +119,90 @@ pub fn parse(attr: TokenStream, mut item: ItemEnum) -> Result<TokenStream> {
/// Returns a bitmask that contains all values.
///
/// This will include bits that do not have any flags.
/// Use `::full()` if you only want to use flags.
/// Use `::all_flags()` if you only want to use flags.
#[inline]
#vis const fn all() -> Self {
#vis const fn all_bits() -> Self {
Self { bits: !0 }
}

/// Returns a bitmask that contains all flags.
#[inline]
#vis const fn all_flags() -> Self {
Self { bits: #(#all_flags.bits |)* 0 }
}

/// Returns `true` if the bitmask contains all values.
///
/// This will check for `bits == !0`,
/// use `.is_full()` if you only want to check for all flags
/// use `.is_all_flags()` if you only want to check for all flags
#[inline]
#vis const fn is_all(&self) -> bool {
#vis const fn is_all_bits(&self) -> bool {
self.bits == !0
}

/// Returns a bitmask that does not contain any values.
/// Returns `true` if the bitmask contains all flags.
///
/// This will fail if any unused bit is set,
/// consider using `.truncate()` first.
#[inline]
#vis const fn none() -> Self {
Self { bits: 0 }
#vis const fn is_all_flags(&self) -> bool {
self.bits == Self::all_flags().bits
}

/// Returns `true` if the bitmask does not contain any values.
/// Returns a bitmask that contains all values.
///
/// This will include bits that do not have any flags.
/// Use `::all_flags()` if you only want to use flags.
#[inline]
#vis const fn is_none(&self) -> bool {
self.bits == 0
#[deprecated(note = "Please use the `::all_bits()` constructor")]
#vis const fn all() -> Self {
Self::all_bits()
}

/// Returns `true` if the bitmask contains all values.
///
/// This will check for `bits == !0`,
/// use `.is_all_flags()` if you only want to check for all flags
#[inline]
#[deprecated(note = "Please use the `.is_all_bits()` method")]
#vis const fn is_all(&self) -> bool {
self.is_all_bits()
}


/// Returns a bitmask that contains all flags.
#[inline]
#[deprecated(note = "Please use the `::all_flags()` constructor")]
#vis const fn full() -> Self {
Self { bits: #(#all_flags.bits |)* 0 }
Self::all_flags()
}

/// Returns `true` if the bitmask contains all flags.
///
/// This will fail if any unused bit is set,
/// consider using `.truncate()` first.
#[inline]
#[deprecated(note = "Please use the `.is_all_flags()` method")]
#vis const fn is_full(&self) -> bool {
self.bits == Self::full().bits
self.is_all_flags()
}

/// Returns a bitmask that does not contain any values.
#[inline]
#vis const fn none() -> Self {
Self { bits: 0 }
}

/// Returns `true` if the bitmask does not contain any values.
#[inline]
#vis const fn is_none(&self) -> bool {
self.bits == 0
}

/// Returns a bitmask that only has bits corresponding to flags
#[inline]
#vis const fn truncate(&self) -> Self {
Self { bits: self.bits & Self::full().bits }
Self { bits: self.bits & Self::all_flags().bits }
}

/// Returns `true` if `self` intersects with any value in `other`,
Expand Down
39 changes: 20 additions & 19 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ mod tests {
assert_eq!(bm, 0b10000000);

bm |= !Bitmask::Flag8;
assert_eq!(bm.is_all(), true);
assert_eq!(bm.is_all_bits(), true);
}

#[test]
fn test_bits() {
let all = Bitmask::all();
let all = Bitmask::all_bits();
assert_eq!(all.bits(), std::usize::MAX);
}

#[test]
fn test_all() {
let all = Bitmask::all();
assert_eq!(all.is_all(), true);
fn test_all_bits() {
let all = Bitmask::all_bits();
assert_eq!(all.is_all_bits(), true);
assert_eq!(all, std::usize::MAX);
}

Expand All @@ -53,11 +53,12 @@ mod tests {
}

#[test]
fn test_full() {
let full = Bitmask::full();
assert_eq!(full.is_full(), true);

fn test_all_flags() {
let all_flags = Bitmask::all_flags();
assert_eq!(all_flags.is_all_flags(), true);
assert_eq!(
full,
all_flags,
Bitmask::Flag1
| Bitmask::Flag2
| Bitmask::Flag3
Expand All @@ -71,10 +72,10 @@ mod tests {

#[test]
fn test_truncate() {
let all = Bitmask::all();
assert_eq!(all.is_all(), true);
assert_eq!(all.is_full(), false);
assert_eq!(all.truncate().is_full(), true);
let all = Bitmask::all_bits();
assert_eq!(all.is_all_bits(), true);
assert_eq!(all.is_all_flags(), false);
assert_eq!(all.truncate().is_all_flags(), true);
}

#[test]
Expand Down Expand Up @@ -251,19 +252,19 @@ mod tests {
}
assert_eq!(
BitmaskInverted::InvertedFlag1,
BitmaskInverted::all().xor(BitmaskInverted::Flag1)
BitmaskInverted::all_bits().xor(BitmaskInverted::Flag1)
);
assert_eq!(
BitmaskInverted::InvertedFlag2,
BitmaskInverted::all().xor(BitmaskInverted::Flag2)
BitmaskInverted::all_bits().xor(BitmaskInverted::Flag2)
);
assert_eq!(
BitmaskInverted::InvertedFlag3,
BitmaskInverted::all().xor(BitmaskInverted::Flag3)
BitmaskInverted::all_bits().xor(BitmaskInverted::Flag3)
);
assert_eq!(
BitmaskInverted::InvertedFlag4,
BitmaskInverted::all().xor(BitmaskInverted::Flag4)
BitmaskInverted::all_bits().xor(BitmaskInverted::Flag4)
);
}

Expand Down Expand Up @@ -382,7 +383,7 @@ mod tests {
"BitmaskVecDebug[Flag2, Flag3]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::full()),
format!("{:?}", BitmaskVecDebug::all_flags()),
"BitmaskVecDebug[Flag1, Flag2, Flag12, Flag3]"
);

Expand Down Expand Up @@ -427,7 +428,7 @@ mod tests {
"BitmaskVecDebug[Flag1, InvertedFlag2]"
);
assert_eq!(
format!("{:?}", BitmaskVecDebug::full()),
format!("{:?}", BitmaskVecDebug::all_flags()),
"BitmaskVecDebug[Flag1, InvertedFlag1, Flag2, InvertedFlag2]"
);
}
Expand Down

0 comments on commit 46c7f14

Please sign in to comment.