From 498b377d0e7d31a77cecd4540ce84c4a132aee98 Mon Sep 17 00:00:00 2001 From: Cryptjar Date: Fri, 18 Jun 2021 02:23:40 +0200 Subject: [PATCH 1/3] Add `into_slice[_mut]` and impl to SliceStorage[Mut]. --- src/base/matrix_slice.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/base/matrix_slice.rs b/src/base/matrix_slice.rs index 6cb713f1..2b287f33 100644 --- a/src/base/matrix_slice.rs +++ b/src/base/matrix_slice.rs @@ -77,6 +77,21 @@ macro_rules! slice_storage_impl( $T::from_raw_parts(storage.$get_addr(start.0, start.1), shape, strides) } } + + impl <'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> + $T<'a, T, R, C, RStride, CStride> + { + /// Extracts the original slice from this storage + pub fn into_slice(self) -> &'a [T] { + let (nrows, ncols) = self.shape(); + if nrows.value() != 0 && ncols.value() != 0 { + let sz = self.linear_index(nrows.value() - 1, ncols.value() - 1); + unsafe { slice::from_raw_parts(self.ptr, sz + 1) } + } else { + unsafe { slice::from_raw_parts(self.ptr, 0) } + } + } + } } ); @@ -108,6 +123,21 @@ impl<'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> Clone } } +impl<'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> + SliceStorageMut<'a, T, R, C, RStride, CStride> +{ + /// Extracts the original slice from this storage + pub fn into_slice_mut(self) -> &'a mut [T] { + let (nrows, ncols) = self.shape(); + if nrows.value() != 0 && ncols.value() != 0 { + let sz = self.linear_index(nrows.value() - 1, ncols.value() - 1); + unsafe { slice::from_raw_parts_mut(self.ptr, sz + 1) } + } else { + unsafe { slice::from_raw_parts_mut(self.ptr, 0) } + } + } +} + macro_rules! storage_impl( ($($T: ident),* $(,)*) => {$( unsafe impl<'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> Storage From 801d8a61a68d1d2ecaa0c19481ac06e84fef558d Mon Sep 17 00:00:00 2001 From: Cryptjar Date: Fri, 18 Jun 2021 02:43:05 +0200 Subject: [PATCH 2/3] Add a `From` impl to convert `DVectorSlice[mut]` back to into a slice. --- src/base/conversion.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/base/conversion.rs b/src/base/conversion.rs index 532ab8ed..94fd4a3a 100644 --- a/src/base/conversion.rs +++ b/src/base/conversion.rs @@ -452,6 +452,12 @@ impl<'a, T: Scalar + Copy> From<&'a [T]> for DVectorSlice<'a, T> { } } +impl<'a, T: Scalar> From> for &'a [T] { + fn from(vec: DVectorSlice<'a, T>) -> &'a [T] { + vec.data.into_slice() + } +} + impl<'a, T: Scalar + Copy> From<&'a mut [T]> for DVectorSliceMut<'a, T> { #[inline] fn from(slice: &'a mut [T]) -> Self { @@ -459,6 +465,12 @@ impl<'a, T: Scalar + Copy> From<&'a mut [T]> for DVectorSliceMut<'a, T> { } } +impl<'a, T: Scalar> From> for &'a mut [T] { + fn from(vec: DVectorSliceMut<'a, T>) -> &'a mut [T] { + vec.data.into_slice_mut() + } +} + impl From<[OMatrix; 2]> for OMatrix where From 1554c0114e4bfdbcae24ab6bed6e22252b9be424 Mon Sep 17 00:00:00 2001 From: Cryptjar Date: Fri, 18 Jun 2021 13:18:33 +0200 Subject: [PATCH 3/3] Fix unsoundness in `into_slice` by adding `ContiguousStorage` bound. --- src/base/matrix_slice.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/base/matrix_slice.rs b/src/base/matrix_slice.rs index 2b287f33..b275fa7f 100644 --- a/src/base/matrix_slice.rs +++ b/src/base/matrix_slice.rs @@ -80,6 +80,8 @@ macro_rules! slice_storage_impl( impl <'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> $T<'a, T, R, C, RStride, CStride> + where + Self: ContiguousStorage { /// Extracts the original slice from this storage pub fn into_slice(self) -> &'a [T] { @@ -125,6 +127,8 @@ impl<'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> Clone impl<'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> SliceStorageMut<'a, T, R, C, RStride, CStride> +where + Self: ContiguousStorageMut, { /// Extracts the original slice from this storage pub fn into_slice_mut(self) -> &'a mut [T] {