|
|
@@ -52,6 +52,24 @@ impl<T> Drop for List<T> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/// Tuple structs are an alternative form of struct,
|
|
|
+/// useful for trivial wrappers around other types.
|
|
|
+pub struct IntoIter<T>(List<T>);
|
|
|
+impl<T> List<T> {
|
|
|
+ pub fn into_iter(self) -> IntoIter<T> {
|
|
|
+ IntoIter(self)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<T> Iterator for IntoIter<T> {
|
|
|
+ type Item = T;
|
|
|
+
|
|
|
+ fn next(&mut self) -> Option<Self::Item> {
|
|
|
+ // access fields of a tuple struct numerically
|
|
|
+ self.0.pop()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
#[cfg(test)]
|
|
|
mod test {
|
|
|
use super::List;
|
|
|
@@ -102,4 +120,18 @@ mod test {
|
|
|
assert_eq!(list.peek(), Some(&42));
|
|
|
assert_eq!(list.pop(), Some(42));
|
|
|
}
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn into_iter() {
|
|
|
+ let mut list = List::new();
|
|
|
+ list.push(1);
|
|
|
+ list.push(2);
|
|
|
+ list.push(3);
|
|
|
+
|
|
|
+ let mut iter = list.into_iter();
|
|
|
+ assert_eq!(iter.next(), Some(3));
|
|
|
+ assert_eq!(iter.next(), Some(2));
|
|
|
+ assert_eq!(iter.next(), Some(1));
|
|
|
+ assert_eq!(iter.next(), None);
|
|
|
+ }
|
|
|
}
|