-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Add core::iter::once_with() #57579
Merged
Merged
Add core::iter::once_with() #57579
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -377,6 +377,117 @@ pub fn once<T>(value: T) -> Once<T> { | |
Once { inner: Some(value).into_iter() } | ||
} | ||
|
||
/// An iterator that repeats elements of type `A` endlessly by | ||
/// applying the provided closure `F: FnMut() -> A`. | ||
/// | ||
/// This `struct` is created by the [`once_with`] function. | ||
/// See its documentation for more. | ||
/// | ||
/// [`once_with`]: fn.once_with.html | ||
#[derive(Copy, Clone, Debug)] | ||
#[unstable(feature = "iter_once_with", issue = "57581")] | ||
pub struct OnceWith<F> { | ||
gen: Option<F>, | ||
} | ||
|
||
#[unstable(feature = "iter_once_with", issue = "57581")] | ||
impl<A, F: FnOnce() -> A> Iterator for OnceWith<F> { | ||
type Item = A; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<A> { | ||
self.gen.take().map(|f| f()) | ||
} | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.gen.iter().size_hint() | ||
} | ||
} | ||
|
||
#[unstable(feature = "iter_once_with", issue = "57581")] | ||
impl<A, F: FnOnce() -> A> DoubleEndedIterator for OnceWith<F> { | ||
fn next_back(&mut self) -> Option<A> { | ||
self.next() | ||
} | ||
} | ||
|
||
#[unstable(feature = "iter_once_with", issue = "57581")] | ||
impl<A, F: FnOnce() -> A> ExactSizeIterator for OnceWith<F> { | ||
fn len(&self) -> usize { | ||
self.gen.iter().len() | ||
} | ||
} | ||
|
||
#[unstable(feature = "iter_once_with", issue = "57581")] | ||
impl<A, F: FnOnce() -> A> FusedIterator for OnceWith<F> {} | ||
|
||
#[unstable(feature = "iter_once_with", issue = "57581")] | ||
unsafe impl<A, F: FnOnce() -> A> TrustedLen for OnceWith<F> {} | ||
|
||
/// Creates an iterator that lazily generates a value exactly once by invoking | ||
/// the provided closure. | ||
/// | ||
/// This is commonly used to adapt a single value generator into a [`chain`] of | ||
/// other kinds of iteration. Maybe you have an iterator that covers almost | ||
/// everything, but you need an extra special case. Maybe you have a function | ||
/// which works on iterators, but you only need to process one value. | ||
/// | ||
/// Unlike [`once`], this function will lazily generate the value on request. | ||
/// | ||
/// [`once`]: fn.once.html | ||
/// [`chain`]: trait.Iterator.html#method.chain | ||
/// | ||
/// # Examples | ||
/// | ||
/// Basic usage: | ||
/// | ||
/// ``` | ||
/// #![feature(iter_once_with)] | ||
/// | ||
/// use std::iter; | ||
/// | ||
/// // one is the loneliest number | ||
/// let mut one = iter::once_with(|| 1); | ||
/// | ||
/// assert_eq!(Some(1), one.next()); | ||
/// | ||
/// // just one, that's all we get | ||
/// assert_eq!(None, one.next()); | ||
/// ``` | ||
/// | ||
/// Chaining together with another iterator. Let's say that we want to iterate | ||
/// over each file of the `.foo` directory, but also a configuration file, | ||
/// `.foorc`: | ||
/// | ||
/// ```no_run | ||
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 guess here too? IIRC |
||
/// use std::iter; | ||
/// use std::fs; | ||
/// use std::path::PathBuf; | ||
/// | ||
/// let dirs = fs::read_dir(".foo").unwrap(); | ||
/// | ||
/// // we need to convert from an iterator of DirEntry-s to an iterator of | ||
/// // PathBufs, so we use map | ||
/// let dirs = dirs.map(|file| file.unwrap().path()); | ||
/// | ||
/// // now, our iterator just for our config file | ||
/// let config = iter::once_with(|| PathBuf::from(".foorc")); | ||
/// | ||
/// // chain the two iterators together into one big iterator | ||
/// let files = dirs.chain(config); | ||
/// | ||
/// // this will give us all of the files in .foo as well as .foorc | ||
/// for f in files { | ||
/// println!("{:?}", f); | ||
/// } | ||
/// ``` | ||
#[inline] | ||
#[unstable(feature = "iter_once_with", issue = "57581")] | ||
pub fn once_with<A, F: FnOnce() -> A>(gen: F) -> OnceWith<F> { | ||
OnceWith { gen: Some(gen) } | ||
} | ||
|
||
/// Creates a new iterator where each iteration calls the provided closure | ||
/// `F: FnMut(&mut St) -> Option<T>`. | ||
/// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It seems like his comment was copy pasted from
RepeatWith
.It talks about endless repeating and
FnMut()
.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.
@kumbayo could you file a PR to fix this?