66// option. This file may not be copied, modified, or distributed
77// except according to those terms.
88
9- use std:: ptr as std_ptr;
109use alloc:: slice;
1110use alloc:: vec;
1211use alloc:: vec:: Vec ;
@@ -630,11 +629,18 @@ where
630629 S : DataMut ,
631630 I : NdIndex < D > ,
632631 {
633- let ptr1: * mut _ = & mut self [ index1] ;
634- let ptr2: * mut _ = & mut self [ index2] ;
635- unsafe {
636- std_ptr:: swap ( ptr1, ptr2) ;
632+ let ptr = self . as_mut_ptr ( ) ;
633+ let offset1 = index1. index_checked ( & self . dim , & self . strides ) ;
634+ let offset2 = index2. index_checked ( & self . dim , & self . strides ) ;
635+ if let Some ( offset1) = offset1 {
636+ if let Some ( offset2) = offset2 {
637+ unsafe {
638+ std:: ptr:: swap ( ptr. offset ( offset1) , ptr. offset ( offset2) ) ;
639+ }
640+ return ;
641+ }
637642 }
643+ panic ! ( "swap: index out of bounds for indices {:?} {:?}" , index1, index2) ;
638644 }
639645
640646 /// Swap elements *unchecked* at indices `index1` and `index2`.
@@ -661,7 +667,7 @@ where
661667 arraytraits:: debug_bounds_check ( self , & index2) ;
662668 let off1 = index1. index_unchecked ( & self . strides ) ;
663669 let off2 = index2. index_unchecked ( & self . strides ) ;
664- std_ptr :: swap (
670+ std :: ptr :: swap (
665671 self . ptr . as_ptr ( ) . offset ( off1) ,
666672 self . ptr . as_ptr ( ) . offset ( off2) ,
667673 ) ;
0 commit comments