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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use stable_fn::{StableFn,StableFnMut,StableFnOnce};

pub struct ClosureRec<State,Input,Output> {
    func: fn(&ClosureRec<State,Input,Output>, Input) -> Output,
    state: State
}
impl<State,Input,Output> Copy for ClosureRec<State,Input,Output>
where
    State: Copy{}
impl<State,Input,Output> Clone for ClosureRec<State,Input,Output>
where
    State: Clone
{
    fn clone(&self) -> Self {
        let Self { func, state} = self;
        Self { func:*func, state:state.clone() }
    }
}
impl<State,Input,Output> ClosureRec<State,Input,Output> {
    pub fn new(func: fn(&Self, Input) -> Output, s: State) -> Self {
        Self { func: func, state: s}
    }
    pub fn call_with_state(&self, s:State, i:Input) -> Output {
        (self.func)(&Self::new(self.func, s), i)
    }
}

pub struct ClosureMutRec<State,Input,Output> {
    func: fn(&mut ClosureMutRec<State,Input,Output>, Input) -> Output,
    state: State
}
impl<State,Input,Output> Copy for ClosureMutRec<State,Input,Output>
where
    State: Copy{}
impl<State,Input,Output> Clone for ClosureMutRec<State,Input,Output>
where
    State: Clone
{
    fn clone(&self) -> Self {
        let Self { func, state} = self;
        Self { func:*func, state:state.clone() }
    }
}
impl<State,Input,Output> ClosureMutRec<State,Input,Output> {
    pub fn new(func: fn(&mut Self, Input) -> Output, s: State) -> Self {
        Self { func: func, state: s}
    }
    pub fn call_with_state(&self, s:State, i:Input) -> Output {
        (self.func)(&mut Self::new(self.func, s), i)
    }
}

pub struct ClosureRecMut<'a, State,Input,Output>
where
    State: 'a
{
    func: fn(&mut ClosureRecMut<'a, State,Input,Output>, Input) -> Output,
    state: &'a mut State
}
impl<'a, State,Input,Output> ClosureRecMut<'a, State,Input,Output> {
    pub fn new(func: fn(&mut ClosureRecMut<'a, State,Input,Output>, Input) -> Output, s: &'a mut State) -> Self {
        Self { func: func, state: s}
    }
}

pub struct ClosureOnceRec<State,Input,Output> {
    func: fn(ClosureOnceRec<State,Input,Output>, Input) -> Output,
    state: State
}
impl<State,Input,Output> Copy for ClosureOnceRec<State,Input,Output>
where
    State: Copy{}
impl<State,Input,Output> Clone for ClosureOnceRec<State,Input,Output>
where
    State: Clone
{
    fn clone(&self) -> Self {
        let Self { func, state} = self;
        Self { func:*func, state:state.clone() }
    }
}
impl<State,Input,Output> ClosureOnceRec<State,Input,Output> {
    pub fn new(func: fn(Self, Input) -> Output, s: State) -> Self {
        Self { func: func, state: s}
    }
    pub fn call_with_state(&self, s:State, i:Input) -> Output {
        (self.func)(Self::new(self.func, s), i)
    }
}

impl<State,Input,Output> StableFnOnce<Input> for ClosureRec<State,Input,Output> {
    type Output=Output;
    fn stable_call_once(self, i:Input) -> Self::Output {
        (self.func)(&self, i)
    }
}
impl<State,Input,Output> StableFnMut<Input> for ClosureRec<State,Input,Output> {
    fn stable_call_mut(&mut self, i:Input) -> Output {
        (self.func)(self, i)
    }
}
impl<State,Input,Output> StableFn<Input> for ClosureRec<State,Input,Output> {
    fn stable_call(&self, i:Input) -> Output {
        (self.func)(self, i)
    }
}


impl<State,Input,Output> StableFnOnce<Input> for ClosureMutRec<State,Input,Output> {
    type Output=Output;
    fn stable_call_once(mut self, i:Input) -> Self::Output {
        (self.func)(&mut self, i)
    }
}
impl<State,Input,Output> StableFnMut<Input> for ClosureMutRec<State,Input,Output> {
    fn stable_call_mut(&mut self, i:Input) -> Output {
        (self.func)(self, i)
    }
}
impl<State,Input,Output> StableFn<Input> for ClosureMutRec<State,Input,Output>
where
    State: Copy
{
    fn stable_call(&self, i:Input) -> Output {
        let mut s = *self;
        (s.func)(&mut s, i)
    }
}

impl<'a,State,Input,Output> StableFnOnce<Input> for ClosureRecMut<'a,State,Input,Output> {
    type Output=Output;
    fn stable_call_once(mut self, i:Input) -> Self::Output {
        (self.func)(&mut self, i)
    }
}
impl<'a,State,Input,Output> StableFnMut<Input> for ClosureRecMut<'a,State,Input,Output> {
    fn stable_call_mut(&mut self, i:Input) -> Output {
        (self.func)(self, i)
    }
}

impl<State,Input,Output> StableFnOnce<Input> for ClosureOnceRec<State,Input,Output> {
    type Output=Output;
    fn stable_call_once(self, i:Input) -> Self::Output {
        (self.func)(self, i)
    }
}
impl<State,Input,Output> StableFnMut<Input> for ClosureOnceRec<State,Input,Output>
where
    State: Copy
{
    fn stable_call_mut(&mut self, i:Input) -> Output {
        (self.func)(*self, i)
    }
}
impl<State,Input,Output> StableFn<Input> for ClosureOnceRec<State,Input,Output>
where
    State: Copy
{
    fn stable_call(&self, i:Input) -> Output {
        (self.func)(*self, i)
    }
}

#[cfg(feature="nightly")]
impl<State,Input,Output> FnOnce<Input> for ClosureRec<State,Input,Output> {
    type Output=Output;
    extern "rust-call" fn call_once(self, i:Input) -> Self::Output {
        (self.func)(&self, i)
    }
}
#[cfg(feature="nightly")]
impl<State,Input,Output> FnMut<Input> for ClosureRec<State,Input,Output> {
    extern "rust-call" fn call_mut(&mut self, i:Input) -> Output {
        (self.func)(self, i)
    }
}
#[cfg(feature="nightly")]
impl<State,Input,Output> Fn<Input> for ClosureRec<State,Input,Output> {
    extern "rust-call" fn call(&self, i:Input) -> Output {
        (self.func)(self, i)
    }
}

#[cfg(feature="nightly")]
impl<State,Input,Output> FnOnce<Input> for ClosureMutRec<State,Input,Output> {
    type Output=Output;
    extern "rust-call" fn call_once(mut self, i:Input) -> Self::Output {
        (self.func)(&mut self, i)
    }
}
#[cfg(feature="nightly")]
impl<State,Input,Output> FnMut<Input> for ClosureMutRec<State,Input,Output> {
    extern "rust-call" fn call_mut(&mut self, i:Input) -> Output {
        (self.func)(self, i)
    }
}
#[cfg(feature="nightly")]
impl<State,Input,Output> Fn<Input> for ClosureMutRec<State,Input,Output>
where
    State: Copy
{
    extern "rust-call" fn call(&self, i:Input) -> Output {
        let mut s = *self;
        (s.func)(&mut s, i)
    }
}

#[cfg(feature="nightly")]
impl<'a,State,Input,Output> FnOnce<Input> for ClosureRecMut<'a,State,Input,Output> {
    type Output=Output;
    extern "rust-call" fn call_once(mut self, i:Input) -> Self::Output {
        (self.func)(&mut self, i)
    }
}
#[cfg(feature="nightly")]
impl<'a,State,Input,Output> FnMut<Input> for ClosureRecMut<'a,State,Input,Output> {
    extern "rust-call" fn call_mut(&mut self, i:Input) -> Output {
        (self.func)(self, i)
    }
}

#[cfg(feature="nightly")]
impl<State,Input,Output> FnOnce<Input> for ClosureOnceRec<State,Input,Output> {
    type Output=Output;
    extern "rust-call" fn call_once(self, i:Input) -> Self::Output {
        (self.func)(self, i)
    }
}
#[cfg(feature="nightly")]
impl<State,Input,Output> FnMut<Input> for ClosureOnceRec<State,Input,Output>
where
    State: Copy
{
    extern "rust-call" fn call_mut(&mut self, i:Input) -> Output {
        (self.func)(*self, i)
    }
}
#[cfg(feature="nightly")]
impl<State,Input,Output> Fn<Input> for ClosureOnceRec<State,Input,Output>
where
    State: Copy
{
    extern "rust-call" fn call(&self, i:Input) -> Output {
        (self.func)(*self, i)
    }
}

#[cfg(test)]
mod test {
    use closure_rec::{ClosureRec,ClosureMutRec};
    use stable_fn::{StableFn,StableFnMut};
    #[test]
    fn test_fac() {
        let fac:ClosureRec<(),(i32,),i32> = 
            closure_rec!(me.state=() => ref |i| 
                if i==0 {1} else {me.stable_call((i-1,)) * i}
            );
        assert_eq!(fac.stable_call((10,)),3628800);
    }
    #[test]
    fn test_fib() {
        let fib:ClosureRec<(i32,i32),(i32,),i32> = 
            closure_rec!(me.state=(1,1) => ref |i| {
                let (i0,i1) = me.state;
                match i {
                    0 => i0,
                    1 => i1,
                    n => me.call_with_state((i1,i0+i1), (i-1,))
                }                
            });        
        assert_eq!(fib.stable_call((10,)),89);
        assert_eq!(fib.state, (1,1));
    }
}