diff options
Diffstat (limited to 'src/libcore/mem.rs')
| -rw-r--r-- | src/libcore/mem.rs | 108 |
1 files changed, 16 insertions, 92 deletions
diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index a0fe6e98806..1a54f03bb00 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -918,7 +918,6 @@ pub fn discriminant<T>(v: &T) -> Discriminant<T> { } } - /// A wrapper to inhibit compiler from automatically calling `T`’s destructor. /// /// This wrapper is 0-cost. @@ -954,11 +953,18 @@ pub fn discriminant<T>(v: &T) -> Discriminant<T> { /// } /// } /// ``` +#[cfg(not(stage0))] #[stable(feature = "manually_drop", since = "1.20.0")] -#[allow(unions_with_drop_fields)] -#[derive(Copy)] -pub union ManuallyDrop<T>{ value: T } +#[lang = "manually_drop"] +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct ManuallyDrop<T> { + value: T, +} + +#[cfg(stage0)] +include!("manually_drop_stage0.rs"); +#[cfg(not(stage0))] impl<T> ManuallyDrop<T> { /// Wrap a value to be manually dropped. /// @@ -972,7 +978,7 @@ impl<T> ManuallyDrop<T> { #[rustc_const_unstable(feature = "const_manually_drop_new")] #[inline] pub const fn new(value: T) -> ManuallyDrop<T> { - ManuallyDrop { value: value } + ManuallyDrop { value } } /// Extract the value from the ManuallyDrop container. @@ -987,9 +993,7 @@ impl<T> ManuallyDrop<T> { #[stable(feature = "manually_drop", since = "1.20.0")] #[inline] pub fn into_inner(slot: ManuallyDrop<T>) -> T { - unsafe { - slot.value - } + slot.value } /// Manually drops the contained value. @@ -1006,102 +1010,22 @@ impl<T> ManuallyDrop<T> { } } +#[cfg(not(stage0))] #[stable(feature = "manually_drop", since = "1.20.0")] impl<T> Deref for ManuallyDrop<T> { type Target = T; #[inline] fn deref(&self) -> &Self::Target { - unsafe { - &self.value - } + &self.value } } +#[cfg(not(stage0))] #[stable(feature = "manually_drop", since = "1.20.0")] impl<T> DerefMut for ManuallyDrop<T> { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { - unsafe { - &mut self.value - } - } -} - -#[stable(feature = "manually_drop", since = "1.20.0")] -impl<T: ::fmt::Debug> ::fmt::Debug for ManuallyDrop<T> { - fn fmt(&self, fmt: &mut ::fmt::Formatter) -> ::fmt::Result { - unsafe { - fmt.debug_tuple("ManuallyDrop").field(&self.value).finish() - } - } -} - -#[stable(feature = "manually_drop_impls", since = "1.22.0")] -impl<T: Clone> Clone for ManuallyDrop<T> { - fn clone(&self) -> Self { - ManuallyDrop::new(self.deref().clone()) - } - - fn clone_from(&mut self, source: &Self) { - self.deref_mut().clone_from(source); - } -} - -#[stable(feature = "manually_drop_impls", since = "1.22.0")] -impl<T: Default> Default for ManuallyDrop<T> { - fn default() -> Self { - ManuallyDrop::new(Default::default()) - } -} - -#[stable(feature = "manually_drop_impls", since = "1.22.0")] -impl<T: PartialEq> PartialEq for ManuallyDrop<T> { - fn eq(&self, other: &Self) -> bool { - self.deref().eq(other) - } - - fn ne(&self, other: &Self) -> bool { - self.deref().ne(other) - } -} - -#[stable(feature = "manually_drop_impls", since = "1.22.0")] -impl<T: Eq> Eq for ManuallyDrop<T> {} - -#[stable(feature = "manually_drop_impls", since = "1.22.0")] -impl<T: PartialOrd> PartialOrd for ManuallyDrop<T> { - fn partial_cmp(&self, other: &Self) -> Option<::cmp::Ordering> { - self.deref().partial_cmp(other) - } - - fn lt(&self, other: &Self) -> bool { - self.deref().lt(other) - } - - fn le(&self, other: &Self) -> bool { - self.deref().le(other) - } - - fn gt(&self, other: &Self) -> bool { - self.deref().gt(other) - } - - fn ge(&self, other: &Self) -> bool { - self.deref().ge(other) - } -} - -#[stable(feature = "manually_drop_impls", since = "1.22.0")] -impl<T: Ord> Ord for ManuallyDrop<T> { - fn cmp(&self, other: &Self) -> ::cmp::Ordering { - self.deref().cmp(other) - } -} - -#[stable(feature = "manually_drop_impls", since = "1.22.0")] -impl<T: ::hash::Hash> ::hash::Hash for ManuallyDrop<T> { - fn hash<H: ::hash::Hasher>(&self, state: &mut H) { - self.deref().hash(state); + &mut self.value } } |
