Skip to content

Tracking issue for std::iter::from_fn #55977

Closed
@SimonSapin

Description

@SimonSapin

Update: API modified in #58062:

pub fn from_fn<T, F>(f: F) -> FromFn<F> where F: FnMut() -> Option<T> {}

#[derive(Clone)] pub struct FromFn<F> {}

impl<T, F> Iterator for FromFn<F> where F: FnMut() -> Option<T> {
    type Item = T;
}

impl<St: fmt::Debug, F> fmt::Debug for FromFn<F> {}

This API is being proposed in #55869:

pub fn unfold<St, T, F>(initial_state: St, f: F) -> Unfold<St, F>
    where F: FnMut(&mut St) -> Option<T> {}

#[derive(Clone)] pub struct Unfold<St, F> {}

impl<St, T, F> Iterator for Unfold<St, F> where F: FnMut(&mut St) -> Option<T> {
    type Item = T;
}

impl<St: fmt::Debug, F> fmt::Debug for Unfold<St, F> {}

unfold was previously in the standard library but was deprecated and then removed in Rust 1.3 for not having “gained enough traction to retain its position in the standard library”. It is now available in the itertools crate.

My personal opinion is that we’ve been very conservative with inclusion in std in the months before and after 1.0 but we’ve slowing been relaxing the criteria, with in increasing number of small convenience functions and methods. Both unfold and successors feel general-purpose and fundamental enough to me to belong in the standard library.

Unresolved questions:

  • Should the state field of Unfold be public?

  • Should unfold have explicit state for consistency with fold, or should it be a more trivial bridging of closures to iterators with state kept in the closure’s environment or captures?

    impl<T, F> Iterator for Unfold<F> where F: FnMut() -> Option<T> {
        type Item = T;
        fn next(&mut self) -> Option<T> { (self.0)() }
    }

Update: moved to Moved to #58045:

pub fn successors<T, F>(first: Option<T>, succ: F) -> Successors<T, F>
    where F: FnMut(&T) -> Option<T> {}

#[derive(Clone)] pub struct Successors<T, F> {}

impl<T, F> Iterator for Successors<T, F> where F: FnMut(&T) -> Option<T> {
    type Item = T;
}

impl<T, F> FusedIterator for Successors<T, F> where F: FnMut(&T) -> Option<T> {}

impl<T: fmt::Debug, F> fmt::Debug for Successors<T, F> {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-iteratorsArea: IteratorsB-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions