Skip to content

Commit ceaa1ef

Browse files
committed
Simplify nalgebra conversion implementations
1 parent 3decddd commit ceaa1ef

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/convert_nalgebra_0_15.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ where
6666
/// ```
6767
fn from(slice: na::MatrixSlice<'a, A, R, na::U1, RStride, CStride>) -> ArrayView<'a, A, Ix1> {
6868
if slice.is_empty() {
69-
ArrayView::from_shape(slice.shape().0, &[]).unwrap()
69+
ArrayView::from(&[])
7070
} else {
7171
let dim = Dim(slice.shape().0);
7272
let strides = Dim(slice.strides().0);
@@ -77,8 +77,7 @@ where
7777
);
7878
dimension::max_abs_offset_check_overflow::<A, _>(&dim, &strides)
7979
.expect("overflow converting `nalgebra::MatrixSlice` to `nalgebra::ArrayView`");
80-
let ptr: *const A = slice.iter().next().unwrap();
81-
unsafe { ArrayView::from_shape_ptr(dim.strides(strides), ptr) }
80+
unsafe { ArrayView::from_shape_ptr(dim.strides(strides), slice.get_unchecked(0, 0)) }
8281
}
8382
}
8483
}
@@ -119,7 +118,7 @@ where
119118
mut slice: na::MatrixSliceMut<'a, A, R, na::U1, RStride, CStride>,
120119
) -> ArrayViewMut<'a, A, Ix1> {
121120
if slice.is_empty() {
122-
ArrayViewMut::from_shape(slice.shape().0, &mut []).unwrap()
121+
ArrayViewMut::from(&mut [])
123122
} else {
124123
let dim = Dim(slice.shape().0);
125124
let strides = Dim(slice.strides().0);
@@ -139,8 +138,9 @@ where
139138
dimension::max_abs_offset_check_overflow::<A, _>(&dim, &strides).expect(
140139
"overflow converting `nalgebra::MatrixSliceMut` to `nalgebra::ArrayViewMut`",
141140
);
142-
let ptr: *mut A = slice.iter_mut().next().unwrap();
143-
unsafe { ArrayViewMut::from_shape_ptr(dim.strides(strides), ptr) }
141+
unsafe {
142+
ArrayViewMut::from_shape_ptr(dim.strides(strides), slice.get_unchecked_mut(0, 0))
143+
}
144144
}
145145
}
146146
}
@@ -225,8 +225,7 @@ where
225225
);
226226
dimension::max_abs_offset_check_overflow::<A, _>(&dim, &strides)
227227
.expect("overflow converting `nalgebra::MatrixSlice` to `nalgebra::ArrayView`");
228-
let ptr: *const A = slice.iter().next().unwrap();
229-
unsafe { ArrayView::from_shape_ptr(dim.strides(strides), ptr) }
228+
unsafe { ArrayView::from_shape_ptr(dim.strides(strides), slice.get_unchecked(0, 0)) }
230229
}
231230
}
232231
}
@@ -289,8 +288,9 @@ where
289288
dimension::max_abs_offset_check_overflow::<A, _>(&dim, &strides).expect(
290289
"overflow converting `nalgebra::MatrixSliceMut` to `nalgebra::ArrayViewMut`",
291290
);
292-
let ptr: *mut A = slice.iter_mut().next().unwrap();
293-
unsafe { ArrayViewMut::from_shape_ptr(dim.strides(strides), ptr) }
291+
unsafe {
292+
ArrayViewMut::from_shape_ptr(dim.strides(strides), slice.get_unchecked_mut(0, 0))
293+
}
294294
}
295295
}
296296
}

src/convert_nalgebra_0_16.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ where
6666
/// ```
6767
fn from(slice: na::MatrixSlice<'a, A, R, na::U1, RStride, CStride>) -> ArrayView<'a, A, Ix1> {
6868
if slice.is_empty() {
69-
ArrayView::from_shape(slice.shape().0, &[]).unwrap()
69+
ArrayView::from(&[])
7070
} else {
7171
let dim = Dim(slice.shape().0);
7272
let strides = Dim(slice.strides().0);
@@ -77,8 +77,7 @@ where
7777
);
7878
dimension::max_abs_offset_check_overflow::<A, _>(&dim, &strides)
7979
.expect("overflow converting `nalgebra::MatrixSlice` to `nalgebra::ArrayView`");
80-
let ptr: *const A = slice.iter().next().unwrap();
81-
unsafe { ArrayView::from_shape_ptr(dim.strides(strides), ptr) }
80+
unsafe { ArrayView::from_shape_ptr(dim.strides(strides), slice.get_unchecked(0, 0)) }
8281
}
8382
}
8483
}
@@ -119,7 +118,7 @@ where
119118
mut slice: na::MatrixSliceMut<'a, A, R, na::U1, RStride, CStride>,
120119
) -> ArrayViewMut<'a, A, Ix1> {
121120
if slice.is_empty() {
122-
ArrayViewMut::from_shape(slice.shape().0, &mut []).unwrap()
121+
ArrayViewMut::from(&mut [])
123122
} else {
124123
let dim = Dim(slice.shape().0);
125124
let strides = Dim(slice.strides().0);
@@ -139,8 +138,9 @@ where
139138
dimension::max_abs_offset_check_overflow::<A, _>(&dim, &strides).expect(
140139
"overflow converting `nalgebra::MatrixSliceMut` to `nalgebra::ArrayViewMut`",
141140
);
142-
let ptr: *mut A = slice.iter_mut().next().unwrap();
143-
unsafe { ArrayViewMut::from_shape_ptr(dim.strides(strides), ptr) }
141+
unsafe {
142+
ArrayViewMut::from_shape_ptr(dim.strides(strides), slice.get_unchecked_mut(0, 0))
143+
}
144144
}
145145
}
146146
}
@@ -225,8 +225,7 @@ where
225225
);
226226
dimension::max_abs_offset_check_overflow::<A, _>(&dim, &strides)
227227
.expect("overflow converting `nalgebra::MatrixSlice` to `nalgebra::ArrayView`");
228-
let ptr: *const A = slice.iter().next().unwrap();
229-
unsafe { ArrayView::from_shape_ptr(dim.strides(strides), ptr) }
228+
unsafe { ArrayView::from_shape_ptr(dim.strides(strides), slice.get_unchecked(0, 0)) }
230229
}
231230
}
232231
}
@@ -289,8 +288,9 @@ where
289288
dimension::max_abs_offset_check_overflow::<A, _>(&dim, &strides).expect(
290289
"overflow converting `nalgebra::MatrixSliceMut` to `nalgebra::ArrayViewMut`",
291290
);
292-
let ptr: *mut A = slice.iter_mut().next().unwrap();
293-
unsafe { ArrayViewMut::from_shape_ptr(dim.strides(strides), ptr) }
291+
unsafe {
292+
ArrayViewMut::from_shape_ptr(dim.strides(strides), slice.get_unchecked_mut(0, 0))
293+
}
294294
}
295295
}
296296
}

0 commit comments

Comments
 (0)