Skip to content

Tracking issue for Result::into_ok #61695

Closed

Description

@phaylon

View all comments

This is (now) a tracking issue for
https://doc.rust-lang.org/nightly/std/result/enum.Result.html#method.into_ok
https://doc.rust-lang.org/nightly/std/result/enum.Result.html#method.into_err
#![feature(unwrap_infallible)]


I would like to propose adding a unwrap_infallible associated function to core::result::Result. The purpose is to convert Result<T, core::convert::Infallible> to a T, as the error case is impossible.

The implementation would basically be:

impl<T> Result<T, Infallible> {
    #[inline]
    pub fn unwrap_infallible(self) -> T {
        match self {
            Ok(value) => value,
        }
    }
}

An example use-case I have is a wrapper type with generic value verification, like Handle<T:Verify>. Some verifications can fail, but some can not. When verification is infallible, a Result<Handle<T>, Infallible> will be returned.

Since some can fail, the Handle type implements TryFrom and not From. Because of the blanket implementation of TryFrom for all types implementing From, I can't additionally add a From conversion for the infallible cases. This blanket implementation makes sense, as it allows an API to take a T: TryFrom and handle all possible conversions, even infallible ones.

But for the API consumer it would be beneficial to be able to go from an infallible Result<T, Infallible> to a plain T without having to manually match or use expect. The latter is shorter and chainable, but has the disadvantage that it will still compile when the types are changed and the conversion is no longer infallible.

It might be that there is a different solution to infallible conversions via TryFrom in the future, for example via specialization. I believe that having an unwrap_infallible would still be beneficial in many other cases where generically fallible actions can be infallible in certain situations. One example is when working with a library that is based on fallible operations with a user supplied error type, but where the specific implementation from the user is infallible.

I'd be happy to work on a PR for this if it's acceptable to add, though I might require some guidance with regards to stability attributes, feature flags and so on. It's a bit hard to find information on that, and experimentation is costly as it takes me a while to complete a ./x.py test src/libcore :)

Activity

  1. Lonami commented on Jun 9, 2019

    @Lonami
    Contributor

    How is this different from the never (!) type? In fact, that page mentions Infallible errors with the same wording.

  2. phaylon commented on Jun 9, 2019

    @phaylon
    ContributorAuthor

    @Lonami I'm not sure I understand the question in relation to the proposal?

    core::convert::Infallible is an existing type that acts as a stand-in for the never type, since the never type itself is still unstable. Core APIs like TryFrom already use Infallible.

    I'm proposing an unwrapping fn on Result that is specific to dealing with core::convert::Infallible, and later (when it's stable) the never type.

  3. Lonami commented on Jun 9, 2019

    @Lonami
    Contributor

    Oh, sorry, I should've checked first, I was unaware of that ^^

  4. phaylon commented on Jun 9, 2019

    @phaylon
    ContributorAuthor

    No problem! Should've maybe added some background information, TryFrom and Infallible are rather recent additions :)

  5. scottmcm commented on Jun 11, 2019

    @scottmcm
    Member

    Looks like this might be rust-lang/rfcs#1723?

  6. phaylon commented on Jun 11, 2019

    @phaylon
    ContributorAuthor

    @scottmcm Ah, yes. I didn't go back to 2016, and didn't search in the RFCs :)

  7. mzabaluev commented on Nov 2, 2019

    @mzabaluev
    Contributor

    Hijacking this as a tracking issue for rust-lang/rfcs#2799
    I'm amazed at: 1) myself for never finding this before writing the RFC; 2) anyone who has seen my proposal at internals.rust-lang.org not bringing up this issue either.

  8. changed the title [-]Add Result::unwrap_infallible[/-] [+]Tracking issue for `Result::into_ok`[/+] on Nov 17, 2019
  9. added
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    and removed
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    on Nov 17, 2019
  10. added a commit that references this issue on Dec 10, 2019
  11. added a commit that references this issue on Jan 10, 2020
  12. 40 remaining items

  13. added a commit that references this issue on Aug 29, 2026
  14. added a commit that references this issue on Sep 24, 2026
  15. added 2 commits that reference this issue on Sep 28, 2026
  16. added 2 commits that reference this issue on Sep 29, 2026
  17. added 2 commits that reference this issue on Oct 1, 2026
  18. added a commit that references this issue on Oct 1, 2026
  19. theemathas commented on Oct 4, 2026

    @theemathas
    Contributor

    Stabilized in #161712

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-result-optionArea: Result and Option combinatorsB-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs-SmallLibs issues that are considered "small" or self-containedT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions