-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confusing wording of Vec::from_raw_parts pointer allocation requirement #98780
Comments
@rustbot label A-docs |
Relevant: #99216 looks like it will fix this if approved, by explicitly allowing allocation using something other than Vec (which can be a Vec or even manual allocation) |
Hm, that PR does remove the text I pointed out, but it also adds this text:
Isn’t that equally problematic (albeit a slightly different but related problem, since it’s talking about the contents of the memory rather than its ), since it is actually fine for them to be properly initialized values of some other type |
Rust doesn't have typed memory, types only have effect on accesses, so if some memory can be interpreted as some type T and it's valid at that type, it's a properly initialized value of type T. Nothing remembers the "original type" of some data. Just like how you can cast a |
docs: be less harsh in wording for Vec::from_raw_parts In particular, be clear that it is sound to specify memory not originating from a previous `Vec` allocation. That is already suggested in other parts of the documentation about zero-alloc conversions to Box<[T]>. Incorporate a constraint from `slice::from_raw_parts` that was missing but needs to be fulfilled, since a `Vec` can be converted into a slice. Fixes rust-lang#98780.
docs: be less harsh in wording for Vec::from_raw_parts In particular, be clear that it is sound to specify memory not originating from a previous `Vec` allocation. That is already suggested in other parts of the documentation about zero-alloc conversions to Box<[T]>. Incorporate a constraint from `slice::from_raw_parts` that was missing but needs to be fulfilled, since a `Vec` can be converted into a slice. Fixes rust-lang#98780.
docs: be less harsh in wording for Vec::from_raw_parts In particular, be clear that it is sound to specify memory not originating from a previous `Vec` allocation. That is already suggested in other parts of the documentation about zero-alloc conversions to Box<[T]>. Incorporate a constraint from `slice::from_raw_parts` that was missing but needs to be fulfilled, since a `Vec` can be converted into a slice. Fixes rust-lang/rust#98780.
The Alternatives section of the documentation for
core::mem::transmute
has this to say about a better way to turn aVec<&X>
into aVec<Option<&X>>
:The documentation for
Vec<T>::from_raw_parts
has this to say about where you can get the pointer from:A strict reading, if I understand correctly, says that these two statements are contradictory. The
transmute
documentation says that it is safe to create aVec<&i32>
and then usefrom_raw_parts
to create aVec<Option<&i32>>
out of its pointer. But theVec
documentation says that, when creating aVec<T>
,ptr
must have previously been allocated via precisely and onlyVec<T>
, notVec<U>
for some other typeU
—not even ifU
happens to be transmutable fromT
, not even ifT
andU
are ABI-compatible, not only ifT
is arepr(transparent)
wrapper aroundU
, but onlyT
itself.I suspect it was meant to say it must have previously been allocated by
Vec
, notVec<T>
; after all, if it could only have been allocated byVec<T>
in the first place, the next bullet point would be redundant (because obviouslyT
has the same alignment asT
).The text was updated successfully, but these errors were encountered: