rust-lang / rust

Empowering everyone to build reliable and efficient software.

Home Page:https://www.rust-lang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tracking Issue for `const_option_ext`

fee1-dead opened this issue · comments

commented

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

  • Implementation: #91928
  • Final comment period (FCP)
  • Stabilization PR

Unresolved Questions

  • None yet.

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

commented

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

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"),
    }
}

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.

Greetings!

Any way to do such thing in stable?

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

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

commented

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

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?

commented

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.

unwrap_or doesn't work.

commented

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

1.60.

I fixed with:

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

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>
}
commented

@poliorcetics Yes. Feel free to open a PR.

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