Skip to content

Commit f605612

Browse files
committed
---
yaml --- r: 128729 b: refs/heads/try c: 11b8f9c h: refs/heads/master i: 128727: caaec62 v: v3
1 parent 8a7c5f5 commit f605612

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 07d86b46a949a94223da714e35b343243e4ecce4
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a86d9ad15e339ab343a12513f9c90556f677b9ca
5-
refs/heads/try: 2dd6bc6887858f91b846ffa7dbd9f0f49b9f3b87
5+
refs/heads/try: 11b8f9c3f666e3a0533da0f3a00188689fc60cb4
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libcollections/smallintmap.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,15 @@ impl<V> SmallIntMap<V> {
209209
/// # Example
210210
///
211211
/// ```
212+
/// #![allow(deprecated)]
213+
///
212214
/// use std::collections::SmallIntMap;
213215
///
214216
/// let mut map = SmallIntMap::new();
215217
/// map.insert(1, "a");
216218
/// assert_eq!(map.get(&1), &"a");
217219
/// ```
220+
#[deprecated = "prefer using indexing, e.g., map[0]"]
218221
pub fn get<'a>(&'a self, key: &uint) -> &'a V {
219222
self.find(key).expect("key not present")
220223
}
@@ -330,11 +333,11 @@ impl<V:Clone> SmallIntMap<V> {
330333
///
331334
/// // Key does not exist, will do a simple insert
332335
/// 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]);
334337
///
335338
/// // Key exists, update the value
336339
/// 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]);
338341
/// ```
339342
pub fn update(&mut self, key: uint, newval: V, ff: |V, V| -> V) -> bool {
340343
self.update_with_key(key, newval, |_k, v, v1| ff(v,v1))
@@ -354,11 +357,11 @@ impl<V:Clone> SmallIntMap<V> {
354357
///
355358
/// // Key does not exist, will do a simple insert
356359
/// 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);
358361
///
359362
/// // Key exists, update the value
360363
/// 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);
362365
/// ```
363366
pub fn update_with_key(&mut self,
364367
key: uint,
@@ -416,6 +419,21 @@ impl<V> Extendable<(uint, V)> for SmallIntMap<V> {
416419
}
417420
}
418421

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+
419437
macro_rules! iterator {
420438
(impl $name:ident -> $elem:ty, $getter:ident) => {
421439
impl<'a, T> Iterator<$elem> for $name<'a, T> {
@@ -843,6 +861,29 @@ mod test_map {
843861
assert_eq!(map.find(&k), Some(&v));
844862
}
845863
}
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+
}
846887
}
847888

848889
#[cfg(test)]

0 commit comments

Comments
 (0)