Skip to main content

core/mem/
manually_drop.rs

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