1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*!
[![github]](https://github.com/Josh194/ConstEnumUtils) [![crates-io]](https://crates.io/crates/cenum-utils) [![docs-rs]](https://docs.rs/cenum-utils)
[github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
[crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
[docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
`cenum_utils` provides a minimal set of traits and (optionally) derive macros providing const access to certain enum properties.
Currently this includes:
- [EnumCount] — Variant counts.
- [EnumDiscriminants] — Variant discriminants.
- [EnumNames] — Variant names.
Unfortunately, due to rust's currently lack of const trait support, actually interacting with some of the features this crate provides in const contexts can be somewhat difficult.
# Example
```rust
use cenum_utils::*;
#[derive(ConstEnum)]
#[repr(u8)]
enum Enum {
X,
Y,
Z
}
fn test() {
assert_eq!(Enum::COUNT, 3);
assert_eq!(Enum::DISCRIMINANTS, &[0, 1, 2]);
assert_eq!(Enum::NAMES, &["X", "Y", "Z"])
}
const fn const_test() {
assert!(Enum::COUNT == 3);
static NAMES: &[u8] = &[b'X', b'Y', b'Z'];
let mut i = 0;
while i < Enum::COUNT {
assert!(Enum::DISCRIMINANTS[i] as usize == i);
assert!(Enum::NAMES[i].as_bytes()[0] == NAMES[i]);
i += 1;
}
}
# test();
# const_test();
```
# Features
- **`derive`** *(enabled by default)* — Derive macros for the core traits provided by this crate.
*/
pub use ConstEnum;
/// A trait providing access to the number of enum variants a type contains.
/// A trait providing access to the discriminants of an enum's variants.
/// A trait providing access to the names of an enum's variants.