Is it safe to clone a type-erased Arc via raw pointer?

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();

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!

It's not quite safe with c_void (may work in practice, but invalid in theory). If you cast the pointer to the right type of the pointer, then it's OK.

It's kinda weird that the original needs to be forgotten, but it seems like the way to do it.

Yeah, I realize this would work if you actually know the type of the underlying data. My case specifically calls for working with type-erased data, so I can't (easily) determine the type of the underlying data.

Someone on Stack Overflow was able to show that it isn't safe, so looks like that's my answer :sweat_smile: Thanks for the help!