Skip to main content

examples/reflection/
reflection_basics_simple_enum.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5#[derive(Debug, Component)]
6#[repr(C)]
7#[flecs(meta)]
8pub enum Color {
9    Red,
10    Green,
11    Blue,
12}
13
14#[derive(Debug, Component)]
15#[flecs(meta)]
16pub struct TypeWithEnum {
17    pub color: Color,
18}
19
20fn main() {
21    let mut world = World::new();
22
23    /* Alternatively without the meta attribute,
24    you can do it manually like so (without the derive macro)
25    .constant("Red", Color::Red as i32)
26    .constant("Green", Color::Green as i32)
27    .constant("Blue", Color::Blue as i32);
28    */
29
30    /* Alternatively without the meta attribute,
31    you can do it manually like so (without the derive macro)
32    .member(Color::id(),"color", 1, core::mem::offset_of!(TypeWithEnum, color));
33     */
34
35    // Create a new entity
36    let e = world.entity().set(TypeWithEnum {
37        color: Color::Green,
38    });
39
40    // Convert TypeWithEnum component to flecs expression string
41    e.get::<&TypeWithEnum>(|p| {
42        let expr: String = world.to_expr(p);
43        println!("TypeWithEnum: {expr}");
44    });
45
46    // Output:
47    //  TypeWithEnum: {color: Green}
48}
49
50#[cfg(feature = "flecs_nightly_tests")]
51#[test]
52fn test() {
53    let output_capture = OutputCapture::capture().unwrap();
54    main();
55    output_capture.test("reflection_basics_simple_enum".to_string());
56}