Skip to content

Commit

Permalink
Make functions that can be const as of 1.61, const. (Fixes #45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lymia committed Jul 22, 2024
1 parent 11a2482 commit fa62127
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
29 changes: 17 additions & 12 deletions enumset/src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,25 @@ pub struct EnumSet<T: EnumSetType> {

//region EnumSet operations
impl<T: EnumSetType> EnumSet<T> {
const EMPTY_REPR: Self = EnumSet { __priv_repr: T::Repr::EMPTY };
const ALL_REPR: Self = EnumSet { __priv_repr: T::ALL_BITS };

/// An empty `EnumSet`.
///
/// This is available as a constant for use in constant expressions.
pub const EMPTY: Self = EnumSet { __priv_repr: T::Repr::EMPTY };
/// This is deprecated because [`EnumSet::empty`] is now `const`.
#[deprecated = "Use `EnumSet::empty()` instead."]
pub const EMPTY: Self = Self::EMPTY_REPR;

/// An `EnumSet` containing all valid variants of the enum.
///
/// This is available as a constant for use in constant expressions.
pub const ALL: Self = EnumSet { __priv_repr: T::ALL_BITS };
/// This is deprecated because [`EnumSet::all`] is now `const`.
#[deprecated = "Use `EnumSet::all()` instead."]
pub const ALL: Self = Self::ALL_REPR;

/// Creates an empty `EnumSet`.
#[inline(always)]
pub fn new() -> Self {
Self::EMPTY
pub const fn new() -> Self {
Self::EMPTY_REPR
}

/// Returns an `EnumSet` containing a single element.
Expand All @@ -159,14 +164,14 @@ impl<T: EnumSetType> EnumSet<T> {
///
/// This is an alias for [`EnumSet::new`].
#[inline(always)]
pub fn empty() -> Self {
Self::EMPTY
pub const fn empty() -> Self {
Self::EMPTY_REPR
}

/// Returns an `EnumSet` containing all valid variants of the enum.
#[inline(always)]
pub fn all() -> Self {
Self::ALL
pub const fn all() -> Self {
Self::ALL_REPR
}

/// Total number of bits used by this type. Note that the actual amount of space used is
Expand All @@ -175,7 +180,7 @@ impl<T: EnumSetType> EnumSet<T> {
/// This is the same as [`EnumSet::variant_count`] except in enums with "sparse" variants.
/// (e.g. `enum Foo { A = 10, B = 20 }`)
#[inline(always)]
pub fn bit_width() -> u32 {
pub const fn bit_width() -> u32 {
T::BIT_WIDTH
}

Expand All @@ -184,7 +189,7 @@ impl<T: EnumSetType> EnumSet<T> {
/// This is the same as [`EnumSet::bit_width`] except in enums with "sparse" variants.
/// (e.g. `enum Foo { A = 10, B = 20 }`)
#[inline(always)]
pub fn variant_count() -> u32 {
pub const fn variant_count() -> u32 {
T::VARIANT_COUNT
}

Expand Down
10 changes: 5 additions & 5 deletions enumset/tests/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ macro_rules! check_simple_conversion {
// array tests
assert_eq!(($e::A | $e::B | $e::C).as_array_truncated(),
[]);
assert_eq!(EnumSet::<$e>::EMPTY.as_array_truncated(),
assert_eq!(EnumSet::<$e>::empty().as_array_truncated(),
[]);
assert_eq!(($e::A | $e::B | $e::C).as_array(),
[7]);
Expand Down Expand Up @@ -165,7 +165,7 @@ macro_rules! check_simple_conversion {
#[test]
fn basic_from_array() {
// array tests
assert_eq!(EnumSet::<$e>::EMPTY,
assert_eq!(EnumSet::<$e>::empty(),
EnumSet::<$e>::from_array([]));
assert_eq!($e::A | $e::B | $e::C,
EnumSet::<$e>::from_array([7]));
Expand All @@ -177,7 +177,7 @@ macro_rules! check_simple_conversion {
EnumSet::<$e>::from_array([7, 0, 0, 0, 0]));

// array tests
assert_eq!(EnumSet::<$e>::EMPTY,
assert_eq!(EnumSet::<$e>::empty(),
EnumSet::<$e>::from_slice(&[]));
assert_eq!($e::A | $e::B | $e::C,
EnumSet::<$e>::from_slice(&[7]));
Expand All @@ -192,7 +192,7 @@ macro_rules! check_simple_conversion {
#[test]
fn basic_from_array_truncated() {
// array tests
assert_eq!(EnumSet::<$e>::EMPTY,
assert_eq!(EnumSet::<$e>::empty(),
EnumSet::<$e>::from_array_truncated([]));
assert_eq!($e::A | $e::B | $e::C,
EnumSet::<$e>::from_array_truncated([7 | (1 << 31)]));
Expand All @@ -204,7 +204,7 @@ macro_rules! check_simple_conversion {
EnumSet::<$e>::from_array_truncated([7, 0, 0, 0, 16]));

// array tests
assert_eq!(EnumSet::<$e>::EMPTY,
assert_eq!(EnumSet::<$e>::empty(),
EnumSet::<$e>::from_slice_truncated(&[]));
assert_eq!($e::A | $e::B | $e::C,
EnumSet::<$e>::from_slice_truncated(&[7 | (1 << 31)]));
Expand Down
6 changes: 5 additions & 1 deletion enumset/tests/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,18 @@ macro_rules! test_enum {
($e:ident, $mem_size:expr) => {
const CONST_SET: EnumSet<$e> = enum_set!($e::A | $e::C);
const CONST_1_SET: EnumSet<$e> = enum_set!($e::A);
const EMPTY_SET: EnumSet<$e> = EnumSet::EMPTY;
const EMPTY_SET: EnumSet<$e> = EnumSet::empty();
const ALL_SET: EnumSet<$e> = EnumSet::all();
const VARIANT_COUNT: usize = EnumSet::<$e>::variant_count() as usize;

#[test]
fn const_set() {
assert_eq!(CONST_SET.len(), 2);
assert_eq!(CONST_1_SET.len(), 1);
assert!(CONST_SET.contains($e::A));
assert!(CONST_SET.contains($e::C));
assert!(EMPTY_SET.is_empty());
assert_eq!(ALL_SET.len(), VARIANT_COUNT);
}

#[test]
Expand Down

0 comments on commit fa62127

Please sign in to comment.