Skip to content

Commit b5184e5

Browse files
authored
Rollup merge of rust-lang#61348 - dronesforwork-forks:clone-from, r=KodrAus
Implement Clone::clone_from for Option and Result See rust-lang#28481
2 parents a60a5db + 67fd995 commit b5184e5

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/libcore/option.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ use crate::pin::Pin;
145145
// which basically means it must be `Option`.
146146

147147
/// The `Option` type. See [the module level documentation](index.html) for more.
148-
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
148+
#[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
149149
#[stable(feature = "rust1", since = "1.0.0")]
150150
pub enum Option<T> {
151151
/// No value
@@ -1040,6 +1040,25 @@ fn expect_failed(msg: &str) -> ! {
10401040
// Trait implementations
10411041
/////////////////////////////////////////////////////////////////////////////
10421042

1043+
#[stable(feature = "rust1", since = "1.0.0")]
1044+
impl<T: Clone> Clone for Option<T> {
1045+
#[inline]
1046+
fn clone(&self) -> Self {
1047+
match self {
1048+
Some(x) => Some(x.clone()),
1049+
None => None,
1050+
}
1051+
}
1052+
1053+
#[inline]
1054+
fn clone_from(&mut self, source: &Self) {
1055+
match (self, source) {
1056+
(Some(to), Some(from)) => to.clone_from(from),
1057+
(to, from) => *to = from.clone(),
1058+
}
1059+
}
1060+
}
1061+
10431062
#[stable(feature = "rust1", since = "1.0.0")]
10441063
impl<T> Default for Option<T> {
10451064
/// Returns [`None`][Option::None].

src/libcore/result.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ use crate::ops::{self, Deref};
240240
///
241241
/// [`Ok`]: enum.Result.html#variant.Ok
242242
/// [`Err`]: enum.Result.html#variant.Err
243-
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
243+
#[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
244244
#[must_use = "this `Result` may be an `Err` variant, which should be handled"]
245245
#[stable(feature = "rust1", since = "1.0.0")]
246246
pub enum Result<T, E> {
@@ -1003,6 +1003,27 @@ fn unwrap_failed<E: fmt::Debug>(msg: &str, error: E) -> ! {
10031003
// Trait implementations
10041004
/////////////////////////////////////////////////////////////////////////////
10051005

1006+
#[stable(feature = "rust1", since = "1.0.0")]
1007+
impl<T: Clone, E: Clone> Clone for Result<T, E> {
1008+
#[inline]
1009+
fn clone(&self) -> Self {
1010+
match self {
1011+
Ok(x) => Ok(x.clone()),
1012+
Err(x) => Err(x.clone()),
1013+
}
1014+
}
1015+
1016+
#[inline]
1017+
fn clone_from(&mut self, source: &Self) {
1018+
match (self, source) {
1019+
(Ok(to), Ok(from)) => to.clone_from(from),
1020+
(Err(to), Err(from)) => to.clone_from(from),
1021+
(to, from) => *to = from.clone(),
1022+
}
1023+
}
1024+
}
1025+
1026+
10061027
#[stable(feature = "rust1", since = "1.0.0")]
10071028
impl<T, E> IntoIterator for Result<T, E> {
10081029
type Item = T;

0 commit comments

Comments
 (0)