diff --git a/library/core/src/option.rs b/library/core/src/option.rs index dd7556758be7d..75151faa3e454 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -134,8 +134,8 @@ use crate::iter::{FromIterator, FusedIterator, TrustedLen}; use crate::pin::Pin; use crate::{ - convert, fmt, hint, mem, - ops::{self, Deref, DerefMut}, + convert, fmt, hint, mem, ptr, + ops::{self, Deref, DerefMut, Range}, }; /// The `Option` type. See [the module level documentation](self) for more. @@ -1228,7 +1228,7 @@ fn expect_none_failed(msg: &str, value: &dyn fmt::Debug) -> ! { #[stable(feature = "rust1", since = "1.0.0")] impl Clone for Option { #[inline] - fn clone(&self) -> Self { + default fn clone(&self) -> Self { match self { Some(x) => Some(x.clone()), None => None, @@ -1236,7 +1236,7 @@ impl Clone for Option { } #[inline] - fn clone_from(&mut self, source: &Self) { + default fn clone_from(&mut self, source: &Self) { match (self, source) { (Some(to), Some(from)) => to.clone_from(from), (to, from) => *to = from.clone(), @@ -1244,6 +1244,30 @@ impl Clone for Option { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl Clone for Option { + #[inline] + fn clone(&self) -> Self { + *self + } + + #[inline] + fn clone_from(&mut self, source: &Self) { + *self = *source + } +} + +// Range is not Copy even if T is copy (see #27186), +// so provide an efficient implementation. +#[stable(feature = "rust1", since = "1.0.0")] +impl Clone for Option> { + #[inline] + fn clone(&self) -> Self { + // SAFETY: 'self' is not Drop so memcpy is OK. + unsafe { ptr::read(self as *const Self) } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Default for Option { /// Returns [`None`][Option::None].