devela/examples/code/enumint.rs
1// devela::examples::code::enumint
2//
3//! Shows how to use the [`enumint!`] declarative macro.
4//!
5//! # Examples
6//!
7//! This will create the [`ExampleEnumIntU8`] type.
8//! ```
9//! # use devela::enumint;
10//! enumint![pub ExampleEnumIntU8, i8, -126, 125];
11//! ```
12//
13// Note that having a huge number of variants needs a lot of resources. E.g.:
14// enumint![pub ExampleEnumIntU16, u16, 0, 16384]; // +5s to compile
15// enumint![pub ExampleEnumIntU16, u16, -16384, 16384]; // +17s +25GB to compile
16
17use devela::enumint;
18
19enumint![pub ExampleEnumIntU8, i8, -126, 126];
20
21fn main() {
22 assert_eq!(ExampleEnumIntU8::VALID_VALUES, 253);
23 assert_eq!(ExampleEnumIntU8::NICHE_VALUES, 3);
24 // We can nest 3 Options before the memory representation increases:
25 assert_eq!(size_of::<ExampleEnumIntU8>(), 1); // 0 niches used
26 assert_eq!(size_of::<Option<ExampleEnumIntU8>>(), 1); // 1 niche used
27 assert_eq!(size_of::<Option<Option<ExampleEnumIntU8>>>(), 1); // 2 niches used
28 assert_eq!(size_of::<Option<Option<Option<ExampleEnumIntU8>>>>(), 1); // all 3 niches used
29 assert_eq!(size_of::<Option<Option<Option<Option<ExampleEnumIntU8>>>>>(), 2);
30}