Skip to main content

examples/reflection/
reflection_nested_struct.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5#[derive(Default, Component)]
6#[flecs(meta)]
7pub struct Point {
8    pub x: f32,
9    pub y: f32,
10}
11
12#[derive(Default, Component)]
13#[flecs(meta)]
14pub struct Line {
15    pub start: Point,
16    pub stop: Point,
17}
18
19fn main() {
20    let world = World::new();
21
22    // Create entity, set Line component
23    let e = world.entity().set(Line {
24        start: Point { x: 10.0, y: 20.0 },
25        stop: Point { x: 30.0, y: 40.0 },
26    });
27
28    // Convert Line component to flecs expression string
29    e.get::<&mut Line>(|line| {
30        // Convert component to string
31        println!("{}", world.to_expr(line));
32    });
33
34    // Output:
35    //  {start: {x: 10, y: 20}, stop: {x: 30, y: 40}}
36}
37
38#[cfg(feature = "flecs_nightly_tests")]
39#[test]
40fn test() {
41    let output_capture = OutputCapture::capture().unwrap();
42    main();
43    output_capture.test("reflection_nested_struct".to_string());
44}