I'm in a situation where I'm working with data wrapped in an Arc
, and I sometimes end up using into_raw
to get the raw pointer to the underlying data. My use case also calls for type-erasure, so the raw pointer often gets cast to a *const c_void
, then cast back to the appropriate concrete type when re-constructing the Arc
.
I've run into a situation where it would be useful to be able to clone the Arc
without needing to know the concrete type of the underlying data. As I understand it, it should be safe to reconstruct the Arc
with a dummy type solely for the purpose of calling clone
, so long as I never actually dereference the data. So, for example, this should be safe:
pub unsafe fn clone_raw(handle: *const c_void) -> *const c_void {
let original = Arc::from_raw(handle);
let copy = original.clone();
mem::forget(original);
Arc::into_raw(copy)
}
Is there anything that I'm missing that would make this actually unsafe? Also, I assume the answer would apply to Rc
as well, but if there are any differences please let me know!
I've also posted this to StackOverflow, so feel free to answer there as well!