closures/
lib.rs

1#![feature(unboxed_closures,fn_traits)]
2#![no_std]
3
4//! ```rust
5//! extern crate closures;
6//!
7//! use std::thread;
8//! use std::sync::mpsc;
9//! use closures::Closure;
10//!
11//! struct State {
12//!     id: i32,
13//!     messages: Vec<&'static str>,
14//!     tx: mpsc::Sender<(i32, &'static str)>,
15//! }
16//!
17//! fn main() {
18//!     let (tx, rx) = mpsc::channel();
19//!     
20//!     let state = State {
21//!         id: 0,
22//!         messages: vec!["hello", "rusty", "world"],
23//!         tx: tx.clone(),
24//!     };
25//!     thread::spawn(Closure::new(state, thread));
26//!     
27//!     let state = State {
28//!         id: 1,
29//!         messages: vec!["veni", "vidi", "vici"],
30//!         tx: tx,
31//!     };
32//!     thread::spawn(Closure::new(state, thread));
33//!     
34//!     for (id, msg) in rx {
35//!         println!("Thread {} sent: {}", id, msg);
36//!     }
37//! }
38//!
39//! fn thread(this: &State) {
40//!     for msg in &this.messages {
41//!         this.tx.send((this.id, msg)).unwrap();
42//!     }
43//! }
44//! ```
45
46#[macro_use]
47mod macros;
48
49impl_closures!(ClosureOnce, ClosureMut, Closure, RecClosureOnce, RecClosureMut, RecClosure,);
50impl_closures!(ClosureOnce1, ClosureMut1, Closure1, RecClosureOnce1, RecClosureMut1, RecClosure1, T1,);
51impl_closures!(ClosureOnce2, ClosureMut2, Closure2, RecClosureOnce2, RecClosureMut2, RecClosure2, T1, T2);
52impl_closures!(ClosureOnce3, ClosureMut3, Closure3, RecClosureOnce3, RecClosureMut3, RecClosure3, T1, T2, T3);
53impl_closures!(ClosureOnce4, ClosureMut4, Closure4, RecClosureOnce4, RecClosureMut4, RecClosure4, T1, T2, T3, T4);
54impl_closures!(ClosureOnce5, ClosureMut5, Closure5, RecClosureOnce5, RecClosureMut5, RecClosure5, T1, T2, T3, T4, T5);
55impl_closures!(ClosureOnce6, ClosureMut6, Closure6, RecClosureOnce6, RecClosureMut6, RecClosure6, T1, T2, T3, T4, T5, T6);
56impl_closures!(ClosureOnce7, ClosureMut7, Closure7, RecClosureOnce7, RecClosureMut7, RecClosure7, T1, T2, T3, T4, T5, T6, T7);
57impl_closures!(ClosureOnce8, ClosureMut8, Closure8, RecClosureOnce8, RecClosureMut8, RecClosure8, T1, T2, T3, T4, T5, T6, T7, T8);
58impl_closures!(ClosureOnce9, ClosureMut9, Closure9, RecClosureOnce9, RecClosureMut9, RecClosure9, T1, T2, T3, T4, T5, T6, T7, T8, T9);
59impl_closures!(ClosureOnce10, ClosureMut10, Closure10, RecClosureOnce10, RecClosureMut10, RecClosure10, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
60
61impl<S, R> ::core::iter::Iterator for ClosureMut<S, Option<R>> {
62    type Item = R;
63
64    fn next(&mut self) -> Option<Self::Item> {
65        self()
66    }
67}
68
69impl<S, R> ::core::iter::Iterator for Closure<S, Option<R>> {
70    type Item = R;
71
72    fn next(&mut self) -> Option<Self::Item> {
73        self()
74    }
75}
76
77impl<'a, S, R> ::core::iter::Iterator for &'a Closure<S, Option<R>> {
78    type Item = R;
79
80    fn next(&mut self) -> Option<Self::Item> {
81        self()
82    }
83}