diff options
| author | bors <bors@rust-lang.org> | 2015-05-30 01:12:35 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-05-30 01:12:35 +0000 |
| commit | a0f028da07d313ad554623d5700dcbd16f3a4bde (patch) | |
| tree | 23f1b4e4dd08de86ec4b99533244f5fa536355a5 /src | |
| parent | b77d60adb019bb5de05e884a99f3290ec4694137 (diff) | |
| parent | 103e79d26a60ef87df304799e8032c6b54698973 (diff) | |
| download | rust-a0f028da07d313ad554623d5700dcbd16f3a4bde.tar.gz rust-a0f028da07d313ad554623d5700dcbd16f3a4bde.zip | |
Auto merge of #25817 - XMPPwocky:once_cleanedup, r=alexcrichton
Closes #24443.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/iter.rs | 94 | ||||
| -rw-r--r-- | src/libcoretest/iter.rs | 13 | ||||
| -rw-r--r-- | src/libcoretest/lib.rs | 2 |
3 files changed, 109 insertions, 0 deletions
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<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 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::<i32>(); + assert_eq!(it.next(), None); +} + #[bench] fn bench_rposition(b: &mut Bencher) { let it: Vec<usize> = (0..300).collect(); diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs index 6ec3f5b7869..deef26022f7 100644 --- a/src/libcoretest/lib.rs +++ b/src/libcoretest/lib.rs @@ -26,6 +26,8 @@ #![feature(slice_patterns)] #![feature(float_from_str_radix)] #![feature(cell_extras)] +#![feature(iter_empty)] +#![feature(iter_once)] extern crate core; extern crate test; |
