1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use core::iter;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Unconditional<T> {
    Halt,
    Jump(T),
    Return,
    Unknown,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Dummy<T>(pub core::marker::PhantomData<T>);

pub trait ForeachTarget {
    type JumpTarget;

    fn foreach_target<F>(&self, f: F)
    where
        F: FnMut(&Self::JumpTarget);

    fn foreach_target_mut<F>(&mut self, f: F)
    where
        F: FnMut(&mut Self::JumpTarget);
}

impl<T> ForeachTarget for Dummy<T> {
    type JumpTarget = T;

    #[inline]
    fn foreach_target<F>(&self, _f: F)
    where
        F: FnMut(&Self::JumpTarget),
    {
    }

    #[inline]
    fn foreach_target_mut<F>(&mut self, _f: F)
    where
        F: FnMut(&mut Self::JumpTarget),
    {
    }
}

impl<T> ForeachTarget for Unconditional<T> {
    type JumpTarget = T;

    #[inline]
    fn foreach_target<F>(&self, mut f: F)
    where
        F: FnMut(&Self::JumpTarget),
    {
        if let Unconditional::Jump(t) = self {
            f(t);
        }
    }

    #[inline]
    fn foreach_target_mut<F>(&mut self, mut f: F)
    where
        F: FnMut(&mut Self::JumpTarget),
    {
        if let Unconditional::Jump(t) = self {
            f(t);
        }
    }
}

impl<C, T> ForeachTarget for C
where
    for<'a> &'a C: iter::IntoIterator<Item = &'a T>,
    for<'a> &'a mut C: iter::IntoIterator<Item = &'a mut T>,
    T: ForeachTarget,
{
    type JumpTarget = T::JumpTarget;

    #[inline]
    fn foreach_target<F>(&self, mut f: F)
    where
        F: FnMut(&Self::JumpTarget),
    {
        for i in self {
            i.foreach_target(&mut f);
        }
    }

    #[inline]
    fn foreach_target_mut<F>(&mut self, mut f: F)
    where
        F: FnMut(&mut Self::JumpTarget),
    {
        for i in self {
            i.foreach_target_mut(&mut f);
        }
    }
}