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

Tracking Issue for const Result methods #82814

Open
jhpratt opened this issue Mar 5, 2021 · 9 comments
Open

Tracking Issue for const Result methods #82814

jhpratt opened this issue Mar 5, 2021 · 9 comments
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@jhpratt
Copy link
Member

jhpratt commented Mar 5, 2021

Feature gate: #![feature(const_result)]

This is a tracking issue for making a number of Result methods const fn.

Public API

NB: Many of these methods will need ~const Drop. They're omitted here for brevity.

impl<T, E> Result<T, E> {
    pub const fn as_mut(&mut self) -> Result<&mut T, &mut E>;
}

impl<T, E> Result<Option<T>, E> {
    pub const fn transpose(self) -> Option<Result<T, E>>;
}

impl<T, E> Result<&T, E> {
    pub const fn copied(self) -> Result<T, E>
    where
        T: Copy;
}

impl<T, E> Result<&mut T, E> {
    pub const fn copied(self) -> Result<T, E>
    where
        T: Copy;
}

Note some things are missing compared with Option (#67441):

  • unwrap, expect: drops the error, so cannot be done in const.
  • flatten: is not stable yet on Result, even outside const.
  • take, replace: does not exist on Result.

See also #57563.

Please post a comment in this issue if you're submitting a PR that changes any of the above!

@jhpratt jhpratt added C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Mar 5, 2021
@rustbot rustbot added the A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. label Mar 5, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 17, 2021
Make Result::as_mut const

Adding `const` for `Result::as_mut`.

Tracking issue: rust-lang#82814
@netsutetsu
Copy link

Why aren't there unwrap_unchecked and unwrap_err_unchecked while Option::unwrap_unchecked() is going to be const in #91930?

@jhpratt
Copy link
Member Author

jhpratt commented Nov 10, 2022

The methods probably didn't exist when this issue was created. I'll add them when I'm on my laptop.

@netsutetsu
Copy link

netsutetsu commented Nov 11, 2022

Could you also add contains, contains_err (introduced in #62358), inspect, inspect_err (introduced in #91345), flatten (introduced in #70142), copied, cloned and iter since Option::contains(), Option::inspect(), Option::flatten(), Option::copied(), Option::cloned() and Option::iter() will be const in #91930, #67441 and #91582?

@jhpratt
Copy link
Member Author

jhpratt commented Nov 13, 2022

Going through the full list now. I'm only going to be adding methods that are already stable, as the unstable methods are tracked in their associated issues.

@pitaj
Copy link
Contributor

pitaj commented Jan 29, 2023

@jhpratt PR #104407 for Result::{map, map_or, map_or_else, map_err, as_deref, as_deref_mut, unwrap_or_default, and_then, or_else, unwrap_or_else, unwrap_unchecked, unwrap_err_unchecked, copied, cloned}

@jhpratt
Copy link
Member Author

jhpratt commented Jan 29, 2023

@pitaj Thanks. I was already subscribed to the issue, but there's nothing actionable in this tracking issue at the moment.

@Pzixel
Copy link

Pzixel commented Apr 20, 2023

What happened to const_result? About a week ago everything was fine and now feature gate is gone and with it some of my code started to fail. I understand it's nightly and things change but I don't see any motivation/discussion why this happened. Does anyone have any insights?

@c410-f3r
Copy link
Contributor

#110393

Looks like any stuff related to constant bounds will take a considerable amount of time to be resolved.

@RalfJung
Copy link
Member

RalfJung commented Sep 9, 2024

Yeah, most of the things that were in this feature gate unfortunately depend on "const traits" and that's still very experimental.

The remaining methods listed here, however, can probably be stabilized fairly soon. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants