directed 0.3.0

Evaluate programs based on Directed Acyclic Graphs
Documentation
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! The registry is the "global" store of logic and state.

use crate::{
    NodeTypeMismatchError, NodesNotFoundError, RegistryError,
    node::{AnyNode, Node},
    stage::{Stage, StageShape, ValueStage, ValueWrapper},
};
use std::{any::TypeId, marker::PhantomData};

/// Used to access nodes within the registry, just a simple `usize` alias
#[derive(Debug, Clone, Copy, Eq, Hash, Ord)]
#[repr(transparent)]
pub struct NodeId<S: Stage>(pub(crate) usize, pub(crate) PhantomData<S>);
impl<S: Stage> From<NodeId<S>> for usize {
    fn from(value: NodeId<S>) -> Self {
        value.0
    }
}
impl<S0: Stage, S1: Stage> PartialEq<NodeId<S1>> for NodeId<S0> {
    fn eq(&self, other: &NodeId<S1>) -> bool {
        self.0 == other.0
    }
}
impl<S0: Stage, S1: Stage> PartialOrd<NodeId<S1>> for NodeId<S0> {
    fn partial_cmp(&self, other: &NodeId<S1>) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(&other.0)
    }
}
impl<S: Stage> NodeId<S> {
    pub fn stage_shape(&self) -> &'static StageShape {
        Into::<NodeReflection>::into(self).shape
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NodeReflection {
    pub(crate) id: usize,
    pub(crate) shape: &'static StageShape,
}
impl<S: Stage + 'static> From<NodeId<S>> for NodeReflection {
    fn from(value: NodeId<S>) -> Self {
        Self {
            id: value.0,
            shape: &S::SHAPE,
        }
    }
}
impl<S: Stage + 'static> From<&NodeId<S>> for NodeReflection {
    fn from(value: &NodeId<S>) -> Self {
        Self {
            id: value.0,
            shape: &S::SHAPE,
        }
    }
}
impl PartialOrd for NodeReflection {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.id.partial_cmp(&other.id)
    }
}
impl Ord for NodeReflection {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.id.cmp(&other.id)
    }
}

/// A [Registry] stores each node, its state, and the logical [Stage]
/// associated with it.
pub struct Registry(
    pub(super) Vec<Option<Box<dyn AnyNode>>>,
    #[cfg(feature = "tokio")]
    pub(super)  Vec<(
        tokio::sync::watch::Sender<bool>,
        tokio::sync::watch::Receiver<bool>,
    )>,
);

impl Registry {
    pub fn new() -> Self {
        Self(
            Vec::new(),
            #[cfg(feature = "tokio")]
            Vec::new(),
        )
    }

    /// Get a reference to the state of a specific node.
    pub fn state<S: Stage + 'static>(&self, id: NodeId<S>) -> Result<&S::State, RegistryError> {
        self.validate_node_type::<S>(&id)?;
        match self.0.get(id.0) {
            Some(Some(any_node)) => match any_node.as_any().downcast_ref::<Node<S>>() {
                Some(node) => Ok(&node.state),
                None => Err(NodeTypeMismatchError {
                    got: any_node.type_id(),
                    expected: TypeId::of::<Node<S>>(),
                }
                .into()),
            },
            // TODO: Handle Some(None) as a special case, as this means the node is busy
            None | Some(None) => {
                Err(NodesNotFoundError::from(&[id.into()] as &[NodeReflection]).into())
            }
        }
    }

    /// Get a mutable reference to the state of a specific node.
    pub fn state_mut<S: Stage + 'static>(
        &mut self,
        id: NodeId<S>,
    ) -> Result<&mut S::State, RegistryError> {
        self.validate_node_type::<S>(&id)?;
        match self.0.get_mut(id.0) {
            Some(Some(any_node)) => {
                let node_type_id = any_node.type_id();
                match any_node.as_any_mut().downcast_mut::<Node<S>>() {
                    Some(node) => Ok(&mut node.state),
                    None => Err(NodeTypeMismatchError {
                        got: node_type_id,
                        expected: TypeId::of::<Node<S>>(),
                    }
                    .into()),
                }
            }
            // TODO: Handle Some(None) as a special case, as this means the node is busy
            None | Some(None) => {
                Err(NodesNotFoundError::from(&[id.into()] as &[NodeReflection]).into())
            }
        }
    }

    /// Same as [Self::register], but creates a simple stage that just outputs the
    /// value given
    pub fn value<T: Send + Sync + Clone + 'static>(&mut self, value: T) -> NodeId<ValueStage<T>> {
        let next = self.0.len();
        self.0.push(Some(Box::new(Node::new(
            ValueStage::<T>::new(),
            ValueWrapper(Some(value)),
        ))));
        #[cfg(feature = "tokio")]
        self.1.push(tokio::sync::watch::channel(true));
        NodeId(next, PhantomData)
    }

    /// Add a node to the registry. This returns a unique identifier for that
    /// node, which can be used to add it to a [crate::Graph]. This uses default
    /// state
    pub fn register<S: Stage + Send + Sync + 'static>(&mut self, stage: S) -> NodeId<S>
    where
        S::State: Default,
    {
        let next = self.0.len();
        self.0
            .push(Some(Box::new(Node::new(stage, S::State::default()))));
        #[cfg(feature = "tokio")]
        self.1.push(tokio::sync::watch::channel(true));
        NodeId(next, PhantomData)
    }

    /// Add a node to the registry. This returns a unique identifier for that
    /// node, which can be used to add it to a [crate::Graph].
    ///
    /// [crate::stage::Stage::State] is taken as a tuple of all the state
    /// parameters in the order they were defined.
    pub fn register_with_state<S: Stage + Send + Sync + 'static>(
        &mut self,
        stage: S,
        state: S::State,
    ) -> NodeId<S> {
        let next = self.0.len();
        self.0.push(Some(Box::new(Node::new(stage, state))));
        #[cfg(feature = "tokio")]
        self.1.push(tokio::sync::watch::channel(true));
        NodeId(next, PhantomData)
    }

    /// Returns an error if the registry doesn't contain a node with a stage
    /// of the specified type with the given id.
    pub fn validate_node_type<S: Stage + 'static>(
        &self,
        id: impl Into<NodeReflection>,
    ) -> Result<(), RegistryError> {
        let id = id.into();
        match self.0.get(id.id) {
            Some(Some(node)) => match node.as_any().downcast_ref::<Node<S>>() {
                Some(_) => Ok(()),
                None => Err(NodeTypeMismatchError {
                    got: TypeId::of::<Node<S>>(),
                    expected: node.as_any().type_id(),
                }
                .into()),
            },
            // TODO: Handle Some(None) as a special case, as this means the node is busy
            None | Some(None) => {
                Err(NodesNotFoundError::from(&[id.clone().into()] as &[NodeReflection]).into())
            }
        }
    }

    /// Remove a node from the registry and return it. This will return an error if
    /// S doesn't match the stage type for that node.
    pub fn unregister<S: Stage + 'static>(
        &mut self,
        id: NodeReflection,
    ) -> Result<Option<Node<S>>, RegistryError> {
        self.validate_node_type::<S>(id)?;
        match self.0.get_mut(id.id) {
            Some(maybe_node) => maybe_node
                .take()
                .map(|node| match node.into_any().downcast() {
                    Ok(node) => Ok(Some(*node)),
                    Err(node) => Err(NodeTypeMismatchError {
                        got: TypeId::of::<Node<S>>(),
                        expected: node.type_id(),
                    }
                    .into()),
                })
                .unwrap_or(Ok(None)),
            None => Ok(None),
        }
    }

    /// Remove a node from the registry and drop it.
    pub fn unregister_and_drop(
        &mut self,
        id: impl Into<NodeReflection>,
    ) -> Result<(), RegistryError> {
        let id = id.into();
        match self.0.get_mut(id.id).take().map(drop) {
            Some(_) => Ok(()),
            None => Err(NodesNotFoundError::from(&[id.into()] as &[NodeReflection]).into()),
        }
    }

    /// Get a node
    pub fn get_node<S: Stage + 'static>(&self, id: NodeId<S>) -> Option<&Node<S>> {
        match self.0.get(id.0) {
            Some(Some(node)) => node.as_any().downcast_ref(),
            // TODO: Handle Some(None) as a special case, as this means the node is busy
            Some(None) => None,
            None => None,
        }
    }

    /// Get a type-erased node
    pub fn get_node_any(&self, id: impl Into<NodeReflection>) -> Option<&Box<dyn AnyNode>> {
        match self.0.get(id.into().id) {
            Some(Some(node)) => Some(node),
            // TODO: Handle Some(None) as a special case, as this means the node is busy
            Some(None) => None,
            None => None,
        }
    }

    /// Get a mutable node
    pub fn get_node_mut<S: Stage + 'static>(&mut self, id: NodeId<S>) -> Option<&mut Node<S>> {
        match self.0.get_mut(id.0) {
            Some(Some(node)) => node.as_any_mut().downcast_mut(),
            // TODO: Handle Some(None) as a special case, as this means the node is busy
            Some(None) => None,
            None => None,
        }
    }

    /// Get a mutable type-erased node
    pub fn get_node_any_mut(
        &mut self,
        id: impl Into<NodeReflection>,
    ) -> Option<&mut Box<dyn AnyNode>> {
        match self.0.get_mut(id.into().id) {
            Some(Some(node)) => Some(node),
            // TODO: Handle Some(None) as a special case, as this means the node is busy
            Some(None) => None,
            None => None,
        }
    }

    /// Get 2 mutable type-erased nodes
    ///
    /// This is an important internal detail: a parent a child node often need
    /// to be modified together.
    ///
    /// This function will panic if id0 and id1 are the same.
    pub fn get2_nodes_any_mut(
        &mut self,
        id0: NodeReflection,
        id1: NodeReflection,
    ) -> Result<(&mut Box<dyn AnyNode>, &mut Box<dyn AnyNode>), NodesNotFoundError> {
        if id0.id == id1.id {
            panic!(
                "Attempted to borrow node id {:?} twice",
                Into::<NodeReflection>::into(id0)
            )
        }
        let first_id = std::cmp::min(id0.id, id1.id);
        let second_id = std::cmp::max(id0.id, id1.id);
        match (self.0.len() < id0.id, self.0.len() < id1.id) {
            (true, true) => {
                return Err(NodesNotFoundError::from(
                    &[id0.into(), id1.into()] as &[NodeReflection]
                ));
            }
            (true, false) => {
                return Err(NodesNotFoundError::from(&[id0.into()] as &[NodeReflection]));
            }
            (false, true) => {
                return Err(NodesNotFoundError::from(&[id1.into()] as &[NodeReflection]));
            }
            _ => (),
        }

        if let [first, .., second] = &mut self.0[first_id..=second_id] {
            match (first, second) {
                (None, None) => Err(NodesNotFoundError::from(
                    &[id0.into(), id1.into()] as &[NodeReflection]
                )),
                (None, Some(_)) => {
                    Err(NodesNotFoundError::from(&[id0.into()] as &[NodeReflection]))
                }
                (Some(_), None) => {
                    Err(NodesNotFoundError::from(&[id1.into()] as &[NodeReflection]))
                }
                (Some(first), Some(second)) => {
                    if first_id == id1.id {
                        Ok((first, second))
                    } else {
                        Ok((second, first))
                    }
                }
            }
        } else {
            unreachable!()
        }
    }

    /// Used to await node availability
    #[cfg(feature = "tokio")]
    pub fn node_availability(
        &self,
        id: NodeReflection,
    ) -> Option<tokio::sync::watch::Receiver<bool>> {
        match self.1.get(id.id) {
            Some((_, rx)) => Some(rx.clone()),
            None => None,
        }
    }

    /// Takes a node out of the registry, gaining ownership of it.
    /// This does not unregister the node, and it can be placed back
    /// inside the registry via it's ID. Returns None if the node is
    /// already taken or never existed.
    #[cfg(feature = "tokio")]
    pub async fn take_node(&mut self, id: NodeReflection) -> Option<Box<dyn AnyNode>> {
        match self.0.get_mut(id.id) {
            Some(maybe_node) => {
                match maybe_node.take() {
                    Some(node) => {
                        // TODO: Handle errors sanely
                        self.1.get_mut(id.id).unwrap().0.send(false).unwrap();
                        Some(node)
                    }
                    None => {
                        panic!("TODO: Handle missing node")
                    }
                }
            }
            None => None,
        }
    }

    /// Insert a node with the given ID back into the registry. Panics
    /// if the node never existed at that ID in the first place.
    pub fn replace_node(&mut self, id: NodeReflection, node: Box<dyn AnyNode>) {
        *self.0.get_mut(id.id).unwrap() = Some(node);
        // TODO: Handle errors sanely
        #[cfg(feature = "tokio")]
        self.1.get_mut(id.id).unwrap().0.send(true).unwrap();
    }

    /// Gets a reference to the inputs from a node
    pub fn get_inputs<S: Stage + 'static>(&self, node_id: NodeId<S>) -> Option<&S::Input> {
        self.get_node(node_id).map(|node| &node.inputs)
    }

    /// Gets a mutable reference to the inputs from a node
    pub fn get_inputs_mut<S: Stage + 'static>(
        &mut self,
        node_id: NodeId<S>,
    ) -> Option<&mut S::Input> {
        self.get_node_mut(node_id).map(|node| &mut node.inputs)
    }

    /// Gets a reference to the outputs from a node
    pub fn get_outputs<S: Stage + 'static>(&self, node_id: NodeId<S>) -> Option<&S::Output> {
        self.get_node(node_id).map(|node| &node.outputs)
    }

    /// Gets a mutable reference to the outputs from a node
    pub fn get_outputs_mut<S: Stage + 'static>(
        &mut self,
        node_id: NodeId<S>,
    ) -> Option<&mut S::Output> {
        self.get_node_mut(node_id).map(|node| &mut node.outputs)
    }
}