Skip to content

Commit 2e128a2

Browse files
Rollup merge of rust-lang#130090 - RalfJung:result-copied, r=Noratrieb
make Result::copied unstably const The corresponding `Option::copied` is unstably const, so seems reasonable to do the same here.
2 parents 2ee8304 + 295946c commit 2e128a2

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

core/src/option.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ impl<T> Option<&T> {
18931893
where
18941894
T: Copy,
18951895
{
1896-
// FIXME: this implementation, which sidesteps using `Option::map` since it's not const
1896+
// FIXME(const-hack): this implementation, which sidesteps using `Option::map` since it's not const
18971897
// ready yet, should be reverted when possible to avoid code repetition
18981898
match self {
18991899
Some(&v) => Some(v),
@@ -1941,7 +1941,7 @@ impl<T> Option<&mut T> {
19411941
/// ```
19421942
#[must_use = "`self` will be dropped if the result is not used"]
19431943
#[stable(feature = "copied", since = "1.35.0")]
1944-
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
1944+
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
19451945
pub const fn copied(self) -> Option<T>
19461946
where
19471947
T: Copy,

core/src/result.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1535,11 +1535,17 @@ impl<T, E> Result<&T, E> {
15351535
/// ```
15361536
#[inline]
15371537
#[stable(feature = "result_copied", since = "1.59.0")]
1538-
pub fn copied(self) -> Result<T, E>
1538+
#[rustc_const_unstable(feature = "const_result", issue = "82814")]
1539+
pub const fn copied(self) -> Result<T, E>
15391540
where
15401541
T: Copy,
15411542
{
1542-
self.map(|&t| t)
1543+
// FIXME(const-hack): this implementation, which sidesteps using `Result::map` since it's not const
1544+
// ready yet, should be reverted when possible to avoid code repetition
1545+
match self {
1546+
Ok(&v) => Ok(v),
1547+
Err(e) => Err(e),
1548+
}
15431549
}
15441550

15451551
/// Maps a `Result<&T, E>` to a `Result<T, E>` by cloning the contents of the
@@ -1579,11 +1585,17 @@ impl<T, E> Result<&mut T, E> {
15791585
/// ```
15801586
#[inline]
15811587
#[stable(feature = "result_copied", since = "1.59.0")]
1582-
pub fn copied(self) -> Result<T, E>
1588+
#[rustc_const_unstable(feature = "const_result", issue = "82814")]
1589+
pub const fn copied(self) -> Result<T, E>
15831590
where
15841591
T: Copy,
15851592
{
1586-
self.map(|&mut t| t)
1593+
// FIXME(const-hack): this implementation, which sidesteps using `Result::map` since it's not const
1594+
// ready yet, should be reverted when possible to avoid code repetition
1595+
match self {
1596+
Ok(&mut v) => Ok(v),
1597+
Err(e) => Err(e),
1598+
}
15871599
}
15881600

15891601
/// Maps a `Result<&mut T, E>` to a `Result<T, E>` by cloning the contents of the

0 commit comments

Comments
 (0)