From 78a722076a79707d4284363e9c31ea4c9513c7cf Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Tue, 24 Dec 2019 17:37:27 +0100 Subject: [PATCH] Add matrix/slice conversions. Fix #687. --- src/base/conversion.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/base/conversion.rs b/src/base/conversion.rs index 883f3fb8..414a3406 100644 --- a/src/base/conversion.rs +++ b/src/base/conversion.rs @@ -21,7 +21,7 @@ use crate::base::storage::{ContiguousStorage, ContiguousStorageMut, Storage, Sto #[cfg(any(feature = "std", feature = "alloc"))] use crate::base::VecStorage; use crate::base::{SliceStorage, SliceStorageMut}; -use crate::base::{DefaultAllocator, Matrix, ArrayStorage, MatrixMN, MatrixSlice, MatrixSliceMut, Scalar}; +use crate::base::{DefaultAllocator, Matrix, ArrayStorage, MatrixMN, MatrixSlice, MatrixSliceMut, Scalar, DVectorSlice, DVectorSliceMut}; use crate::constraint::DimEq; // FIXME: too bad this won't work allo slice conversions. @@ -524,4 +524,33 @@ for MatrixSliceMut<'a, N, RSlice, CSlice, RStride, CStride> Matrix::from_data_statically_unchecked(data) } } +} + +impl<'a, N: Scalar + Copy, R: Dim, C: Dim, S: ContiguousStorage> Into<&'a [N]> for &'a Matrix { + #[inline] + fn into(self) -> &'a [N] { + self.as_slice() + } +} + +impl<'a, N: Scalar + Copy, R: Dim, C: Dim, S: ContiguousStorageMut> Into<&'a mut [N]> for &'a mut Matrix { + #[inline] + fn into(self) -> &'a mut [N] { + self.as_mut_slice() + } +} + + +impl<'a, N: Scalar + Copy> From<&'a [N]> for DVectorSlice<'a, N> { + #[inline] + fn from(slice: &'a [N]) -> Self { + Self::from_slice(slice, slice.len()) + } +} + +impl<'a, N: Scalar + Copy> From<&'a mut [N]> for DVectorSliceMut<'a, N> { + #[inline] + fn from(slice: &'a mut [N]) -> Self { + Self::from_slice(slice, slice.len()) + } } \ No newline at end of file