@@ -299,6 +299,7 @@ impl<K, V, S> IndexMap<K, V, S> {
299299 ///
300300 /// ***Panics*** if the starting point is greater than the end point or if
301301 /// the end point is greater than the length of the map.
302+ #[ track_caller]
302303 pub fn drain < R > ( & mut self , range : R ) -> Drain < ' _ , K , V >
303304 where
304305 R : RangeBounds < usize > ,
@@ -313,6 +314,7 @@ impl<K, V, S> IndexMap<K, V, S> {
313314 /// the elements `[0, at)` with its previous capacity unchanged.
314315 ///
315316 /// ***Panics*** if `at > len`.
317+ #[ track_caller]
316318 pub fn split_off ( & mut self , at : usize ) -> Self
317319 where
318320 S : Clone ,
@@ -493,8 +495,15 @@ where
493495 /// assert_eq!(map.get_index_of(&'+'), Some(27));
494496 /// assert_eq!(map.len(), 28);
495497 /// ```
498+ #[ track_caller]
496499 pub fn insert_before ( & mut self , mut index : usize , key : K , value : V ) -> ( usize , Option < V > ) {
497- assert ! ( index <= self . len( ) , "index out of bounds" ) ;
500+ let len = self . len ( ) ;
501+
502+ assert ! (
503+ index <= len,
504+ "index out of bounds: the len is {len} but the index is {index}. Expected index <= len"
505+ ) ;
506+
498507 match self . entry ( key) {
499508 Entry :: Occupied ( mut entry) => {
500509 if index > entry. index ( ) {
@@ -571,17 +580,26 @@ where
571580 /// // This is an invalid index for moving an existing key!
572581 /// map.shift_insert(map.len(), 'a', ());
573582 /// ```
583+ #[ track_caller]
574584 pub fn shift_insert ( & mut self , index : usize , key : K , value : V ) -> Option < V > {
575585 let len = self . len ( ) ;
576586 match self . entry ( key) {
577587 Entry :: Occupied ( mut entry) => {
578- assert ! ( index < len, "index out of bounds" ) ;
588+ assert ! (
589+ index < len,
590+ "index out of bounds: the len is {len} but the index is {index}"
591+ ) ;
592+
579593 let old = mem:: replace ( entry. get_mut ( ) , value) ;
580594 entry. move_index ( index) ;
581595 Some ( old)
582596 }
583597 Entry :: Vacant ( entry) => {
584- assert ! ( index <= len, "index out of bounds" ) ;
598+ assert ! (
599+ index <= len,
600+ "index out of bounds: the len is {len} but the index is {index}. Expected index <= len"
601+ ) ;
602+
585603 entry. shift_insert ( index, value) ;
586604 None
587605 }
@@ -627,6 +645,7 @@ where
627645 /// assert!(map.into_iter().eq([(0, '_'), (1, 'A'), (5, 'E'), (3, 'C'), (2, 'B'), (4, 'D')]));
628646 /// assert_eq!(removed, &[(2, 'b'), (3, 'c')]);
629647 /// ```
648+ #[ track_caller]
630649 pub fn splice < R , I > ( & mut self , range : R , replace_with : I ) -> Splice < ' _ , I :: IntoIter , K , V , S >
631650 where
632651 R : RangeBounds < usize > ,
@@ -1278,6 +1297,7 @@ impl<K, V, S> IndexMap<K, V, S> {
12781297 /// ***Panics*** if `from` or `to` are out of bounds.
12791298 ///
12801299 /// Computes in **O(n)** time (average).
1300+ #[ track_caller]
12811301 pub fn move_index ( & mut self , from : usize , to : usize ) {
12821302 self . core . move_index ( from, to)
12831303 }
@@ -1287,6 +1307,7 @@ impl<K, V, S> IndexMap<K, V, S> {
12871307 /// ***Panics*** if `a` or `b` are out of bounds.
12881308 ///
12891309 /// Computes in **O(1)** time (average).
1310+ #[ track_caller]
12901311 pub fn swap_indices ( & mut self , a : usize , b : usize ) {
12911312 self . core . swap_indices ( a, b)
12921313 }
@@ -1325,7 +1346,7 @@ where
13251346 ///
13261347 /// ***Panics*** if `key` is not present in the map.
13271348 fn index ( & self , key : & Q ) -> & V {
1328- self . get ( key) . expect ( "IndexMap: key not found" )
1349+ self . get ( key) . expect ( "no entry found for key " )
13291350 }
13301351}
13311352
@@ -1367,7 +1388,7 @@ where
13671388 ///
13681389 /// ***Panics*** if `key` is not present in the map.
13691390 fn index_mut ( & mut self , key : & Q ) -> & mut V {
1370- self . get_mut ( key) . expect ( "IndexMap: key not found" )
1391+ self . get_mut ( key) . expect ( "no entry found for key " )
13711392 }
13721393}
13731394
@@ -1411,7 +1432,12 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
14111432 /// ***Panics*** if `index` is out of bounds.
14121433 fn index ( & self , index : usize ) -> & V {
14131434 self . get_index ( index)
1414- . expect ( "IndexMap: index out of bounds" )
1435+ . unwrap_or_else ( || {
1436+ panic ! (
1437+ "index out of bounds: the len is {len} but the index is {index}" ,
1438+ len = self . len( )
1439+ ) ;
1440+ } )
14151441 . 1
14161442 }
14171443}
@@ -1450,8 +1476,12 @@ impl<K, V, S> IndexMut<usize> for IndexMap<K, V, S> {
14501476 ///
14511477 /// ***Panics*** if `index` is out of bounds.
14521478 fn index_mut ( & mut self , index : usize ) -> & mut V {
1479+ let len: usize = self . len ( ) ;
1480+
14531481 self . get_index_mut ( index)
1454- . expect ( "IndexMap: index out of bounds" )
1482+ . unwrap_or_else ( || {
1483+ panic ! ( "index out of bounds: the len is {len} but the index is {index}" ) ;
1484+ } )
14551485 . 1
14561486 }
14571487}
0 commit comments