E5F3 Add support for empty s![] call by jturner314 · Pull Request #715 · rust-ndarray/ndarray · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,17 @@ macro_rules! s(
}
}
};
// empty call, i.e. `s![]`
(@parse ::std::marker::PhantomData::<$crate::Ix0>, []) => {
{
#[allow(unsafe_code)]
unsafe {
$crate::SliceInfo::new_unchecked([], ::std::marker::PhantomData::<$crate::Ix0>)
}
}
};
// Catch-all clause for syntax errors
(@parse $($t:tt)*) => { compile_error!("Invalid syntax in s![] call.") };
// convert range/index into SliceOrIndex
(@convert $r:expr) => {
<$crate::SliceOrIndex as ::std::convert::From<_>>::from($r)
Expand All @@ -612,8 +623,6 @@ macro_rules! s(
(@convert $r:expr, $s:expr) => {
<$crate::SliceOrIndex as ::std::convert::From<_>>::from($r).step_by($s as isize)
};
// Catch-all clause for syntax errors
(@parse $($t:tt)*) => { compile_error!("Invalid syntax in s![], expected at least one index or range") };
($($t:tt)*) => {
// The extra `*&` is a workaround for this compiler bug:
// https://github.com/rust-lang/rust/issues/23014
Expand Down
7 changes: 7 additions & 0 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ fn test_slice() {
assert!(vi.iter().zip(A.iter()).all(|(a, b)| a == b));
}

#[deny(unsafe_code)]
#[test]
fn test_slice_ix0() {
let arr = arr0(5);
assert_eq!(arr.slice(s![]), aview0(&5));
}

#[test]
fn test_slice_edge_cases() {
let mut arr = Array3::<u8>::zeros((3, 4, 5));
Expand Down
0