Skip to content

Tracking Issue for option_result_unwrap_unchecked #81383

Closed
@ojeda

Description

@ojeda

Feature gate: #![feature(option_result_unwrap_unchecked)]

This is a tracking issue for the unchecked variants of unwrap() and unwrap_err() of Option and Result.

These are unsafe functions intended to be used in hot paths of performance sensitive applications where the variant an Option/Result holds is known, allowing to skip the branch that the safe version introduces. They complement other *_unchecked() methods already available, such as get_unchecked() in slices.

Public API

impl<T> Option<T> {
    pub unsafe fn unwrap_unchecked(self) -> T;
}

impl<T, E> Result<T, E> {
    pub unsafe fn unwrap_unchecked(self) -> T;
    pub unsafe fn unwrap_err_unchecked(self) -> E;
}

Steps / History

Unresolved Questions

  • Should we add expect_unchecked, and if yes, do it within another feature flag, to unblock unwrap_unchecked? Tracking Issue for option_result_unwrap_unchecked #81383 (comment)

    • It seems best to have it under another feature flag to avoid blocking this one.
  • Should we panic on failure in debug (i.e. for those projects that build core themselves)? If yes, that would mean there is no way to actually remove the check for those projects, which could be potentially needed by someone. If we do, should it be documented? Tracking Issue for option_result_unwrap_unchecked #81383 (comment)

    • The current implementation uses debug_assert!, which may be useful for projects that build core with debug assertions enabled (e.g. the bors test suite or some embedded projects), but this is not documented, and indeed most users will not see such behavior, since core is shipped in release mode.
    • Furthermore, since it is UB anyway, we have room for changing the implementation, e.g. if we end up distributing the standard library with debug assertions enabled for some use cases.
  • Should we name them unchecked_unwrap instead? Tracking Issue for option_result_unwrap_unchecked #81383 (comment).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-result-optionArea: Result and Option combinatorsC-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