Skip to content

Commit 646de85

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 4690923 commit 646de85

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/arraytraits.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,21 @@ where Slice: AsRef<[A]>
333333
}
334334
}
335335

336-
/// Implementation of ArrayView2::from(&S) where S is a slice to a 2D array
336+
/// Implementation of ArrayView2::from(&[[A; N]; M])
337+
///
338+
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
339+
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
340+
/// **Panics** if N == 0 and the number of rows is greater than isize::MAX.
341+
impl<'a, A, const M: usize, const N: usize> From<&'a [[A; N]; M]> for ArrayView<'a, A, Ix2>
342+
{
343+
/// Create a two-dimensional read-only array view of the data in `slice`
344+
fn from(xs: &'a [[A; N]; M]) -> Self
345+
{
346+
Self::from(&xs[..])
347+
}
348+
}
349+
350+
/// Implementation of ArrayView2::from(&[[A; N]])
337351
///
338352
/// **Panics** if the product of non-zero axis lengths overflows `isize`. (This
339353
/// can only occur if A is zero-sized or if `N` is zero, because slices cannot
@@ -380,7 +394,21 @@ where Slice: AsMut<[A]>
380394
}
381395
}
382396

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

tests/append.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,9 @@ fn test_append_zero_size()
383383

384384
{
385385
let mut a = Array::<i32, _>::zeros((0, 0));
386-
a.append(Axis(1), ArrayView::from(&[]).into_shape_with_order((0, 1)).unwrap())
386+
a.append(Axis(1), ArrayView1::from(&[]).into_shape_with_order((0, 1)).unwrap())
387387
.unwrap();
388-
a.append(Axis(1), ArrayView::from(&[]).into_shape_with_order((0, 1)).unwrap())
388+
a.append(Axis(1), ArrayView1::from(&[]).into_shape_with_order((0, 1)).unwrap())
389389
.unwrap();
390390
assert_eq!(a.len(), 0);
391391
assert_eq!(a.shape(), &[0, 2]);

0 commit comments

Comments
 (0)