From ceaa1ef013389eb00cc90f4b0a25550844217bbb Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Sun, 9 Dec 2018 20:17:56 -0500 Subject: [PATCH] Simplify nalgebra conversion implementations --- src/convert_nalgebra_0_15.rs | 20 ++++++++++---------- src/convert_nalgebra_0_16.rs | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/convert_nalgebra_0_15.rs b/src/convert_nalgebra_0_15.rs index 70fdd7721..faf0cd13d 100644 --- a/src/convert_nalgebra_0_15.rs +++ b/src/convert_nalgebra_0_15.rs @@ -66,7 +66,7 @@ where /// ``` fn from(slice: na::MatrixSlice<'a, A, R, na::U1, RStride, CStride>) -> ArrayView<'a, A, Ix1> { if slice.is_empty() { - ArrayView::from_shape(slice.shape().0, &[]).unwrap() + ArrayView::from(&[]) } else { let dim = Dim(slice.shape().0); let strides = Dim(slice.strides().0); @@ -77,8 +77,7 @@ where ); dimension::max_abs_offset_check_overflow::(&dim, &strides) .expect("overflow converting `nalgebra::MatrixSlice` to `nalgebra::ArrayView`"); - let ptr: *const A = slice.iter().next().unwrap(); - unsafe { ArrayView::from_shape_ptr(dim.strides(strides), ptr) } + unsafe { ArrayView::from_shape_ptr(dim.strides(strides), slice.get_unchecked(0, 0)) } } } } @@ -119,7 +118,7 @@ where mut slice: na::MatrixSliceMut<'a, A, R, na::U1, RStride, CStride>, ) -> ArrayViewMut<'a, A, Ix1> { if slice.is_empty() { - ArrayViewMut::from_shape(slice.shape().0, &mut []).unwrap() + ArrayViewMut::from(&mut []) } else { let dim = Dim(slice.shape().0); let strides = Dim(slice.strides().0); @@ -139,8 +138,9 @@ where dimension::max_abs_offset_check_overflow::(&dim, &strides).expect( "overflow converting `nalgebra::MatrixSliceMut` to `nalgebra::ArrayViewMut`", ); - let ptr: *mut A = slice.iter_mut().next().unwrap(); - unsafe { ArrayViewMut::from_shape_ptr(dim.strides(strides), ptr) } + unsafe { + ArrayViewMut::from_shape_ptr(dim.strides(strides), slice.get_unchecked_mut(0, 0)) + } } } } @@ -225,8 +225,7 @@ where ); dimension::max_abs_offset_check_overflow::(&dim, &strides) .expect("overflow converting `nalgebra::MatrixSlice` to `nalgebra::ArrayView`"); - let ptr: *const A = slice.iter().next().unwrap(); - unsafe { ArrayView::from_shape_ptr(dim.strides(strides), ptr) } + unsafe { ArrayView::from_shape_ptr(dim.strides(strides), slice.get_unchecked(0, 0)) } } } } @@ -289,8 +288,9 @@ where dimension::max_abs_offset_check_overflow::(&dim, &strides).expect( "overflow converting `nalgebra::MatrixSliceMut` to `nalgebra::ArrayViewMut`", ); - let ptr: *mut A = slice.iter_mut().next().unwrap(); - unsafe { ArrayViewMut::from_shape_ptr(dim.strides(strides), ptr) } + unsafe { + ArrayViewMut::from_shape_ptr(dim.strides(strides), slice.get_unchecked_mut(0, 0)) + } } } } diff --git a/src/convert_nalgebra_0_16.rs b/src/convert_nalgebra_0_16.rs index 4aa3af2f1..36f39b2c2 100644 --- a/src/convert_nalgebra_0_16.rs +++ b/src/convert_nalgebra_0_16.rs @@ -66,7 +66,7 @@ where /// ``` fn from(slice: na::MatrixSlice<'a, A, R, na::U1, RStride, CStride>) -> ArrayView<'a, A, Ix1> { if slice.is_empty() { - ArrayView::from_shape(slice.shape().0, &[]).unwrap() + ArrayView::from(&[]) } else { let dim = Dim(slice.shape().0); let strides = Dim(slice.strides().0); @@ -77,8 +77,7 @@ where ); dimension::max_abs_offset_check_overflow::(&dim, &strides) .expect("overflow converting `nalgebra::MatrixSlice` to `nalgebra::ArrayView`"); - let ptr: *const A = slice.iter().next().unwrap(); - unsafe { ArrayView::from_shape_ptr(dim.strides(strides), ptr) } + unsafe { ArrayView::from_shape_ptr(dim.strides(strides), slice.get_unchecked(0, 0)) } } } } @@ -119,7 +118,7 @@ where mut slice: na::MatrixSliceMut<'a, A, R, na::U1, RStride, CStride>, ) -> ArrayViewMut<'a, A, Ix1> { if slice.is_empty() { - ArrayViewMut::from_shape(slice.shape().0, &mut []).unwrap() + ArrayViewMut::from(&mut []) } else { let dim = Dim(slice.shape().0); let strides = Dim(slice.strides().0); @@ -139,8 +138,9 @@ where dimension::max_abs_offset_check_overflow::(&dim, &strides).expect( "overflow converting `nalgebra::MatrixSliceMut` to `nalgebra::ArrayViewMut`", ); - let ptr: *mut A = slice.iter_mut().next().unwrap(); - unsafe { ArrayViewMut::from_shape_ptr(dim.strides(strides), ptr) } + unsafe { + ArrayViewMut::from_shape_ptr(dim.strides(strides), slice.get_unchecked_mut(0, 0)) + } } } } @@ -225,8 +225,7 @@ where ); dimension::max_abs_offset_check_overflow::(&dim, &strides) .expect("overflow converting `nalgebra::MatrixSlice` to `nalgebra::ArrayView`"); - let ptr: *const A = slice.iter().next().unwrap(); - unsafe { ArrayView::from_shape_ptr(dim.strides(strides), ptr) } + unsafe { ArrayView::from_shape_ptr(dim.strides(strides), slice.get_unchecked(0, 0)) } } } } @@ -289,8 +288,9 @@ where dimension::max_abs_offset_check_overflow::(&dim, &strides).expect( "overflow converting `nalgebra::MatrixSliceMut` to `nalgebra::ArrayViewMut`", ); - let ptr: *mut A = slice.iter_mut().next().unwrap(); - unsafe { ArrayViewMut::from_shape_ptr(dim.strides(strides), ptr) } + unsafe { + ArrayViewMut::from_shape_ptr(dim.strides(strides), slice.get_unchecked_mut(0, 0)) + } } } }