You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Update safety requirements of Vec::from_raw_parts? #94950
length needs to be less than or equal to capacity.
capacity needs to be the capacity that the pointer was allocated with.
I think the first requirement should also add “The first length elements starting from ptr are initialized”. And the second requirement could be relaxed into “capacity needs to be less than or equal to the capacity that the pointer was allocated with”, this is because Allocator::deallocate does not require specifying exact size that the pointer was allocated with.
The Allocator API is unstable, I don't think it would make sense to adjust documentation to guarantee unstable behavior.
It's actually even more tricky than your wording, because it also needs to be at least the size of the Layout that was used when allocating the memory block, which you may or may not even know about depending on whether the Vec itself was using the Allocator or you yourself are constructing a Vec from a memory block you manually allocated. So given that Vec probably doesn't want to guarantee any growing behavior, you can't know the Layout that the Vec used, and thus it wouldn't be safe (in general) to use any capacity that is less than what you got from the Vec.
I just noticed that from_raw_parts and from_raw_parts_in doesn't list a requirement for the first length elements of ptrmust be initialized. A possible wording would be
Documentation currently includes this line: "The first length values must be properly initialized values of type T."
The suggestion about less strict capacity does not seem to be the current reality as Layout for allocation and drop must be the same size and alignment. So there is nothing more to do here.
Activity
CryZe commentedon Mar 15, 2022
Two opinions:
TheDestroyer19 commentedon Apr 21, 2022
I just noticed that
from_raw_partsandfrom_raw_parts_indoesn't list a requirement for the firstlengthelements ofptrmust be initialized. A possible wording would behkBst commentedon Jan 24, 2025
Documentation currently includes this line: "The first length values must be properly initialized values of type T."
The suggestion about less strict capacity does not seem to be the current reality as Layout for allocation and drop must be the same size and alignment. So there is nothing more to do here.
Can be closed.