devela/examples/code/enumset.rs
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
// devela::examples::code::enumset
//
//! Shows how to use the [`enumset!`] declarative macro.
//!
//! # Examples
//!
//! This will create the [`ExampleEnum`] and [`ExampleEnumSet`] interrelated types.
//! ```
//! # use devela::enumset;
//! enumset! {
//! /// An example created with [`enumset!`].
//! #[allow(dead_code)]
//! #[derive(Clone, Default)]
//! #[repr(u64)]
//! pub enum ExampleEnum<'a, 'b, T>(
//! /// Represents a set of [`ExampleEnum`] variants.
//! pub ExampleEnumSet: u8
//! )
//! [where T: Clone] // supports where clauses (between [])
//! {
//! #[default]
//! Variant0 = 1,
//! /// A tuple variant.
//! Variant1([u8; 3]),
//! /// A self-referential tuple variant.
//! #[cfg(feature = "std")]
//! Variant2(Box<Self>),
//! /// A struct variant with discriminant.
//! Variant3 {
//! /// field1 docs.
//! some: [u8; 2],
//! /// field2 docs.
//! other: u32
//! } = 30,
//! /// Supports generics and lifetimes.
//! Variant4(T, &'a str, &'b u32),
//! }
//! }
//!
//! assert_eq![5, ExampleEnum::<()>::ENUM_VARIANTS];
//! ```
//
use devela::enumset;
enumset! {
/// An example created with [`enumset!`].
#[allow(dead_code)]
#[derive(Clone, Default)]
#[repr(u64)]
pub enum ExampleEnum<'a, 'b, T>(
/// Represents a set of [`ExampleEnum`] variants.
pub ExampleEnumSet: u8
)
[where T: Clone] // supports where clauses (between [])
{
/// A default unit variant.
#[default]
Variant0 = 1,
/// A tuple variant.
Variant1([u8; 3]),
/// A self-referential tuple variant.
#[cfg(feature = "std")]
Variant2(Box<Self>),
/// A struct variant with discriminant.
Variant3 {
/// field1 docs.
some: [u8; 2],
/// field2 docs.
other: u32
} = 30,
/// Supports generics and lifetimes.
Variant4(T, &'a str, &'b u32),
}
}
fn main() {
let v1 = ExampleEnum::<()>::Variant1([3, 2, 1]);
let _es = ExampleEnum::<()>::new_empty_set();
assert_eq![ExampleEnum::<()>::ENUM_VARIANTS, 5];
assert_eq![v1.enum_variants(), 5];
}