Skip to main content

rill_core/executor/
mod.rs

1//! Graph executor for driving signal processing using the topology graph and active ports.
2//!
3//! The `GraphExecutor` coordinates clock ticks, pulls data from input ports,
4//! calls node processing, and pushes data to output ports.
5//!
6//! This module provides a prototype implementation that demonstrates the active ports flow.
7//! A real implementation would integrate with a concrete graph (e.g., `rill_graph::SignalGraph`).
8//!
9//! # Example
10//! ```
11//! use rill_core::executor::GraphExecutor;
12//! use rill_core::math::Transcendental;
13//!
14//! // Create a graph executor (graph omitted for prototype).
15//! // let mut executor = GraphExecutor::new(graph);
16//! // executor.process_block().unwrap();
17//! ```
18
19use crate::math::Transcendental;
20use crate::traits::ActivePort;
21
22/// Executor for a signal graph that processes nodes in topological order.
23///
24/// This is a prototype that outlines the structure. In a real implementation,
25/// the executor would hold a concrete graph and iterate over its nodes.
26pub struct GraphExecutor<T: Transcendental, const BUF_SIZE: usize> {
27    // Placeholder for the signal graph.
28    // In reality, this would be something like `graph: rill_graph::SignalGraph<T, BUF_SIZE>`.
29    _phantom: std::marker::PhantomData<T>,
30}
31
32impl<T: Transcendental, const BUF_SIZE: usize> GraphExecutor<T, BUF_SIZE> {
33    /// Create a new executor from an existing graph.
34    pub fn new() -> Self {
35        Self {
36            _phantom: std::marker::PhantomData,
37        }
38    }
39
40    /// Process one block of audio.
41    ///
42    /// This advances the clock, obtains topological order, processes each node
43    /// by pulling inputs, calling appropriate processing method, and pushing outputs.
44    ///
45    /// # Returns
46    /// `Ok(())` if processing succeeded, or an error if something went wrong.
47    ///
48    /// # Algorithm (conceptual)
49    /// 1. Advance the clock (if the graph provides a clock source).
50    /// 2. Obtain topological order (if the graph provides it).
51    /// 3. For each node in order:
52    ///    a. Gather input data by calling `pull` on each input port (ActivePort trait).
53    ///    b. Determine node type (Source/Processor/Sink) via metadata.
54    ///    c. Call the appropriate processing method (generate/process/consume).
55    ///    d. Push output data by calling `push` on each output port.
56    /// 4. Handle errors (disconnected ports, missing data).
57    pub fn process_block(&mut self) -> Result<(), Box<dyn std::error::Error>> {
58        // This is a stub implementation. A real executor would need access to
59        // the graph's nodes and connections, which is beyond the scope of this prototype.
60        Ok(())
61    }
62}
63
64/// Example of using ActivePort trait to pull data from an input port.
65/// This function illustrates the intended pattern.
66pub fn demonstrate_pull<T: Transcendental, const BUF_SIZE: usize>(
67    port: &mut dyn ActivePort<T, BUF_SIZE>,
68) -> Option<[T; BUF_SIZE]> {
69    // Pull data from the port (if connected).
70    port.pull()
71}
72
73/// Example of pushing data to an output port.
74pub fn demonstrate_push<T: Transcendental, const BUF_SIZE: usize>(
75    port: &mut dyn ActivePort<T, BUF_SIZE>,
76    data: [T; BUF_SIZE],
77) -> Result<(), crate::traits::PortError> {
78    port.push(data)
79}