Skip to content

Commit ea5cf10

Browse files
committed
Add additional array -> array view conversions
It is best to be "complete" with implementations when possible, since it can always be a breaking change to add it later. As shown in the test change in this commit, this is a minor type inference breakage due to `ArrayView::from(&[])` being ambiguous. Empty array views should be relatively rare - and one can use `ArrayView1` or other means to disambiguate.
1 parent bc15c85 commit ea5cf10

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/arraytraits.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,18 @@ where
326326
}
327327
}
328328

329-
/// Implementation of ArrayView2::from(&S) where S is a slice to a 2D array
329+
/// Implementation of ArrayView2::from(&[[A; N]; M])
330+
///
331+
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
332+
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
333+
impl<'a, A, const M: usize, const N: usize> From<&'a [[A; N]; M]> for ArrayView<'a, A, Ix2> {
334+
/// Create a two-dimensional read-only array view of the data in `slice`
335+
fn from(xs: &'a [[A; N]; M]) -> Self {
336+
Self::from(&xs[..])
337+
}
338+
}
339+
340+
/// Implementation of ArrayView2::from(&[[A; N]])
330341
///
331342
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
332343
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
@@ -382,7 +393,18 @@ where
382393
}
383394
}
384395

385-
/// Implementation of ArrayViewMut2::from(&S) where S is a slice to a 2D array
396+
/// Implementation of ArrayViewMut2::from(&mut [[A; N]; M])
397+
///
398+
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
399+
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
400+
impl<'a, A, const M: usize, const N: usize> From<&'a mut [[A; N]; M]> for ArrayViewMut<'a, A, Ix2> {
401+
/// Create a two-dimensional read-write array view of the data in `slice`
402+
fn from(xs: &'a mut [[A; N]; M]) -> Self {
403+
Self::from(&mut xs[..])
404+
}
405+
}
406+
407+
/// Implementation of ArrayViewMut2::from(&mut [[A; N]; M])
386408
///
387409
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
388410
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).

tests/append.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ fn test_append_zero_size() {
339339

340340
{
341341
let mut a = Array::<i32, _>::zeros((0, 0));
342-
a.append(Axis(1), ArrayView::from(&[]).into_shape((0, 1)).unwrap()).unwrap();
343-
a.append(Axis(1), ArrayView::from(&[]).into_shape((0, 1)).unwrap()).unwrap();
342+
a.append(Axis(1), ArrayView1::from(&[]).into_shape((0, 1)).unwrap()).unwrap();
343+
a.append(Axis(1), ArrayView1::from(&[]).into_shape((0, 1)).unwrap()).unwrap();
344344
assert_eq!(a.len(), 0);
345345
assert_eq!(a.shape(), &[0, 2]);
346346
}

0 commit comments

Comments
 (0)