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]
158pub struct ManuallyDrop<T: ?Sized> {
159 value: MaybeDangling<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: MaybeDangling::new(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 // Cannot use `MaybeDangling::into_inner` as that does not yet have the desired semantics.
201 // SAFETY: We know this is a valid `T`. `slot` will not be dropped.
202 unsafe { (&raw const slot).cast::<T>().read() }
203 }
204
205 /// Takes the value from the `ManuallyDrop<T>` container out.
206 ///
207 /// This method is primarily intended for moving out values in drop.
208 /// Instead of using [`ManuallyDrop::drop`] to manually drop the value,
209 /// you can use this method to take the value and use it however desired.
210 ///
211 /// Whenever possible, it is preferable to use [`into_inner`][`ManuallyDrop::into_inner`]
212 /// instead, which prevents duplicating the content of the `ManuallyDrop<T>`.
213 ///
214 /// # Safety
215 ///
216 /// This function semantically moves out the contained value without preventing further usage,
217 /// leaving the state of this container unchanged.
218 /// It is your responsibility to ensure that this `ManuallyDrop` is not used again.
219 ///
220 #[must_use = "if you don't need the value, you can use `ManuallyDrop::drop` instead"]
221 #[stable(feature = "manually_drop_take", since = "1.42.0")]
222 #[rustc_const_unstable(feature = "const_manually_drop_take", issue = "148773")]
223 #[inline]
224 pub const unsafe fn take(slot: &mut ManuallyDrop<T>) -> T {
225 // SAFETY: we are reading from a reference, which is guaranteed
226 // to be valid for reads.
227 unsafe { ptr::read(slot.value.as_ref()) }
228 }
229}
230
231impl<T: ?Sized> ManuallyDrop<T> {
232 /// Manually drops the contained value.
233 ///
234 /// This is exactly equivalent to calling [`ptr::drop_in_place`] with a
235 /// pointer to the contained value. As such, unless the contained value is a
236 /// packed struct, the destructor will be called in-place without moving the
237 /// value, and thus can be used to safely drop [pinned] data.
238 ///
239 /// If you have ownership of the value, you can use [`ManuallyDrop::into_inner`] instead.
240 ///
241 /// # Safety
242 ///
243 /// This function runs the destructor of the contained value. Other than changes made by
244 /// the destructor itself, the memory is left unchanged, and so as far as the compiler is
245 /// concerned still holds a bit-pattern which is valid for the type `T`.
246 ///
247 /// However, this "zombie" value should not be exposed to safe code, and this function
248 /// should not be called more than once. To use a value after it's been dropped, or drop
249 /// a value multiple times, can cause Undefined Behavior (depending on what `drop` does).
250 /// This is normally prevented by the type system, but users of `ManuallyDrop` must
251 /// uphold those guarantees without assistance from the compiler.
252 ///
253 /// [pinned]: crate::pin
254 #[stable(feature = "manually_drop", since = "1.20.0")]
255 #[inline]
256 #[rustc_const_unstable(feature = "const_drop_in_place", issue = "109342")]
257 pub const unsafe fn drop(slot: &mut ManuallyDrop<T>)
258 where
259 T: [const] Destruct,
260 {
261 // SAFETY: we are dropping the value pointed to by a mutable reference
262 // which is guaranteed to be valid for writes.
263 // It is up to the caller to make sure that `slot` isn't dropped again.
264 unsafe { ptr::drop_in_place(slot.value.as_mut()) }
265 }
266}
267
268#[stable(feature = "manually_drop", since = "1.20.0")]
269#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
270const impl<T: ?Sized> Deref for ManuallyDrop<T> {
271 type Target = T;
272 #[inline(always)]
273 fn deref(&self) -> &T {
274 self.value.as_ref()
275 }
276}
277
278#[stable(feature = "manually_drop", since = "1.20.0")]
279#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
280const impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
281 #[inline(always)]
282 fn deref_mut(&mut self) -> &mut T {
283 self.value.as_mut()
284 }
285}
286
287#[unstable(feature = "deref_pure_trait", issue = "87121")]
288unsafe impl<T: ?Sized> DerefPure for ManuallyDrop<T> {}
289
290#[stable(feature = "manually_drop", since = "1.20.0")]
291impl<T: ?Sized + Eq> Eq for ManuallyDrop<T> {}
292
293#[stable(feature = "manually_drop", since = "1.20.0")]
294impl<T: ?Sized + PartialEq> PartialEq for ManuallyDrop<T> {
295 fn eq(&self, other: &Self) -> bool {
296 self.value.as_ref().eq(other.value.as_ref())
297 }
298}
299
300#[stable(feature = "manually_drop", since = "1.20.0")]
301impl<T: ?Sized> StructuralPartialEq for ManuallyDrop<T> {}
302
303#[stable(feature = "manually_drop", since = "1.20.0")]
304impl<T: ?Sized + Ord> Ord for ManuallyDrop<T> {
305 fn cmp(&self, other: &Self) -> Ordering {
306 self.value.as_ref().cmp(other.value.as_ref())
307 }
308}
309
310#[stable(feature = "manually_drop", since = "1.20.0")]
311impl<T: ?Sized + PartialOrd> PartialOrd for ManuallyDrop<T> {
312 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
313 self.value.as_ref().partial_cmp(other.value.as_ref())
314 }
315}
316
317#[stable(feature = "manually_drop", since = "1.20.0")]
318impl<T: ?Sized + Hash> Hash for ManuallyDrop<T> {
319 fn hash<H: Hasher>(&self, state: &mut H) {
320 self.value.as_ref().hash(state);
321 }
322}