MaybeUninit::write_slice_cloned() makes it very easy to accidentally leak · Issue #80376 · rust-lang/rust (original) (raw)
It is expected behavior that objects assigned/written to MaybeUninit
might leak at some point (if assume_init()
or similar is not used). However, MaybeUninit::write_slice_cloned()
returns a &mut [T]
(which might be passed on to further functions, where its origin might not be apparent), and objects assigned to that might leak as well, which I think is less obvious.
I don't know if there is a way to improve the API to mitigate this problem, but I think at least the documentation should point out the potential leaking.
Currently, it is noted that already initialized values in MaybeUnint
will not be dropped:
/// Any already initalized elements will not be dropped. |
---|
However, it is not mentioned that assigning to the resulting slice might also lead to leaks.
MaybeUninit::write_slice_cloned()
was added in #79607
Tracking issue: #79995
See also https://users.rust-lang.org/t/is-there-a-way-to-copy-t-into-mut-maybeuninit-t-without-unsafe/51301