Skip to content
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 into_value for ControlFlow #474

Closed
carbotaniuman opened this issue Nov 4, 2024 · 3 comments
Closed

Add into_value for ControlFlow #474

carbotaniuman opened this issue Nov 4, 2024 · 3 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@carbotaniuman
Copy link

Proposal

Problem statement

When using ControlFlow as a kind of early exit, and not to represent a distinct state, getting the final value out of the resulting ControlFlow requires a cumbersome match.

Motivating examples or use cases

let ret = slice.iter().try_fold(0, |prev, val: i32| {
    let next = expensive_computation(val);
    let ret = combine(prev, next);
    if ret.is_nan() {
        ControlFlow::Break(ret)
    } else {
        ControlFlow::Continue(ret)
    }
});

match ret {
      ControlFlow::Continue(t) => t,
      ControlFlow::Break(t) => t,
}

Solution sketch

impl<T> ControlFlow<T, T> {
    pub fn into_value(self) -> T {
        match self {
            ControlFlow::Continue(value) | ControlFlow::Break(value) => value,
        }
    }
}

Alternatives

Just write the match manually every time.

Links and related work

Mentioned in rust-lang/rust#75744 (comment)

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.
@carbotaniuman carbotaniuman added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Nov 4, 2024
@programmerjake
Copy link
Member

is let (ControlFlow::Break(v) | ControlFlow::Continue(v)) = ... not good enough? there is no equivalent on Result or any other type implementing Try afaik

@scottmcm
Copy link
Member

scottmcm commented Nov 5, 2024

@programmerjake well it doesn't really make sense on Option, and it's intentionally not provided on Result because of the strongly differing intent between the two variants there. This was rejected on Result (IIRC) -- which has map and map_err because of the differing intent, and Result<Error, i32> is a code smell because of that differing intent -- but ControlFlow doesn't prioritize either variant, so I think it's plausible here.

(Not quite as obvious as Either::into_inner where there's no meaningful difference between the variants, but still at least plausible enough to be worth an ACP if people would find it useful.)

@joshtriplett
Copy link
Member

We talked about this in today's libs-api meeting. Nobody felt strongly about this one way or another, but the use case of handling NaN in-band was sufficiently motivating that we were inclined to say "sure, let's see this on nightly, we can see how many people say on the tracking issue that they want this".

@joshtriplett joshtriplett added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

4 participants