Skip to content

Commit 50c9728

Browse files
committed
---
yaml --- r: 149613 b: refs/heads/try2 c: 52524bf h: refs/heads/master i: 149611: dc16dae v: v3
1 parent 7d8a166 commit 50c9728

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 2b362768ff3cab2c966f1f18cf119b21fc96ea30
8+
refs/heads/try2: 52524bfe880f3722d8d70e4433429c1b4a3f31d3
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcollections/list.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,42 @@ pub enum List<T> {
1717
Nil,
1818
}
1919

20+
pub struct Items<'a, T> {
21+
priv head: &'a List<T>,
22+
priv next: Option<&'a @List<T>>
23+
}
24+
25+
impl<'a, T> Iterator<&'a T> for Items<'a, T> {
26+
fn next(&mut self) -> Option<&'a T> {
27+
match self.next {
28+
None => match *self.head {
29+
Nil => None,
30+
Cons(ref value, ref tail) => {
31+
self.next = Some(tail);
32+
Some(value)
33+
}
34+
},
35+
Some(next) => match **next {
36+
Nil => None,
37+
Cons(ref value, ref tail) => {
38+
self.next = Some(tail);
39+
Some(value)
40+
}
41+
}
42+
}
43+
}
44+
}
45+
46+
impl<T> List<T> {
47+
/// Returns a forward iterator
48+
pub fn iter<'a>(&'a self) -> Items<'a, T> {
49+
Items {
50+
head: self,
51+
next: None
52+
}
53+
}
54+
}
55+
2056
/**
2157
* Left fold
2258
*
@@ -181,6 +217,16 @@ mod tests {
181217

182218
use std::option;
183219

220+
#[test]
221+
fn test_iter() {
222+
let list = List::from_vec([0, 1, 2]);
223+
let mut iter = list.iter();
224+
assert_eq!(&0, iter.next().unwrap());
225+
assert_eq!(&1, iter.next().unwrap());
226+
assert_eq!(&2, iter.next().unwrap());
227+
assert_eq!(None, iter.next());
228+
}
229+
184230
#[test]
185231
fn test_is_empty() {
186232
let empty : @list::List<int> = @List::from_vec([]);

0 commit comments

Comments
 (0)