Skip to main content

LoopCondition

Trait LoopCondition 

Source
pub trait LoopCondition: Send + Sync {
    // Required method
    fn should_continue(
        &mut self,
        input: &InChannels,
        out: &OutChannels,
        env: Arc<EnvVar>,
    ) -> bool;

    // Provided methods
    fn reset(&mut self) { ... }
    fn restore_from_checkpoint(
        &mut self,
        _completed_iterations: usize,
    ) -> DagrsResult<()> { ... }
}
Expand description

A trait defining the condition for a loop to continue.

This trait allows users to define custom logic for controlling loop execution. The condition is evaluated after the execution of the nodes within the loop body.

§Evaluation Semantics

  • Post-check: The loop body executes at least once before should_continue is called.
  • Frequency: Called once per iteration, after all nodes in the loop body have finished.

§Example

use dagrs::{LoopCondition, InChannels, OutChannels, EnvVar};
use std::sync::Arc;

struct MyLoopCondition {
    count: usize,
}

impl LoopCondition for MyLoopCondition {
    fn should_continue(&mut self, _input: &InChannels, _out: &OutChannels, _env: Arc<EnvVar>) -> bool {
        self.count += 1;
        self.count < 5
    }
     
    fn reset(&mut self) {
        self.count = 0;
    }
}

Required Methods§

Source

fn should_continue( &mut self, input: &InChannels, out: &OutChannels, env: Arc<EnvVar>, ) -> bool

Determines whether the loop should continue for another iteration.

This method is called by the LoopNode during execution.

§Arguments
  • input - Input channels to the loop node.
  • out - Output channels from the loop node.
  • env - The environment variables.
§Returns

true if the loop should continue (jump back to target), false otherwise (proceed to next node).

Provided Methods§

Source

fn reset(&mut self)

Reset the condition state.

This is called when Graph::reset() is invoked. Implementors MUST reset any internal counters or state here to ensure the loop behaves correctly on subsequent graph executions.

Source

fn restore_from_checkpoint( &mut self, _completed_iterations: usize, ) -> DagrsResult<()>

Restore the condition state when resuming from a checkpoint.

completed_iterations is the graph-level loop jump count restored from the checkpoint. Custom conditions that maintain their own iteration counters should override this to keep resume semantics idempotent. The built-in CountLoopCondition restores its internal counter from this value.

Implementors§