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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[doc="
A Closure owns its state, and only refers to the state when called.
 
Correspond to unnamable closures like:
```ignore
|...| { /* only refers captured variables */ }
```
# Example:
```rust
# #[macro_use] extern crate namable_closures;
# use namable_closures::Closure;
# use namable_closures::StableFnMut;
# use namable_closures::StableFn;
# struct Point{x:i32,y:i32}
# impl Point{ fn new(x:i32,y:i32) -> Point {Point{x:x,y:y}} }
let state = 10;
let add_ten:Closure<i32,(i32,),i32>
    = closure!(state=&state => |i| i+10);
assert_eq!(add_ten.stable_call((1,)),11);
let state = Point::new(10,20);
let mut offset:Closure<Point,(i32,i32),Point>
    = closure!(state=&state => |a,b| Point::new(state.x+a,state.y+b));
let p = offset.stable_call_mut((1i32,2i32));
assert_eq!(p.x,11);
assert_eq!(p.y,22);
```
"]
#[cfg_attr(feature="nightly", doc="
# The same example that uses the unstable features:

```rust
# #[macro_use] extern crate namable_closures;
# use namable_closures::Closure;
# struct Point{x:i32,y:i32}
# impl Point{ fn new(x:i32,y:i32) -> Point {Point{x:x,y:y}} }
    let state = 10;
    let add_ten:Closure<i32,(i32,),i32>
        = closure!(state=&state => |i| i+10);
        assert_eq!(add_ten(1),11);
    let state = Point::new(10,20);
    let offset:Closure<Point,(i32,i32),Point>
        = closure!(state=&state => |a,b| Point::new(state.x+a,state.y+b));
    let p = offset(1i32,2i32);
    assert_eq!(p.x,11);
    assert_eq!(p.y,22);
```
")]
pub struct Closure<'a, State, Input, Output>
where
    State: 'a
{
    f: fn(&State, Input) -> Output,
    t: &'a State,
}
impl<'a, State, Input, Output> Copy for Closure<'a, State, Input, Output> 
{}
impl<'a, State, Input, Output> Clone for Closure<'a, State, Input, Output>
{
    fn clone(&self) -> Self {
        *self
    }
}
impl<'a, State, Input, Output> Closure<'a, State, Input, Output> {
    pub fn new(f: fn(&State, Input) -> Output, t: &'a State) -> Self {
        Self { f, t }
    }
    pub fn call_with_state(&self, s:&State, i: Input) -> Output {        
        (self.f)(s, i)
    }
}
#[doc="
A Closure does not own its state, and only refers to the state when called.
 
Correspond to unnamable closures like:
```ignore
|...| { /* only refers captured variables */ }
```
Examples:

```rust
# #[macro_use] extern crate namable_closures;
# use namable_closures::ClosureRef;
# use namable_closures::StableFnMut;
# use namable_closures::StableFn;
# struct Point{x:i32,y:i32}
# impl Point{ fn new(x:i32,y:i32) -> Point {Point{x:x,y:y}} }
// state refered as reference in body, but moved to the closure
let add_ten:ClosureRef<i32,(i32,),i32>
    = closure!(ref state=10 => move |i| i+*state);
assert_eq!(add_ten.stable_call((1,)),11);
let mut offset:ClosureRef<Point,(i32,i32),Point>
    = closure!(ref state=Point::new(10,20) => move |a,b| Point::new(state.x+a,state.y+b));
let p = offset.stable_call_mut((1,2));
assert_eq!(p.x,11);
assert_eq!(p.y,22);
```
"]
#[cfg_attr(feature="nightly", doc="
# The same example that uses the unstable features:

```rust
# #[macro_use] extern crate namable_closures;
# use namable_closures::ClosureRef;
# struct Point{x:i32,y:i32}
# impl Point{ fn new(x:i32,y:i32) -> Point {Point{x:x,y:y}} }
// state refered as reference in body, but moved to the closure
let add_ten:ClosureRef<i32,(i32,),i32>
    = closure!(ref state=10 => move |i| i+*state);
assert_eq!(add_ten(1),11);
let offset:ClosureRef<Point,(i32,i32),Point>
    = closure!(ref state=Point::new(10,20) => move |a,b| Point::new(state.x+a,state.y+b));
let p = offset(1,2);
assert_eq!(p.x,11);
assert_eq!(p.y,22);
```
")]
pub struct ClosureRef<State, Input, Output>
{
    f: fn(&State, Input) -> Output,
    t: State,
}
impl<State, Input, Output> Copy for ClosureRef<State, Input, Output> 
where
    State: Copy
{}
impl<State, Input, Output> Clone for ClosureRef<State, Input, Output>
where
    State: Clone
{
    fn clone(&self) -> Self {
        Self { f:self.f, t:self.t.clone()}
    }
}
impl<State, Input, Output> ClosureRef<State, Input, Output> {
    pub fn new(f: fn(&State, Input) -> Output, t: State) -> Self {
        Self { f, t }
    }
    pub fn call_with_state(&self, s:&State, i: Input) -> Output {        
        (self.f)(s, i)
    }
}

#[doc="
A namable closure that does not own its state and can mutate it when called.

It is not possible to implement `Copy` or `Clone` for this type.
 
Correspond to unnamable closures like:
```ignore
 |...| { /* mutates captured variables */ }
```

# Example:

```rust
# #[macro_use] extern crate namable_closures;
# use namable_closures::ClosureMut;
# use namable_closures::StableFnMut;
let mut state = 0;
{
   let mut match_cnt:ClosureMut<i32,(i32,i32),()>
       = closure!(mut state=&mut state => |a,b| {if a==b { *state+=1 }});
   for i in 0..10 { match_cnt.stable_call_mut((i,i*3%10)); }
}
assert_eq!(state,2);
```
"]
#[cfg_attr(feature="nightly", doc="
# The same example that uses the unstable features:

```rust
# #[macro_use] extern crate namable_closures;
# use namable_closures::ClosureMut;

let mut state = 0;
{
   let mut match_cnt:ClosureMut<i32,(i32,i32),()>
       = closure!(mut state=&mut state => |a,b| {if a==b { *state+=1 }});
   for i in 0..10 { match_cnt(i,i*3%10); }
}
assert_eq!(state,2);
```
")]
pub struct ClosureMut<'a, State, Input, Output>
where
    State: 'a
{
    f: fn(&mut State, Input) -> Output,
    t: &'a mut State,
}
impl<'a, State, Input, Output> ClosureMut<'a, State, Input, Output> {
    pub fn new(f: fn(&mut State, Input) -> Output, t: &'a mut State) -> Self {
        Self { f, t }
    }
    pub fn call_with_state(&self, s:&mut State, i: Input) -> Output {        
        (self.f)(s, i)
    }
}

#[doc="
A namable closure that owns its state and can mutate it when called.
 
Correspond to unnamable closures like:
```ignore
 move |...| { /* mutates captured variables */ }
```

# Example:

```rust
# #[macro_use] extern crate namable_closures;
# use namable_closures::ClosureRefMut;
# use namable_closures::StableFnMut;
# use namable_closures::StableFnOnce;
let mut accumulate:ClosureRefMut<i32,(i32,),i32>
    = closure!(ref mut state=0 => move |c| {*state+=c;*state});
assert_eq!(accumulate.stable_call_mut((1,)),1);
assert_eq!(accumulate.stable_call_once((2,)),3);
```
"]
#[cfg_attr(feature="nightly", doc="
# The same example that uses the unstable features:

```rust
# #[macro_use] extern crate namable_closures;
# use namable_closures::ClosureRefMut;
let mut accumulate:ClosureRefMut<i32,(i32,),i32>
    = closure!(ref mut state=0 => move |c| {*state+=c;*state});
assert_eq!(accumulate(1),1);
assert_eq!(accumulate(2),3);
```
")]
pub struct ClosureRefMut<State, Input, Output> {
    f: fn(&mut State, Input) -> Output,
    t: State,
}
impl<State,Input,Output> Copy for ClosureRefMut<State, Input, Output>
where
    State: Copy,
{}
impl<State,Input,Output> Clone for ClosureRefMut<State, Input, Output>
where
    State: Clone
{
    fn clone(&self) -> Self {
        Self{ f: self.f, t: self.t.clone() }
    }
}
impl<State, Input, Output> ClosureRefMut<State, Input, Output> {
    pub fn new(f: fn(&mut State, Input) -> Output, t: State) -> Self {
        Self { f, t }
    }
    pub fn call_with_state(&mut self, t: &mut State, i:Input) -> Output {
        (self.f)(t, i)
    }
}

#[doc="
When called, it consumes its state. So it can only be
called once.

Correspond to unnamable closures like:
```ignore
move |...| { /*consumes captured variables */ }
```

Example:

```rust
# #[macro_use] extern crate namable_closures;
# use namable_closures::ClosureOnce;
# use namable_closures::StableFnOnce;
# use std::io;
# struct RoleSet();
# impl RoleSet { fn from_config() -> RoleSet { RoleSet() }}
# struct Passwd();
# impl Passwd { fn get_from_cache() -> Passwd { Passwd() }}
# fn authenticate(_:String,_:Passwd) -> Result<(),io::Error> { Ok(()) }
# fn check_user(_:RoleSet,_:String,_:Passwd) -> Result<(),io::Error> { Ok(()) }
let sign_on:ClosureOnce<Passwd,(String,),Result<(),io::Error>>
    = closure!(passwd=Passwd::get_from_cache() => move |user| authenticate(user,passwd));
let auth:ClosureOnce<RoleSet,(String,Passwd),Result<(),io::Error>>
    = closure!(role_set=RoleSet::from_config() => move |user,passwd| check_user(role_set,user,passwd));
# struct MyStream();
# impl MyStream{
#   fn new() -> MyStream { MyStream() }
#   fn write_all(&mut self, _:&[u8]) -> Result<usize,io::Error> { Ok(0) }
#   fn read_exact_ex(&mut self, _:&mut [u8], _:usize) -> Result<(),io::Error> { Ok(()) }
# }
let send_data:ClosureOnce<MyStream,(&[u8],),Result<usize,io::Error>>
    = closure!(mut stream=MyStream::new() => move |data| stream.write_all(data));
send_data.stable_call_once((&[1u8],));
let read_data:ClosureOnce<MyStream,(&mut [u8],usize),Result<(),io::Error>>
    = closure!(mut stream=MyStream::new() => move |buf,len| stream.read_exact_ex(buf, len));
```
"]
#[cfg_attr(feature="nightly", doc="
# The same example that uses the unstable features:

```rust
# #[macro_use] extern crate namable_closures;
# use namable_closures::ClosureOnce;
# use std::io;
# struct RoleSet();
# impl RoleSet { fn from_config() -> RoleSet { RoleSet() }}
# struct Passwd();
# impl Passwd { fn get_from_cache() -> Passwd { Passwd() }}
# fn authenticate(_:String,_:Passwd) -> Result<(),io::Error> { Ok(()) }
# fn check_user(_:RoleSet,_:String,_:Passwd) -> Result<(),io::Error> { Ok(()) }
let sign_on:ClosureOnce<Passwd,(String,),Result<(),io::Error>>
    = closure!(passwd=Passwd::get_from_cache() => move |user| authenticate(user,passwd));
let auth:ClosureOnce<RoleSet,(String,Passwd),Result<(),io::Error>>
    = closure!(role_set=RoleSet::from_config() => move |user,passwd| check_user(role_set,user,passwd));
# struct MyStream();
# impl MyStream{
#   fn new() -> MyStream { MyStream() }
#   fn write_all(&mut self, _:&[u8]) -> Result<usize,io::Error> { Ok(0) }
#   fn read_exact_ex(&mut self, _:&mut [u8], _:usize) -> Result<(),io::Error> { Ok(()) }
# }
let send_data:ClosureOnce<MyStream,(&[u8],),Result<usize,io::Error>>
    = closure!(mut stream=MyStream::new() => move |data| stream.write_all(data));
send_data(&[1u8]);
let read_data:ClosureOnce<MyStream,(&mut [u8],usize),Result<(),io::Error>>
    = closure!(mut stream=MyStream::new() => move |buf,len| stream.read_exact_ex(buf, len));
```
")]
pub struct ClosureOnce<State, Input, Output> {
    f: fn(State, Input) -> Output,
    t: State,
}
impl<State, Input, Output> Copy for ClosureOnce<State, Input, Output>
where
    State: Copy,
{
}
impl<State, Input, Output> Clone for ClosureOnce<State, Input, Output>
where
    State: Clone,
{
    fn clone(&self) -> ClosureOnce<State, Input, Output> {
        Self::new(self.f, self.t.clone())
    }
}
impl<State, Input, Output> ClosureOnce<State, Input, Output> {
    pub fn new(f: fn(State, Input) -> Output, t: State) -> ClosureOnce<State, Input, Output> {
        Self { f, t }
    }
    pub fn call_with_state(&mut self, t: State, i:Input) -> Output {
        (self.f)(t, i)
    }
}

use stable_fn::{StableFn,StableFnMut,StableFnOnce};

//All Closures implements StableFnOnce
impl<'a, State, Input, Output> StableFnOnce<Input> for Closure<'a, State, Input, Output> {
    type Output = Output;
    fn stable_call_once(self, i: Input) -> Output {
        let Self { f, t } = self;
        f(&t, i)
    }
}
impl<State, Input, Output> StableFnOnce<Input> for ClosureRef<State, Input, Output> {
    type Output = Output;
    fn stable_call_once(self, i: Input) -> Output {
        let Self { f, t } = self;
        f(&t, i)
    }
}
impl<'a, State, Input, Output> StableFnOnce<Input> for ClosureMut<'a, State, Input, Output> {
    type Output = Output;
    fn stable_call_once(self, i: Input) -> Output {
        let Self { f, mut t } = self;
        f(&mut t, i)
    }
}
impl<State, Input, Output> StableFnOnce<Input> for ClosureRefMut<State, Input, Output> {
    type Output = Output;
    fn stable_call_once(self, i: Input) -> Output {
        let Self { f, mut t } = self;
        f(&mut t, i)
    }
}
impl<State, Input, Output> StableFnOnce<Input> for ClosureOnce<State, Input, Output> {
    type Output = Output;
    fn stable_call_once(self, i: Input) -> Output {
        let ClosureOnce { f, t } = self;
        f(t, i)
    }
}

impl<'a, State, Input, Output> StableFnMut<Input> for Closure<'a, State, Input, Output> {
    fn stable_call_mut(&mut self, i: Input) -> Output {
        let Self { f, t } = self;
        f(t, i)
    }
}
impl<State, Input, Output> StableFnMut<Input> for ClosureRef<State, Input, Output> {
    fn stable_call_mut(&mut self, i: Input) -> Output {
        let Self { f, t } = self;
        f(t, i)
    }
}
impl<'a, State, Input, Output> StableFnMut<Input> for ClosureMut<'a, State, Input, Output> {
    fn stable_call_mut(&mut self, i: Input) -> Output {
        let Self { ref f, ref mut t } = self;
        f(t, i)
    }
}
impl<State, Input, Output> StableFnMut<Input> for ClosureRefMut<State, Input, Output> {
    fn stable_call_mut(&mut self, i: Input) -> Output {
        let Self { ref f, ref mut t } = self;
        f(t, i)
    }
}
impl<State, Input, Output> StableFnMut<Input> for ClosureOnce<State, Input, Output>
where
    State: Copy
{
    fn stable_call_mut(&mut self, i: Input) -> Output {
        let Self { f, t } = *self;
        f(t, i)
    }
}

impl<'a, State, Input, Output> StableFn<Input> for Closure<'a, State, Input, Output> {
    fn stable_call(&self, i: Input) -> Output {
        let Self { f, t } = self;
        f(t, i)
    }
}
impl<State, Input, Output> StableFn<Input> for ClosureRef<State, Input, Output> {
    fn stable_call(&self, i: Input) -> Output {
        let Self { f, t } = self;
        f(t, i)
    }
}
/// Note we implemented `StableFn` but not `Fn` for `ClosureRefMut`.
/// 
/// The reason is that if we implement `Fn` in this way it will be chosen to use
/// when called, but its behavour is different than `call_mut` as it does not
/// actually mutate the state - only a copy of the state was mutated.
/// 
/// This argument does not apply to `StableFn`, as it is up to the user to decide
/// whether `stable_call` (do not mutate) or `stable_call_mut` (do mutate) should
/// be called.
impl<State, Input, Output> StableFn<Input> for ClosureRefMut<State, Input, Output>
where
    State: Copy
{
    fn stable_call(&self, i: Input) -> Output {
        let Self { f, mut t } = *self;
        f(&mut t, i)
    }
}
impl<State, Input, Output> StableFn<Input> for ClosureOnce<State, Input, Output>
where
    State: Copy
{
    fn stable_call(&self, i: Input) -> Output {
        let Self { f, t } = *self;
        f(t, i)
    }
}

#[cfg(feature="nightly")]
impl<'a, State, Input, Output> FnOnce<Input> for Closure<'a, State, Input, Output> {
    type Output = Output;
    extern "rust-call" fn call_once(self, i: Input) -> Output {
        let Self { f, t } = self;
        f(&t, i)
    }
}
#[cfg(feature="nightly")]
impl<State, Input, Output> FnOnce<Input> for ClosureRef<State, Input, Output> {
    type Output = Output;
    extern "rust-call" fn call_once(self, i: Input) -> Output {
        let Self { f, t } = self;
        f(&t, i)
    }
}
#[cfg(feature="nightly")]
impl<'a, State, Input, Output> FnOnce<Input> for ClosureMut<'a, State, Input, Output> {
    type Output = Output;
    extern "rust-call" fn call_once(self, i: Input) -> Output {
        let Self { f, mut t } = self;
        f(&mut t, i)
    }
}
#[cfg(feature="nightly")]
impl<State, Input, Output> FnOnce<Input> for ClosureRefMut<State, Input, Output> {
    type Output = Output;
    extern "rust-call" fn call_once(self, i: Input) -> Output {
        let Self { f, mut t } = self;
        f(&mut t, i)
    }
}
#[cfg(feature="nightly")]
impl<State, Input, Output> FnOnce<Input> for ClosureOnce<State, Input, Output> {
    type Output = Output;
    extern "rust-call" fn call_once(self, i: Input) -> Output {
        let Self { f, t } = self;
        f(t, i)
    }
}

#[cfg(feature="nightly")]
impl<'a, State, Input, Output> FnMut<Input> for Closure<'a, State, Input, Output> {
    extern "rust-call" fn call_mut(&mut self, i: Input) -> Output {
        let Self { f, t } = self;
        f(t, i)
    }
}
#[cfg(feature="nightly")]
impl<State, Input, Output> FnMut<Input> for ClosureRef<State, Input, Output> {
    extern "rust-call" fn call_mut(&mut self, i: Input) -> Output {
        let Self { f, t } = self;
        f(t, i)
    }
}
#[cfg(feature="nightly")]
impl<'a, State, Input, Output> FnMut<Input> for ClosureMut<'a, State, Input, Output> {
    extern "rust-call" fn call_mut(&mut self, i: Input) -> Output {
        let Self { ref f, ref mut t } = self;
        f(t, i)
    }
}
#[cfg(feature="nightly")]
impl<State, Input, Output> FnMut<Input> for ClosureRefMut<State, Input, Output> {
    extern "rust-call" fn call_mut(&mut self, i: Input) -> Output {
        let Self { ref f, ref mut t } = self;
        f(t, i)
    }
}
#[cfg(feature="nightly")]
impl<State, Input, Output> FnMut<Input> for ClosureOnce<State, Input, Output>
where
    State: Copy
{
    extern "rust-call" fn call_mut(&mut self, i: Input) -> Output {
        let Self { f, t } = *self;
        f(t, i)
    }
}

#[cfg(feature="nightly")]
impl<'a, State, Input, Output> Fn<Input> for Closure<'a, State, Input, Output> {
    extern "rust-call" fn call(&self, i: Input) -> Output {
        let Self { f, t } = self;
        f(t, i)
    }
}
#[cfg(feature="nightly")]
impl<State, Input, Output> Fn<Input> for ClosureRef<State, Input, Output> {
    extern "rust-call" fn call(&self, i: Input) -> Output {
        let Self { f, t } = self;
        f(t, i)
    }
}
//This can be done; but will cause confusion, as it is not mutating itself
// when it supposed to be so
//#[cfg(feature="nightly")]
//impl<State, Input, Output> Fn<Input> for ClosureRefMut<State, Input, Output>
//where
//    State: Copy
//{
//    extern "rust-call" fn call(&self, i: Input) -> Output {
//        let Self { f, mut t } = *self;
//        f(&mut t, i)
//    }
//}
#[cfg(feature="nightly")]
impl<State, Input, Output> Fn<Input> for ClosureOnce<State, Input, Output>
where
    State: Copy
{
    extern "rust-call" fn call(&self, i: Input) -> Output {
        let Self { f, t } = *self;
        f(t, i)
    }
}

#[cfg(test)]
mod tests {
    use {Closure, ClosureMut, ClosureOnce, ClosureRef, ClosureRefMut};
    use {StableFn,StableFnMut,StableFnOnce};

    #[cfg(feature="nightly")]
    #[test]
    fn test_closure_copy_clone_nightly() {
        let c:ClosureRef<i32,(i32,),i32>
             = closure!(ref a=10 => move |b| *a + b);
        let copied = c;
        let cloned = c.clone();
        assert_eq!(c(20), 30);
        assert_eq!(copied(20), c(20));
        assert_eq!(cloned(20), c(20));

        let mut state = 0;
        {
            let mut match_cnt:ClosureMut<i32,(i32,i32),()>
                = closure!(mut state=&mut state => |a,b| if a==b { *state+=1 });
            for i in 0..10 { match_cnt(i,i*3%10); }
        }
        assert_eq!(state,2);

        let mut accumulate:ClosureRefMut<i32,(i32,),i32>
            = closure!(ref mut state=0 => move |c| {*state+=c;*state});
        assert_eq!(accumulate(1),1);
        assert_eq!(accumulate(2),3);
    }

    #[test]
    fn test_closure_ref_copy_clone() {
        let mut c:ClosureRef<i32,(i32,),i32>
             = closure!(ref a=10 => move |b| *a + b);
        let mut copied = c;
        let cloned = c.clone();
        assert_eq!(call!(ref c (20)), 30);
        assert_eq!(call!(mut copied(20)), call!(mut c(20)));
        assert_eq!(call!(cloned(20)), call!(c(20)));
    }
    #[test]
    fn test_closure_copy_clone() {
        let mut v = 10;
        {
            let mut c:Closure<i32,(i32,),i32>
                 = closure!(a=&v => |b| *a + b);
            let mut copied = c;
            let cloned = c.clone();
            assert_eq!(call!(ref c(20)), 30);
            assert_eq!(call!(mut copied(20)), regulate!(|i|mut c)(20));
            assert_eq!(call!(cloned(20)), regulate!(|i|c)(20));
        }
        v = 20;
        let mut c:Closure<i32,(i32,),i32>
             = closure!(a=&v => |b| *a + b);
        let mut copied = c;
        let cloned = c.clone();
        assert_eq!(c.stable_call((20,)), 40);
        assert_eq!(copied.stable_call_mut((20,)), c.stable_call_mut((20,)));
        assert_eq!(cloned.stable_call_once((20,)), c.stable_call_once((20,)));
    }
    #[test]
    fn test_closure_ref_mut_copy_clone() {
        let mut c:ClosureRefMut<i32,(i32,),i32>
                 = closure!(ref mut a=10 => move |b| {*a+=b;*a});
        let mut copied = c;
        let mut cloned = c.clone();
        assert_eq!(c.stable_call_mut((20,)), 30);
        assert_eq!(c.stable_call_once((20,)), 50);
        assert_eq!(copied.stable_call_mut((20,)), 30);
        assert_eq!(copied.stable_call_once((20,)), 50);
        assert_eq!(cloned.stable_call_mut((20,)), 30);
        assert_eq!(cloned.stable_call_once((20,)), 50);
    }
    #[test]
    fn test_closure_mut() {
        let mut v = 10;
        let mut c:ClosureMut<i32,(i32,),i32>
             = closure!(mut a=&mut v => |b| {*a+=b;*a});
        assert_eq!(c.stable_call_mut((20,)), 30);
        assert_eq!(c.stable_call_once((20,)), 50);
    }
    #[test]
    fn test_closure_once_copy_clone() {
        let c:ClosureOnce<i32,(i32,),i32>
             = closure!(a=10 => move |b| {a + b});
        let copied = c;
        let cloned = c.clone();
        assert_eq!(c.stable_call_once((20,)), 30);
        assert_eq!(c.stable_call_once((20,)), 30);
        assert_eq!(copied.stable_call_once((20,)), 30);
        assert_eq!(copied.stable_call_once((20,)), 30);
        assert_eq!(cloned.stable_call_once((20,)), 30);
        assert_eq!(cloned.stable_call_once((20,)), 30);
    }
}