Currently, the shrink_to and shrink_to_fit functions on Vec invoke the handle_alloc_error function which either panics or aborts the program. This is also the reason the functions are compiled out on builds which do not allow invoking the oom-handler, see the function definition of shrink_to in std, making it impossible to reduce the size of a vectors allocation safely.
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shrink_to", since = "1.56.0")]
pub fn shrink_to(&mut self, min_capacity: usize)
This can be solved with two different approaches.
Silent Failure
Currently the documentation for neither shrink_to nor shrink_to_fit make no guarantees that the allocation will actually be shrunk.
Vec::shrink_toThe capacity will remain at least as large as both the length and the supplied value.
The current allocator api proclaims for the shrink function that
Alllocator::shrinkIf this method returnsErr, then ownership of the memory block has not been transferred to this allocator, and the contents of the memory block are unaltered.
Therefore, shrinking could ignore the allocation error returned and proceed as if nothing happened.
try_shrink_*
This would be my preferred solution to the problem which would be to introduce a mirror of the try_reserve_* APIs but for shrinking, so the methods try_shrink_to and try_shrink_to_fit. This would also makes it clearer to the caller what the current allocation behaviour was.
Although, I am interested in if I am missing something, as I couldn't find any prior discussion about this.