-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement std::iter::once/empty (RFC 771) #25817
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd do that if I used it twice, but adding an import to a 3000+ line file to save two colons on one line seemed excessive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's unusual to use a global path, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively just store Option and return self.inner.take() on next? |
||
} | ||
|
||
#[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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add another assert_eq to check that the iterators are fused? And above for once. |
||
} | ||
|
||
#[bench] | ||
fn bench_rposition(b: &mut Bencher) { | ||
let it: Vec<usize> = (0..300).collect(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess a
use option
at the beginning would be nicer than the global path here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
::option::IntoIter<T>
isn't really necessary. You could just useOption
+take()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend using
IntoIter
(or some other iterator adapator) as it allows proxying thenext
,next_back
, andsize_hint
functions (primarilysize_hint
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's three levels of indirection (IntoIter -> Item -> Option) but I guess they'll just get optimized away anyways.