forked from oconnor663/jacko.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonic.rs
More file actions
61 lines (51 loc) · 1.33 KB
/
pythonic.rs
File metadata and controls
61 lines (51 loc) · 1.33 KB
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
use std::rc::Rc;
use std::cell::RefCell;
use std::iter::{Iterator, IntoIterator};
// Look at this Python-style iteration. I can mutate a list while I'm
// iterating over it! How does this work? The push() method takes a
// *shared* reference, and everything is reference counted.
fn main() {
let mylist = PythonicList::new();
mylist.push(1);
mylist.push(2);
mylist.push(3);
for i in &mylist {
println!("{}", i);
if *i == 2 {
mylist.push(4);
}
}
}
struct PythonicList<T> {
vec: Rc<RefCell<Vec<Rc<T>>>>,
}
impl<T> PythonicList<T> {
fn new() -> PythonicList<T> {
PythonicList { vec: Rc::new(RefCell::new(Vec::new())) }
}
fn push(&self, item: T) {
self.vec.borrow_mut().push(Rc::new(item));
}
}
impl<'a, T> IntoIterator for &'a PythonicList<T> {
type Item = Rc<T>;
type IntoIter = PythonicIterator<T>;
fn into_iter(self) -> PythonicIterator<T> {
PythonicIterator {
vec: self.vec.clone(),
index: 0,
}
}
}
struct PythonicIterator<T> {
vec: Rc<RefCell<Vec<Rc<T>>>>,
index: usize,
}
impl<T> Iterator for PythonicIterator<T> {
type Item = Rc<T>;
fn next(&mut self) -> Option<Rc<T>> {
let ret = self.vec.borrow().get(self.index).cloned();
self.index += 1;
ret
}
}