-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Consider adding Box::uninitialized function #46406
Comments
We eventually expect to introduce |
How would it work for |
It doesn't work for unsized That said, unlike some other fat pointer shenanigans, this is already possible to implement externally as you've shown – except the really fishy |
On second thought, getting the metadata for the fat pointer right is pretty tricky. The Edit: But to be clear this is just for the general case of unsized box contents. For slices, the metadata is just the length and there's |
Closed/fixed by #63291. |
The reasons for such a function are the same as for
std::mem::uninitialized
. I saw somewhere a discussion about deprecatingstd::mem::uninitialized
, but I cannot remember where. If this is the case, how to avoid the cost of initializing a huge structure in the heap that will be initialized again in a ffi function call (without writing c code)?Also, it would be great to have
Box::uninitialized_from_value
that take a reference to a value and return a uninitialized boxed value that could store a copy of the original value. This function is useful to implement clone for structures containing unsized types. For example:If instead of
[u32]
, we had a generic[T]
we would had to be very careful in the clone implementation, but it would be possible do to so using the stable compiler. The issue is that withoutBox::uninitialized_from_value
I think it would be impossible to create an efficient implementation forX::clone
(generic over the array item) without usingstd:heap
and the nightly compiler.Here is a possible implementation (I'm not sure it is correct):
This code is based on
Rc::allocate_for_ptr
(which is similar toArc::allocate_for_ptr
).Box::uninitialized_from_value
could be used inRc::allocate_for_ptr
, which indicates that there are uses cases for such a function.The text was updated successfully, but these errors were encountered: