@@ -497,7 +497,13 @@ where
497
497
/// ```
498
498
#[ track_caller]
499
499
pub fn insert_before ( & mut self , mut index : usize , key : K , value : V ) -> ( usize , Option < V > ) {
500
- 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
+
501
507
match self . entry ( key) {
502
508
Entry :: Occupied ( mut entry) => {
503
509
if index > entry. index ( ) {
@@ -579,13 +585,21 @@ where
579
585
let len = self . len ( ) ;
580
586
match self . entry ( key) {
581
587
Entry :: Occupied ( mut entry) => {
582
- 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
+
583
593
let old = mem:: replace ( entry. get_mut ( ) , value) ;
584
594
entry. move_index ( index) ;
585
595
Some ( old)
586
596
}
587
597
Entry :: Vacant ( entry) => {
588
- 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
+
589
603
entry. shift_insert ( index, value) ;
590
604
None
591
605
}
@@ -1332,7 +1346,7 @@ where
1332
1346
///
1333
1347
/// ***Panics*** if `key` is not present in the map.
1334
1348
fn index ( & self , key : & Q ) -> & V {
1335
- self . get ( key) . expect ( "IndexMap: key not found" )
1349
+ self . get ( key) . expect ( "no entry found for key " )
1336
1350
}
1337
1351
}
1338
1352
@@ -1374,7 +1388,7 @@ where
1374
1388
///
1375
1389
/// ***Panics*** if `key` is not present in the map.
1376
1390
fn index_mut ( & mut self , key : & Q ) -> & mut V {
1377
- self . get_mut ( key) . expect ( "IndexMap: key not found" )
1391
+ self . get_mut ( key) . expect ( "no entry found for key " )
1378
1392
}
1379
1393
}
1380
1394
@@ -1418,7 +1432,12 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
1418
1432
/// ***Panics*** if `index` is out of bounds.
1419
1433
fn index ( & self , index : usize ) -> & V {
1420
1434
self . get_index ( index)
1421
- . 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
+ } )
1422
1441
. 1
1423
1442
}
1424
1443
}
@@ -1457,8 +1476,12 @@ impl<K, V, S> IndexMut<usize> for IndexMap<K, V, S> {
1457
1476
///
1458
1477
/// ***Panics*** if `index` is out of bounds.
1459
1478
fn index_mut ( & mut self , index : usize ) -> & mut V {
1479
+ let len: usize = self . len ( ) ;
1480
+
1460
1481
self . get_index_mut ( index)
1461
- . 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
+ } )
1462
1485
. 1
1463
1486
}
1464
1487
}
0 commit comments