@@ -43,7 +43,7 @@ use core::ops::{Index, IndexMut, RangeBounds};
4343use std:: hash:: RandomState ;
4444
4545use crate :: inner:: Core ;
46- use crate :: util:: { third, try_simplify_range} ;
46+ use crate :: util:: { assert_index_le , assert_index_lt , third, try_simplify_range} ;
4747use crate :: { Bucket , Equivalent , GetDisjointMutError , HashValue , TryReserveError } ;
4848
4949/// A hash table where the iteration order of the key-value pairs is independent
@@ -595,12 +595,7 @@ where
595595 /// ```
596596 #[ track_caller]
597597 pub fn insert_before ( & mut self , mut index : usize , key : K , value : V ) -> ( usize , Option < V > ) {
598- let len = self . len ( ) ;
599-
600- assert ! (
601- index <= len,
602- "index out of bounds: the len is {len} but the index is {index}. Expected index <= len"
603- ) ;
598+ assert_index_le ( index, self . len ( ) ) ;
604599
605600 match self . entry ( key) {
606601 Entry :: Occupied ( mut entry) => {
@@ -683,21 +678,13 @@ where
683678 let len = self . len ( ) ;
684679 match self . entry ( key) {
685680 Entry :: Occupied ( mut entry) => {
686- assert ! (
687- index < len,
688- "index out of bounds: the len is {len} but the index is {index}"
689- ) ;
690-
681+ assert_index_lt ( index, len) ;
691682 let old = mem:: replace ( entry. get_mut ( ) , value) ;
692683 entry. move_index ( index) ;
693684 Some ( old)
694685 }
695686 Entry :: Vacant ( entry) => {
696- assert ! (
697- index <= len,
698- "index out of bounds: the len is {len} but the index is {index}. Expected index <= len"
699- ) ;
700-
687+ assert_index_le ( index, len) ;
701688 entry. shift_insert ( index, value) ;
702689 None
703690 }
@@ -721,6 +708,8 @@ where
721708 /// Computes in **O(1)** time (average).
722709 #[ track_caller]
723710 pub fn replace_index ( & mut self , index : usize , key : K ) -> Result < K , ( usize , K ) > {
711+ assert_index_lt ( index, self . len ( ) ) ;
712+
724713 // If there's a direct match, we don't even need to hash it.
725714 let entry = & mut self . as_entries_mut ( ) [ index] ;
726715 if key == entry. key {
@@ -1709,14 +1698,8 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
17091698 ///
17101699 /// ***Panics*** if `index` is out of bounds.
17111700 fn index ( & self , index : usize ) -> & V {
1712- if let Some ( ( _, value) ) = self . get_index ( index) {
1713- value
1714- } else {
1715- panic ! (
1716- "index out of bounds: the len is {len} but the index is {index}" ,
1717- len = self . len( )
1718- ) ;
1719- }
1701+ assert_index_lt ( index, self . len ( ) ) ;
1702+ & self . as_entries ( ) [ index] . value
17201703 }
17211704}
17221705
@@ -1754,13 +1737,8 @@ impl<K, V, S> IndexMut<usize> for IndexMap<K, V, S> {
17541737 ///
17551738 /// ***Panics*** if `index` is out of bounds.
17561739 fn index_mut ( & mut self , index : usize ) -> & mut V {
1757- let len: usize = self . len ( ) ;
1758-
1759- if let Some ( ( _, value) ) = self . get_index_mut ( index) {
1760- value
1761- } else {
1762- panic ! ( "index out of bounds: the len is {len} but the index is {index}" ) ;
1763- }
1740+ assert_index_lt ( index, self . len ( ) ) ;
1741+ & mut self . as_entries_mut ( ) [ index] . value
17641742 }
17651743}
17661744
0 commit comments