diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 74f3eccab68..9337c501a3a 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -3030,6 +3030,100 @@ pub fn repeat(elt: T) -> Repeat { Repeat{element: elt} } +/// An iterator that yields nothing. +#[unstable(feature="iter_empty", reason = "new addition")] +pub struct Empty(marker::PhantomData); + +#[unstable(feature="iter_empty", reason = "new addition")] +impl Iterator for Empty { + type Item = T; + + fn next(&mut self) -> Option { + None + } + + fn size_hint(&self) -> (usize, Option){ + (0, Some(0)) + } +} + +#[unstable(feature="iter_empty", reason = "new addition")] +impl DoubleEndedIterator for Empty { + fn next_back(&mut self) -> Option { + None + } +} + +#[unstable(feature="iter_empty", reason = "new addition")] +impl ExactSizeIterator for Empty { + 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 Clone for Empty { + fn clone(&self) -> Empty { + 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 Default for Empty { + fn default() -> Empty { + Empty(marker::PhantomData) + } +} + +/// Creates an iterator that yields nothing. +#[unstable(feature="iter_empty", reason = "new addition")] +pub fn empty() -> Empty { + Empty(marker::PhantomData) +} + +/// An iterator that yields an element exactly once. +#[unstable(feature="iter_once", reason = "new addition")] +pub struct Once { + inner: ::option::IntoIter +} + +#[unstable(feature="iter_once", reason = "new addition")] +impl Iterator for Once { + type Item = T; + + fn next(&mut self) -> Option { + self.inner.next() + } + + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } +} + +#[unstable(feature="iter_once", reason = "new addition")] +impl DoubleEndedIterator for Once { + fn next_back(&mut self) -> Option { + self.inner.next_back() + } +} + +#[unstable(feature="iter_once", reason = "new addition")] +impl ExactSizeIterator for Once { + 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(value: T) -> Once { + Once { inner: Some(value).into_iter() } +} + /// Functions for lexicographical ordering of sequences. /// /// Lexicographical ordering through `<`, `<=`, `>=`, `>` requires diff --git a/src/libcoretest/iter.rs b/src/libcoretest/iter.rs index 0415c75aa52..8e817bcbc2a 100644 --- a/src/libcoretest/iter.rs +++ b/src/libcoretest/iter.rs @@ -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::(); + assert_eq!(it.next(), None); +} + #[bench] fn bench_rposition(b: &mut Bencher) { let it: Vec = (0..300).collect(); diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs index 3d14b3f3c81..970ae11e749 100644 --- a/src/libcoretest/lib.rs +++ b/src/libcoretest/lib.rs @@ -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;