From 538fe4b28d3aca5657a95aadf65800d2237276d1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 30 Dec 2021 10:37:27 -0800 Subject: [PATCH] Consolidate impl Option<&T> --- library/core/src/option.rs | 59 +++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index c3bea55772f..9b3b27b68a3 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1672,7 +1672,7 @@ impl Option<(T, U)> { } } -impl Option<&T> { +impl Option<&T> { /// Maps an `Option<&T>` to an `Option` by copying the contents of the /// option. /// @@ -1688,7 +1688,10 @@ impl Option<&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", issue = "67441")] - pub const fn copied(self) -> Option { + pub const fn copied(self) -> Option + where + T: Copy, + { // FIXME: 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 { @@ -1696,6 +1699,31 @@ impl Option<&T> { None => None, } } + + /// Maps an `Option<&T>` to an `Option` by cloning the contents of the + /// option. + /// + /// # Examples + /// + /// ``` + /// let x = 12; + /// let opt_x = Some(&x); + /// assert_eq!(opt_x, Some(&12)); + /// let cloned = opt_x.cloned(); + /// assert_eq!(cloned, Some(12)); + /// ``` + #[must_use = "`self` will be dropped if the result is not used"] + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_unstable(feature = "const_option_cloned", issue = "91582")] + pub const fn cloned(self) -> Option + where + T: ~const Clone, + { + match self { + Some(t) => Some(t.clone()), + None => None, + } + } } impl Option<&mut T> { @@ -1722,33 +1750,6 @@ impl Option<&mut T> { } } -impl Option<&T> { - /// Maps an `Option<&T>` to an `Option` by cloning the contents of the - /// option. - /// - /// # Examples - /// - /// ``` - /// let x = 12; - /// let opt_x = Some(&x); - /// assert_eq!(opt_x, Some(&12)); - /// let cloned = opt_x.cloned(); - /// assert_eq!(cloned, Some(12)); - /// ``` - #[must_use = "`self` will be dropped if the result is not used"] - #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_option_cloned", issue = "91582")] - pub const fn cloned(self) -> Option - where - T: ~const Clone, - { - match self { - Some(t) => Some(t.clone()), - None => None, - } - } -} - impl Option<&mut T> { /// Maps an `Option<&mut T>` to an `Option` by cloning the contents of the /// option.