Skip to content

Commit e8204a8

Browse files
committed
auto merge of #16195 : P1start/rust/more-index, r=aturon
Implement `Index` for `RingBuf`, `HashMap`, `TreeMap`, `SmallIntMap`, and `TrieMap`. If there’s anything that I missed or should be removed, let me know.
2 parents 49a970f + 8f71cb0 commit e8204a8

File tree

8 files changed

+216
-22
lines changed

8 files changed

+216
-22
lines changed

src/libcollections/ringbuf.rs

+40-3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ impl<T> RingBuf<T> {
139139
/// # Example
140140
///
141141
/// ```rust
142+
/// #![allow(deprecated)]
143+
///
142144
/// use std::collections::RingBuf;
143145
///
144146
/// let mut buf = RingBuf::new();
@@ -147,6 +149,7 @@ impl<T> RingBuf<T> {
147149
/// buf.push(5);
148150
/// assert_eq!(buf.get(1), &4);
149151
/// ```
152+
#[deprecated = "prefer using indexing, e.g., ringbuf[0]"]
150153
pub fn get<'a>(&'a self, i: uint) -> &'a T {
151154
let idx = self.raw_index(i);
152155
match *self.elts.get(idx) {
@@ -169,7 +172,7 @@ impl<T> RingBuf<T> {
169172
/// buf.push(4);
170173
/// buf.push(5);
171174
/// *buf.get_mut(1) = 7;
172-
/// assert_eq!(buf.get(1), &7);
175+
/// assert_eq!(buf[1], 7);
173176
/// ```
174177
pub fn get_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
175178
let idx = self.raw_index(i);
@@ -195,8 +198,8 @@ impl<T> RingBuf<T> {
195198
/// buf.push(4);
196199
/// buf.push(5);
197200
/// buf.swap(0, 2);
198-
/// assert_eq!(buf.get(0), &5);
199-
/// assert_eq!(buf.get(2), &3);
201+
/// assert_eq!(buf[0], 5);
202+
/// assert_eq!(buf[2], 3);
200203
/// ```
201204
pub fn swap(&mut self, i: uint, j: uint) {
202205
assert!(i < self.len());
@@ -476,6 +479,21 @@ impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
476479
}
477480
}
478481

482+
impl<A> Index<uint, A> for RingBuf<A> {
483+
#[inline]
484+
fn index<'a>(&'a self, i: &uint) -> &'a A {
485+
self.get(*i)
486+
}
487+
}
488+
489+
// FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
490+
/*impl<A> IndexMut<uint, A> for RingBuf<A> {
491+
#[inline]
492+
fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut A {
493+
self.get_mut(*index)
494+
}
495+
}*/
496+
479497
impl<A> FromIterator<A> for RingBuf<A> {
480498
fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> {
481499
let (lower, _) = iterator.size_hint();
@@ -653,6 +671,25 @@ mod tests {
653671
}
654672
}
655673

674+
#[test]
675+
fn test_index() {
676+
let mut deq = RingBuf::new();
677+
for i in range(1u, 4) {
678+
deq.push_front(i);
679+
}
680+
assert_eq!(deq[1], 2);
681+
}
682+
683+
#[test]
684+
#[should_fail]
685+
fn test_index_out_of_bounds() {
686+
let mut deq = RingBuf::new();
687+
for i in range(1u, 4) {
688+
deq.push_front(i);
689+
}
690+
deq[3];
691+
}
692+
656693
#[bench]
657694
fn bench_new(b: &mut test::Bencher) {
658695
b.iter(|| {

src/libcollections/smallintmap.rs

+45-4
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)]

src/libcollections/treemap.rs

+36
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,20 @@ impl<K: Ord, V> Default for TreeMap<K,V> {
246246
fn default() -> TreeMap<K, V> { TreeMap::new() }
247247
}
248248

249+
impl<K: Ord, V> Index<K, V> for TreeMap<K, V> {
250+
#[inline]
251+
fn index<'a>(&'a self, i: &K) -> &'a V {
252+
self.find(i).expect("no entry found for key")
253+
}
254+
}
255+
256+
/*impl<K: Ord, V> IndexMut<K, V> for TreeMap<K, V> {
257+
#[inline]
258+
fn index_mut<'a>(&'a mut self, i: &K) -> &'a mut V {
259+
self.find_mut(i).expect("no entry found for key")
260+
}
261+
}*/
262+
249263
impl<K: Ord, V> TreeMap<K, V> {
250264
/// Create an empty `TreeMap`.
251265
///
@@ -2149,6 +2163,28 @@ mod test_treemap {
21492163
}
21502164
}
21512165

2166+
#[test]
2167+
fn test_index() {
2168+
let mut map: TreeMap<int, int> = TreeMap::new();
2169+
2170+
map.insert(1, 2);
2171+
map.insert(2, 1);
2172+
map.insert(3, 4);
2173+
2174+
assert_eq!(map[2], 1);
2175+
}
2176+
2177+
#[test]
2178+
#[should_fail]
2179+
fn test_index_nonexistent() {
2180+
let mut map: TreeMap<int, int> = TreeMap::new();
2181+
2182+
map.insert(1, 2);
2183+
map.insert(2, 1);
2184+
map.insert(3, 4);
2185+
2186+
map[4];
2187+
}
21522188
}
21532189

21542190
#[cfg(test)]

src/libcollections/trie.rs

+38
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,21 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
502502
}
503503
}
504504

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+
505520
/// A set implemented as a radix trie.
506521
///
507522
/// # Example
@@ -1391,6 +1406,29 @@ mod test_map {
13911406
assert!(map_str == "{1: a, 2: b}".to_string());
13921407
assert_eq!(format!("{}", empty), "{}".to_string());
13931408
}
1409+
1410+
#[test]
1411+
fn test_index() {
1412+
let mut map = TrieMap::new();
1413+
1414+
map.insert(1, 2i);
1415+
map.insert(2, 1i);
1416+
map.insert(3, 4i);
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, 2i);
1427+
map.insert(2, 1i);
1428+
map.insert(3, 4i);
1429+
1430+
map[4];
1431+
}
13941432
}
13951433

13961434
#[cfg(test)]

src/librustdoc/html/format.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: ast::DefId, p: &clean::Path,
166166
if ast_util::is_local(did) || cache.inlined.contains(&did) {
167167
Some(("../".repeat(loc.len())).to_string())
168168
} else {
169-
match *cache.extern_locations.get(&did.krate) {
169+
match cache.extern_locations[did.krate] {
170170
render::Remote(ref s) => Some(s.to_string()),
171171
render::Local => {
172172
Some(("../".repeat(loc.len())).to_string())
@@ -291,11 +291,11 @@ fn primitive_link(f: &mut fmt::Formatter,
291291
needs_termination = true;
292292
}
293293
Some(&cnum) => {
294-
let path = m.paths.get(&ast::DefId {
294+
let path = &m.paths[ast::DefId {
295295
krate: cnum,
296296
node: ast::CRATE_NODE_ID,
297-
});
298-
let loc = match *m.extern_locations.get(&cnum) {
297+
}];
298+
let loc = match m.extern_locations[cnum] {
299299
render::Remote(ref s) => Some(s.to_string()),
300300
render::Local => {
301301
let loc = current_location_key.get().unwrap();
@@ -343,11 +343,11 @@ impl fmt::Show for clean::Type {
343343
match *self {
344344
clean::TyParamBinder(id) => {
345345
let m = cache_key.get().unwrap();
346-
f.write(m.typarams.get(&ast_util::local_def(id)).as_bytes())
346+
f.write(m.typarams[ast_util::local_def(id)].as_bytes())
347347
}
348348
clean::Generic(did) => {
349349
let m = cache_key.get().unwrap();
350-
f.write(m.typarams.get(&did).as_bytes())
350+
f.write(m.typarams[did].as_bytes())
351351
}
352352
clean::ResolvedPath{ did, ref typarams, ref path } => {
353353
try!(resolved_path(f, did, path, false));

src/librustdoc/html/render.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1274,8 +1274,8 @@ impl<'a> Item<'a> {
12741274
// located, then we return `None`.
12751275
} else {
12761276
let cache = cache_key.get().unwrap();
1277-
let path = cache.external_paths.get(&self.item.def_id);
1278-
let root = match *cache.extern_locations.get(&self.item.def_id.krate) {
1277+
let path = &cache.external_paths[self.item.def_id];
1278+
let root = match cache.extern_locations[self.item.def_id.krate] {
12791279
Remote(ref s) => s.to_string(),
12801280
Local => self.cx.root_path.clone(),
12811281
Unknown => return None,

src/librustdoc/visit_ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<'a> RustdocVisitor<'a> {
229229
core::Typed(ref tcx) => tcx,
230230
core::NotTyped(_) => return false
231231
};
232-
let def = tcx.def_map.borrow().get(&id).def_id();
232+
let def = (*tcx.def_map.borrow())[id].def_id();
233233
if !ast_util::is_local(def) { return false }
234234
let analysis = match self.analysis {
235235
Some(analysis) => analysis, None => return false

0 commit comments

Comments
 (0)