diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-06-12 04:22:45 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-12 04:22:45 +0200 |
| commit | b5184e56a49652aadb279e42a4e55e7328071b35 (patch) | |
| tree | 974034911ea77840547f5c40c434edabe6b262ea /src/libcore | |
| parent | a60a5db7de3107e7b91bd8a4006146b521066da4 (diff) | |
| parent | 67fd99589ad161fcd154cfdb0f33d349ec23896d (diff) | |
| download | rust-b5184e56a49652aadb279e42a4e55e7328071b35.tar.gz rust-b5184e56a49652aadb279e42a4e55e7328071b35.zip | |
Rollup merge of #61348 - dronesforwork-forks:clone-from, r=KodrAus
Implement Clone::clone_from for Option and Result See https://github.com/rust-lang/rust/issues/28481
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/option.rs | 21 | ||||
| -rw-r--r-- | src/libcore/result.rs | 23 |
2 files changed, 42 insertions, 2 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 6b7f491effb..c75ecb059e8 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -145,7 +145,7 @@ use crate::pin::Pin; // which basically means it must be `Option`. /// The `Option` type. See [the module level documentation](index.html) for more. -#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] +#[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] #[stable(feature = "rust1", since = "1.0.0")] pub enum Option<T> { /// No value @@ -1041,6 +1041,25 @@ fn expect_failed(msg: &str) -> ! { ///////////////////////////////////////////////////////////////////////////// #[stable(feature = "rust1", since = "1.0.0")] +impl<T: Clone> Clone for Option<T> { + #[inline] + fn clone(&self) -> Self { + match self { + Some(x) => Some(x.clone()), + None => None, + } + } + + #[inline] + fn clone_from(&mut self, source: &Self) { + match (self, source) { + (Some(to), Some(from)) => to.clone_from(from), + (to, from) => *to = from.clone(), + } + } +} + +#[stable(feature = "rust1", since = "1.0.0")] impl<T> Default for Option<T> { /// Returns [`None`][Option::None]. #[inline] 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>; |
