compose_rt/
node.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
use std::any::Any;
use std::fmt::Debug;
use std::ops::RangeBounds;
use std::vec::Drain;

use crate::{ComposeNode, NodeKey, ScopeId};

pub trait NodeData: Debug + 'static {
    type Context;
}

impl NodeData for () {
    type Context = ();
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Node<T> {
    pub scope_id: ScopeId,
    pub parent: NodeKey,
    pub children: Vec<NodeKey>,
    pub data: Option<T>,
}

impl<T> ComposeNode for Node<T>
where
    T: NodeData,
{
    type Context = T::Context;

    type Data = T;

    #[inline(always)]
    fn new(scope_id: ScopeId, parent: NodeKey) -> Self {
        Self {
            scope_id,
            parent,
            children: Vec::new(),
            data: None,
        }
    }

    #[inline(always)]
    fn as_any(&self) -> &dyn Any {
        self
    }

    #[inline(always)]
    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    #[inline(always)]
    fn scope_id(&self) -> ScopeId {
        self.scope_id
    }

    #[inline(always)]
    fn set_scope_id(&mut self, scope_id: ScopeId) {
        self.scope_id = scope_id;
    }

    #[inline(always)]
    fn parent(&self) -> NodeKey {
        self.parent
    }

    #[inline(always)]
    fn set_parent(&mut self, parent: NodeKey) {
        self.parent = parent;
    }

    #[inline(always)]
    fn data(&self) -> Option<&Self::Data> {
        self.data.as_ref()
    }

    #[inline(always)]
    fn data_mut(&mut self) -> Option<&mut Self::Data> {
        self.data.as_mut()
    }

    #[inline(always)]
    fn set_data(&mut self, data: Self::Data) {
        self.data = Some(data);
    }

    #[inline(always)]
    fn children(&self) -> &[NodeKey] {
        &self.children
    }

    #[inline(always)]
    fn children_mut(&mut self) -> &mut [NodeKey] {
        &mut self.children
    }

    #[inline(always)]
    fn children_push(&mut self, node_key: NodeKey) {
        self.children.push(node_key);
    }

    #[inline(always)]
    fn children_len(&self) -> usize {
        self.children.len()
    }

    #[inline(always)]
    fn children_drain<R>(&mut self, range: R) -> Drain<NodeKey>
    where
        R: RangeBounds<usize>,
    {
        self.children.drain(range)
    }
}