hydro2_operator/
operator.rs

1// ---------------- [ File: src/operator.rs ]
2crate::ix!();
3
4pub type NetworkNodeIoChannelArray<NetworkItem>              = [Option<Arc<AsyncRwLock<NetworkItem>>>;         4];
5pub type NetworkNodeIoChannelValues<NetworkItem>             = [Option<NetworkItem>;                           4];
6pub type NetworkNodeIoChannelReadGuardArray<'a,NetworkItem>  = [Option<AsyncRwLockReadGuard<'a,NetworkItem>>;  4];
7pub type NetworkNodeIoChannelWriteGuardArray<'a,NetworkItem> = [Option<AsyncRwLockWriteGuard<'a,NetworkItem>>; 4];
8
9/// A trait that describes a single operator within the network.
10/// Each operator is responsible for processing input buffers
11/// to produce output buffers.
12#[async_trait]
13pub trait Operator<NetworkItem>: Debug + Named + Send + Sync 
14where NetworkItem: Debug + Send + Sync
15{
16    /// Returns the operation code, which can be used to inform
17    /// specialized handling or diagnostics.
18    fn opcode(&self) -> std::sync::Arc<dyn OpCode>;
19
20    /// How many actual inputs does this operator need?
21    fn input_count(&self) -> usize;
22
23    /// How many outputs does this operator produce?
24    fn output_count(&self) -> usize;
25
26    /// used by the network! dag compiler to verify that the input port of one operator is
27    /// compatible with the data flowing into it from the output port of another operator
28    fn input_port_type_str(&self, port: usize) -> Option<&'static str>;
29
30    /// used by the network! dag compiler to verify that the output port of one operator is
31    /// compatible with the data required by the input port of its downstream operator
32    fn output_port_type_str(&self, port: usize) -> Option<&'static str>;
33
34    /// used by the network! dag compiler to verify that this input port needs an output connection
35    fn input_port_connection_required(&self, port: usize) -> bool;
36
37    /// used by the network! dag compiler to verify that this output port needs an input connection
38    fn output_port_connection_required(&self, port: usize) -> bool;
39
40    /// The big 4×4 method:
41    /// You receive up to 4 inputs and must fill up to 4 outputs.
42    async fn execute(
43        &self,
44        input:  [Option<&NetworkItem>; 4],
45        output: &mut [Option<NetworkItem>; 4],
46    ) -> NetResult<()>;
47}
48
49/// A local trait to convert any `T` that implements `Operator` into `Arc<dyn Operator>`.
50pub trait IntoArcOperator<NetworkItem> {
51    fn into_arc_operator(self) -> Arc<dyn Operator<NetworkItem>>;
52}
53
54impl<T,NetworkItem> IntoArcOperator<NetworkItem> for T
55where
56    T: Operator<NetworkItem> + 'static,
57    NetworkItem: Debug + Send + Sync,
58{
59    fn into_arc_operator(self) -> Arc<dyn Operator<NetworkItem>> {
60        Arc::new(self)
61    }
62}
63
64/// The trait describing up to 4 inputs and 4 outputs for an operator.
65/// Each `#[derive(Operator)]` implementation will provide a hidden struct
66/// implementing these 8 associated types.
67pub trait OperatorSignature {
68    type Input0; 
69    type Input1; 
70    type Input2; 
71    type Input3;
72    type Output0; 
73    type Output1; 
74    type Output2; 
75    type Output3;
76}