Skip to content

Commit 103e79d

Browse files
committed
Implement RFC 771: std::iter::once
1 parent 2d447e4 commit 103e79d

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/libcore/iter.rs

+94
Original file line numberDiff line numberDiff line change
@@ -3030,6 +3030,100 @@ pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
30303030
Repeat{element: elt}
30313031
}
30323032

3033+
/// An iterator that yields nothing.
3034+
#[unstable(feature="iter_empty", reason = "new addition")]
3035+
pub struct Empty<T>(marker::PhantomData<T>);
3036+
3037+
#[unstable(feature="iter_empty", reason = "new addition")]
3038+
impl<T> Iterator for Empty<T> {
3039+
type Item = T;
3040+
3041+
fn next(&mut self) -> Option<T> {
3042+
None
3043+
}
3044+
3045+
fn size_hint(&self) -> (usize, Option<usize>){
3046+
(0, Some(0))
3047+
}
3048+
}
3049+
3050+
#[unstable(feature="iter_empty", reason = "new addition")]
3051+
impl<T> DoubleEndedIterator for Empty<T> {
3052+
fn next_back(&mut self) -> Option<T> {
3053+
None
3054+
}
3055+
}
3056+
3057+
#[unstable(feature="iter_empty", reason = "new addition")]
3058+
impl<T> ExactSizeIterator for Empty<T> {
3059+
fn len(&self) -> usize {
3060+
0
3061+
}
3062+
}
3063+
3064+
// not #[derive] because that adds a Clone bound on T,
3065+
// which isn't necessary.
3066+
#[unstable(feature="iter_empty", reason = "new addition")]
3067+
impl<T> Clone for Empty<T> {
3068+
fn clone(&self) -> Empty<T> {
3069+
Empty(marker::PhantomData)
3070+
}
3071+
}
3072+
3073+
// not #[derive] because that adds a Default bound on T,
3074+
// which isn't necessary.
3075+
#[unstable(feature="iter_empty", reason = "new addition")]
3076+
impl<T> Default for Empty<T> {
3077+
fn default() -> Empty<T> {
3078+
Empty(marker::PhantomData)
3079+
}
3080+
}
3081+
3082+
/// Creates an iterator that yields nothing.
3083+
#[unstable(feature="iter_empty", reason = "new addition")]
3084+
pub fn empty<T>() -> Empty<T> {
3085+
Empty(marker::PhantomData)
3086+
}
3087+
3088+
/// An iterator that yields an element exactly once.
3089+
#[unstable(feature="iter_once", reason = "new addition")]
3090+
pub struct Once<T> {
3091+
inner: ::option::IntoIter<T>
3092+
}
3093+
3094+
#[unstable(feature="iter_once", reason = "new addition")]
3095+
impl<T> Iterator for Once<T> {
3096+
type Item = T;
3097+
3098+
fn next(&mut self) -> Option<T> {
3099+
self.inner.next()
3100+
}
3101+
3102+
fn size_hint(&self) -> (usize, Option<usize>) {
3103+
self.inner.size_hint()
3104+
}
3105+
}
3106+
3107+
#[unstable(feature="iter_once", reason = "new addition")]
3108+
impl<T> DoubleEndedIterator for Once<T> {
3109+
fn next_back(&mut self) -> Option<T> {
3110+
self.inner.next_back()
3111+
}
3112+
}
3113+
3114+
#[unstable(feature="iter_once", reason = "new addition")]
3115+
impl<T> ExactSizeIterator for Once<T> {
3116+
fn len(&self) -> usize {
3117+
self.inner.len()
3118+
}
3119+
}
3120+
3121+
/// Creates an iterator that yields an element exactly once.
3122+
#[unstable(feature="iter_once", reason = "new addition")]
3123+
pub fn once<T>(value: T) -> Once<T> {
3124+
Once { inner: Some(value).into_iter() }
3125+
}
3126+
30333127
/// Functions for lexicographical ordering of sequences.
30343128
///
30353129
/// Lexicographical ordering through `<`, `<=`, `>=`, `>` requires

src/libcoretest/iter.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,19 @@ fn test_fuse_count() {
10961096
// Can't check len now because count consumes.
10971097
}
10981098

1099+
#[test]
1100+
fn test_once() {
1101+
let mut it = once(42);
1102+
assert_eq!(it.next(), Some(42));
1103+
assert_eq!(it.next(), None);
1104+
}
1105+
1106+
#[test]
1107+
fn test_empty() {
1108+
let mut it = empty::<i32>();
1109+
assert_eq!(it.next(), None);
1110+
}
1111+
10991112
#[bench]
11001113
fn bench_rposition(b: &mut Bencher) {
11011114
let it: Vec<usize> = (0..300).collect();

src/libcoretest/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#![feature(slice_patterns)]
2626
#![feature(float_from_str_radix)]
2727
#![feature(cell_extras)]
28+
#![feature(iter_empty)]
29+
#![feature(iter_once)]
2830

2931
extern crate core;
3032
extern crate test;

0 commit comments

Comments
 (0)