Skip to content

Commit d4dd6f5

Browse files
authored
Merge pull request #715 from jturner314/empty-s-macro
Add support for empty s![] call
2 parents a9c4392 + 13f636f commit d4dd6f5

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/slice.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,17 @@ macro_rules! s(
604604
}
605605
}
606606
};
607+
// empty call, i.e. `s![]`
608+
(@parse ::std::marker::PhantomData::<$crate::Ix0>, []) => {
609+
{
610+
#[allow(unsafe_code)]
611+
unsafe {
612+
$crate::SliceInfo::new_unchecked([], ::std::marker::PhantomData::<$crate::Ix0>)
613+
}
614+
}
615+
};
616+
// Catch-all clause for syntax errors
617+
(@parse $($t:tt)*) => { compile_error!("Invalid syntax in s![] call.") };
607618
// convert range/index into SliceOrIndex
608619
(@convert $r:expr) => {
609620
<$crate::SliceOrIndex as ::std::convert::From<_>>::from($r)
@@ -612,8 +623,6 @@ macro_rules! s(
612623
(@convert $r:expr, $s:expr) => {
613624
<$crate::SliceOrIndex as ::std::convert::From<_>>::from($r).step_by($s as isize)
614625
};
615-
// Catch-all clause for syntax errors
616-
(@parse $($t:tt)*) => { compile_error!("Invalid syntax in s![], expected at least one index or range") };
617626
($($t:tt)*) => {
618627
// The extra `*&` is a workaround for this compiler bug:
619628
// https://github.com/rust-lang/rust/issues/23014

tests/array.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ fn test_slice() {
9292
assert!(vi.iter().zip(A.iter()).all(|(a, b)| a == b));
9393
}
9494

95+
#[deny(unsafe_code)]
96+
#[test]
97+
fn test_slice_ix0() {
98+
let arr = arr0(5);
99+
assert_eq!(arr.slice(s![]), aview0(&5));
100+
}
101+
95102
#[test]
96103
fn test_slice_edge_cases() {
97104
let mut arr = Array3::<u8>::zeros((3, 4, 5));

0 commit comments

Comments
 (0)