Fix issue with UninitVec and zero-sized types

This commit is contained in:
Andreas Longva 2021-01-19 15:02:23 +01:00
parent ef3477f411
commit 4b395523dd
1 changed files with 7 additions and 3 deletions

View File

@ -403,13 +403,17 @@ impl<'a, T> CsLaneMut<'a, T> {
/// Helper struct for working with uninitialized data in vectors. /// Helper struct for working with uninitialized data in vectors.
/// TODO: This doesn't belong here. /// TODO: This doesn't belong here.
struct UninitVec<T> { struct UninitVec<T> {
vec: Vec<T> vec: Vec<T>,
len: usize
} }
impl<T> UninitVec<T> { impl<T> UninitVec<T> {
pub fn from_len(len: usize) -> Self { pub fn from_len(len: usize) -> Self {
Self { Self {
vec: Vec::with_capacity(len) vec: Vec::with_capacity(len),
// We need to store len separately, because for zero-sized types,
// Vec::with_capacity(len) does not give vec.capacity() == len
len
} }
} }
@ -425,7 +429,7 @@ impl<T> UninitVec<T> {
/// It is undefined behavior to call this function unless *all* elements have been written to /// It is undefined behavior to call this function unless *all* elements have been written to
/// exactly once. /// exactly once.
pub unsafe fn assume_init(mut self) -> Vec<T> { pub unsafe fn assume_init(mut self) -> Vec<T> {
self.vec.set_len(self.vec.capacity()); self.vec.set_len(self.len);
self.vec self.vec
} }
} }