compose_rt/
composer.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use std::any::Any;
use std::fmt::{Debug, Formatter};
use std::ops::RangeBounds;
use std::vec::Drain;

use generational_box::{AnyStorage, UnsyncStorage};
use slab::Slab;

use crate::map::{HashMapExt, HashSetExt, Map, Set};
use crate::{Recomposer, Root, Scope, ScopeId, State, StateId};

pub trait Composable {
    fn compose(&self) -> NodeKey;
    fn clone_box(&self) -> Box<dyn Composable>;
}

impl<T> Composable for T
where
    T: Fn() -> NodeKey + Clone + 'static,
{
    fn compose(&self) -> NodeKey {
        self()
    }

    fn clone_box(&self) -> Box<dyn Composable> {
        Box::new(self.clone())
    }
}

impl Clone for Box<dyn Composable> {
    fn clone(&self) -> Self {
        self.clone_box()
    }
}

pub trait ComposeNode: 'static {
    type Context;
    type Data;

    fn new(scope_id: ScopeId, parent: NodeKey) -> Self;
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;

    fn scope_id(&self) -> ScopeId;
    fn set_scope_id(&mut self, scope_id: ScopeId);

    fn parent(&self) -> NodeKey;
    fn set_parent(&mut self, parent: NodeKey);

    fn data(&self) -> Option<&Self::Data>;
    fn data_mut(&mut self) -> Option<&mut Self::Data>;
    fn set_data(&mut self, data: Self::Data);

    fn children(&self) -> &[NodeKey];
    fn children_mut(&mut self) -> &mut [NodeKey];
    fn children_push(&mut self, node_key: NodeKey);
    fn children_len(&self) -> usize;
    fn children_drain<R>(&mut self, range: R) -> Drain<NodeKey>
    where
        R: RangeBounds<usize>;
}

pub trait AnyData<T> {
    fn new(val: T) -> Self;
    fn value(&self) -> &T;
    fn value_mut(&mut self) -> &mut T;
}

impl<T> AnyData<T> for Box<dyn Any>
where
    T: 'static,
{
    #[inline(always)]
    fn new(val: T) -> Self {
        Box::new(val)
    }

    #[inline(always)]
    fn value(&self) -> &T {
        self.downcast_ref::<T>().unwrap()
    }

    #[inline(always)]
    fn value_mut(&mut self) -> &mut T {
        self.downcast_mut::<T>().unwrap()
    }
}

pub type NodeKey = usize;

pub struct Composer<N>
where
    N: ComposeNode,
{
    pub context: N::Context,
    pub nodes: Slab<N>,
    pub(crate) initialized: bool,
    pub(crate) root_node_key: NodeKey,
    pub(crate) composables: Map<NodeKey, Box<dyn Composable>>,
    pub(crate) states: Map<NodeKey, Map<StateId, Box<dyn Any>>>,
    pub(crate) used_by: Map<StateId, Set<NodeKey>>,
    pub(crate) uses: Map<NodeKey, Set<StateId>>,
    pub(crate) current_node_key: NodeKey,
    pub(crate) key_stack: Vec<usize>,
    pub(crate) child_idx_stack: Vec<usize>,
    pub(crate) dirty_states: Set<StateId>,
    pub(crate) dirty_nodes: Set<NodeKey>,
    pub(crate) mount_nodes: Set<NodeKey>,
    pub(crate) unmount_nodes: Set<NodeKey>,
}

impl<N> Composer<N>
where
    N: ComposeNode,
{
    pub fn new(context: N::Context) -> Self {
        Self {
            context,
            nodes: Slab::new(),
            initialized: false,
            root_node_key: 0,
            composables: Map::new(),
            states: Map::new(),
            used_by: Map::new(),
            uses: Map::new(),
            current_node_key: 0,
            key_stack: Vec::new(),
            child_idx_stack: Vec::new(),
            dirty_states: Set::new(),
            dirty_nodes: Set::new(),
            mount_nodes: Set::new(),
            unmount_nodes: Set::new(),
        }
    }

    pub fn with_capacity(context: N::Context, capacity: usize) -> Self {
        Self {
            context,
            nodes: Slab::with_capacity(capacity),
            initialized: false,
            root_node_key: 0,
            composables: Map::with_capacity(capacity),
            states: Map::with_capacity(capacity),
            used_by: Map::with_capacity(capacity),
            uses: Map::with_capacity(capacity),
            current_node_key: 0,
            child_idx_stack: Vec::new(),
            key_stack: Vec::new(),
            dirty_states: Set::new(),
            dirty_nodes: Set::new(),
            mount_nodes: Set::with_capacity(capacity),
            unmount_nodes: Set::new(),
        }
    }

    // TODO: fine control over the capacity of the HashMaps
    #[track_caller]
    pub fn compose<R>(root: R, context: N::Context) -> Recomposer<(), N>
    where
        R: Fn(Scope<Root, N>),
    {
        let owner = UnsyncStorage::owner();
        let composer = owner.insert(Composer::with_capacity(context, 1024));
        let id = ScopeId::new();
        let scope = Scope::new(id, composer);
        composer.write().start_root(scope.id);
        let root_state = scope.use_state(|| {});
        root(scope);
        composer.write().end_root();
        let mut c = composer.write();
        c.initialized = true;
        Recomposer {
            owner,
            composer,
            root_state,
        }
    }

    #[track_caller]
    pub fn compose_with<R, F, T>(root: R, context: N::Context, state_fn: F) -> Recomposer<T, N>
    where
        R: Fn(Scope<Root, N>, State<T, N>),
        F: Fn() -> T + 'static,
        T: 'static,
    {
        let owner = UnsyncStorage::owner();
        let composer = owner.insert(Composer::with_capacity(context, 1024));
        let id = ScopeId::new();
        let scope = Scope::new(id, composer);
        composer.write().start_root(scope.id);
        let root_state = scope.use_state(state_fn);
        root(scope, root_state);
        composer.write().end_root();
        let mut c = composer.write();
        c.initialized = true;
        Recomposer {
            owner,
            composer,
            root_state,
        }
    }

    #[inline(always)]
    pub(crate) fn start_root(&mut self, scope_id: ScopeId) {
        let parent_node_key = 0;
        let node_key = self.nodes.insert(N::new(scope_id, parent_node_key));
        self.child_idx_stack.push(0);
        self.current_node_key = node_key;
    }

    #[inline(always)]
    pub(crate) fn end_root(&mut self) {
        let child_count = self.child_idx_stack.pop().unwrap();
        assert_eq!(1, child_count, "Root scope must have exactly one child");
        self.root_node_key = self.nodes[self.current_node_key].children()[0];
    }

    #[inline(always)]
    pub(crate) fn start_node(&mut self, parent_node_key: NodeKey, scope_id: ScopeId) {
        if self.initialized {
            let child_idx = self.child_idx_stack.last().cloned();
            if let Some(child_idx) = child_idx {
                let parent_node = &mut self.nodes[parent_node_key];
                if child_idx < parent_node.children_len() {
                    let child_key = parent_node.children()[child_idx];
                    let child_node = &mut self.nodes[child_key];
                    if child_node.scope_id() == scope_id {
                        // reuse existing node
                        self.current_node_key = child_key;
                        self.mount_nodes.insert(child_key);
                        self.child_idx_stack.push(0);
                    } else {
                        // replace existing node
                        let node_key = self.nodes.insert(N::new(scope_id, parent_node_key));
                        self.nodes[parent_node_key].children_mut()[child_idx] = node_key;
                        self.unmount_nodes.insert(child_key);
                        self.mount_nodes.insert(node_key);
                        self.current_node_key = node_key;
                        self.child_idx_stack.push(0);
                    }
                } else {
                    // append new node
                    let node_key = self.nodes.insert(N::new(scope_id, parent_node_key));
                    self.nodes[parent_node_key].children_push(node_key);
                    self.mount_nodes.insert(node_key);
                    self.current_node_key = node_key;
                    self.child_idx_stack.push(0);
                }
            } else {
                // recompose root
                self.child_idx_stack.push(0);
            }
        } else {
            // first compose
            let node_key = self.nodes.insert(N::new(scope_id, parent_node_key));
            self.nodes[parent_node_key].children_push(node_key);
            self.current_node_key = node_key;
            self.child_idx_stack.push(0);
        }
    }

    #[inline(always)]
    pub(crate) fn end_node(&mut self, parent_node_key: NodeKey) {
        let child_count = self.child_idx_stack.pop().unwrap();
        let node = &mut self.nodes[self.current_node_key];
        let old_child_count = node.children_len();
        if child_count < old_child_count {
            let unmount_nodes = node.children_drain(child_count..);
            self.unmount_nodes.extend(unmount_nodes);
        }
        if let Some(parent_child_count) = self.child_idx_stack.last_mut() {
            *parent_child_count += 1;
        }
        self.current_node_key = parent_node_key;
    }

    #[inline(always)]
    pub(crate) fn skip_node(&mut self, parent_node_key: NodeKey) {
        let _ = self.child_idx_stack.pop().unwrap();
        if let Some(parent_child_count) = self.child_idx_stack.last_mut() {
            *parent_child_count += 1;
        }
        self.current_node_key = parent_node_key;
    }
}

impl<N> Debug for Composer<N>
where
    N: ComposeNode + Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Composer")
            .field("nodes", &self.nodes)
            .field("states", &self.states)
            .field("dirty_states", &self.dirty_states)
            .field("used_by", &self.used_by)
            .finish()
    }
}