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
Would it make sense to change most of the APIs listed above in MaybeUninit to return RefInit rather than having this be an option on the side?
All such APIs in their current state will cause a destructor leak without future use of unsafe, and with RefInit's leak function the old behavior can be preserved if desired while also making it more explicit that destructors will be leaked unless other measures are taken.
For assume_init_mut / slice_assume_init_mut there should be a separate method for the InitRef version though, because in those you aren't necessarily initializing elements on the spot.
ajwock
changed the title
RefInit: Dropper for initialized MaybeUninit references
RefInit: RAII wrapper for initialized MaybeUninit references
Feb 20, 2024
We talked about this in the libs-api call today. People were uncertain about whether the motivation was strong enough. We found ourselves speculating a bit about use cases.
Since this can be implemented as a library, perhaps you could do that, and then add some examples in that library that demonstrate the use cases clearly. That would be the best next step here to help with future consideration of this.
Proposal
Problem statement
Values initialized by MaybeUninit ref APIs and MaybeUninit slice APIs will have their destructors leaked unless the user manually drops them.
Motivating examples or use cases
rust-lang/rust#80376 MaybeUninit::write_slice_cloned() makes it very easy to accidentally leak
https://users.rust-lang.org/t/is-there-a-way-to-copy-t-into-mut-maybeuninit-t-without-unsafe/51301/2
This is also an issue that has come up in discussion with this ACP: #156
A similar solution to the below is also used in MaybeUninit::clone_from_slice to Drop elements that are already initialized in the case of a panic: https://github.com/rust-lang/rust/blob/02438348b9c26c0d657cc7b990fd1f45a8c0c736/library/core/src/mem/maybe_uninit.rs#L1128
Solution sketch
Introducing: RefInit.
An RAII wrapper that wraps the now-initialized reference, and drops the elements inside of it when it is dropped.
A rough sketch demonstrating what a solution might look like for the current set of APIs (including pending ones from #156 )
Alternatives
Alternatives are rather lacking here. Manually dropping is an alternative, albeit not a safe alternative.
Linking relevant APIs
rust-lang/rust#63567
rust-lang/rust#79995
#156
The text was updated successfully, but these errors were encountered: