core/mem/manually_drop.rs
1use crate::marker::Destruct;
2use crate::ops::{Deref, DerefMut, DerefPure};
3use crate::ptr;
4
5/// A wrapper to inhibit the compiler from automatically calling `T`’s
6/// destructor. This wrapper is 0-cost.
7///
8/// `ManuallyDrop<T>` is guaranteed to have the same layout and bit validity as
9/// `T`, and is subject to the same layout optimizations as `T`. As a
10/// consequence, it has *no effect* on the assumptions that the compiler makes
11/// about its contents. For example, initializing a `ManuallyDrop<&mut T>` with
12/// [`mem::zeroed`] is undefined behavior. If you need to handle uninitialized
13/// data, use [`MaybeUninit<T>`] instead.
14///
15/// Note that accessing the value inside a `ManuallyDrop<T>` is safe. This means
16/// that a `ManuallyDrop<T>` whose content has been dropped must not be exposed
17/// through a public safe API. Correspondingly, `ManuallyDrop::drop` is unsafe.
18///
19/// # `ManuallyDrop` and drop order
20///
21/// Rust has a well-defined [drop order] of values. To make sure that fields or
22/// locals are dropped in a specific order, reorder the declarations such that
23/// the implicit drop order is the correct one.
24///
25/// It is possible to use `ManuallyDrop` to control the drop order, but this
26/// requires unsafe code and is hard to do correctly in the presence of
27/// unwinding.
28///
29/// For example, if you want to make sure that a specific field is dropped after
30/// the others, make it the last field of a struct:
31///
32/// ```
33/// struct Context;
34///
35/// struct Widget {
36/// children: Vec<Widget>,
37/// // `context` will be dropped after `children`.
38/// // Rust guarantees that fields are dropped in the order of declaration.
39/// context: Context,
40/// }
41/// ```
42///
43/// # Interaction with `Box`
44///
45/// Currently, if you have a `ManuallyDrop<T>`, where the type `T` is a `Box` or
46/// contains a `Box` inside, then dropping the `T` followed by moving the
47/// `ManuallyDrop<T>` is [considered to be undefined
48/// behavior](https://github.com/rust-lang/unsafe-code-guidelines/issues/245).
49/// That is, the following code causes undefined behavior:
50///
51/// ```no_run
52/// use std::mem::ManuallyDrop;
53///
54/// let mut x = ManuallyDrop::new(Box::new(42));
55/// unsafe {
56/// ManuallyDrop::drop(&mut x);
57/// }
58/// let y = x; // Undefined behavior!
59/// ```
60///
61/// This is [likely to change in the
62/// future](https://rust-lang.github.io/rfcs/3336-maybe-dangling.html). In the
63/// meantime, consider using [`MaybeUninit`] instead.
64///
65/// # Safety hazards when storing `ManuallyDrop` in a struct or an enum.
66///
67/// Special care is needed when all of the conditions below are met:
68/// * A struct or enum contains a `ManuallyDrop`.
69/// * The `ManuallyDrop` is not inside a `union`.
70/// * The struct or enum is part of public API, or is stored in a struct or an
71/// enum that is part of public API.
72/// * There is code that drops the contents of the `ManuallyDrop` field, and
73/// this code is outside the struct or enum's `Drop` implementation.
74///
75/// In particular, the following hazards may occur:
76///
77/// #### Storing generic types
78///
79/// If the `ManuallyDrop` contains a client-supplied generic type, the client
80/// might provide a `Box` as that type. This would cause undefined behavior when
81/// the struct or enum is later moved, as mentioned in the previous section. For
82/// example, the following code causes undefined behavior:
83///
84/// ```no_run
85/// use std::mem::ManuallyDrop;
86///
87/// pub struct BadOption<T> {
88/// // Invariant: Has been dropped if `is_some` is false.
89/// value: ManuallyDrop<T>,
90/// is_some: bool,
91/// }
92/// impl<T> BadOption<T> {
93/// pub fn new(value: T) -> Self {
94/// Self { value: ManuallyDrop::new(value), is_some: true }
95/// }
96/// pub fn change_to_none(&mut self) {
97/// if self.is_some {
98/// self.is_some = false;
99/// unsafe {
100/// // SAFETY: `value` hasn't been dropped yet, as per the invariant
101/// // (This is actually unsound!)
102/// ManuallyDrop::drop(&mut self.value);
103/// }
104/// }
105/// }
106/// }
107///
108/// // In another crate:
109///
110/// let mut option = BadOption::new(Box::new(42));
111/// option.change_to_none();
112/// let option2 = option; // Undefined behavior!
113/// ```
114///
115/// #### Deriving traits
116///
117/// Deriving `Debug`, `Clone`, `PartialEq`, `PartialOrd`, `Ord`, or `Hash` on
118/// the struct or enum could be unsound, since the derived implementations of
119/// these traits would access the `ManuallyDrop` field. For example, the
120/// following code causes undefined behavior:
121///
122/// ```no_run
123/// use std::mem::ManuallyDrop;
124///
125/// // This derive is unsound in combination with the `ManuallyDrop::drop` call.
126/// #[derive(Debug)]
127/// pub struct Foo {
128/// value: ManuallyDrop<String>,
129/// }
130/// impl Foo {
131/// pub fn new() -> Self {
132/// let mut temp = Self {
133/// value: ManuallyDrop::new(String::from("Unsafe rust is hard."))
134/// };
135/// unsafe {
136/// // SAFETY: `value` hasn't been dropped yet.
137/// ManuallyDrop::drop(&mut temp.value);
138/// }
139/// temp
140/// }
141/// }
142///
143/// // In another crate:
144///
145/// let foo = Foo::new();
146/// println!("{:?}", foo); // Undefined behavior!
147/// ```
148///
149/// [drop order]: https://doc.rust-lang.org/reference/destructors.html
150/// [`mem::zeroed`]: crate::mem::zeroed
151/// [`MaybeUninit<T>`]: crate::mem::MaybeUninit
152/// [`MaybeUninit`]: crate::mem::MaybeUninit
153#[stable(feature = "manually_drop", since = "1.20.0")]
154#[lang = "manually_drop"]
155#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
156#[repr(transparent)]
157#[rustc_pub_transparent]
158pub struct ManuallyDrop<T: ?Sized> {
159 value: T,
160}
161
162impl<T> ManuallyDrop<T> {
163 /// Wrap a value to be manually dropped.
164 ///
165 /// # Examples
166 ///
167 /// ```rust
168 /// use std::mem::ManuallyDrop;
169 /// let mut x = ManuallyDrop::new(String::from("Hello World!"));
170 /// x.truncate(5); // You can still safely operate on the value
171 /// assert_eq!(*x, "Hello");
172 /// // But `Drop` will not be run here
173 /// # // FIXME(https://github.com/rust-lang/miri/issues/3670):
174 /// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.
175 /// # let _ = ManuallyDrop::into_inner(x);
176 /// ```
177 #[must_use = "if you don't need the wrapper, you can use `mem::forget` instead"]
178 #[stable(feature = "manually_drop", since = "1.20.0")]
179 #[rustc_const_stable(feature = "const_manually_drop", since = "1.32.0")]
180 #[inline(always)]
181 pub const fn new(value: T) -> ManuallyDrop<T> {
182 ManuallyDrop { value }
183 }
184
185 /// Extracts the value from the `ManuallyDrop` container.
186 ///
187 /// This allows the value to be dropped again.
188 ///
189 /// # Examples
190 ///
191 /// ```rust
192 /// use std::mem::ManuallyDrop;
193 /// let x = ManuallyDrop::new(Box::new(()));
194 /// let _: Box<()> = ManuallyDrop::into_inner(x); // This drops the `Box`.
195 /// ```
196 #[stable(feature = "manually_drop", since = "1.20.0")]
197 #[rustc_const_stable(feature = "const_manually_drop", since = "1.32.0")]
198 #[inline(always)]
199 pub const fn into_inner(slot: ManuallyDrop<T>) -> T {
200 slot.value
201 }
202
203 /// Takes the value from the `ManuallyDrop<T>` container out.
204 ///
205 /// This method is primarily intended for moving out values in drop.
206 /// Instead of using [`ManuallyDrop::drop`] to manually drop the value,
207 /// you can use this method to take the value and use it however desired.
208 ///
209 /// Whenever possible, it is preferable to use [`into_inner`][`ManuallyDrop::into_inner`]
210 /// instead, which prevents duplicating the content of the `ManuallyDrop<T>`.
211 ///
212 /// # Safety
213 ///
214 /// This function semantically moves out the contained value without preventing further usage,
215 /// leaving the state of this container unchanged.
216 /// It is your responsibility to ensure that this `ManuallyDrop` is not used again.
217 ///
218 #[must_use = "if you don't need the value, you can use `ManuallyDrop::drop` instead"]
219 #[stable(feature = "manually_drop_take", since = "1.42.0")]
220 #[rustc_const_unstable(feature = "const_manually_drop_take", issue = "148773")]
221 #[inline]
222 pub const unsafe fn take(slot: &mut ManuallyDrop<T>) -> T {
223 // SAFETY: we are reading from a reference, which is guaranteed
224 // to be valid for reads.
225 unsafe { ptr::read(&slot.value) }
226 }
227}
228
229impl<T: ?Sized> ManuallyDrop<T> {
230 /// Manually drops the contained value.
231 ///
232 /// This is exactly equivalent to calling [`ptr::drop_in_place`] with a
233 /// pointer to the contained value. As such, unless the contained value is a
234 /// packed struct, the destructor will be called in-place without moving the
235 /// value, and thus can be used to safely drop [pinned] data.
236 ///
237 /// If you have ownership of the value, you can use [`ManuallyDrop::into_inner`] instead.
238 ///
239 /// # Safety
240 ///
241 /// This function runs the destructor of the contained value. Other than changes made by
242 /// the destructor itself, the memory is left unchanged, and so as far as the compiler is
243 /// concerned still holds a bit-pattern which is valid for the type `T`.
244 ///
245 /// However, this "zombie" value should not be exposed to safe code, and this function
246 /// should not be called more than once. To use a value after it's been dropped, or drop
247 /// a value multiple times, can cause Undefined Behavior (depending on what `drop` does).
248 /// This is normally prevented by the type system, but users of `ManuallyDrop` must
249 /// uphold those guarantees without assistance from the compiler.
250 ///
251 /// [pinned]: crate::pin
252 #[stable(feature = "manually_drop", since = "1.20.0")]
253 #[inline]
254 #[rustc_const_unstable(feature = "const_drop_in_place", issue = "109342")]
255 pub const unsafe fn drop(slot: &mut ManuallyDrop<T>)
256 where
257 T: [const] Destruct,
258 {
259 // SAFETY: we are dropping the value pointed to by a mutable reference
260 // which is guaranteed to be valid for writes.
261 // It is up to the caller to make sure that `slot` isn't dropped again.
262 unsafe { ptr::drop_in_place(&mut slot.value) }
263 }
264}
265
266#[stable(feature = "manually_drop", since = "1.20.0")]
267#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
268impl<T: ?Sized> const Deref for ManuallyDrop<T> {
269 type Target = T;
270 #[inline(always)]
271 fn deref(&self) -> &T {
272 &self.value
273 }
274}
275
276#[stable(feature = "manually_drop", since = "1.20.0")]
277#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
278impl<T: ?Sized> const DerefMut for ManuallyDrop<T> {
279 #[inline(always)]
280 fn deref_mut(&mut self) -> &mut T {
281 &mut self.value
282 }
283}
284
285#[unstable(feature = "deref_pure_trait", issue = "87121")]
286unsafe impl<T: ?Sized> DerefPure for ManuallyDrop<T> {}