@@ -884,58 +884,61 @@ struct Hole<'a, T: 'a> {
884
884
885
885
impl < ' a , T > Hole < ' a , T > {
886
886
/// Create a new Hole at index `pos`.
887
- fn new ( data : & ' a mut [ T ] , pos : usize ) -> Self {
888
- unsafe {
889
- let elt = ptr:: read ( & data[ pos] ) ;
890
- Hole {
891
- data : data,
892
- elt : Some ( elt) ,
893
- pos : pos,
894
- }
887
+ ///
888
+ /// Unsafe because pos must be within the data slice.
889
+ #[ inline]
890
+ unsafe fn new ( data : & ' a mut [ T ] , pos : usize ) -> Self {
891
+ debug_assert ! ( pos < data. len( ) ) ;
892
+ let elt = ptr:: read ( & data[ pos] ) ;
893
+ Hole {
894
+ data : data,
895
+ elt : Some ( elt) ,
896
+ pos : pos,
895
897
}
896
898
}
897
899
898
- #[ inline( always ) ]
900
+ #[ inline]
899
901
fn pos ( & self ) -> usize {
900
902
self . pos
901
903
}
902
904
903
905
/// Return a reference to the element removed
904
- #[ inline( always ) ]
906
+ #[ inline]
905
907
fn element ( & self ) -> & T {
906
908
self . elt . as_ref ( ) . unwrap ( )
907
909
}
908
910
909
911
/// Return a reference to the element at `index`.
910
912
///
911
- /// Panics if the index is out of bounds.
912
- ///
913
- /// Unsafe because index must not equal pos.
914
- #[ inline( always) ]
913
+ /// Unsafe because index must be within the data slice and not equal to pos.
914
+ #[ inline]
915
915
unsafe fn get ( & self , index : usize ) -> & T {
916
916
debug_assert ! ( index != self . pos) ;
917
- & self . data [ index]
917
+ debug_assert ! ( index < self . data. len( ) ) ;
918
+ self . data . get_unchecked ( index)
918
919
}
919
920
920
921
/// Move hole to new location
921
922
///
922
- /// Unsafe because index must not equal pos.
923
- #[ inline( always ) ]
923
+ /// Unsafe because index must be within the data slice and not equal to pos.
924
+ #[ inline]
924
925
unsafe fn move_to ( & mut self , index : usize ) {
925
926
debug_assert ! ( index != self . pos) ;
926
- let index_ptr: * const _ = & self . data [ index] ;
927
- let hole_ptr = & mut self . data [ self . pos ] ;
927
+ debug_assert ! ( index < self . data. len( ) ) ;
928
+ let index_ptr: * const _ = self . data . get_unchecked ( index) ;
929
+ let hole_ptr = self . data . get_unchecked_mut ( self . pos ) ;
928
930
ptr:: copy_nonoverlapping ( index_ptr, hole_ptr, 1 ) ;
929
931
self . pos = index;
930
932
}
931
933
}
932
934
933
935
impl < ' a , T > Drop for Hole < ' a , T > {
936
+ #[ inline]
934
937
fn drop ( & mut self ) {
935
938
// fill the hole again
936
939
unsafe {
937
940
let pos = self . pos ;
938
- ptr:: write ( & mut self . data [ pos] , self . elt . take ( ) . unwrap ( ) ) ;
941
+ ptr:: write ( self . data . get_unchecked_mut ( pos) , self . elt . take ( ) . unwrap ( ) ) ;
939
942
}
940
943
}
941
944
}
0 commit comments