clarify provenance issue in new_owned

This commit is contained in:
missing 2022-12-25 22:43:23 -06:00
parent b9e3c090d7
commit eb64585d3c

View file

@ -94,6 +94,9 @@ impl Stringish {
/// Alternatively, use the [`Ish::ish`] method.
#[must_use]
pub fn new_owned(s: String) -> Self {
// converting to a `Vec` is necessary since `s.as_mut_ptr()` would call `str::as_mut_ptr`
// through deref coercion, which would only give us a pointer with provenance to the
// initialized part of the `String`, which is not enough to deallocate.
let mut v = ManuallyDrop::new(s.into_bytes());
Self {
// SAFETY: `v.as_mut_ptr()` is never null