7
7
// except according to those terms.
8
8
use crate :: dimension:: slices_intersect;
9
9
use crate :: error:: { ErrorKind , ShapeError } ;
10
+ use crate :: { ArrayViewMut , DimAdd , Dimension , Ix0 , Ix1 , Ix2 , Ix3 , Ix4 , Ix5 , Ix6 , IxDyn } ;
10
11
use std:: fmt;
11
12
use std:: marker:: PhantomData ;
12
13
use std:: ops:: { Deref , Range , RangeFrom , RangeFull , RangeInclusive , RangeTo , RangeToInclusive } ;
13
- use crate :: { ArrayViewMut , Dimension , Ix0 , Ix1 , Ix2 , Ix3 , Ix4 , Ix5 , Ix6 , IxDyn } ;
14
14
15
15
/// A slice (range with step size).
16
16
///
@@ -536,72 +536,51 @@ where
536
536
}
537
537
}
538
538
539
+ /// Trait for determining dimensionality of input and output for [`s!`] macro.
539
540
#[ doc( hidden) ]
540
- pub trait SliceNextInDim < D1 , D2 > {
541
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D2 > ;
542
- }
541
+ pub trait SliceArg {
542
+ /// Number of dimensions that this slicing argument consumes in the input array.
543
+ type InDim : Dimension ;
544
+ /// Number of dimensions that this slicing argument produces in the output array.
545
+ type OutDim : Dimension ;
543
546
544
- impl < D1 : Dimension > SliceNextInDim < D1 , D1 > for NewAxis {
545
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D1 > {
547
+ fn next_in_dim < D > ( & self , _: PhantomData < D > ) -> PhantomData < D :: Out >
548
+ where
549
+ D : Dimension + DimAdd < Self :: InDim > ,
550
+ {
546
551
PhantomData
547
552
}
548
- }
549
553
550
- macro_rules! impl_slicenextindim_larger {
551
- ( ( $( $generics: tt) * ) , $self: ty) => {
552
- impl <D1 : Dimension , $( $generics) ,* > SliceNextInDim <D1 , D1 :: Larger > for $self {
553
- fn next_dim( & self , _: PhantomData <D1 >) -> PhantomData <D1 :: Larger > {
554
- PhantomData
555
- }
556
- }
554
+ fn next_out_dim < D > ( & self , _: PhantomData < D > ) -> PhantomData < D :: Out >
555
+ where
556
+ D : Dimension + DimAdd < Self :: OutDim > ,
557
+ {
558
+ PhantomData
557
559
}
558
560
}
559
- impl_slicenextindim_larger ! ( ( ) , isize ) ;
560
- impl_slicenextindim_larger ! ( ( ) , usize ) ;
561
- impl_slicenextindim_larger ! ( ( ) , i32 ) ;
562
- impl_slicenextindim_larger ! ( ( T ) , Range <T >) ;
563
- impl_slicenextindim_larger ! ( ( T ) , RangeInclusive <T >) ;
564
- impl_slicenextindim_larger ! ( ( T ) , RangeFrom <T >) ;
565
- impl_slicenextindim_larger ! ( ( T ) , RangeTo <T >) ;
566
- impl_slicenextindim_larger ! ( ( T ) , RangeToInclusive <T >) ;
567
- impl_slicenextindim_larger ! ( ( ) , RangeFull ) ;
568
- impl_slicenextindim_larger ! ( ( ) , Slice ) ;
569
-
570
- #[ doc( hidden) ]
571
- pub trait SliceNextOutDim < D1 , D2 > {
572
- fn next_dim ( & self , _: PhantomData < D1 > ) -> PhantomData < D2 > ;
573
- }
574
561
575
- macro_rules! impl_slicenextoutdim_equal {
576
- ( $self: ty) => {
577
- impl <D1 : Dimension > SliceNextOutDim <D1 , D1 > for $self {
578
- fn next_dim( & self , _: PhantomData <D1 >) -> PhantomData <D1 > {
579
- PhantomData
580
- }
562
+ macro_rules! impl_slicearg {
563
+ ( ( $( $generics: tt) * ) , $self: ty, $in: ty, $out: ty) => {
564
+ impl <$( $generics) * > SliceArg for $self {
565
+ type InDim = $in;
566
+ type OutDim = $out;
581
567
}
582
568
} ;
583
569
}
584
- impl_slicenextoutdim_equal ! ( isize ) ;
585
- impl_slicenextoutdim_equal ! ( usize ) ;
586
- impl_slicenextoutdim_equal ! ( i32 ) ;
587
-
588
- macro_rules! impl_slicenextoutdim_larger {
589
- ( ( $( $generics: tt) * ) , $self: ty) => {
590
- impl <D1 : Dimension , $( $generics) * > SliceNextOutDim <D1 , D1 :: Larger > for $self {
591
- fn next_dim( & self , _: PhantomData <D1 >) -> PhantomData <D1 :: Larger > {
592
- PhantomData
593
- }
594
- }
595
- }
596
- }
597
- impl_slicenextoutdim_larger ! ( ( T ) , Range <T >) ;
598
- impl_slicenextoutdim_larger ! ( ( T ) , RangeInclusive <T >) ;
599
- impl_slicenextoutdim_larger ! ( ( T ) , RangeFrom <T >) ;
600
- impl_slicenextoutdim_larger ! ( ( T ) , RangeTo <T >) ;
601
- impl_slicenextoutdim_larger ! ( ( T ) , RangeToInclusive <T >) ;
602
- impl_slicenextoutdim_larger ! ( ( ) , RangeFull ) ;
603
- impl_slicenextoutdim_larger ! ( ( ) , Slice ) ;
604
- impl_slicenextoutdim_larger ! ( ( ) , NewAxis ) ;
570
+
571
+ impl_slicearg ! ( ( ) , isize , Ix1 , Ix0 ) ;
572
+ impl_slicearg ! ( ( ) , usize , Ix1 , Ix0 ) ;
573
+ impl_slicearg ! ( ( ) , i32 , Ix1 , Ix0 ) ;
574
+
575
+ impl_slicearg ! ( ( T ) , Range <T >, Ix1 , Ix1 ) ;
576
+ impl_slicearg ! ( ( T ) , RangeInclusive <T >, Ix1 , Ix1 ) ;
577
+ impl_slicearg ! ( ( T ) , RangeFrom <T >, Ix1 , Ix1 ) ;
578
+ impl_slicearg ! ( ( T ) , RangeTo <T >, Ix1 , Ix1 ) ;
579
+ impl_slicearg ! ( ( T ) , RangeToInclusive <T >, Ix1 , Ix1 ) ;
580
+ impl_slicearg ! ( ( ) , RangeFull , Ix1 , Ix1 ) ;
581
+ impl_slicearg ! ( ( ) , Slice , Ix1 , Ix1 ) ;
582
+
583
+ impl_slicearg ! ( ( ) , NewAxis , Ix0 , Ix1 ) ;
605
584
606
585
/// Slice argument constructor.
607
586
///
@@ -703,8 +682,8 @@ macro_rules! s(
703
682
( @parse $in_dim: expr, $out_dim: expr, [ $( $stack: tt) * ] $r: expr; $s: expr) => {
704
683
match $r {
705
684
r => {
706
- let in_dim = $crate:: SliceNextInDim :: next_dim ( & r, $in_dim) ;
707
- let out_dim = $crate:: SliceNextOutDim :: next_dim ( & r, $out_dim) ;
685
+ let in_dim = $crate:: SliceArg :: next_in_dim ( & r, $in_dim) ;
686
+ let out_dim = $crate:: SliceArg :: next_out_dim ( & r, $out_dim) ;
708
687
#[ allow( unsafe_code) ]
709
688
unsafe {
710
689
$crate:: SliceInfo :: new_unchecked(
@@ -720,8 +699,8 @@ macro_rules! s(
720
699
( @parse $in_dim: expr, $out_dim: expr, [ $( $stack: tt) * ] $r: expr) => {
721
700
match $r {
722
701
r => {
723
- let in_dim = $crate:: SliceNextInDim :: next_dim ( & r, $in_dim) ;
724
- let out_dim = $crate:: SliceNextOutDim :: next_dim ( & r, $out_dim) ;
702
+ let in_dim = $crate:: SliceArg :: next_in_dim ( & r, $in_dim) ;
703
+ let out_dim = $crate:: SliceArg :: next_out_dim ( & r, $out_dim) ;
725
704
#[ allow( unsafe_code) ]
726
705
unsafe {
727
706
$crate:: SliceInfo :: new_unchecked(
@@ -746,8 +725,8 @@ macro_rules! s(
746
725
match $r {
747
726
r => {
748
727
$crate:: s![ @parse
749
- $crate:: SliceNextInDim :: next_dim ( & r, $in_dim) ,
750
- $crate:: SliceNextOutDim :: next_dim ( & r, $out_dim) ,
728
+ $crate:: SliceArg :: next_in_dim ( & r, $in_dim) ,
729
+ $crate:: SliceArg :: next_out_dim ( & r, $out_dim) ,
751
730
[ $( $stack) * $crate:: s!( @convert r, $s) , ]
752
731
$( $t) *
753
732
]
@@ -759,8 +738,8 @@ macro_rules! s(
759
738
match $r {
760
739
r => {
761
740
$crate:: s![ @parse
762
- $crate:: SliceNextInDim :: next_dim ( & r, $in_dim) ,
763
- $crate:: SliceNextOutDim :: next_dim ( & r, $out_dim) ,
741
+ $crate:: SliceArg :: next_in_dim ( & r, $in_dim) ,
742
+ $crate:: SliceArg :: next_out_dim ( & r, $out_dim) ,
764
743
[ $( $stack) * $crate:: s!( @convert r) , ]
765
744
$( $t) *
766
745
]
0 commit comments