forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-drop.rs
197 lines (163 loc) · 5.01 KB
/
async-drop.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//@ run-pass
//@ check-run-results
#![feature(async_drop, impl_trait_in_assoc_type, noop_waker, async_closure)]
#![allow(incomplete_features, dead_code)]
//@ edition: 2021
// FIXME(zetanumbers): consider AsyncDestruct::async_drop cleanup tests
use core::future::{async_drop_in_place, AsyncDrop, Future};
use core::hint::black_box;
use core::mem::{self, ManuallyDrop};
use core::pin::{pin, Pin};
use core::task::{Context, Poll, Waker};
async fn test_async_drop<T>(x: T) {
let mut x = mem::MaybeUninit::new(x);
let dtor = pin!(unsafe { async_drop_in_place(x.as_mut_ptr()) });
test_idempotency(dtor).await;
}
fn test_idempotency<T>(mut x: Pin<&mut T>) -> impl Future<Output = ()> + '_
where
T: Future<Output = ()>,
{
core::future::poll_fn(move |cx| {
assert_eq!(x.as_mut().poll(cx), Poll::Ready(()));
assert_eq!(x.as_mut().poll(cx), Poll::Ready(()));
Poll::Ready(())
})
}
fn main() {
let waker = Waker::noop();
let mut cx = Context::from_waker(&waker);
let i = 13;
let fut = pin!(async {
test_async_drop(Int(0)).await;
test_async_drop(AsyncInt(0)).await;
test_async_drop([AsyncInt(1), AsyncInt(2)]).await;
test_async_drop((AsyncInt(3), AsyncInt(4))).await;
test_async_drop(5).await;
let j = 42;
test_async_drop(&i).await;
test_async_drop(&j).await;
test_async_drop(AsyncStruct { b: AsyncInt(8), a: AsyncInt(7), i: 6 }).await;
test_async_drop(ManuallyDrop::new(AsyncInt(9))).await;
let foo = AsyncInt(10);
test_async_drop(AsyncReference { foo: &foo }).await;
let foo = AsyncInt(11);
test_async_drop(|| {
black_box(foo);
let foo = AsyncInt(10);
foo
}).await;
test_async_drop(AsyncEnum::A(AsyncInt(12))).await;
test_async_drop(AsyncEnum::B(SyncInt(13))).await;
test_async_drop(SyncInt(14)).await;
test_async_drop(SyncThenAsync {
i: 15,
a: AsyncInt(16),
b: SyncInt(17),
c: AsyncInt(18),
}).await;
let async_drop_fut = pin!(core::future::async_drop(AsyncInt(19)));
test_idempotency(async_drop_fut).await;
let foo = AsyncInt(20);
test_async_drop(async || {
black_box(foo);
let foo = AsyncInt(19);
// Await point there, but this is async closure so it's fine
black_box(core::future::ready(())).await;
foo
}).await;
test_async_drop(AsyncUnion { signed: 21 }).await;
});
let res = fut.poll(&mut cx);
assert_eq!(res, Poll::Ready(()));
}
struct AsyncInt(i32);
impl AsyncDrop for AsyncInt {
type Dropper<'a> = impl Future<Output = ()>;
fn async_drop(self: Pin<&mut Self>) -> Self::Dropper<'_> {
async move {
println!("AsyncInt::Dropper::poll: {}", self.0);
}
}
}
struct SyncInt(i32);
impl Drop for SyncInt {
fn drop(&mut self) {
println!("SyncInt::drop: {}", self.0);
}
}
struct SyncThenAsync {
i: i32,
a: AsyncInt,
b: SyncInt,
c: AsyncInt,
}
impl Drop for SyncThenAsync {
fn drop(&mut self) {
println!("SyncThenAsync::drop: {}", self.i);
}
}
struct AsyncReference<'a> {
foo: &'a AsyncInt,
}
impl AsyncDrop for AsyncReference<'_> {
type Dropper<'a> = impl Future<Output = ()> where Self: 'a;
fn async_drop(self: Pin<&mut Self>) -> Self::Dropper<'_> {
async move {
println!("AsyncReference::Dropper::poll: {}", self.foo.0);
}
}
}
struct Int(i32);
struct AsyncStruct {
i: i32,
a: AsyncInt,
b: AsyncInt,
}
impl AsyncDrop for AsyncStruct {
type Dropper<'a> = impl Future<Output = ()>;
fn async_drop(self: Pin<&mut Self>) -> Self::Dropper<'_> {
async move {
println!("AsyncStruct::Dropper::poll: {}", self.i);
}
}
}
enum AsyncEnum {
A(AsyncInt),
B(SyncInt),
}
impl AsyncDrop for AsyncEnum {
type Dropper<'a> = impl Future<Output = ()>;
fn async_drop(mut self: Pin<&mut Self>) -> Self::Dropper<'_> {
async move {
let new_self = match &*self {
AsyncEnum::A(foo) => {
println!("AsyncEnum(A)::Dropper::poll: {}", foo.0);
AsyncEnum::B(SyncInt(foo.0))
}
AsyncEnum::B(foo) => {
println!("AsyncEnum(B)::Dropper::poll: {}", foo.0);
AsyncEnum::A(AsyncInt(foo.0))
}
};
mem::forget(mem::replace(&mut *self, new_self));
}
}
}
// FIXME(zetanumbers): Disallow types with `AsyncDrop` in unions
union AsyncUnion {
signed: i32,
unsigned: u32,
}
impl AsyncDrop for AsyncUnion {
type Dropper<'a> = impl Future<Output = ()>;
fn async_drop(self: Pin<&mut Self>) -> Self::Dropper<'_> {
async move {
println!(
"AsyncUnion::Dropper::poll: {}, {}",
unsafe { self.signed },
unsafe { self.unsigned },
);
}
}
}