44
55// WARNING: If you would ever want to modify this test,
66// please consider modifying rustc's async drop test at
7- // `tests/ui/async-await/async-drop.rs`.
7+ // `tests/ui/async-await/async-drop/async-drop-initial .rs`.
88
99#![ feature( async_drop, impl_trait_in_assoc_type) ]
1010#![ allow( incomplete_features, dead_code) ]
1111
1212// FIXME(zetanumbers): consider AsyncDestruct::async_drop cleanup tests
13- use core:: future:: { AsyncDrop , Future , async_drop_in_place } ;
13+ use core:: future:: { async_drop_in_place , AsyncDrop , Future } ;
1414use core:: hint:: black_box;
1515use core:: mem:: { self , ManuallyDrop } ;
16- use core:: pin:: { Pin , pin } ;
16+ use core:: pin:: { pin , Pin } ;
1717use core:: task:: { Context , Poll , Waker } ;
1818
1919async fn test_async_drop < T > ( x : T ) {
@@ -68,7 +68,8 @@ fn main() {
6868 test_async_drop( SyncThenAsync { i: 15 , a: AsyncInt ( 16 ) , b: SyncInt ( 17 ) , c: AsyncInt ( 18 ) } )
6969 . await ;
7070
71- let async_drop_fut = pin!( core:: future:: async_drop( AsyncInt ( 19 ) ) ) ;
71+ let mut ptr19 = mem:: MaybeUninit :: new( AsyncInt ( 19 ) ) ;
72+ let async_drop_fut = pin!( unsafe { async_drop_in_place( ptr19. as_mut_ptr( ) ) } ) ;
7273 test_idempotency( async_drop_fut) . await ;
7374
7475 let foo = AsyncInt ( 20 ) ;
@@ -89,13 +90,14 @@ fn main() {
8990
9091struct AsyncInt ( i32 ) ;
9192
93+ impl Drop for AsyncInt {
94+ fn drop ( & mut self ) {
95+ println ! ( "AsyncInt::drop: {}" , self . 0 ) ;
96+ }
97+ }
9298impl AsyncDrop for AsyncInt {
93- type Dropper < ' a > = impl Future < Output = ( ) > ;
94-
95- fn async_drop ( self : Pin < & mut Self > ) -> Self :: Dropper < ' _ > {
96- async move {
97- println ! ( "AsyncInt::Dropper::poll: {}" , self . 0 ) ;
98- }
99+ async fn drop ( self : Pin < & mut Self > ) {
100+ println ! ( "AsyncInt::async_drop: {}" , self . 0 ) ;
99101 }
100102}
101103
@@ -124,16 +126,14 @@ struct AsyncReference<'a> {
124126 foo : & ' a AsyncInt ,
125127}
126128
129+ impl Drop for AsyncReference < ' _ > {
130+ fn drop ( & mut self ) {
131+ println ! ( "AsyncReference::drop: {}" , self . foo. 0 ) ;
132+ }
133+ }
127134impl AsyncDrop for AsyncReference < ' _ > {
128- type Dropper < ' a >
129- = impl Future < Output = ( ) >
130- where
131- Self : ' a ;
132-
133- fn async_drop ( self : Pin < & mut Self > ) -> Self :: Dropper < ' _ > {
134- async move {
135- println ! ( "AsyncReference::Dropper::poll: {}" , self . foo. 0 ) ;
136- }
135+ async fn drop ( self : Pin < & mut Self > ) {
136+ println ! ( "AsyncReference::async_drop: {}" , self . foo. 0 ) ;
137137 }
138138}
139139
@@ -145,13 +145,14 @@ struct AsyncStruct {
145145 b : AsyncInt ,
146146}
147147
148+ impl Drop for AsyncStruct {
149+ fn drop ( & mut self ) {
150+ println ! ( "AsyncStruct::drop: {}" , self . i) ;
151+ }
152+ }
148153impl AsyncDrop for AsyncStruct {
149- type Dropper < ' a > = impl Future < Output = ( ) > ;
150-
151- fn async_drop ( self : Pin < & mut Self > ) -> Self :: Dropper < ' _ > {
152- async move {
153- println ! ( "AsyncStruct::Dropper::poll: {}" , self . i) ;
154- }
154+ async fn drop ( self : Pin < & mut Self > ) {
155+ println ! ( "AsyncStruct::async_drop: {}" , self . i) ;
155156 }
156157}
157158
@@ -160,23 +161,34 @@ enum AsyncEnum {
160161 B ( SyncInt ) ,
161162}
162163
164+ impl Drop for AsyncEnum {
165+ fn drop ( & mut self ) {
166+ let new_self = match self {
167+ AsyncEnum :: A ( foo) => {
168+ println ! ( "AsyncEnum(A)::drop: {}" , foo. 0 ) ;
169+ AsyncEnum :: B ( SyncInt ( foo. 0 ) )
170+ }
171+ AsyncEnum :: B ( foo) => {
172+ println ! ( "AsyncEnum(B)::drop: {}" , foo. 0 ) ;
173+ AsyncEnum :: A ( AsyncInt ( foo. 0 ) )
174+ }
175+ } ;
176+ mem:: forget ( mem:: replace ( & mut * self , new_self) ) ;
177+ }
178+ }
163179impl AsyncDrop for AsyncEnum {
164- type Dropper < ' a > = impl Future < Output = ( ) > ;
165-
166- fn async_drop ( mut self : Pin < & mut Self > ) -> Self :: Dropper < ' _ > {
167- async move {
168- let new_self = match & * self {
169- AsyncEnum :: A ( foo) => {
170- println ! ( "AsyncEnum(A)::Dropper::poll: {}" , foo. 0 ) ;
171- AsyncEnum :: B ( SyncInt ( foo. 0 ) )
172- }
173- AsyncEnum :: B ( foo) => {
174- println ! ( "AsyncEnum(B)::Dropper::poll: {}" , foo. 0 ) ;
175- AsyncEnum :: A ( AsyncInt ( foo. 0 ) )
176- }
177- } ;
178- mem:: forget ( mem:: replace ( & mut * self , new_self) ) ;
179- }
180+ async fn drop ( mut self : Pin < & mut Self > ) {
181+ let new_self = match & * self {
182+ AsyncEnum :: A ( foo) => {
183+ println ! ( "AsyncEnum(A)::async_drop: {}" , foo. 0 ) ;
184+ AsyncEnum :: B ( SyncInt ( foo. 0 ) )
185+ }
186+ AsyncEnum :: B ( foo) => {
187+ println ! ( "AsyncEnum(B)::async_drop: {}" , foo. 0 ) ;
188+ AsyncEnum :: A ( AsyncInt ( foo. 0 ) )
189+ }
190+ } ;
191+ mem:: forget ( mem:: replace ( & mut * self , new_self) ) ;
180192 }
181193}
182194
@@ -186,14 +198,19 @@ union AsyncUnion {
186198 unsigned : u32 ,
187199}
188200
201+ impl Drop for AsyncUnion {
202+ fn drop ( & mut self ) {
203+ println ! (
204+ "AsyncUnion::drop: {}, {}" ,
205+ unsafe { self . signed } ,
206+ unsafe { self . unsigned } ,
207+ ) ;
208+ }
209+ }
189210impl AsyncDrop for AsyncUnion {
190- type Dropper < ' a > = impl Future < Output = ( ) > ;
191-
192- fn async_drop ( self : Pin < & mut Self > ) -> Self :: Dropper < ' _ > {
193- async move {
194- println ! ( "AsyncUnion::Dropper::poll: {}, {}" , unsafe { self . signed } , unsafe {
195- self . unsigned
196- } ) ;
197- }
211+ async fn drop ( self : Pin < & mut Self > ) {
212+ println ! ( "AsyncUnion::async_drop: {}, {}" , unsafe { self . signed } , unsafe {
213+ self . unsigned
214+ } ) ;
198215 }
199216}
0 commit comments