|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +/// A wrapper to inhibit compiler from automatically calling `T`’s destructor. |
| 12 | +/// |
| 13 | +/// This wrapper is 0-cost. |
| 14 | +/// |
| 15 | +/// # Examples |
| 16 | +/// |
| 17 | +/// This wrapper helps with explicitly documenting the drop order dependencies between fields of |
| 18 | +/// the type: |
| 19 | +/// |
| 20 | +/// ```rust |
| 21 | +/// use std::mem::ManuallyDrop; |
| 22 | +/// struct Peach; |
| 23 | +/// struct Banana; |
| 24 | +/// struct Melon; |
| 25 | +/// struct FruitBox { |
| 26 | +/// // Immediately clear there’s something non-trivial going on with these fields. |
| 27 | +/// peach: ManuallyDrop<Peach>, |
| 28 | +/// melon: Melon, // Field that’s independent of the other two. |
| 29 | +/// banana: ManuallyDrop<Banana>, |
| 30 | +/// } |
| 31 | +/// |
| 32 | +/// impl Drop for FruitBox { |
| 33 | +/// fn drop(&mut self) { |
| 34 | +/// unsafe { |
| 35 | +/// // Explicit ordering in which field destructors are run specified in the intuitive |
| 36 | +/// // location – the destructor of the structure containing the fields. |
| 37 | +/// // Moreover, one can now reorder fields within the struct however much they want. |
| 38 | +/// ManuallyDrop::drop(&mut self.peach); |
| 39 | +/// ManuallyDrop::drop(&mut self.banana); |
| 40 | +/// } |
| 41 | +/// // After destructor for `FruitBox` runs (this function), the destructor for Melon gets |
| 42 | +/// // invoked in the usual manner, as it is not wrapped in `ManuallyDrop`. |
| 43 | +/// } |
| 44 | +/// } |
| 45 | +/// ``` |
| 46 | +#[stable(feature = "manually_drop", since = "1.20.0")] |
| 47 | +#[allow(unions_with_drop_fields)] |
| 48 | +#[derive(Copy)] |
| 49 | +pub union ManuallyDrop<T>{ value: T } |
| 50 | + |
| 51 | +impl<T> ManuallyDrop<T> { |
| 52 | + /// Wrap a value to be manually dropped. |
| 53 | + /// |
| 54 | + /// # Examples |
| 55 | + /// |
| 56 | + /// ```rust |
| 57 | + /// use std::mem::ManuallyDrop; |
| 58 | + /// ManuallyDrop::new(Box::new(())); |
| 59 | + /// ``` |
| 60 | + #[stable(feature = "manually_drop", since = "1.20.0")] |
| 61 | + #[rustc_const_unstable(feature = "const_manually_drop_new")] |
| 62 | + #[inline] |
| 63 | + pub const fn new(value: T) -> ManuallyDrop<T> { |
| 64 | + ManuallyDrop { value: value } |
| 65 | + } |
| 66 | + |
| 67 | + /// Extract the value from the ManuallyDrop container. |
| 68 | + /// |
| 69 | + /// # Examples |
| 70 | + /// |
| 71 | + /// ```rust |
| 72 | + /// use std::mem::ManuallyDrop; |
| 73 | + /// let x = ManuallyDrop::new(Box::new(())); |
| 74 | + /// let _: Box<()> = ManuallyDrop::into_inner(x); |
| 75 | + /// ``` |
| 76 | + #[stable(feature = "manually_drop", since = "1.20.0")] |
| 77 | + #[inline] |
| 78 | + pub fn into_inner(slot: ManuallyDrop<T>) -> T { |
| 79 | + unsafe { |
| 80 | + slot.value |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// Manually drops the contained value. |
| 85 | + /// |
| 86 | + /// # Safety |
| 87 | + /// |
| 88 | + /// This function runs the destructor of the contained value and thus the wrapped value |
| 89 | + /// now represents uninitialized data. It is up to the user of this method to ensure the |
| 90 | + /// uninitialized data is not actually used. |
| 91 | + #[stable(feature = "manually_drop", since = "1.20.0")] |
| 92 | + #[inline] |
| 93 | + pub unsafe fn drop(slot: &mut ManuallyDrop<T>) { |
| 94 | + ptr::drop_in_place(&mut slot.value) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[stable(feature = "manually_drop", since = "1.20.0")] |
| 99 | +impl<T> Deref for ManuallyDrop<T> { |
| 100 | + type Target = T; |
| 101 | + #[inline] |
| 102 | + fn deref(&self) -> &Self::Target { |
| 103 | + unsafe { |
| 104 | + &self.value |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#[stable(feature = "manually_drop", since = "1.20.0")] |
| 110 | +impl<T> DerefMut for ManuallyDrop<T> { |
| 111 | + #[inline] |
| 112 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 113 | + unsafe { |
| 114 | + &mut self.value |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[stable(feature = "manually_drop", since = "1.20.0")] |
| 120 | +impl<T: ::fmt::Debug> ::fmt::Debug for ManuallyDrop<T> { |
| 121 | + fn fmt(&self, fmt: &mut ::fmt::Formatter) -> ::fmt::Result { |
| 122 | + unsafe { |
| 123 | + fmt.debug_tuple("ManuallyDrop").field(&self.value).finish() |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +#[stable(feature = "manually_drop_impls", since = "1.22.0")] |
| 129 | +impl<T: Clone> Clone for ManuallyDrop<T> { |
| 130 | + fn clone(&self) -> Self { |
| 131 | + ManuallyDrop::new(self.deref().clone()) |
| 132 | + } |
| 133 | + |
| 134 | + fn clone_from(&mut self, source: &Self) { |
| 135 | + self.deref_mut().clone_from(source); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +#[stable(feature = "manually_drop_impls", since = "1.22.0")] |
| 140 | +impl<T: Default> Default for ManuallyDrop<T> { |
| 141 | + fn default() -> Self { |
| 142 | + ManuallyDrop::new(Default::default()) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#[stable(feature = "manually_drop_impls", since = "1.22.0")] |
| 147 | +impl<T: PartialEq> PartialEq for ManuallyDrop<T> { |
| 148 | + fn eq(&self, other: &Self) -> bool { |
| 149 | + self.deref().eq(other) |
| 150 | + } |
| 151 | + |
| 152 | + fn ne(&self, other: &Self) -> bool { |
| 153 | + self.deref().ne(other) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +#[stable(feature = "manually_drop_impls", since = "1.22.0")] |
| 158 | +impl<T: Eq> Eq for ManuallyDrop<T> {} |
| 159 | + |
| 160 | +#[stable(feature = "manually_drop_impls", since = "1.22.0")] |
| 161 | +impl<T: PartialOrd> PartialOrd for ManuallyDrop<T> { |
| 162 | + fn partial_cmp(&self, other: &Self) -> Option<::cmp::Ordering> { |
| 163 | + self.deref().partial_cmp(other) |
| 164 | + } |
| 165 | + |
| 166 | + fn lt(&self, other: &Self) -> bool { |
| 167 | + self.deref().lt(other) |
| 168 | + } |
| 169 | + |
| 170 | + fn le(&self, other: &Self) -> bool { |
| 171 | + self.deref().le(other) |
| 172 | + } |
| 173 | + |
| 174 | + fn gt(&self, other: &Self) -> bool { |
| 175 | + self.deref().gt(other) |
| 176 | + } |
| 177 | + |
| 178 | + fn ge(&self, other: &Self) -> bool { |
| 179 | + self.deref().ge(other) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +#[stable(feature = "manually_drop_impls", since = "1.22.0")] |
| 184 | +impl<T: Ord> Ord for ManuallyDrop<T> { |
| 185 | + fn cmp(&self, other: &Self) -> ::cmp::Ordering { |
| 186 | + self.deref().cmp(other) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +#[stable(feature = "manually_drop_impls", since = "1.22.0")] |
| 191 | +impl<T: ::hash::Hash> ::hash::Hash for ManuallyDrop<T> { |
| 192 | + fn hash<H: ::hash::Hasher>(&self, state: &mut H) { |
| 193 | + self.deref().hash(state); |
| 194 | + } |
| 195 | +} |
0 commit comments