Skip to content

Commit 61e92b5

Browse files
committed
Rename iter::unfold to iter::from_fn and remove explicit state
This API is unstable. CC #55977 (comment)
1 parent 741a3d4 commit 61e92b5

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

src/libcore/iter/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub use self::sources::{Once, once};
327327
#[unstable(feature = "iter_once_with", issue = "57581")]
328328
pub use self::sources::{OnceWith, once_with};
329329
#[unstable(feature = "iter_unfold", issue = "55977")]
330-
pub use self::sources::{Unfold, unfold, Successors, successors};
330+
pub use self::sources::{FromFn, from_fn, Successors, successors};
331331

332332
#[stable(feature = "rust1", since = "1.0.0")]
333333
pub use self::traits::{FromIterator, IntoIterator, DoubleEndedIterator, Extend};

src/libcore/iter/sources.rs

+24-31
Original file line numberDiff line numberDiff line change
@@ -491,24 +491,24 @@ pub fn once_with<A, F: FnOnce() -> A>(gen: F) -> OnceWith<F> {
491491
}
492492

493493
/// Creates a new iterator where each iteration calls the provided closure
494-
/// `F: FnMut(&mut St) -> Option<T>`.
494+
/// `F: FnMut() -> Option<T>`.
495495
///
496496
/// This allows creating a custom iterator with any behavior
497497
/// without using the more verbose syntax of creating a dedicated type
498498
/// and implementing the `Iterator` trait for it.
499499
///
500-
/// In addition to its captures and environment,
501-
/// the closure is given a mutable reference to some state
502-
/// that is preserved across iterations.
503-
/// That state starts as the given `initial_state` value.
504-
///
505-
/// Note that the `Unfold` iterator doesn’t make assumptions about the behavior of the closure,
500+
/// Note that the `FromFn` iterator doesn’t make assumptions about the behavior of the closure,
506501
/// and therefore conservatively does not implement [`FusedIterator`],
507502
/// or override [`Iterator::size_hint`] from its default `(0, None)`.
508503
///
509504
/// [`FusedIterator`]: trait.FusedIterator.html
510505
/// [`Iterator::size_hint`]: trait.Iterator.html#method.size_hint
511506
///
507+
/// The closure can use its its captures and environment
508+
/// to track state across iterations.
509+
/// Depending on how the iterator is used,
510+
/// this may require specifying the `move` keyword on the closure.
511+
///
512512
/// # Examples
513513
///
514514
/// Let’s re-implement the counter iterator from [module-level documentation]:
@@ -517,13 +517,14 @@ pub fn once_with<A, F: FnOnce() -> A>(gen: F) -> OnceWith<F> {
517517
///
518518
/// ```
519519
/// #![feature(iter_unfold)]
520-
/// let counter = std::iter::unfold(0, |count| {
520+
/// let mut count = 0;
521+
/// let counter = std::iter::from_fn(move || {
521522
/// // Increment our count. This is why we started at zero.
522-
/// *count += 1;
523+
/// count += 1;
523524
///
524525
/// // Check to see if we've finished counting or not.
525-
/// if *count < 6 {
526-
/// Some(*count)
526+
/// if count < 6 {
527+
/// Some(count)
527528
/// } else {
528529
/// None
529530
/// }
@@ -532,46 +533,38 @@ pub fn once_with<A, F: FnOnce() -> A>(gen: F) -> OnceWith<F> {
532533
/// ```
533534
#[inline]
534535
#[unstable(feature = "iter_unfold", issue = "55977")]
535-
pub fn unfold<St, T, F>(initial_state: St, f: F) -> Unfold<St, F>
536-
where F: FnMut(&mut St) -> Option<T>
536+
pub fn from_fn<T, F>(f: F) -> FromFn<F>
537+
where F: FnMut() -> Option<T>
537538
{
538-
Unfold {
539-
state: initial_state,
540-
f,
541-
}
539+
FromFn(f)
542540
}
543541

544-
/// An iterator where each iteration calls the provided closure `F: FnMut(&mut St) -> Option<T>`.
542+
/// An iterator where each iteration calls the provided closure `F: FnMut() -> Option<T>`.
545543
///
546-
/// This `struct` is created by the [`unfold`] function.
544+
/// This `struct` is created by the [`iter::from_fn`] function.
547545
/// See its documentation for more.
548546
///
549-
/// [`unfold`]: fn.unfold.html
547+
/// [`iter::from_fn`]: fn.from_fn.html
550548
#[derive(Clone)]
551549
#[unstable(feature = "iter_unfold", issue = "55977")]
552-
pub struct Unfold<St, F> {
553-
state: St,
554-
f: F,
555-
}
550+
pub struct FromFn<F>(F);
556551

557552
#[unstable(feature = "iter_unfold", issue = "55977")]
558-
impl<St, T, F> Iterator for Unfold<St, F>
559-
where F: FnMut(&mut St) -> Option<T>
553+
impl<T, F> Iterator for FromFn<F>
554+
where F: FnMut() -> Option<T>
560555
{
561556
type Item = T;
562557

563558
#[inline]
564559
fn next(&mut self) -> Option<Self::Item> {
565-
(self.f)(&mut self.state)
560+
(self.0)()
566561
}
567562
}
568563

569564
#[unstable(feature = "iter_unfold", issue = "55977")]
570-
impl<St: fmt::Debug, F> fmt::Debug for Unfold<St, F> {
565+
impl<F> fmt::Debug for FromFn<F> {
571566
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
572-
f.debug_struct("Unfold")
573-
.field("state", &self.state)
574-
.finish()
567+
f.debug_struct("FromFn").finish()
575568
}
576569
}
577570

0 commit comments

Comments
 (0)