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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use bevy::prelude::*;

mod flex;
pub use self::flex::{flex, handler_system, Flex};

mod lazy;
pub use self::lazy::{lazy, Lazy, LazyState};

mod memo;
pub use self::memo::{memo, Memo};

pub trait Compose: Send + Sync + 'static {
    type State: Send + Sync + 'static;

    fn build(&mut self, world: &mut World, children: &mut Vec<Entity>) -> Self::State;

    fn rebuild(
        &mut self,
        target: &mut Self,
        state: &mut Self::State,
        world: &mut World,
        children: &mut Vec<Entity>,
    );

    fn remove(&mut self, _state: &mut Self::State) {}
}

impl Compose for () {
    type State = ();

    fn build(&mut self, _world: &mut World, _children: &mut Vec<Entity>) -> Self::State {}

    fn rebuild(
        &mut self,
        _target: &mut Self,
        _state: &mut Self::State,
        _world: &mut World,
        _children: &mut Vec<Entity>,
    ) {
    }
}

impl Compose for &'static str {
    type State = Entity;

    fn build(&mut self, world: &mut World, children: &mut Vec<Entity>) -> Self::State {
        let entity = world.spawn(TextBundle::from_section(
            self.to_owned(),
            Default::default(),
        ));
        let id = entity.id();
        children.push(id);
        id
    }

    fn rebuild(
        &mut self,
        target: &mut Self,
        state: &mut Self::State,
        world: &mut World,
        children: &mut Vec<Entity>,
    ) {
        children.push(*state);

        if self != target {
            world.get_mut::<Text>(*state).unwrap().sections[0] =
                TextSection::new(self.to_owned(), TextStyle::default());
            *target = self;
        }
    }
}

impl Compose for String {
    type State = Entity;

    fn build(&mut self, world: &mut World, children: &mut Vec<Entity>) -> Self::State {
        let entity = world.spawn(TextBundle::from_section(self.clone(), Default::default()));
        let id = entity.id();
        children.push(id);
        id
    }

    fn rebuild(
        &mut self,
        target: &mut Self,
        state: &mut Self::State,
        world: &mut World,
        children: &mut Vec<Entity>,
    ) {
        children.push(*state);

        if self != target {
            world.get_mut::<Text>(*state).unwrap().sections[0] =
                TextSection::new(self.clone(), TextStyle::default());
            target.clone_from(self)
        }
    }
}

impl<C: Compose> Compose for Option<C> {
    type State = Option<C::State>;

    fn build(&mut self, world: &mut World, children: &mut Vec<Entity>) -> Self::State {
        self.as_mut().map(|compose| compose.build(world, children))
    }

    fn rebuild(
        &mut self,
        target: &mut Self,
        state: &mut Self::State,
        world: &mut World,
        children: &mut Vec<Entity>,
    ) {
        if let Some(mut compose) = self.take() {
            if let Some(target) = target {
                let state = state.as_mut().unwrap();
                compose.rebuild(target, state, world, children);
            } else {
                let new_state = compose.build(world, children);
                *state = Some(new_state);
                *target = Some(compose);
            }
        } else if let Some(mut target) = target.take() {
            let state = state.as_mut().unwrap();
            target.remove(state);
        }
    }
}

macro_rules! impl_compose_for_tuple {
    ($($t:tt: $idx:tt),*) => {
        impl<$($t:Compose),*> Compose for ($($t),*) {
            type State = ($($t::State),*);

            fn build(&mut self, world: &mut World, children: &mut Vec<Entity>) -> Self::State {
                ( $(self.$idx.build(world, children)),* )
            }

            fn rebuild(
                &mut self,
                target: &mut Self,
                state: &mut Self::State,
                world: &mut World,
                children: &mut Vec<Entity>,
            ) {
                $( self.$idx.rebuild(&mut target.$idx, &mut state.$idx, world, children) );*
            }
        }
    };
}

impl_compose_for_tuple!(C1: 0, C2: 1);
impl_compose_for_tuple!(C1: 0, C2: 1, C3: 2);
impl_compose_for_tuple!(C1: 0, C2: 1, C3: 2, C4: 3);
impl_compose_for_tuple!(C1: 0, C2: 1, C3: 2, C4: 3, C5: 4);
impl_compose_for_tuple!(C1: 0, C2: 1, C3: 2, C4: 3, C5: 4, C6: 5);
impl_compose_for_tuple!(C1: 0, C2: 1, C3: 2, C4: 3, C5: 4, C6: 5, C7: 6);
impl_compose_for_tuple!(C1: 0, C2: 1, C3: 2, C4: 3, C5: 4, C6: 5, C7: 6, C8: 7);