diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2019-05-30 12:50:06 +0200 |
|---|---|---|
| committer | Mara Bos <m-ou.se@m-ou.se> | 2019-06-01 09:14:59 +0200 |
| commit | 67fd99589ad161fcd154cfdb0f33d349ec23896d (patch) | |
| tree | dfd7042965f0edac819ff9e22c96e3311fad9752 /src/libcore | |
| parent | cb12460467aa98cb82587308341939679ff2a7d1 (diff) | |
| download | rust-67fd99589ad161fcd154cfdb0f33d349ec23896d.tar.gz rust-67fd99589ad161fcd154cfdb0f33d349ec23896d.zip | |
Implement Clone::clone_from for Result.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/result.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/libcore/result.rs b/src/libcore/result.rs index bf8fd63b644..8a09877ce1f 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -240,7 +240,7 @@ use crate::ops::{self, Deref}; /// /// [`Ok`]: enum.Result.html#variant.Ok /// [`Err`]: enum.Result.html#variant.Err -#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] +#[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] #[must_use = "this `Result` may be an `Err` variant, which should be handled"] #[stable(feature = "rust1", since = "1.0.0")] pub enum Result<T, E> { @@ -1004,6 +1004,27 @@ fn unwrap_failed<E: fmt::Debug>(msg: &str, error: E) -> ! { ///////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] +impl<T: Clone, E: Clone> Clone for Result<T, E> { + #[inline] + fn clone(&self) -> Self { + match self { + Ok(x) => Ok(x.clone()), + Err(x) => Err(x.clone()), + } + } + + #[inline] + fn clone_from(&mut self, source: &Self) { + match (self, source) { + (Ok(to), Ok(from)) => to.clone_from(from), + (Err(to), Err(from)) => to.clone_from(from), + (to, from) => *to = from.clone(), + } + } +} + + +#[stable(feature = "rust1", since = "1.0.0")] impl<T, E> IntoIterator for Result<T, E> { type Item = T; type IntoIter = IntoIter<T>; |
