From 03e0c8edb2114aaf92fb8d93a18dfb0028690765 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 7 Sep 2024 22:19:30 +0200 Subject: [PATCH 1/3] make Result::copied unstably const --- library/core/src/result.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 73b11f803d929..a7fd95e007982 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1535,11 +1535,17 @@ impl Result<&T, E> { /// ``` #[inline] #[stable(feature = "result_copied", since = "1.59.0")] - pub fn copied(self) -> Result + #[rustc_const_unstable(feature = "const_result", issue = "82814")] + pub const fn copied(self) -> Result where T: Copy, { - self.map(|&t| t) + // FIXME: this implementation, which sidesteps using `Result::map` since it's not const + // ready yet, should be reverted when possible to avoid code repetition + match self { + Ok(&v) => Ok(v), + Err(e) => Err(e), + } } /// Maps a `Result<&T, E>` to a `Result` by cloning the contents of the From 3de6838238dd72004acf0e6b3f158837952c1dc3 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 8 Sep 2024 08:30:28 +0200 Subject: [PATCH 2/3] add some FIXME(const-hack) --- library/core/src/option.rs | 2 +- library/core/src/result.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 50cb22b7eb3f5..b62eec1897b09 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1894,7 +1894,7 @@ impl Option<&T> { where T: Copy, { - // FIXME: this implementation, which sidesteps using `Option::map` since it's not const + // FIXME(const-hack): this implementation, which sidesteps using `Option::map` since it's not const // ready yet, should be reverted when possible to avoid code repetition match self { Some(&v) => Some(v), diff --git a/library/core/src/result.rs b/library/core/src/result.rs index a7fd95e007982..5b6a81c8dae91 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1540,7 +1540,7 @@ impl Result<&T, E> { where T: Copy, { - // FIXME: this implementation, which sidesteps using `Result::map` since it's not const + // FIXME(const-hack): this implementation, which sidesteps using `Result::map` since it's not const // ready yet, should be reverted when possible to avoid code repetition match self { Ok(&v) => Ok(v), From f7b4f4a73bcaa2987fddfa86c240a497780c88c8 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 8 Sep 2024 09:24:06 +0200 Subject: [PATCH 3/3] Option, Result: put the &mut variants of 'copied' under the same feature as the '&' variants --- library/core/src/option.rs | 2 +- library/core/src/result.rs | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index b62eec1897b09..80598b8852955 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1942,7 +1942,7 @@ impl Option<&mut T> { /// ``` #[must_use = "`self` will be dropped if the result is not used"] #[stable(feature = "copied", since = "1.35.0")] - #[rustc_const_unstable(feature = "const_option_ext", issue = "91930")] + #[rustc_const_unstable(feature = "const_option", issue = "67441")] pub const fn copied(self) -> Option where T: Copy, diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 5b6a81c8dae91..02f6f783b512c 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1585,11 +1585,17 @@ impl Result<&mut T, E> { /// ``` #[inline] #[stable(feature = "result_copied", since = "1.59.0")] - pub fn copied(self) -> Result + #[rustc_const_unstable(feature = "const_result", issue = "82814")] + pub const fn copied(self) -> Result where T: Copy, { - self.map(|&mut t| t) + // FIXME(const-hack): this implementation, which sidesteps using `Result::map` since it's not const + // ready yet, should be reverted when possible to avoid code repetition + match self { + Ok(&mut v) => Ok(v), + Err(e) => Err(e), + } } /// Maps a `Result<&mut T, E>` to a `Result` by cloning the contents of the