From d2c11ad7975732b374d1448a4667db0f830f8634 Mon Sep 17 00:00:00 2001 From: Andreas Longva Date: Sat, 17 Apr 2021 17:00:37 +0200 Subject: [PATCH] Impl DMatrix/DVector::from_vec_storage These methods enable safe & const construction of DMatrix/DVector from a given VecStorage. --- src/base/vec_storage.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/base/vec_storage.rs b/src/base/vec_storage.rs index 29da127d..15ea9237 100644 --- a/src/base/vec_storage.rs +++ b/src/base/vec_storage.rs @@ -15,6 +15,7 @@ use crate::base::{Scalar, Vector}; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; +use crate::{DMatrix, DVector}; /* * @@ -410,3 +411,23 @@ impl Extend for VecStorage { self.nrows = Dynamic::new(self.data.len()); } } + +impl DMatrix +{ + /// Creates a new heap-allocated matrix from the given [VecStorage]. + pub const fn from_vec_storage(storage: VecStorage) -> Self { + // This is sound because the dimensions of the matrix and the storage are guaranteed + // to be the same + unsafe { Self::from_data_statically_unchecked(storage) } + } +} + +impl DVector +{ + /// Creates a new heap-allocated matrix from the given [VecStorage]. + pub const fn from_vec_storage(storage: VecStorage) -> Self { + // This is sound because the dimensions of the matrix and the storage are guaranteed + // to be the same + unsafe { Self::from_data_statically_unchecked(storage) } + } +}