diff --git a/src/base/conversion.rs b/src/base/conversion.rs index 46747f0e..7b2159f1 100644 --- a/src/base/conversion.rs +++ b/src/base/conversion.rs @@ -20,7 +20,7 @@ use crate::base::{ MatrixSliceMut, OMatrix, Scalar, }; #[cfg(any(feature = "std", feature = "alloc"))] -use crate::base::{DVector, VecStorage}; +use crate::base::{DVector, RowDVector, VecStorage}; use crate::base::{SliceStorage, SliceStorageMut}; use crate::constraint::DimEq; use crate::{IsNotStaticOne, RowSVector, SMatrix, SVector}; @@ -453,6 +453,14 @@ impl<'a, T: Scalar> From> for DVector { } } +#[cfg(any(feature = "std", feature = "alloc"))] +impl<'a, T: Scalar> From> for RowDVector { + #[inline] + fn from(vec: Vec) -> Self { + Self::from_vec(vec) + } +} + impl<'a, T: Scalar + Copy, R: Dim, C: Dim, S: RawStorage + IsContiguous> From<&'a Matrix> for &'a [T] { diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 4dccc439..18930fb7 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -32,7 +32,7 @@ use crate::{ArrayStorage, SMatrix, SimdComplexField, Storage, UninitMatrix}; use crate::storage::IsContiguous; use crate::uninit::{Init, InitStatus, Uninit}; #[cfg(any(feature = "std", feature = "alloc"))] -use crate::{DMatrix, DVector, Dynamic, VecStorage}; +use crate::{DMatrix, DVector, Dynamic, RowDVector, VecStorage}; use std::mem::MaybeUninit; /// A square matrix. @@ -411,6 +411,21 @@ impl DVector { } } +// TODO: Consider removing/deprecating `from_vec_storage` once we are able to make +// `from_data` const fn compatible +#[cfg(any(feature = "std", feature = "alloc"))] +impl RowDVector { + /// Creates a new heap-allocated matrix from the given [`VecStorage`]. + /// + /// This method exists primarily as a workaround for the fact that `from_data` can not + /// work in `const fn` contexts. + 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 UninitMatrix where DefaultAllocator: Allocator,