diff --git a/src/slice.rs b/src/slice.rs index 27c7b7c..08c3717 100644 --- a/src/slice.rs +++ b/src/slice.rs @@ -364,11 +364,7 @@ impl HalfFloatSliceExt for [f16] { #[inline] #[allow(clippy::uninit_vec)] fn to_f32_vec(&self) -> Vec { - let mut vec = Vec::with_capacity(self.len()); - // SAFETY: convert will initialize every value in the vector without reading them, - // so this is safe to do instead of double initialize from resize, and we're setting it to - // same value as capacity. - unsafe { vec.set_len(self.len()) }; + let mut vec = vec![0f32; self.len()]; self.convert_to_f32_slice(&mut vec); vec } @@ -377,11 +373,7 @@ impl HalfFloatSliceExt for [f16] { #[inline] #[allow(clippy::uninit_vec)] fn to_f64_vec(&self) -> Vec { - let mut vec = Vec::with_capacity(self.len()); - // SAFETY: convert will initialize every value in the vector without reading them, - // so this is safe to do instead of double initialize from resize, and we're setting it to - // same value as capacity. - unsafe { vec.set_len(self.len()) }; + let mut vec = vec![0f64; self.len()]; self.convert_to_f64_slice(&mut vec); vec } @@ -466,11 +458,7 @@ impl HalfFloatSliceExt for [bf16] { #[inline] #[allow(clippy::uninit_vec)] fn to_f32_vec(&self) -> Vec { - let mut vec = Vec::with_capacity(self.len()); - // SAFETY: convert will initialize every value in the vector without reading them, - // so this is safe to do instead of double initialize from resize, and we're setting it to - // same value as capacity. - unsafe { vec.set_len(self.len()) }; + let mut vec = vec![0f32; self.len()]; self.convert_to_f32_slice(&mut vec); vec } @@ -479,11 +467,7 @@ impl HalfFloatSliceExt for [bf16] { #[inline] #[allow(clippy::uninit_vec)] fn to_f64_vec(&self) -> Vec { - let mut vec = Vec::with_capacity(self.len()); - // SAFETY: convert will initialize every value in the vector without reading them, - // so this is safe to do instead of double initialize from resize, and we're setting it to - // same value as capacity. - unsafe { vec.set_len(self.len()) }; + let mut vec = vec![0f64; self.len()]; self.convert_to_f64_slice(&mut vec); vec } diff --git a/src/vec.rs b/src/vec.rs index 27ad3e7..48fb02a 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -134,22 +134,14 @@ impl HalfFloatVecExt for Vec { #[allow(clippy::uninit_vec)] fn from_f32_slice(slice: &[f32]) -> Self { - let mut vec = Vec::with_capacity(slice.len()); - // SAFETY: convert will initialize every value in the vector without reading them, - // so this is safe to do instead of double initialize from resize, and we're setting it to - // same value as capacity. - unsafe { vec.set_len(slice.len()) }; + let mut vec = vec![f16::from_bits(0); slice.len()]; vec.convert_from_f32_slice(slice); vec } #[allow(clippy::uninit_vec)] fn from_f64_slice(slice: &[f64]) -> Self { - let mut vec = Vec::with_capacity(slice.len()); - // SAFETY: convert will initialize every value in the vector without reading them, - // so this is safe to do instead of double initialize from resize, and we're setting it to - // same value as capacity. - unsafe { vec.set_len(slice.len()) }; + let mut vec = vec![f16::from_bits(0); slice.len()]; vec.convert_from_f64_slice(slice); vec } @@ -178,22 +170,14 @@ impl HalfFloatVecExt for Vec { #[allow(clippy::uninit_vec)] fn from_f32_slice(slice: &[f32]) -> Self { - let mut vec = Vec::with_capacity(slice.len()); - // SAFETY: convert will initialize every value in the vector without reading them, - // so this is safe to do instead of double initialize from resize, and we're setting it to - // same value as capacity. - unsafe { vec.set_len(slice.len()) }; + let mut vec = vec![bf16::from_bits(0); slice.len()]; vec.convert_from_f32_slice(slice); vec } #[allow(clippy::uninit_vec)] fn from_f64_slice(slice: &[f64]) -> Self { - let mut vec = Vec::with_capacity(slice.len()); - // SAFETY: convert will initialize every value in the vector without reading them, - // so this is safe to do instead of double initialize from resize, and we're setting it to - // same value as capacity. - unsafe { vec.set_len(slice.len()) }; + let mut vec = vec![bf16::from_bits(0); slice.len()]; vec.convert_from_f64_slice(slice); vec }