Skip to content

Add methods for unwrapping Result<T, !> / Result<!, E> #1723

Open
@canndrew

Description

@canndrew

One of the main intended uses for the ! type is to be able construct the types Result<T, !> and Result<!, E>. Right now, if you have r: Result<T, !> there are several ways to unpack it's inner T, but none of them are ideal:

  • r.unwrap(): unwrap is scary and should be avoided. This is also fragile if someone later changes the error type to something other than !.
  • r.unwrap_or_else(|e| e): Kinda confusing.
  • let Ok(val) = r;: Requires a separate statement. (Also doesn't currently work).
  • match r { Ok(v) => v, Err(e) => e }: Both long and kinda confusing.
  • match r { Ok(v) => v }: Not as nice as a method on r. (Also doesn't currently work).

The void crate adds the methods void_unwrap and void_unwrap_err to Result<T, Void> and Result<Void, T>. I think methods like these should be added to libcore as well. Perhaps we could call them always_ok() and always_err().

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions