Skip to content

Commit 1d35662

Browse files
committed
collections: Enable IndexMut for some collections
This commit enables implementations of IndexMut for a number of collections, including Vec, RingBuf, SmallIntMap, TrieMap, TreeMap, and HashMap. At the same time this deprecates the `get_mut` methods on vectors in favor of using the indexing notation. cc rust-lang#18424
1 parent 18a3db6 commit 1d35662

File tree

46 files changed

+165
-271
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+165
-271
lines changed

src/compiletest/runtest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ fn check_expected_errors(expected_errors: Vec<errors::ExpectedError> ,
10131013
if prefix_matches(line, prefixes[i].as_slice()) &&
10141014
line.contains(ee.kind.as_slice()) &&
10151015
line.contains(ee.msg.as_slice()) {
1016-
*found_flags.get_mut(i) = true;
1016+
found_flags[i] = true;
10171017
was_expected = true;
10181018
break;
10191019
}

src/libcollections/bitv.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl Bitv {
243243
let used_bits = bitv.nbits % u32::BITS;
244244
if init && used_bits != 0 {
245245
let largest_used_word = (bitv.nbits + u32::BITS - 1) / u32::BITS - 1;
246-
*bitv.storage.get_mut(largest_used_word) &= (1 << used_bits) - 1;
246+
bitv.storage[largest_used_word] &= (1 << used_bits) - 1;
247247
}
248248

249249
bitv
@@ -297,8 +297,9 @@ impl Bitv {
297297
let w = i / u32::BITS;
298298
let b = i % u32::BITS;
299299
let flag = 1 << b;
300-
*self.storage.get_mut(w) = if x { self.storage[w] | flag }
301-
else { self.storage[w] & !flag };
300+
let val = if x { self.storage[w] | flag }
301+
else { self.storage[w] & !flag };
302+
self.storage[w] = val;
302303
}
303304

304305
/// Sets all bits to 1.
@@ -617,7 +618,7 @@ impl Bitv {
617618
self.storage.truncate(word_len);
618619
if len % u32::BITS > 0 {
619620
let mask = (1 << len % u32::BITS) - 1;
620-
*self.storage.get_mut(word_len - 1) &= mask;
621+
self.storage[word_len - 1] &= mask;
621622
}
622623
}
623624
}
@@ -681,15 +682,15 @@ impl Bitv {
681682
let overhang = self.nbits % u32::BITS; // # of already-used bits
682683
let mask = !((1 << overhang) - 1); // e.g. 5 unused bits => 111110....0
683684
if value {
684-
*self.storage.get_mut(old_last_word) |= mask;
685+
self.storage[old_last_word] |= mask;
685686
} else {
686-
*self.storage.get_mut(old_last_word) &= !mask;
687+
self.storage[old_last_word] &= !mask;
687688
}
688689
}
689690
// Fill in words after the old tail word
690691
let stop_idx = cmp::min(self.storage.len(), new_nwords);
691692
for idx in range(old_last_word + 1, stop_idx) {
692-
*self.storage.get_mut(idx) = full_value;
693+
self.storage[idx] = full_value;
693694
}
694695
// Allocate new words, if needed
695696
if new_nwords > self.storage.len() {
@@ -700,7 +701,7 @@ impl Bitv {
700701
if value {
701702
let tail_word = new_nwords - 1;
702703
let used_bits = new_nbits % u32::BITS;
703-
*self.storage.get_mut(tail_word) &= (1 << used_bits) - 1;
704+
self.storage[tail_word] &= (1 << used_bits) - 1;
704705
}
705706
}
706707
// Adjust internal bit count
@@ -728,7 +729,7 @@ impl Bitv {
728729
let ret = self.get(self.nbits - 1);
729730
// If we are unusing a whole word, make sure it is zeroed out
730731
if self.nbits % u32::BITS == 1 {
731-
*self.storage.get_mut(self.nbits / u32::BITS) = 0;
732+
self.storage[self.nbits / u32::BITS] = 0;
732733
}
733734
self.nbits -= 1;
734735
ret
@@ -1184,7 +1185,7 @@ impl BitvSet {
11841185
for (i, w) in other_words {
11851186
let old = self_bitv.storage[i];
11861187
let new = f(old, w);
1187-
*self_bitv.storage.get_mut(i) = new;
1188+
self_bitv.storage[i] = new;
11881189
}
11891190
}
11901191

src/libcollections/btree/map.rs

+6
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,12 @@ impl<K: Ord, V> Index<K, V> for BTreeMap<K, V> {
690690
}
691691
}
692692

693+
impl<K: Ord, V> IndexMut<K, V> for BTreeMap<K, V> {
694+
fn index_mut(&mut self, key: &K) -> &mut V {
695+
self.find_mut(key).expect("no entry found for key")
696+
}
697+
}
698+
693699
/// Genericises over how to get the correct type of iterator from the correct type
694700
/// of Node ownership.
695701
trait Traverse<N> {

src/libcollections/priority_queue.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
//! let mut pq = PriorityQueue::new();
7272
//!
7373
//! // We're at `start`, with a zero cost
74-
//! *dist.get_mut(start) = 0u;
74+
//! dist[start] = 0u;
7575
//! pq.push(State { cost: 0u, position: start });
7676
//!
7777
//! // Examine the frontier with lower cost nodes first (min-heap)
@@ -96,7 +96,7 @@
9696
//! if next.cost < dist[next.position] {
9797
//! pq.push(next);
9898
//! // Relaxation, we have now found a better way
99-
//! *dist.get_mut(next.position) = next.cost;
99+
//! dist[next.position] = next.cost;
100100
//! }
101101
//! }
102102
//! }
@@ -330,7 +330,7 @@ impl<T: Ord> PriorityQueue<T> {
330330
None => { None }
331331
Some(mut item) => {
332332
if !self.is_empty() {
333-
swap(&mut item, self.data.get_mut(0));
333+
swap(&mut item, &mut self.data[0]);
334334
self.siftdown(0);
335335
}
336336
Some(item)
@@ -378,7 +378,7 @@ impl<T: Ord> PriorityQueue<T> {
378378
/// ```
379379
pub fn push_pop(&mut self, mut item: T) -> T {
380380
if !self.is_empty() && *self.top().unwrap() > item {
381-
swap(&mut item, self.data.get_mut(0));
381+
swap(&mut item, &mut self.data[0]);
382382
self.siftdown(0);
383383
}
384384
item
@@ -402,7 +402,7 @@ impl<T: Ord> PriorityQueue<T> {
402402
/// ```
403403
pub fn replace(&mut self, mut item: T) -> Option<T> {
404404
if !self.is_empty() {
405-
swap(&mut item, self.data.get_mut(0));
405+
swap(&mut item, &mut self.data[0]);
406406
self.siftdown(0);
407407
Some(item)
408408
} else {
@@ -462,40 +462,40 @@ impl<T: Ord> PriorityQueue<T> {
462462
// compared to using swaps, which involves twice as many moves.
463463
fn siftup(&mut self, start: uint, mut pos: uint) {
464464
unsafe {
465-
let new = replace(self.data.get_mut(pos), zeroed());
465+
let new = replace(&mut self.data[pos], zeroed());
466466

467467
while pos > start {
468468
let parent = (pos - 1) >> 1;
469469
if new > self.data[parent] {
470-
let x = replace(self.data.get_mut(parent), zeroed());
471-
ptr::write(self.data.get_mut(pos), x);
470+
let x = replace(&mut self.data[parent], zeroed());
471+
ptr::write(&mut self.data[pos], x);
472472
pos = parent;
473473
continue
474474
}
475475
break
476476
}
477-
ptr::write(self.data.get_mut(pos), new);
477+
ptr::write(&mut self.data[pos], new);
478478
}
479479
}
480480

481481
fn siftdown_range(&mut self, mut pos: uint, end: uint) {
482482
unsafe {
483483
let start = pos;
484-
let new = replace(self.data.get_mut(pos), zeroed());
484+
let new = replace(&mut self.data[pos], zeroed());
485485

486486
let mut child = 2 * pos + 1;
487487
while child < end {
488488
let right = child + 1;
489489
if right < end && !(self.data[child] > self.data[right]) {
490490
child = right;
491491
}
492-
let x = replace(self.data.get_mut(child), zeroed());
493-
ptr::write(self.data.get_mut(pos), x);
492+
let x = replace(&mut self.data[child], zeroed());
493+
ptr::write(&mut self.data[pos], x);
494494
pos = child;
495495
child = 2 * pos + 1;
496496
}
497497

498-
ptr::write(self.data.get_mut(pos), new);
498+
ptr::write(&mut self.data[pos], new);
499499
self.siftup(start, pos);
500500
}
501501
}

src/libcollections/ringbuf.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<T> Deque<T> for RingBuf<T> {
5858

5959
/// Returns a mutable reference to the first element in the `RingBuf`.
6060
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
61-
if self.nelts > 0 { Some(self.get_mut(0)) } else { None }
61+
if self.nelts > 0 { Some(&mut self[0]) } else { None }
6262
}
6363

6464
/// Returns a reference to the last element in the `RingBuf`.
@@ -69,13 +69,13 @@ impl<T> Deque<T> for RingBuf<T> {
6969
/// Returns a mutable reference to the last element in the `RingBuf`.
7070
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
7171
let nelts = self.nelts;
72-
if nelts > 0 { Some(self.get_mut(nelts - 1)) } else { None }
72+
if nelts > 0 { Some(&mut self[nelts - 1]) } else { None }
7373
}
7474

7575
/// Removes and returns the first element in the `RingBuf`, or `None` if it
7676
/// is empty.
7777
fn pop_front(&mut self) -> Option<T> {
78-
let result = self.elts.get_mut(self.lo).take();
78+
let result = self.elts[self.lo].take();
7979
if result.is_some() {
8080
self.lo = (self.lo + 1u) % self.elts.len();
8181
self.nelts -= 1u;
@@ -91,7 +91,7 @@ impl<T> Deque<T> for RingBuf<T> {
9191
if self.lo == 0u {
9292
self.lo = self.elts.len() - 1u;
9393
} else { self.lo -= 1u; }
94-
*self.elts.get_mut(self.lo) = Some(t);
94+
self.elts[self.lo] = Some(t);
9595
self.nelts += 1u;
9696
}
9797
}
@@ -102,14 +102,14 @@ impl<T> MutableSeq<T> for RingBuf<T> {
102102
grow(self.nelts, &mut self.lo, &mut self.elts);
103103
}
104104
let hi = self.raw_index(self.nelts);
105-
*self.elts.get_mut(hi) = Some(t);
105+
self.elts[hi] = Some(t);
106106
self.nelts += 1u;
107107
}
108108
fn pop(&mut self) -> Option<T> {
109109
if self.nelts > 0 {
110110
self.nelts -= 1;
111111
let hi = self.raw_index(self.nelts);
112-
self.elts.get_mut(hi).take()
112+
self.elts[hi].take()
113113
} else {
114114
None
115115
}
@@ -140,6 +140,7 @@ impl<T> RingBuf<T> {
140140
/// # Example
141141
///
142142
/// ```rust
143+
/// # #![allow(deprecated)]
143144
/// use std::collections::RingBuf;
144145
///
145146
/// let mut buf = RingBuf::new();
@@ -149,12 +150,9 @@ impl<T> RingBuf<T> {
149150
/// *buf.get_mut(1) = 7;
150151
/// assert_eq!(buf[1], 7);
151152
/// ```
153+
#[deprecated = "use indexing instead: `buf[index] = value`"]
152154
pub fn get_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
153-
let idx = self.raw_index(i);
154-
match *self.elts.get_mut(idx) {
155-
None => panic!(),
156-
Some(ref mut v) => v
157-
}
155+
&mut self[i]
158156
}
159157

160158
/// Swaps elements at indices `i` and `j`.
@@ -466,13 +464,16 @@ impl<A> Index<uint, A> for RingBuf<A> {
466464
}
467465
}
468466

469-
// FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
470-
/*impl<A> IndexMut<uint, A> for RingBuf<A> {
467+
impl<A> IndexMut<uint, A> for RingBuf<A> {
471468
#[inline]
472-
fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut A {
473-
self.get_mut(*index)
469+
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut A {
470+
let idx = self.raw_index(*i);
471+
match *(&mut self.elts[idx]) {
472+
None => panic!(),
473+
Some(ref mut v) => v
474+
}
474475
}
475-
}*/
476+
}
476477

477478
impl<A> FromIterator<A> for RingBuf<A> {
478479
fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> {

src/libcollections/smallintmap.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<V> MutableMap<uint, V> for SmallIntMap<V> {
100100
/// Returns a mutable reference to the value corresponding to the key.
101101
fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut V> {
102102
if *key < self.v.len() {
103-
match *self.v.get_mut(*key) {
103+
match *self.v.index_mut(key) {
104104
Some(ref mut value) => Some(value),
105105
None => None
106106
}
@@ -118,7 +118,7 @@ impl<V> MutableMap<uint, V> for SmallIntMap<V> {
118118
if len <= key {
119119
self.v.grow_fn(key - len + 1, |_| None);
120120
}
121-
*self.v.get_mut(key) = Some(value);
121+
self.v[key] = Some(value);
122122
!exists
123123
}
124124

@@ -145,7 +145,7 @@ impl<V> MutableMap<uint, V> for SmallIntMap<V> {
145145
if *key >= self.v.len() {
146146
return None;
147147
}
148-
self.v.get_mut(*key).take()
148+
self.v[*key].take()
149149
}
150150
}
151151

@@ -405,13 +405,12 @@ impl<V> Index<uint, V> for SmallIntMap<V> {
405405
}
406406
}
407407

408-
// FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
409-
/*impl<V> IndexMut<uint, V> for SmallIntMap<V> {
408+
impl<V> IndexMut<uint, V> for SmallIntMap<V> {
410409
#[inline]
411410
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V {
412411
self.find_mut(i).expect("key not present")
413412
}
414-
}*/
413+
}
415414

416415
macro_rules! iterator {
417416
(impl $name:ident -> $elem:ty, $($getter:ident),+) => {

src/libcollections/str.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -692,17 +692,17 @@ pub trait StrAllocating: Str {
692692
for (i, sc) in me.chars().enumerate() {
693693

694694
let mut current = i;
695-
*dcol.get_mut(0) = current + 1;
695+
dcol[0] = current + 1;
696696

697697
for (j, tc) in t.chars().enumerate() {
698698

699699
let next = dcol[j + 1];
700700

701701
if sc == tc {
702-
*dcol.get_mut(j + 1) = current;
702+
dcol[j + 1] = current;
703703
} else {
704-
*dcol.get_mut(j + 1) = cmp::min(current, next);
705-
*dcol.get_mut(j + 1) = cmp::min(dcol[j + 1], dcol[j]) + 1;
704+
dcol[j + 1] = cmp::min(current, next);
705+
dcol[j + 1] = cmp::min(dcol[j + 1], dcol[j]) + 1;
706706
}
707707

708708
current = next;

src/libcollections/treemap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ impl<K: Ord, V> Index<K, V> for TreeMap<K, V> {
257257
}
258258
}
259259

260-
/*impl<K: Ord, V> IndexMut<K, V> for TreeMap<K, V> {
260+
impl<K: Ord, V> IndexMut<K, V> for TreeMap<K, V> {
261261
#[inline]
262262
fn index_mut<'a>(&'a mut self, i: &K) -> &'a mut V {
263263
self.find_mut(i).expect("no entry found for key")
264264
}
265-
}*/
265+
}
266266

267267
impl<K: Ord, V> TreeMap<K, V> {
268268
/// Creates an empty `TreeMap`.

src/libcollections/trie.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,12 @@ impl<T> Index<uint, T> for TrieMap<T> {
515515
}
516516
}
517517

518-
// FIXME(#12825) Indexing will always try IndexMut first and that causes issues.
519-
/*impl<T> IndexMut<uint, T> for TrieMap<T> {
518+
impl<T> IndexMut<uint, T> for TrieMap<T> {
520519
#[inline]
521520
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut T {
522521
self.find_mut(i).expect("key not present")
523522
}
524-
}*/
523+
}
525524

526525
/// A set implemented as a radix trie.
527526
///

0 commit comments

Comments
 (0)