File tree Expand file tree Collapse file tree 2 files changed +39
-39
lines changed Expand file tree Collapse file tree 2 files changed +39
-39
lines changed Original file line number Diff line number Diff line change @@ -27,3 +27,41 @@ fn smoke() {
2727 drop ( x) ;
2828 drop ( y) ;
2929}
30+
31+ #[ test]
32+ fn const_drop_in_place ( ) {
33+ const COUNTER : usize = {
34+ use core:: cell:: Cell ;
35+
36+ let counter = Cell :: new ( 0 ) ;
37+
38+ // only exists to make `Drop` indirect impl
39+ #[ allow( dead_code) ]
40+ struct Test < ' a > ( Dropped < ' a > ) ;
41+
42+ struct Dropped < ' a > ( & ' a Cell < usize > ) ;
43+ impl const Drop for Dropped < ' _ > {
44+ fn drop ( & mut self ) {
45+ self . 0 . set ( self . 0 . get ( ) + 1 ) ;
46+ }
47+ }
48+
49+ let mut one = ManuallyDrop :: new ( Test ( Dropped ( & counter) ) ) ;
50+ let mut two = ManuallyDrop :: new ( Test ( Dropped ( & counter) ) ) ;
51+ let mut three = ManuallyDrop :: new ( Test ( Dropped ( & counter) ) ) ;
52+ assert ! ( counter. get( ) == 0 ) ;
53+ unsafe {
54+ ManuallyDrop :: drop ( & mut one) ;
55+ }
56+ assert ! ( counter. get( ) == 1 ) ;
57+ unsafe {
58+ ManuallyDrop :: drop ( & mut two) ;
59+ }
60+ assert ! ( counter. get( ) == 2 ) ;
61+ unsafe {
62+ ManuallyDrop :: drop ( & mut three) ;
63+ }
64+ counter. get ( )
65+ } ;
66+ assert_eq ! ( COUNTER , 3 ) ;
67+ }
Original file line number Diff line number Diff line change 11use core:: cell:: RefCell ;
22use core:: marker:: Freeze ;
3- use core:: mem:: { ManuallyDrop , MaybeUninit } ;
3+ use core:: mem:: MaybeUninit ;
44use core:: num:: NonZero ;
55use core:: ptr;
66use core:: ptr:: * ;
@@ -1045,41 +1045,3 @@ fn test_ptr_default() {
10451045 let default = PtrMutDefaultTest :: default ( ) ;
10461046 assert ! ( default . ptr. is_null( ) ) ;
10471047}
1048-
1049- #[ test]
1050- fn test_const_drop_in_place ( ) {
1051- const COUNTER : usize = {
1052- use core:: cell:: Cell ;
1053-
1054- let counter = Cell :: new ( 0 ) ;
1055-
1056- // only exists to make `Drop` indirect impl
1057- #[ allow( dead_code) ]
1058- struct Test < ' a > ( Dropped < ' a > ) ;
1059-
1060- struct Dropped < ' a > ( & ' a Cell < usize > ) ;
1061- impl const Drop for Dropped < ' _ > {
1062- fn drop ( & mut self ) {
1063- self . 0 . set ( self . 0 . get ( ) + 1 ) ;
1064- }
1065- }
1066-
1067- let mut one = ManuallyDrop :: new ( Test ( Dropped ( & counter) ) ) ;
1068- let mut two = ManuallyDrop :: new ( Test ( Dropped ( & counter) ) ) ;
1069- let mut three = ManuallyDrop :: new ( Test ( Dropped ( & counter) ) ) ;
1070- assert ! ( counter. get( ) == 0 ) ;
1071- unsafe {
1072- ManuallyDrop :: drop ( & mut one) ;
1073- }
1074- assert ! ( counter. get( ) == 1 ) ;
1075- unsafe {
1076- ManuallyDrop :: drop ( & mut two) ;
1077- }
1078- assert ! ( counter. get( ) == 2 ) ;
1079- unsafe {
1080- ManuallyDrop :: drop ( & mut three) ;
1081- }
1082- counter. get ( )
1083- } ;
1084- assert_eq ! ( COUNTER , 3 ) ;
1085- }
You can’t perform that action at this time.
0 commit comments