-
Notifications
You must be signed in to change notification settings - Fork 550
/
Copy pathcollect.cairo
105 lines (103 loc) · 2.84 KB
/
collect.cairo
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
/// Conversion into an [`Iterator`].
///
/// By implementing `IntoIterator` for a type, you define how it will be
/// converted to an iterator. This is common for types which describe a
/// collection of some kind.
///
/// One benefit of implementing `IntoIterator` is that your type will [work
/// with Cairo's `for` loop syntax](crate::iter#for-loops-and-intoiterator).
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let mut iter = array![1, 2, 3].into_iter();
///
/// assert_eq!(Option::Some(1), iter.next());
/// assert_eq!(Option::Some(2), iter.next());
/// assert_eq!(Option::Some(3), iter.next());
/// assert_eq!(Option::None, iter.next());
/// ```
/// Implementing `IntoIterator` for your type:
///
/// ```
/// // A sample collection, that's just a wrapper over Array<u32>
/// #[derive(Drop, Debug)]
/// struct MyCollection {
/// arr: Array<u32>
/// }
///
/// // Let's give it some methods so we can create one and add things
/// // to it.
/// #[generate_trait]
/// impl MyCollectionImpl of MyCollectionTrait {
/// fn new() -> MyCollection {
/// MyCollection {
/// arr: ArrayTrait::new()
/// }
/// }
///
/// fn add(ref self: MyCollection, elem: u32) {
/// self.arr.append(elem);
/// }
/// }
///
/// // and we'll implement IntoIterator
/// impl MyCollectionIntoIterator of IntoIterator<MyCollection> {
/// type IntoIter = crate::array::ArrayIter<u32>;
/// fn into_iter(self: MyCollection) -> Self::IntoIter {
/// self.arr.into_iter()
/// }
/// }
///
/// // Now we can make a new collection...
/// let mut c = MyCollectionTrait::new();
///
/// // ... add some stuff to it ...
/// c.add(0);
/// c.add(1);
/// c.add(2);
///
/// // ... and then turn it into an Iterator:
/// let mut n = 0;
/// for i in c {
/// assert_eq!(i, n);
/// n += 1;
/// };
/// ```
pub trait IntoIterator<T> {
/// The iterator type that will be created.
type IntoIter;
impl Iterator: Iterator<Self::IntoIter>;
/// Creates an iterator from a value.
///
/// See the [module-level documentation] for more.
///
/// [module-level documentation]: crate::iter
///
/// # Examples
///
/// ```
/// let mut iter = array![1, 2, 3].into_iter();
///
/// assert_eq!(Option::Some(1), iter.next());
/// assert_eq!(Option::Some(2), iter.next());
/// assert_eq!(Option::Some(3), iter.next());
/// assert_eq!(Option::None, iter.next());
/// ```
fn into_iter(self: T) -> Self::IntoIter;
}
impl IteratorIntoIterator<T, +Iterator<T>> of IntoIterator<T> {
type IntoIter = T;
fn into_iter(self: T) -> T {
self
}
}
impl SnapshotIteratorSpanBased<C, T, +Into<@C, Span<T>>> of IntoIterator<@C> {
type IntoIter = crate::array::SpanIter<T>;
fn into_iter(self: @C) -> Self::IntoIter {
let span: Span<T> = self.into();
span.into_iter()
}
}