Skip to content

Tracking Issue for const_option_ext #91930

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
2 of 3 tasks
fee1-dead opened this issue Dec 14, 2021 · 22 comments · Fixed by #132966
Closed
2 of 3 tasks

Tracking Issue for const_option_ext #91930

fee1-dead opened this issue Dec 14, 2021 · 22 comments · Fixed by #132966
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. F-const_trait_impl `#![feature(const_trait_impl)]` finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@fee1-dead
Copy link
Member

fee1-dead commented Dec 14, 2021

Feature gate: #![feature(const_option_ext)]

This is a tracking issue for option methods.

Public API

// core::option

impl<T> Option<T> {
    pub const fn as_pin_ref(self: Pin<&Self>) -> Option<Pin<&T>>;
    pub const fn as_pin_mut(self: Pin<&mut Self>) -> Option<Pin<&mut T>>;
    pub const fn as_slice(&self) -> &[T];
    pub const fn as_mut_slice(&mut self) -> &mut [T];
}

Steps / History

Unresolved Questions

  • None yet.
@fee1-dead fee1-dead added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC labels Dec 14, 2021
@PvdBerg1998
Copy link

Out of interest as I found this in std: where can I find details about the ~const syntax?

@fee1-dead
Copy link
Member Author

Out of interest as I found this in std: where can I find details about the ~const syntax?

You can find the most recent proposal on Rust Internals

@Clockwork-Muse
Copy link

What about things like expect() or unwrap()?

Current workaround:

/// Obtains an Instant using seconds and an adjustment in nanoseconds since '1970-01-01 00:00:00.000000000Z'.
///
/// # Parameters
///  - `epoch_seconds`: the seconds since the epoch.
///  - `nano_adjustment`: the adjustment amount from the given second.
///
/// # Panics
/// - if the adjusted amount of seconds would overflow the instant.
pub const fn of_epoch_second_and_adjustment(
    epoch_seconds: i64,
    nano_adjustment: i64,
) -> Instant {
    match Instant::of_epoch_second_and_adjustment_checked(epoch_seconds, nano_adjustment) {
        Some(i) => i,
        _ => panic!("nano adjustment would overflow instant"),
    }
}

@cyqsimon
Copy link
Contributor

In #94317 I noticed this feature is WIP for similar methods, but I don't understand it enough to be confident that what I wrote was correct. Please help me verify, thanks.

@BratSinot
Copy link

Greetings!

Any way to do such thing in stable?

pub const FOO: usize = option_env!("FOO")
    .unwrap_or("123456")
    .parse()
    .unwrap();

@fee1-dead
Copy link
Member Author

fee1-dead commented Apr 5, 2022

Parsing an integer is not converted to const code yet I believe. So you can't do this yet even in nightly.

@DunnAnDusted
Copy link

Hi, I followed the link from the unwrap_or() method, but don't see it included among the methods noted, is this exclusion a mistake...?

@frederikhors
Copy link

I'm new to Rust. I'm using rust 1.60.0.

I'm trying to use this code:

pub const VERSION: &'static str = option_env!("VERSION").unwrap_or_else(|| "dev");

but I'm getting this error:

the trait bound `[closure@src\constants.rs:4:81: 4:89]: ~const FnOnce<()>` is not satisfied
the trait `~const FnOnce<()>` is not implemented for `[closure@src\constants.rs:4:81: 4:89]`
wrap the `[closure@src\constants.rs:4:81: 4:89]` in a closure with no arguments: `|| { /* code */ }`rustcE0277
constants.rs(4, 66): the trait `FnOnce<()>` is implemented for `[closure@src\constants.rs:4:81: 4:89]`, but that implementation is not `const`
option.rs(797, 12): required by a bound in `Option::<T>::unwrap_or_else`

Can you help me understand what's wrong with it?

@fee1-dead
Copy link
Member Author

You could use .unwrap_or:

pub const VERSION: &'static str = option_env!("VERSION").unwrap_or("dev");

Ideally the errors message should be improved. I will work on that.

@frederikhors
Copy link

unwrap_or doesn't work.

@fee1-dead
Copy link
Member Author

Hmm. Are you using nightly? If you aren't using nightly, none of these would work.

@frederikhors
Copy link

1.60.

@frederikhors
Copy link

I fixed with:

pub const VERSION: &'static str = match option_env!("VERSION") {
    Some(v) => v,
    None => "dev",
};

@poliorcetics
Copy link
Contributor

poliorcetics commented Jul 27, 2022

Is it possible to stabilize the methods that do not depend on ~const ?

impl<T> Option<T> {
    pub const fn as_pin_ref(self: Pin<&Self>) -> Option<Pin<&T>>;
    pub const fn as_pin_mut(self: Pin<&mut Self>) -> Option<Pin<&mut T>>;

    pub const unsafe fn unwrap_unchecked(self) -> T;
}

impl<T: Copy> Option<&mut T> {
    pub const fn copied(self) -> Option<T>
}

@fee1-dead
Copy link
Member Author

@poliorcetics Yes. Feel free to open a PR.

@fee1-dead fee1-dead added the F-const_trait_impl `#![feature(const_trait_impl)]` label Mar 24, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 20, 2024
Make Option::as_[mut_]slice const

These two functions can both be made `const`. I have added them to the `const_option_ext` feature, rust-lang#91930. I don't believe there is anything blocking stabilization of `as_slice`, but `as_mut_slice` contains mutable references so depends on `const_mut_refs`.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 20, 2024
Make Option::as_[mut_]slice const

These two functions can both be made `const`. I have added them to the `const_option_ext` feature, rust-lang#91930. I don't believe there is anything blocking stabilization of `as_slice`, but `as_mut_slice` contains mutable references so depends on `const_mut_refs`.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 20, 2024
Rollup merge of rust-lang#126711 - GKFX:const-option-as-slice, r=oli-obk

Make Option::as_[mut_]slice const

These two functions can both be made `const`. I have added them to the `const_option_ext` feature, rust-lang#91930. I don't believe there is anything blocking stabilization of `as_slice`, but `as_mut_slice` contains mutable references so depends on `const_mut_refs`.
@RalfJung
Copy link
Member

RalfJung commented Sep 8, 2024

A lot of this got removed by #110393. I updated the list above.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 15, 2024
…rieb

move Option::unwrap_unchecked into const_option feature gate

That's where `unwrap` and `expect` are so IMO it makes more sense to group them together.

Part of rust-lang#91930, rust-lang#67441
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 15, 2024
Rollup merge of rust-lang#130118 - RalfJung:unwrap_unchecked, r=Noratrieb

move Option::unwrap_unchecked into const_option feature gate

That's where `unwrap` and `expect` are so IMO it makes more sense to group them together.

Part of rust-lang#91930, rust-lang#67441
@RalfJung RalfJung added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Oct 13, 2024
@RalfJung
Copy link
Member

@rust-lang/libs-api with #130136 having passed FCP, this should not have any blockers any more. All functions tracked here are already stable, and just waiting for their const-stability. :-)

@rfcbot
Copy link
Collaborator

rfcbot commented Oct 14, 2024

Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Oct 14, 2024
@dtolnay dtolnay removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Oct 14, 2024
@RalfJung
Copy link
Member

RalfJung commented Nov 1, 2024

@Amanieu @BurntSushi @m-ou-se friendly FCP checkbox reminder :)

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Nov 2, 2024
@rfcbot
Copy link
Collaborator

rfcbot commented Nov 2, 2024

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Nov 12, 2024
@rfcbot
Copy link
Collaborator

rfcbot commented Nov 12, 2024

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@bors bors closed this as completed in ae5c00f Nov 13, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 13, 2024
Rollup merge of rust-lang#132966 - RalfJung:const_option_ext, r=jhpratt

stabilize const_option_ext

Fixes rust-lang#91930

FCP passed in that issue.
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Nov 14, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. F-const_trait_impl `#![feature(const_trait_impl)]` finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.