-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
102 lines (79 loc) · 2.92 KB
/
mod.rs
File metadata and controls
102 lines (79 loc) · 2.92 KB
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
mod connection;
mod executor;
pub use connection::*;
pub use executor::*;
use futures::{future::BoxFuture, Future};
use crate::node_graph::InputId;
use super::nodes::{DynPipelineNode, PipelineNode};
/// Trait defining a task for a node running in the execution system.
pub trait NodeTask: Send + Sync + 'static {
type InputId: From<InputId> + Into<InputId>;
type PipelineNode: PipelineNode;
/// Sync this task with the corresponding node from the high level
/// description.
fn sync_node(&mut self, node: &Self::PipelineNode) {
let _ = node;
}
fn connect(&mut self, input_id: Self::InputId, input: &mut ConnectionHandle);
fn disconnect(&mut self, input_id: Self::InputId);
/// The configuration of this node has changed and any data generated
/// becomes invalid.
fn invalidate(&mut self, cause: InvalidationCause) {
let _ = cause;
}
/// Listen for requests from this nodes outputs asynchronously.
fn run(&mut self) -> impl Future<Output = anyhow::Result<()>> + Send;
}
/// Dynamic version of [NodeTask]. This trait is implemented automatically
/// for all types implementing [NodeTask] and can be used as a trait object.
pub trait DynNodeTask: Send + Sync {
fn sync_node(&mut self, node: &dyn DynPipelineNode);
fn connect(&mut self, input_id: InputId, input: &mut ConnectionHandle);
fn disconnect(&mut self, input_id: InputId);
fn invalidate(&mut self, cause: InvalidationCause);
fn run(&mut self) -> BoxFuture<'_, anyhow::Result<()>>;
}
impl<T: NodeTask + Send + Sync> DynNodeTask for T {
fn sync_node(&mut self, node: &dyn DynPipelineNode) {
let node = node
.as_any()
.downcast_ref::<T::PipelineNode>()
.expect("node should be of type T::PipelineNode");
self.sync_node(node);
}
fn connect(&mut self, input_id: InputId, input: &mut ConnectionHandle) {
self.connect(input_id.into(), input)
}
fn disconnect(&mut self, input_id: InputId) {
self.disconnect(input_id.into())
}
fn invalidate(&mut self, cause: InvalidationCause) {
self.invalidate(cause)
}
fn run(&mut self) -> BoxFuture<'_, anyhow::Result<()>> {
Box::pin(self.run())
}
}
pub enum InvalidationCause {
Connected(InputId),
Disconnected(InputId),
Synced,
InputInvalidated(InputId),
}
/// Passed to [PipelineNode::create_node_task]. Use [NodeTaskBuilder::output] to
/// create the outputs for your node task and [NodeTaskBuilder::task] to submit
/// your created task.
pub trait NodeTaskBuilder {
type PipelineNode: PipelineNode;
fn output<Req: Request>(
&mut self,
output_id: <Self::PipelineNode as PipelineNode>::OutputId,
) -> TaskOutput<Req>;
fn task(
&mut self,
task: impl NodeTask<
InputId = <Self::PipelineNode as PipelineNode>::InputId,
PipelineNode = Self::PipelineNode,
>,
);
}