Interior mutability and safety of ownership transfer in Rust
Eh2406:
If "non-owning" includes
&mut Vec<T>, then no. What if theVec<T>is at capacity and both copies callpush? Then you get a double free . More generally&mutmeans exclusive, so&mut v1[0]and&mut v2[0]is UB on its own.If by "non-owning" you mean
&Vec<T>, you will need better languish lawyers then me.
No, this is not what I mean. In my case there are two bitwise-equal copies of the same Vec (pointer, len and capacity are all equal), but one is essentially a "non-owning" ManuallyDrop<Box<Vec<T>>> and the other in an "owning" Box<Vec<T>>. This is quite typical in unsafe low-level data structures dealing with manually allocated memory*, but it is usually not possible for both instances to be observable externally.
(*) temporarily, inside a single function
Discussion in the ATmosphere