Skip to content

Conversation

@191220029
Copy link
Collaborator

@191220029 191220029 commented Jun 10, 2025

Add Typed Channel Communication

This PR introduces TypedAction and typed channels (TypedInChannels and TypedOutChannels) to provide fixed-type communication between nodes in the DAG. This enhancement ensures compile-time type checking for data flow between nodes.

The original design of adding typed channels directly to the graph structure faced challenges due to Rust's type system constraints. When using generic type parameters for channels, it becomes difficult to maintain type safety while keeping the graph structure flexible and dynamic. This is because:

  1. The graph needs to handle nodes with different input/output types
  2. Generic type parameters would need to be propagated through the entire graph structure
  3. Type erasure would be necessary at some point, potentially compromising type safety

To address these challenges, we introduced TypedAction as a wrapper around the channel communication logic. This approach:

  1. Encapsulates type information within the action implementation
  2. Allows the graph to remain type-agnostic while maintaining type safety at the node level
  3. Provides a clean interface for implementing type-safe data flow

Example Implementation

The examples\typed_action.rs demonstrates this design by rewriting examples\compute_dag.rs:

struct Compute(usize);

#[async_trait]
impl TypedAction for Compute {
    type I = usize;
    type O = usize;

    async fn run(
        &self,
        mut in_channels: TypedInChannels<Self::I>,
        out_channels: TypedOutChannels<Self::O>,
        env: Arc<EnvVar>,
    ) -> Output {
        // Type-safe data processing
        let inputs = in_channels.map(|result| {
            if let Ok(Some(value)) = result {
                *value
            } else {
                0
            }
        }).await;

        // Process and broadcast results
        let sum = inputs.iter().sum();
        out_channels.broadcast(sum).await;
        
        Output::Out(Some(Content::new(sum)))
    }
}

Related Issues

#105 #104

fmt

typed action

example

typed action

TypedChannel + Chnl trait
@191220029 191220029 self-assigned this Jun 10, 2025
@191220029 191220029 added the enhancement New feature or request label Jun 10, 2025
@191220029 191220029 added this to the 0.5.0 milestone Jun 10, 2025
@191220029 191220029 requested a review from genedna June 10, 2025 02:50
@191220029 191220029 marked this pull request as ready for review June 10, 2025 02:51
@genedna genedna merged commit a46579e into dagrs-dev:main Jun 10, 2025
6 checks passed
@191220029 191220029 deleted the typed-channel branch June 11, 2025 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants