From 2ee92419dd17606d6b50f44b413dcf7ef49ac116 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Tue, 28 Jun 2022 11:37:48 +0100 Subject: [PATCH 1/2] Remove feature `const_option` from std --- library/std/src/lib.rs | 1 - library/std/src/sys/windows/args.rs | 16 ++++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 7da9f248c877a..65b8df4299663 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -335,7 +335,6 @@ #![feature(const_ip)] #![feature(const_ipv4)] #![feature(const_ipv6)] -#![feature(const_option)] #![feature(const_socketaddr)] #![feature(thread_local_internals)] // diff --git a/library/std/src/sys/windows/args.rs b/library/std/src/sys/windows/args.rs index c5918103fec25..361cf0590a17c 100644 --- a/library/std/src/sys/windows/args.rs +++ b/library/std/src/sys/windows/args.rs @@ -21,6 +21,14 @@ use crate::vec; use core::iter; +/// This is the const equivalent to `NonZeroU16::new(n).unwrap()` +const fn non_zero_u16(n: u16) -> NonZeroU16 { + match NonZeroU16::new(n) { + Some(n) => n, + None => panic!("called `unwrap` on a `None` value"), + } +} + pub fn args() -> Args { // SAFETY: `GetCommandLineW` returns a pointer to a null terminated UTF-16 // string so it's safe for `WStrUnits` to use. @@ -58,10 +66,10 @@ fn parse_lp_cmd_line<'a, F: Fn() -> OsString>( lp_cmd_line: Option>, exe_name: F, ) -> Vec { - const BACKSLASH: NonZeroU16 = NonZeroU16::new(b'\\' as u16).unwrap(); - const QUOTE: NonZeroU16 = NonZeroU16::new(b'"' as u16).unwrap(); - const TAB: NonZeroU16 = NonZeroU16::new(b'\t' as u16).unwrap(); - const SPACE: NonZeroU16 = NonZeroU16::new(b' ' as u16).unwrap(); + const BACKSLASH: NonZeroU16 = non_zero_u16(b'\\' as u16); + const QUOTE: NonZeroU16 = non_zero_u16(b'"' as u16); + const TAB: NonZeroU16 = non_zero_u16(b'\t' as u16); + const SPACE: NonZeroU16 = non_zero_u16(b' ' as u16); let mut ret_val = Vec::new(); // If the cmd line pointer is null or it points to an empty string then From 720c430822ab093449d495487cc103b39bcf9f4a Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Tue, 28 Jun 2022 12:18:16 +0100 Subject: [PATCH 2/2] Add a fixme comment --- library/std/src/sys/windows/args.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/std/src/sys/windows/args.rs b/library/std/src/sys/windows/args.rs index 361cf0590a17c..01f26298290f0 100644 --- a/library/std/src/sys/windows/args.rs +++ b/library/std/src/sys/windows/args.rs @@ -22,6 +22,9 @@ use crate::vec; use core::iter; /// This is the const equivalent to `NonZeroU16::new(n).unwrap()` +/// +/// FIXME: This can be removed once `Option::unwrap` is stably const. +/// See the `const_option` feature (#67441). const fn non_zero_u16(n: u16) -> NonZeroU16 { match NonZeroU16::new(n) { Some(n) => n,