compact_str icon indicating copy to clipboard operation
compact_str copied to clipboard

CompactString::as_mut_bytes returns a &mut reference to uninitialized bytes

Open goffrie opened this issue 1 year ago • 1 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.

goffrie avatar Oct 15 '23 06:10 goffrie