-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiterator.rs
More file actions
95 lines (75 loc) · 2.33 KB
/
Copy pathiterator.rs
File metadata and controls
95 lines (75 loc) · 2.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
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 std::any::Any;
use std::fmt;
use std::sync::{Arc, RwLock};
use once_cell::sync::Lazy;
use super::gen;
use super::new;
use super::base::{ObjectRef, ObjectTrait, TypeRef, TypeTrait};
use super::class::TYPE_TYPE;
use super::ns::Namespace;
// IteratorType Type ---------------------------------------------------
gen::type_and_impls!(IteratorType, Iterator);
pub static ITERATOR_TYPE: Lazy<gen::obj_ref_t!(IteratorType)> = Lazy::new(|| {
let type_ref = gen::obj_ref!(IteratorType::new());
let mut type_obj = type_ref.write().unwrap();
type_obj.add_attrs(&[
// Instance Methods --------------------------------------------
gen::meth!("next", type_ref, &[], "", |this, _, _| {
let mut this = this.write().unwrap();
let this = this.down_to_iterator_mut().unwrap();
Ok(this.next())
}),
gen::meth!("peek", type_ref, &[], "", |this, _, _| {
let this = this.write().unwrap();
let this = this.down_to_iterator().unwrap();
Ok(this.peek())
}),
]);
type_ref.clone()
});
// Iterator Object -----------------------------------------------------
pub struct FIIterator {
ns: Namespace,
wrapped: Vec<ObjectRef>,
current: usize,
}
gen::standard_object_impls!(FIIterator);
impl FIIterator {
pub fn new(wrapped: Vec<ObjectRef>) -> Self {
Self { ns: Namespace::default(), wrapped, current: 0 }
}
fn next(&mut self) -> ObjectRef {
let obj = self.get_or_nil(self.current);
if self.current < self.len() {
self.current += 1;
}
obj
}
fn peek(&self) -> ObjectRef {
self.get_or_nil(self.current)
}
fn len(&self) -> usize {
self.wrapped.len()
}
fn get_or_nil(&self, index: usize) -> ObjectRef {
if index >= self.len() {
new::nil()
} else {
self.wrapped[index].clone()
}
}
}
impl ObjectTrait for FIIterator {
gen::object_trait_header!(ITERATOR_TYPE);
}
// Display -------------------------------------------------------------
impl fmt::Display for FIIterator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<iterator>")
}
}
impl fmt::Debug for FIIterator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}