Trait canrun::state::Fork[][src]

pub trait Fork<'a, D: Domain<'a>>: Debug {
    fn fork(&self, state: State<'a, D>) -> StateIter<'a, D>;
}
Expand description

Fork a State into zero or more alternate states.

Added to a State with .fork().

Example:

use canrun::{val, var, Fork, Query, State, StateIter, Val};
use canrun::example::I32;
use std::rc::Rc;

#[derive(Debug)]
struct Is1or2 {
    x: Val<i32>,
}

impl<'a> Fork<'a, I32> for Is1or2 {
    fn fork(&self, state: State<'a, I32>) -> StateIter<'a, I32> {
        let s1 = state.clone().unify(&self.x, &val!(1));
        let s2 = state.unify(&self.x, &val!(2));
        Box::new(s1.into_iter().chain(s2.into_iter()))
    }
}

let x = var();
let state: State<I32> = State::new();
let state = state.fork(Rc::new(Is1or2 { x: val!(x) }));
let results: Vec<i32> = state.query(x).collect();
assert_eq!(results, vec![1, 2]);

Required methods

Given a State, return an iterator of states that result from the fork operation.

Implementors