Skip to content

Commit

Permalink
Add additional array -> array view conversions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bluss committed Dec 6, 2021
1 parent 76e5ec4 commit 01fd6fc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 24 additions & 2 deletions src/arraytraits.rs
Expand Up @@ -323,7 +323,18 @@ where
}
}

/// Implementation of ArrayView2::from(&S) where S is a slice to a 2D array
/// Implementation of ArrayView2::from(&[[A; N]; M])
///
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
impl<'a, A, const M: usize, const N: usize> From<&'a [[A; N]; M]> for ArrayView<'a, A, Ix2> {
/// Create a two-dimensional read-only array view of the data in `slice`
fn from(xs: &'a [[A; N]; M]) -> Self {
Self::from(&xs[..])
}
}

/// Implementation of ArrayView2::from(&[[A; N]])
///
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
Expand Down Expand Up @@ -379,7 +390,18 @@ where
}
}

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

/// Implementation of ArrayViewMut2::from(&mut [[A; N]; M])
///
/// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
/// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
Expand Down
4 changes: 2 additions & 2 deletions tests/append.rs
Expand Up @@ -339,8 +339,8 @@ fn test_append_zero_size() {

{
let mut a = Array::<i32, _>::zeros((0, 0));
a.append(Axis(1), ArrayView::from(&[]).into_shape((0, 1)).unwrap()).unwrap();
a.append(Axis(1), ArrayView::from(&[]).into_shape((0, 1)).unwrap()).unwrap();
a.append(Axis(1), ArrayView1::from(&[]).into_shape((0, 1)).unwrap()).unwrap();
a.append(Axis(1), ArrayView1::from(&[]).into_shape((0, 1)).unwrap()).unwrap();
assert_eq!(a.len(), 0);
assert_eq!(a.shape(), &[0, 2]);
}
Expand Down

0 comments on commit 01fd6fc

Please sign in to comment.