Skip to content

Commit 67fd995

Browse files
committed
Implement Clone::clone_from for Result.
1 parent cb12460 commit 67fd995

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

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)