77// except according to those terms.
88use crate :: dimension:: slices_intersect;
99use crate :: error:: { ErrorKind , ShapeError } ;
10+ use crate :: { ArrayViewMut , DimAdd , Dimension , Ix0 , Ix1 , Ix2 , Ix3 , Ix4 , Ix5 , Ix6 , IxDyn } ;
1011use std:: fmt;
1112use std:: marker:: PhantomData ;
1213use std:: ops:: { Deref , Range , RangeFrom , RangeFull , RangeInclusive , RangeTo , RangeToInclusive } ;
13- use crate :: { ArrayViewMut , Dimension , Ix0 , Ix1 , Ix2 , Ix3 , Ix4 , Ix5 , Ix6 , IxDyn } ;
1414
1515/// A slice (range with step size).
1616///
@@ -536,72 +536,51 @@ where
536536 }
537537}
538538
539+ /// Trait for determining dimensionality of input and output for [`s!`] macro.
539540#[ 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 ;
543546
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+ {
546551 PhantomData
547552 }
548- }
549553
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
557559 }
558560}
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- }
574561
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;
581567 }
582568 } ;
583569}
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 ) ;
605584
606585/// Slice argument constructor.
607586///
@@ -703,8 +682,8 @@ macro_rules! s(
703682 ( @parse $in_dim: expr, $out_dim: expr, [ $( $stack: tt) * ] $r: expr; $s: expr) => {
704683 match $r {
705684 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) ;
708687 #[ allow( unsafe_code) ]
709688 unsafe {
710689 $crate:: SliceInfo :: new_unchecked(
@@ -720,8 +699,8 @@ macro_rules! s(
720699 ( @parse $in_dim: expr, $out_dim: expr, [ $( $stack: tt) * ] $r: expr) => {
721700 match $r {
722701 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) ;
725704 #[ allow( unsafe_code) ]
726705 unsafe {
727706 $crate:: SliceInfo :: new_unchecked(
@@ -746,8 +725,8 @@ macro_rules! s(
746725 match $r {
747726 r => {
748727 $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) ,
751730 [ $( $stack) * $crate:: s!( @convert r, $s) , ]
752731 $( $t) *
753732 ]
@@ -759,8 +738,8 @@ macro_rules! s(
759738 match $r {
760739 r => {
761740 $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) ,
764743 [ $( $stack) * $crate:: s!( @convert r) , ]
765744 $( $t) *
766745 ]
0 commit comments