Skip to content

Commit cf9316a

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 cf9316a

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/arraytraits.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,20 @@ 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+
impl<'a, A, const M: usize, const N: usize> From<&'a [[A; N]; M]> for ArrayView<'a, A, Ix2>
341+
{
342+
/// Create a two-dimensional read-only array view of the data in `slice`
343+
fn from(xs: &'a [[A; N]; M]) -> Self
344+
{
345+
Self::from(&xs[..])
346+
}
347+
}
348+
349+
/// Implementation of ArrayView2::from(&[[A; N]])
337350
///
338351
/// **Panics** if the product of non-zero axis lengths overflows `isize`. (This
339352
/// can only occur if A is zero-sized or if `N` is zero, because slices cannot
@@ -380,7 +393,20 @@ where Slice: AsMut<[A]>
380393
}
381394
}
382395

383-
/// 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+
{
402+
/// Create a two-dimensional read-write array view of the data in `slice`
403+
fn from(xs: &'a mut [[A; N]; M]) -> Self
404+
{
405+
Self::from(&mut xs[..])
406+
}
407+
}
408+
409+
/// Implementation of ArrayViewMut2::from(&mut [[A; N]])
384410
///
385411
/// **Panics** if the product of non-zero axis lengths overflows `isize`. (This
386412
/// can only occur if `A` is zero-sized or if `N` is zero, because slices

tests/append.rs

Lines changed: 2 additions & 2 deletions
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)