Skip to content

Commit 2199d76

Browse files
committed
Subpart7 for async drop (major1) - library changes
1 parent 4c1f383 commit 2199d76

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ declare_features! (
360360
(unstable, associated_type_defaults, "1.2.0", Some(29661)),
361361
/// Allows `async || body` closures.
362362
(unstable, async_closure, "1.37.0", Some(62290)),
363+
/// Allows implementing `AsyncDrop`.
364+
(incomplete, async_drop, "CURRENT_RUSTC_VERSION", Some(126482)),
363365
/// Allows `#[track_caller]` on async functions.
364366
(unstable, async_fn_track_caller, "1.73.0", Some(110011)),
365367
/// Allows `for await` loops.

library/core/src/future/async_drop.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#![unstable(feature = "async_drop", issue = "126482")]
2+
3+
#[allow(unused_imports)]
4+
use core::future::Future;
5+
6+
#[allow(unused_imports)]
7+
use crate::pin::Pin;
8+
#[allow(unused_imports)]
9+
use 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"]
30+
pub 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"]
48+
pub 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+
}

library/core/src/future/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
use crate::ptr::NonNull;
1313
use crate::task::Context;
1414

15+
mod async_drop;
1516
mod future;
1617
mod into_future;
1718
mod join;
1819
mod pending;
1920
mod poll_fn;
2021
mod ready;
2122

23+
#[cfg(not(bootstrap))]
24+
#[unstable(feature = "async_drop", issue = "126482")]
25+
pub use async_drop::{async_drop_in_place, AsyncDrop};
2226
#[stable(feature = "into_future", since = "1.64.0")]
2327
pub use into_future::IntoFuture;
2428
#[stable(feature = "future_readiness_fns", since = "1.48.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ edition: 2021
2+
3+
use std::future::AsyncDrop; //~ ERROR use of unstable library feature 'async_drop'
4+
use std::pin::Pin;
5+
6+
struct Foo {}
7+
8+
impl Drop for Foo {
9+
fn drop(&mut self) {}
10+
}
11+
12+
impl AsyncDrop for Foo { //~ ERROR use of unstable library feature 'async_drop'
13+
async fn drop(self: Pin<&mut Self>) {} //~ ERROR use of unstable library feature 'async_drop'
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0658]: use of unstable library feature 'async_drop'
2+
--> $DIR/feature-gate-async-drop.rs:3:5
3+
|
4+
LL | use std::future::AsyncDrop;
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #126482 <https://github.com/rust-lang/rust/issues/126482> for more information
8+
= help: add `#![feature(async_drop)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: use of unstable library feature 'async_drop'
12+
--> $DIR/feature-gate-async-drop.rs:13:5
13+
|
14+
LL | async fn drop(self: Pin<&mut Self>) {}
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #126482 <https://github.com/rust-lang/rust/issues/126482> for more information
18+
= help: add `#![feature(async_drop)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error[E0658]: use of unstable library feature 'async_drop'
22+
--> $DIR/feature-gate-async-drop.rs:12:6
23+
|
24+
LL | impl AsyncDrop for Foo {
25+
| ^^^^^^^^^
26+
|
27+
= note: see issue #126482 <https://github.com/rust-lang/rust/issues/126482> for more information
28+
= help: add `#![feature(async_drop)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
31+
error: aborting due to 3 previous errors
32+
33+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)