File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -502,6 +502,21 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
502
502
}
503
503
}
504
504
505
+ impl < T > Index < uint , T > for TrieMap < T > {
506
+ #[ inline]
507
+ fn index < ' a > ( & ' a self , i : & uint ) -> & ' a T {
508
+ self . find ( i) . expect ( "key not present" )
509
+ }
510
+ }
511
+
512
+ // FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
513
+ /*impl<T> IndexMut<uint, T> for TrieMap<T> {
514
+ #[inline]
515
+ fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut T {
516
+ self.find_mut(i).expect("key not present")
517
+ }
518
+ }*/
519
+
505
520
/// A set implemented as a radix trie.
506
521
///
507
522
/// # Example
@@ -1391,6 +1406,29 @@ mod test_map {
1391
1406
assert ! ( map_str == "{1: a, 2: b}" . to_string( ) ) ;
1392
1407
assert_eq ! ( format!( "{}" , empty) , "{}" . to_string( ) ) ;
1393
1408
}
1409
+
1410
+ #[ test]
1411
+ fn test_index ( ) {
1412
+ let mut map = TrieMap :: new ( ) ;
1413
+
1414
+ map. insert ( 1 , 2 i) ;
1415
+ map. insert ( 2 , 1 i) ;
1416
+ map. insert ( 3 , 4 i) ;
1417
+
1418
+ assert_eq ! ( map[ 2 ] , 1 ) ;
1419
+ }
1420
+
1421
+ #[ test]
1422
+ #[ should_fail]
1423
+ fn test_index_nonexistent ( ) {
1424
+ let mut map = TrieMap :: new ( ) ;
1425
+
1426
+ map. insert ( 1 , 2 i) ;
1427
+ map. insert ( 2 , 1 i) ;
1428
+ map. insert ( 3 , 4 i) ;
1429
+
1430
+ map[ 4 ] ;
1431
+ }
1394
1432
}
1395
1433
1396
1434
#[ cfg( test) ]
You can’t perform that action at this time.
0 commit comments