diff --git a/nalgebra-lapack/src/qr.rs b/nalgebra-lapack/src/qr.rs index 4f290201..314621b2 100644 --- a/nalgebra-lapack/src/qr.rs +++ b/nalgebra-lapack/src/qr.rs @@ -57,9 +57,8 @@ where let (nrows, ncols) = m.data.shape(); let mut info = 0; - let mut tau = unsafe { - Matrix::new_uninitialized_generic(nrows.min(ncols), U1).assume_init() - }; + let mut tau = + unsafe { Matrix::new_uninitialized_generic(nrows.min(ncols), U1).assume_init() }; if nrows.value() == 0 || ncols.value() == 0 { return Self { qr: m, tau: tau }; diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index bcf9df33..ccc676c2 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -1,4 +1,5 @@ -use std::mem;use std::fmt::{self, Debug, Formatter}; +use std::fmt::{self, Debug, Formatter}; +use std::mem; // use std::hash::{Hash, Hasher}; #[cfg(feature = "abomonation-serialize")] use std::io::{Result as IOResult, Write}; @@ -12,8 +13,6 @@ use serde::ser::SerializeSeq; use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "serde-serialize-no-std")] use std::marker::PhantomData; -#[cfg(feature = "serde-serialize-no-std")] -use std::mem; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; diff --git a/src/base/componentwise.rs b/src/base/componentwise.rs index 4ad672f4..02b2cae6 100644 --- a/src/base/componentwise.rs +++ b/src/base/componentwise.rs @@ -146,7 +146,7 @@ macro_rules! component_binop_impl( ); /// # Componentwise operations -impl> Matrix { +impl> Matrix { component_binop_impl!( component_mul, component_mul_mut, component_mul_assign, cmpy, ClosedMul.mul.mul_assign, r" diff --git a/src/base/default_allocator.rs b/src/base/default_allocator.rs index b30e8960..269ef447 100644 --- a/src/base/default_allocator.rs +++ b/src/base/default_allocator.rs @@ -4,7 +4,7 @@ //! heap-allocated buffers for matrices with at least one dimension unknown at compile-time. use std::cmp; -use std::mem::{self, ManuallyDrop, MaybeUninit}; +use std::mem::{ManuallyDrop, MaybeUninit}; use std::ptr; #[cfg(all(feature = "alloc", not(feature = "std")))] @@ -92,9 +92,8 @@ impl Allocator, Const> for Def // SAFETY: // * `ManuallyDrop` and T are guaranteed to have the same layout // * `ManuallyDrop` does not drop, so there are no double-frees - // * `ArrayStorage` is transparent. // And thus the conversion is safe - ArrayStorage(unsafe { mem::transmute_copy(&ManuallyDrop::new(buf.0)) }) + unsafe { ArrayStorage((&ManuallyDrop::new(buf) as *const _ as *const [_; C]).read()) } } } @@ -132,32 +131,35 @@ impl Allocator for DefaultAllocator { #[inline] unsafe fn assume_init(uninit: Owned, Dynamic, C>) -> Owned { - let mut data = ManuallyDrop::new(uninit.data); + // Avoids a double-drop. + let (nrows, ncols) = uninit.shape(); + let vec: Vec<_> = uninit.into(); + let mut md = ManuallyDrop::new(vec); - // Safety: MaybeUninit has the same alignment and layout as T. - let new_data = - Vec::from_raw_parts(data.as_mut_ptr() as *mut T, data.len(), data.capacity()); + // Safety: + // - MaybeUninit has the same alignment and layout as T. + // - The length and capacity come from a valid vector. + let new_data = Vec::from_raw_parts(md.as_mut_ptr() as *mut _, md.len(), md.capacity()); - VecStorage::new(uninit.nrows, uninit.ncols, new_data) + VecStorage::new(nrows, ncols, new_data) } #[inline] fn manually_drop( buf: >::Buffer, ) -> , Dynamic, C>>::Buffer { - // Avoids dropping the buffer that will be used for the result. - let mut data = ManuallyDrop::new(buf.data); + // Avoids a double-drop. + let (nrows, ncols) = buf.shape(); + let vec: Vec<_> = buf.into(); + let mut md = ManuallyDrop::new(vec); - // Safety: ManuallyDrop has the same alignment and layout as T. - let new_data = unsafe { - Vec::from_raw_parts( - data.as_mut_ptr() as *mut ManuallyDrop, - data.len(), - data.capacity(), - ) - }; + // Safety: + // - ManuallyDrop has the same alignment and layout as T. + // - The length and capacity come from a valid vector. + let new_data = + unsafe { Vec::from_raw_parts(md.as_mut_ptr() as *mut _, md.len(), md.capacity()) }; - VecStorage::new(buf.nrows, buf.ncols, new_data) + VecStorage::new(nrows, ncols, new_data) } } @@ -194,32 +196,35 @@ impl Allocator for DefaultAllocator { #[inline] unsafe fn assume_init(uninit: Owned, R, Dynamic>) -> Owned { - let mut data = ManuallyDrop::new(uninit.data); + // Avoids a double-drop. + let (nrows, ncols) = uninit.shape(); + let vec: Vec<_> = uninit.into(); + let mut md = ManuallyDrop::new(vec); - // Safety: MaybeUninit has the same alignment and layout as T. - let new_data = - Vec::from_raw_parts(data.as_mut_ptr() as *mut T, data.len(), data.capacity()); + // Safety: + // - MaybeUninit has the same alignment and layout as T. + // - The length and capacity come from a valid vector. + let new_data = Vec::from_raw_parts(md.as_mut_ptr() as *mut _, md.len(), md.capacity()); - VecStorage::new(uninit.nrows, uninit.ncols, new_data) + VecStorage::new(nrows, ncols, new_data) } #[inline] fn manually_drop( buf: >::Buffer, ) -> , R, Dynamic>>::Buffer { - // Avoids dropping the buffer that will be used for the result. - let mut data = ManuallyDrop::new(buf.data); + // Avoids a double-drop. + let (nrows, ncols) = buf.shape(); + let vec: Vec<_> = buf.into(); + let mut md = ManuallyDrop::new(vec); - // Safety: ManuallyDrop has the same alignment and layout as T. - let new_data = unsafe { - Vec::from_raw_parts( - data.as_mut_ptr() as *mut ManuallyDrop, - data.len(), - data.capacity(), - ) - }; + // Safety: + // - ManuallyDrop has the same alignment and layout as T. + // - The length and capacity come from a valid vector. + let new_data = + unsafe { Vec::from_raw_parts(md.as_mut_ptr() as *mut _, md.len(), md.capacity()) }; - VecStorage::new(buf.nrows, buf.ncols, new_data) + VecStorage::new(nrows, ncols, new_data) } } diff --git a/src/base/indexing.rs b/src/base/indexing.rs index a8db21ec..bb0adddb 100644 --- a/src/base/indexing.rs +++ b/src/base/indexing.rs @@ -643,7 +643,7 @@ macro_rules! impl_index_pair { $(where $CConstraintType: ty: $CConstraintBound: ident $(<$($CConstraintBoundParams: ty $( = $CEqBound: ty )*),*>)* )*] ) => { - impl<'a, T: 'a, $R: Dim, $C: Dim, S, $($RTyP : $RTyPB,)* $($CTyP : $CTyPB),*> + impl<'a, T: 'a, $R: Dim, $C: Dim, S, $($RTyP : $RTyPB,)* $($CTyP : $CTyPB),*> MatrixIndex<'a, T, $R, $C, S> for ($RIdx, $CIdx) where S: Storage, diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 94c3f88e..887d8e6c 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -385,6 +385,10 @@ where /// Assumes a matrix's entries to be initialized. This operation should be near zero-cost. /// /// For the similar method that operates on matrix slices, see [`slice_assume_init`]. + /// + /// # Safety + /// The user must make sure that every single entry of the buffer has been initialized, + /// or Undefined Behavior will immediately occur. pub unsafe fn assume_init(self) -> OMatrix { OMatrix::from_data(>::assume_init( self.data, @@ -408,6 +412,10 @@ where impl Matrix, R, C, S> { /// Creates a full slice from `self` and assumes it to be initialized. + /// + /// # Safety + /// The user must make sure that every single entry of the buffer has been initialized, + /// or Undefined Behavior will immediately occur. pub unsafe fn assume_init_ref(&self) -> MatrixSlice where S: Storage, R, C>, @@ -416,6 +424,10 @@ impl Matrix, R, C, S> { } /// Creates a full mutable slice from `self` and assumes it to be initialized. + /// + /// # Safety + /// The user must make sure that every single entry of the buffer has been initialized, + /// or Undefined Behavior will immediately occur. pub unsafe fn assume_init_mut(&mut self) -> MatrixSliceMut where S: StorageMut, R, C>, diff --git a/src/base/matrix_slice.rs b/src/base/matrix_slice.rs index 25baee55..69d55e3f 100644 --- a/src/base/matrix_slice.rs +++ b/src/base/matrix_slice.rs @@ -237,6 +237,10 @@ impl<'a, T, R: Dim, C: Dim, RStride: Dim, CStride: Dim> SliceStorageMut<'a, MaybeUninit, R, C, RStride, CStride> { /// Assumes a slice storage's entries to be initialized. This operation should be near zero-cost. + /// + /// # Safety + /// The user must make sure that every single entry of the buffer has been initialized, + /// or Undefined Behavior will immediately occur. pub unsafe fn assume_init(self) -> SliceStorageMut<'a, T, R, C, RStride, CStride> { SliceStorageMut::from_raw_parts(self.ptr as *mut T, self.shape, self.strides) } @@ -1012,6 +1016,6 @@ impl<'a, T, R: Dim, C: Dim, RStride: Dim, CStride: Dim> _phantoms: PhantomData, }; - Matrix::from_data(data) + Matrix::from_data(data) } } diff --git a/src/base/properties.rs b/src/base/properties.rs index bf13b6a3..00333708 100644 --- a/src/base/properties.rs +++ b/src/base/properties.rs @@ -7,7 +7,7 @@ use simba::scalar::{ClosedAdd, ClosedMul, ComplexField, RealField}; use crate::base::allocator::Allocator; use crate::base::dimension::{Dim, DimMin}; use crate::base::storage::Storage; -use crate::base::{DefaultAllocator, Matrix, SquareMatrix}; +use crate::base::{DefaultAllocator, Matrix, SquareMatrix}; impl> Matrix { /// The total number of elements of this matrix. diff --git a/src/base/vec_storage.rs b/src/base/vec_storage.rs index 9f9d649d..a6d62faf 100644 --- a/src/base/vec_storage.rs +++ b/src/base/vec_storage.rs @@ -30,9 +30,9 @@ use abomonation::Abomonation; /// A Vec-based matrix data storage. It may be dynamically-sized. #[derive(Eq, Debug, Clone, PartialEq)] pub struct VecStorage { - pub(crate) data: Vec, - pub(crate) nrows: R, - pub(crate) ncols: C, + data: Vec, + nrows: R, + ncols: C, } #[cfg(feature = "serde-serialize")] @@ -193,7 +193,8 @@ where #[inline] fn clone_owned(&self) -> Owned - where T:Clone, + where + T: Clone, DefaultAllocator: InnerAllocator, { self.clone() @@ -242,7 +243,8 @@ where #[inline] fn clone_owned(&self) -> Owned - where T:Clone, + where + T: Clone, DefaultAllocator: InnerAllocator, { self.clone() @@ -413,7 +415,7 @@ impl<'a, T: 'a + Copy, R: Dim> Extend<&'a T> for VecStorage { } } -impl Extend> for VecStorage +impl Extend> for VecStorage where SV: Storage, ShapeConstraint: SameNumberOfRows, diff --git a/src/debug/random_orthogonal.rs b/src/debug/random_orthogonal.rs index 11ea832a..0f4a9a4c 100644 --- a/src/debug/random_orthogonal.rs +++ b/src/debug/random_orthogonal.rs @@ -1,7 +1,5 @@ use std::fmt; -#[cfg(feature = "arbitrary")] -use crate::base::storage::Owned; #[cfg(feature = "arbitrary")] use quickcheck::{Arbitrary, Gen}; diff --git a/src/debug/random_sdp.rs b/src/debug/random_sdp.rs index bec8ea93..08bee9e2 100644 --- a/src/debug/random_sdp.rs +++ b/src/debug/random_sdp.rs @@ -1,7 +1,5 @@ use std::fmt; -#[cfg(feature = "arbitrary")] -use crate::base::storage::Owned; #[cfg(feature = "arbitrary")] use quickcheck::{Arbitrary, Gen}; diff --git a/src/geometry/dual_quaternion_conversion.rs b/src/geometry/dual_quaternion_conversion.rs index c15925a6..2afffe26 100644 --- a/src/geometry/dual_quaternion_conversion.rs +++ b/src/geometry/dual_quaternion_conversion.rs @@ -48,7 +48,7 @@ where impl SubsetOf> for UnitDualQuaternion where - T2: SupersetOf, + T2: SupersetOf, { #[inline] fn to_superset(&self) -> UnitDualQuaternion { diff --git a/src/geometry/point.rs b/src/geometry/point.rs index 9fc8c663..9e0d4d06 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -108,7 +108,7 @@ where impl Serialize for OPoint where DefaultAllocator: Allocator, - >::Buffer: Serialize, + >::Buffer: Serialize, { fn serialize(&self, serializer: S) -> Result where @@ -122,7 +122,7 @@ where impl<'a, T: Deserialize<'a>, D: DimName> Deserialize<'a> for OPoint where DefaultAllocator: Allocator, - >::Buffer: Deserialize<'a>, + >::Buffer: Deserialize<'a>, { fn deserialize(deserializer: Des) -> Result where diff --git a/src/geometry/quaternion_ops.rs b/src/geometry/quaternion_ops.rs index c0e11327..12c371c2 100644 --- a/src/geometry/quaternion_ops.rs +++ b/src/geometry/quaternion_ops.rs @@ -59,7 +59,7 @@ use std::ops::{ use crate::base::dimension::U3; use crate::base::storage::Storage; -use crate::base::{Const, Unit, Vector, Vector3}; +use crate::base::{Const, Unit, Vector, Vector3}; use crate::SimdRealField; use crate::geometry::{Point3, Quaternion, Rotation, UnitQuaternion}; diff --git a/src/linalg/exp.rs b/src/linalg/exp.rs index 4fc5b460..c402e743 100644 --- a/src/linalg/exp.rs +++ b/src/linalg/exp.rs @@ -1,11 +1,16 @@ //! This module provides the matrix exponent (exp) function to square matrices. //! -use crate::{ComplexField, OMatrix, RealField, base::{ +use crate::{ + base::{ allocator::Allocator, dimension::{Const, Dim, DimMin, DimMinimum}, storage::Storage, DefaultAllocator, - }, convert, storage::Owned, try_convert}; + }, + convert, + storage::Owned, + try_convert, ComplexField, OMatrix, RealField, +}; use crate::num::Zero; diff --git a/src/proptest/mod.rs b/src/proptest/mod.rs index 35410ef9..5e06d9fa 100644 --- a/src/proptest/mod.rs +++ b/src/proptest/mod.rs @@ -327,15 +327,10 @@ where D: Dim, DefaultAllocator: Allocator, { - matrix_(value_strategy, length.into(), U1.into()) + matrix_(value_strategy, length.into(), Const::<1>.into()) } -impl Default for MatrixParameters -where - NParameters: Default, - R: DimName, - C: DimName, -{ +impl Default for MatrixParameters { fn default() -> Self { Self { rows: DimRange::from(R::name()), diff --git a/src/sparse/cs_matrix.rs b/src/sparse/cs_matrix.rs index b33a3cdd..173b0fb9 100644 --- a/src/sparse/cs_matrix.rs +++ b/src/sparse/cs_matrix.rs @@ -474,7 +474,7 @@ where { // Size = R let nrows = self.data.shape().0; - let mut workspace = Matrix::new_uninitialized_generic(nrows, Const::<1>); + let mut workspace = CsMatrix::new_uninitialized_generic(nrows, Const::<1>); self.sort_with_workspace(workspace.as_mut_slice()); }