What I would like to do is 're-Box' that bignum_ptr again, and again, as needed. Is this possible?
If you mean creating many boxes from the same pointer without taking it out each time, no.
If you mean putting it in and out repeatedly and round-tripping every time via an integer, probably yes; however, I would be careful with code like that. Most likely, it will work, but be aware that the memory model for Rust is not formalized and the rules around pointer provenance may change in the future. Even the C and C++ standards (from where Rust memory model comes from) have open questions around those, including round-tripping via an integer type.
Furthermore, your code assumes a pointer fits in a u64
, which is likely true for most architectures, but maybe not all in the future.
At the very least, I suggest you use mem::transmute
rather than a cast.
In short: don't do it. There is likely a better design for what you are trying to achieve.
rehydrate
goes out of scope? Should the memory allocated for the box be freed or not? – user4815162342bignum_ptr
would be responsible for dropping, – Frank C.rehydrate
should be a reference, not a box:let rehyrdrate: &MyStruct = unsafe { &*(bignum_ptr as *const MyStruct) };
. When you need to free the box (and know that no outstanding references exist), you can usedrop(Box::from_raw(bignum_ptr as *mut MyStruct))
. – user4815162342usize
instead ofu64
. – trentcl