@@ -209,12 +209,15 @@ impl<V> SmallIntMap<V> {
209
209
/// # Example
210
210
///
211
211
/// ```
212
+ /// #![allow(deprecated)]
213
+ ///
212
214
/// use std::collections::SmallIntMap;
213
215
///
214
216
/// let mut map = SmallIntMap::new();
215
217
/// map.insert(1, "a");
216
218
/// assert_eq!(map.get(&1), &"a");
217
219
/// ```
220
+ #[ deprecated = "prefer using indexing, e.g., map[0]" ]
218
221
pub fn get < ' a > ( & ' a self , key : & uint ) -> & ' a V {
219
222
self . find ( key) . expect ( "key not present" )
220
223
}
@@ -330,11 +333,11 @@ impl<V:Clone> SmallIntMap<V> {
330
333
///
331
334
/// // Key does not exist, will do a simple insert
332
335
/// assert!(map.update(1, vec![1i, 2], |old, new| old.append(new.as_slice())));
333
- /// assert_eq!(map.get(&1), & vec![1i, 2]);
336
+ /// assert_eq!(map[1], vec![1i, 2]);
334
337
///
335
338
/// // Key exists, update the value
336
339
/// assert!(!map.update(1, vec![3i, 4], |old, new| old.append(new.as_slice())));
337
- /// assert_eq!(map.get(&1), & vec![1i, 2, 3, 4]);
340
+ /// assert_eq!(map[1], vec![1i, 2, 3, 4]);
338
341
/// ```
339
342
pub fn update ( & mut self , key : uint , newval : V , ff : |V , V | -> V ) -> bool {
340
343
self . update_with_key ( key, newval, |_k, v, v1| ff ( v, v1) )
@@ -354,11 +357,11 @@ impl<V:Clone> SmallIntMap<V> {
354
357
///
355
358
/// // Key does not exist, will do a simple insert
356
359
/// assert!(map.update_with_key(7, 10, |key, old, new| (old + new) % key));
357
- /// assert_eq!(map.get(&7), & 10);
360
+ /// assert_eq!(map[7], 10);
358
361
///
359
362
/// // Key exists, update the value
360
363
/// assert!(!map.update_with_key(7, 20, |key, old, new| (old + new) % key));
361
- /// assert_eq!(map.get(&7), & 2);
364
+ /// assert_eq!(map[7], 2);
362
365
/// ```
363
366
pub fn update_with_key ( & mut self ,
364
367
key : uint ,
@@ -416,6 +419,21 @@ impl<V> Extendable<(uint, V)> for SmallIntMap<V> {
416
419
}
417
420
}
418
421
422
+ impl < V > Index < uint , V > for SmallIntMap < V > {
423
+ #[ inline]
424
+ fn index < ' a > ( & ' a self , i : & uint ) -> & ' a V {
425
+ self . get ( i)
426
+ }
427
+ }
428
+
429
+ // FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
430
+ /*impl<V> IndexMut<uint, V> for SmallIntMap<V> {
431
+ #[inline]
432
+ fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V {
433
+ self.find_mut(i).expect("key not present")
434
+ }
435
+ }*/
436
+
419
437
macro_rules! iterator {
420
438
( impl $name: ident -> $elem: ty, $getter: ident) => {
421
439
impl <' a, T > Iterator <$elem> for $name<' a, T > {
@@ -843,6 +861,29 @@ mod test_map {
843
861
assert_eq ! ( map. find( & k) , Some ( & v) ) ;
844
862
}
845
863
}
864
+
865
+ #[ test]
866
+ fn test_index ( ) {
867
+ let mut map: SmallIntMap < int > = SmallIntMap :: new ( ) ;
868
+
869
+ map. insert ( 1 , 2 ) ;
870
+ map. insert ( 2 , 1 ) ;
871
+ map. insert ( 3 , 4 ) ;
872
+
873
+ assert_eq ! ( map[ 3 ] , 4 ) ;
874
+ }
875
+
876
+ #[ test]
877
+ #[ should_fail]
878
+ fn test_index_nonexistent ( ) {
879
+ let mut map: SmallIntMap < int > = SmallIntMap :: new ( ) ;
880
+
881
+ map. insert ( 1 , 2 ) ;
882
+ map. insert ( 2 , 1 ) ;
883
+ map. insert ( 3 , 4 ) ;
884
+
885
+ map[ 4 ] ;
886
+ }
846
887
}
847
888
848
889
#[ cfg( test) ]
0 commit comments