Essentially I want to do the following
struct MyStruct<'a> {
additional_data: u32,
ptr: &'a i32
}
fn test() {
let data = 52;
let obj = MyStruct {
additional_data: 0,
ptr: &data
};
let handle = thread::spawn(move || {
let obj = obj;
});
handle.join().unwrap();
// data gets dropped here
// so it lives longer than the created thread
// therefore it is safe
}
Of course this won't work because thread::spawn requires an argument that satisfy 'static lifetime bound. So my question is how can I do this move operation with unsafe code without using a Box (don't want heap allocations). I tried transmuting the obj to [u8;mem::size_of::<MyStruct>()] but I don't know if this is safe and it won't work either because generic const expressions are unstable.