core/future/
async_drop.rs

1#![unstable(feature = "async_drop", issue = "126482")]
2
3#[allow(unused_imports)]
4use core::future::Future;
5
6#[allow(unused_imports)]
7use crate::pin::Pin;
8#[allow(unused_imports)]
9use crate::task::{Context, Poll};
10
11/// Async version of Drop trait.
12///
13/// When a value is no longer needed, Rust will run a "destructor" on that value.
14/// The most common way that a value is no longer needed is when it goes out of
15/// scope. Destructors may still run in other circumstances, but we're going to
16/// focus on scope for the examples here. To learn about some of those other cases,
17/// please see [the reference] section on destructors.
18///
19/// [the reference]: https://doc.rust-lang.org/reference/destructors.html
20///
21/// ## `Copy` and ([`Drop`]|`AsyncDrop`) are exclusive
22///
23/// You cannot implement both [`Copy`] and ([`Drop`]|`AsyncDrop`) on the same type. Types that
24/// are `Copy` get implicitly duplicated by the compiler, making it very
25/// hard to predict when, and how often destructors will be executed. As such,
26/// these types cannot have destructors.
27#[cfg(not(bootstrap))]
28#[unstable(feature = "async_drop", issue = "126482")]
29#[lang = "async_drop"]
30pub trait AsyncDrop {
31    /// Executes the async destructor for this type.
32    ///
33    /// This method is called implicitly when the value goes out of scope,
34    /// and cannot be called explicitly.
35    ///
36    /// When this method has been called, `self` has not yet been deallocated.
37    /// That only happens after the method is over.
38    ///
39    /// # Panics
40    #[allow(async_fn_in_trait)]
41    async fn drop(self: Pin<&mut Self>);
42}
43
44/// Async drop.
45#[cfg(not(bootstrap))]
46#[unstable(feature = "async_drop", issue = "126482")]
47#[lang = "async_drop_in_place"]
48pub async unsafe fn async_drop_in_place<T: ?Sized>(_to_drop: *mut T) {
49    // Code here does not matter - this is replaced by the
50    // real implementation by the compiler.
51}