]> git.lizzy.rs Git - rust.git/commitdiff
Implement RFC 771: std::iter::once
authorNathaniel Theis <nttheis@gmail.com>
Tue, 26 May 2015 23:39:18 +0000 (16:39 -0700)
committerNathaniel Theis <nttheis@gmail.com>
Fri, 29 May 2015 19:02:13 +0000 (12:02 -0700)
src/libcore/iter.rs
src/libcoretest/iter.rs
src/libcoretest/lib.rs

index 74f3eccab6830eabc3c9e83680b08c630ca00b70..9337c501a3a68d71953e4d3212427a0c3a4e1a86 100644 (file)
@@ -3030,6 +3030,100 @@ pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
     Repeat{element: elt}
 }
 
+/// An iterator that yields nothing.
+#[unstable(feature="iter_empty", reason = "new addition")]
+pub struct Empty<T>(marker::PhantomData<T>);
+
+#[unstable(feature="iter_empty", reason = "new addition")]
+impl<T> Iterator for Empty<T> {
+    type Item = T;
+
+    fn next(&mut self) -> Option<T> {
+        None
+    }
+
+    fn size_hint(&self) -> (usize, Option<usize>){
+        (0, Some(0))
+    }
+}
+
+#[unstable(feature="iter_empty", reason = "new addition")]
+impl<T> DoubleEndedIterator for Empty<T> {
+    fn next_back(&mut self) -> Option<T> {
+        None
+    }
+}
+
+#[unstable(feature="iter_empty", reason = "new addition")]
+impl<T> ExactSizeIterator for Empty<T> {
+    fn len(&self) -> usize {
+        0
+    }
+}
+
+// not #[derive] because that adds a Clone bound on T,
+// which isn't necessary.
+#[unstable(feature="iter_empty", reason = "new addition")]
+impl<T> Clone for Empty<T> {
+    fn clone(&self) -> Empty<T> {
+        Empty(marker::PhantomData)
+    }
+}
+
+// not #[derive] because that adds a Default bound on T,
+// which isn't necessary.
+#[unstable(feature="iter_empty", reason = "new addition")]
+impl<T> Default for Empty<T> {
+    fn default() -> Empty<T> {
+        Empty(marker::PhantomData)
+    }
+}
+
+/// Creates an iterator that yields nothing.
+#[unstable(feature="iter_empty", reason = "new addition")]
+pub fn empty<T>() -> Empty<T> {
+    Empty(marker::PhantomData)
+}
+
+/// An iterator that yields an element exactly once.
+#[unstable(feature="iter_once", reason = "new addition")]
+pub struct Once<T> {
+    inner: ::option::IntoIter<T>
+}
+
+#[unstable(feature="iter_once", reason = "new addition")]
+impl<T> Iterator for Once<T> {
+    type Item = T;
+
+    fn next(&mut self) -> Option<T> {
+        self.inner.next()
+    }
+
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        self.inner.size_hint()
+    }
+}
+
+#[unstable(feature="iter_once", reason = "new addition")]
+impl<T> DoubleEndedIterator for Once<T> {
+    fn next_back(&mut self) -> Option<T> {
+        self.inner.next_back()
+    }
+}
+
+#[unstable(feature="iter_once", reason = "new addition")]
+impl<T> ExactSizeIterator for Once<T> {
+    fn len(&self) -> usize {
+        self.inner.len()
+    }
+}
+
+/// Creates an iterator that yields an element exactly once.
+#[unstable(feature="iter_once", reason = "new addition")]
+pub fn once<T>(value: T) -> Once<T> {
+    Once { inner: Some(value).into_iter() }
+}
+
 /// Functions for lexicographical ordering of sequences.
 ///
 /// Lexicographical ordering through `<`, `<=`, `>=`, `>` requires
index 0415c75aa5204cb6eaa43dd6f37296f5dd0a63de..8e817bcbc2a73455def5d3e8baa5de401254f5d6 100644 (file)
@@ -1096,6 +1096,19 @@ fn test_fuse_count() {
     // Can't check len now because count consumes.
 }
 
+#[test]
+fn test_once() {
+    let mut it = once(42);
+    assert_eq!(it.next(), Some(42));
+    assert_eq!(it.next(), None);
+}
+
+#[test]
+fn test_empty() {
+    let mut it = empty::<i32>();
+    assert_eq!(it.next(), None);
+}
+
 #[bench]
 fn bench_rposition(b: &mut Bencher) {
     let it: Vec<usize> = (0..300).collect();
index 3d14b3f3c810631ed0cf49d1807e5e3b2a20784a..970ae11e7491ee52f31359c9e94ce6bb143093ef 100644 (file)
@@ -25,6 +25,8 @@
 #![feature(slice_patterns)]
 #![feature(float_from_str_radix)]
 #![feature(cell_extras)]
+#![feature(iter_empty)]
+#![feature(iter_once)]
 
 extern crate core;
 extern crate test;