@@ -47,6 +47,7 @@ assert!(nums[0] == "2");
47
47
use std:: convert:: TryFrom ;
48
48
use std:: error:: Error ;
49
49
use std:: fmt;
50
+ use std:: mem:: ManuallyDrop ;
50
51
use std:: os:: raw:: c_void;
51
52
use std:: panic:: AssertUnwindSafe ;
52
53
use std:: time:: Duration ;
@@ -114,7 +115,7 @@ where
114
115
{
115
116
#[ derive( Debug ) ]
116
117
struct SyncContext < F , T > {
117
- closure : * mut F ,
118
+ closure : ManuallyDrop < F > ,
118
119
result : Option < std:: thread:: Result < T > > ,
119
120
}
120
121
@@ -123,12 +124,12 @@ where
123
124
F : FnOnce ( ) -> T ,
124
125
{
125
126
let sync_context: & mut SyncContext < F , T > = unsafe { & mut * ( context as * mut _ ) } ;
126
- let closure = unsafe { Box :: from_raw ( sync_context. closure ) } ;
127
+ let closure = unsafe { ManuallyDrop :: take ( & mut sync_context. closure ) } ;
127
128
sync_context. result = Some ( std:: panic:: catch_unwind ( AssertUnwindSafe ( closure) ) ) ;
128
129
}
129
130
130
131
let mut sync_context: SyncContext < F , T > = SyncContext {
131
- closure : Box :: into_raw ( Box :: new ( closure) ) ,
132
+ closure : ManuallyDrop :: new ( closure) ,
132
133
result : None ,
133
134
} ;
134
135
let func: dispatch_function_t = work_execute_closure :: < F , T > ;
@@ -139,7 +140,7 @@ where
139
140
Ok ( res) => {
140
141
if res. is_none ( ) {
141
142
// if the closure didn't run (for example when using `Once`), free the box
142
- std :: mem :: drop ( unsafe { Box :: from_raw ( sync_context. closure ) } ) ;
143
+ unsafe { ManuallyDrop :: drop ( & mut sync_context. closure ) ; } ;
143
144
}
144
145
res
145
146
}
0 commit comments