In the from_raw_parts docs it says:
This is highly unsafe, due to the number of invariants that aren't checked: 1. ptr needs to have been previously allocated via String/Vec<T> (at least, it's highly likely to be incorrect if it wasn't). 2. ptr's T needs to have the same size and alignment as it was allocated with. 3. length needs to be less than or equal to capacity. 4. capacity needs to be the capacity that the pointer was allocated with.
I understand requirements #3 and #4 but not #1 and #2.
- is the usage of
from_raw_partsin that snippet I linked to safe? if not, why not?
- why does T needs to have the same size and alignment as it was allocated with? is there a simple example I can use to see how I could run into problems if T doesn’t have the same size?
- why does it say “ptr needs to have been previously allocated via String/Vec”?