Skip to content

Add Iterator::as_rev #385

Closed as not planned
Closed as not planned
@scottmcm

Description

@scottmcm

Proposal

Problem statement & motivating example

.rev() is a great iterator adapter, and combines wonderfully with everything taking self.

However, for calling iterator methods that take &mut self instead, it's less good. That leads to questions about whether there should be an rfind_map (rust-itertools/itertools#949), since if you need to call it multiple times, .by_ref().rev().find_map is a pain to type and might even be less efficient.

Solution sketch

Add the following method to Iterator:

impl Iterator {
    fn as_rev(&mut self) where Self: Sized -> &mut Rev<Self>;
}

That does mean that, from an implementation perspective, Rev needs to be repr(transparent). But I think that's fine; it's already a newtype today and it has no reason to ever keep state.

Alternatives

  • Do nothing. People can make the Rev once earlier, or use by_ref().rev(), which we can continue to work on optimizations for to make it work well.
  • Add more things like rfind_map so people don't ask about them. But probably never things like rall or rany, which seem kinda silly to me.

Links and related work

original find_map motivation, though it doesn't discuss the possibility of a reverse version: rust-lang/rust#49098

Example where someone used .by_ref().rev() instead: https://github.com/rust-itertools/itertools/pull/950/files#diff-2d3a79a5da014fbfcea9fc628ad9019242ea09d455f4df3cffb94bb90954cdd1R1051

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions