CompactString::as_mut_bytes returns a &mut reference to uninitialized bytes
goffrie opened this issue · comments
e.g. unsafe { CompactString::with_capacity(1234).as_mut_bytes() }
returns a slice of length 1234. This is questionable (even if it hasn't been declared officially UB yet) and IMO should return a &mut [MaybeUninit<u8>]
, e.g. by the precedent set by Vec::spare_capacity_mut
.
Also, the documentation is not very clear that the returned slice includes those uninitialized bytes (i.e. those past len
up to capacity
), and doesn't disclose that there may be uninitialized bytes hiding in the result. It's especially confusing since the documentation lists the methods in order as as_str()
, as_mut_str()
, as_bytes()
, and as_mut_bytes()
- but only the last one reveals the uninitialized part.
Great callout! The as_mut_bytes(...)
API definitely needs to be improved (or removed?), from a types and documentation perspective. I'll try and get to this before we release v0.8.0
since it would be a public API change.
Not sure if the API for as_mut_bytes(...)
should only returns the bytes up-to len
, or if we keep the current behavior and just document it. I'll need to think on this/look at what String
and others do.