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
7 of 66 tasks
jhpratt opened this issue Mar 5, 2021 · 8 comments
Open
7 of 66 tasks

Tracking Issue for const Result methods #82814

jhpratt opened this issue Mar 5, 2021 · 8 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 ok(self) -> Option<T>;
    pub const fn err(self) -> Option<E>;
    pub const fn as_mut(&mut self) -> Result<&mut T, &mut E>;
    pub const fn map<U, F>(self, op: F) -> Result<U, E>
        where F: const FnOnce(T) -> U;
    pub const fn map_or<U, F>(self, default: U, f: F) -> U
        where F: const FnOnce(T) -> U;
    pub const fn map_or_else<U, D, F>(self, default: D, f: F) -> U
        where
            D: const FnOnce(E) -> U,
            F: const FnOnce(T) -> U;
    pub const fn map_err<F, O>(self, op: O) -> Result<T, F>
        where O: const FnOnce(E) -> F;
    pub const fn and<U>(self, res: Result<U, E>) -> Result<U, E>;
    pub const fn and_then<U, F>(self, op: F) -> Result<U, E>
        where F: const FnOnce(T) -> Result<U, E>;
    pub const fn or<F>(self, res: Result<T, F>) -> Result<T, F>;
    pub const fn or_else<F, O>(self, op: O) -> Result<T, F>
        where O: const FnOnce(E) -> Result<T, F>;
    pub const fn unwrap_or(self, default: T) -> T;
    pub const fn unwrap_or_else<F>(self, op: F) -> T
        where F: const FnOnce(E) -> T;
    pub const unsafe fn unwrap_unchecked(self) -> T;
    pub const unsafe fn unwrap_err_unchecked(self) -> E;
}

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

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

impl<T, E> Result<T, E> where E: const Debug {
    pub const fn expect(self, msg: &str) -> T;
    pub const fn unwrap(self) -> T;
}

impl<T, E> Result<T, E> where T: const Debug {
    pub const fn expect_err(self, msg: &str) -> E;
    pub const fn unwrap_err(self) -> E;
}

impl<T, E> Result<T, E> where T: const Default {
    pub const fn unwrap_or_default(self) -> T;
}

impl<T, E> Result<T, E> where T: const Deref {
    pub const fn as_deref(&self) -> Result<&<T as Deref>::Target, &E>;
}

impl<T, E> Result<T, E> where T: const DerefMut {
    pub const fn as_deref_mut(&mut self) -> Result<&mut <T as Deref>::Target, &mut E>;
}

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

Blockers

Status

Unresolved Questions

  • None yet.

See also #57563.

Edit: Please ping @jhpratt if you're submitting a PR that changes any of the above! I'd like to keep the checkboxes up-to-date.

@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.

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

6 participants