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 #18424
This commit is contained in:
Alex Crichton 2014-10-23 08:42:21 -07:00
parent 18a3db6aa1
commit 1d356624a1
46 changed files with 165 additions and 271 deletions

View file

@ -1619,7 +1619,7 @@ fn encode_index<T: Hash>(rbml_w: &mut Encoder, index: Vec<entry<T>>,
let mut buckets: Vec<Vec<entry<T>>> = Vec::from_fn(256, |_| Vec::new());
for elt in index.into_iter() {
let h = hash::hash(&elt.val) as uint;
buckets.get_mut(h % 256).push(elt);
buckets[h % 256].push(elt);
}
rbml_w.start_tag(tag_index);

View file

@ -214,13 +214,13 @@ impl MoveData {
fn set_path_first_move(&self,
index: MovePathIndex,
first_move: MoveIndex) {
self.paths.borrow_mut().get_mut(index.get()).first_move = first_move
(*self.paths.borrow_mut())[index.get()].first_move = first_move
}
fn set_path_first_child(&self,
index: MovePathIndex,
first_child: MovePathIndex) {
self.paths.borrow_mut().get_mut(index.get()).first_child = first_child
(*self.paths.borrow_mut())[index.get()].first_child = first_child
}
fn move_next_move(&self, index: MoveIndex) -> MoveIndex {

View file

@ -161,7 +161,7 @@ fn calculate_type(sess: &session::Session,
if src.dylib.is_none() && !formats.contains_key(&cnum) {
assert!(src.rlib.is_some());
add_library(sess, cnum, cstore::RequireStatic, &mut formats);
*ret.get_mut(cnum as uint - 1) = Some(cstore::RequireStatic);
ret[cnum as uint - 1] = Some(cstore::RequireStatic);
debug!("adding staticlib: {}", data.name);
}
});

View file

@ -142,7 +142,7 @@ impl<N,E> Graph<N,E> {
}
pub fn mut_node_data<'a>(&'a mut self, idx: NodeIndex) -> &'a mut N {
&mut self.nodes.get_mut(idx.get()).data
&mut self.nodes[idx.get()].data
}
pub fn node_data<'a>(&'a self, idx: NodeIndex) -> &'a N {
@ -182,14 +182,14 @@ impl<N,E> Graph<N,E> {
});
// adjust the firsts for each node target be the next object.
self.nodes.get_mut(source.get()).first_edge[Outgoing.repr] = idx;
self.nodes.get_mut(target.get()).first_edge[Incoming.repr] = idx;
self.nodes[source.get()].first_edge[Outgoing.repr] = idx;
self.nodes[target.get()].first_edge[Incoming.repr] = idx;
return idx;
}
pub fn mut_edge_data<'a>(&'a mut self, idx: EdgeIndex) -> &'a mut E {
&mut self.edges.get_mut(idx.get()).data
&mut self.edges[idx.get()].data
}
pub fn edge_data<'a>(&'a self, idx: EdgeIndex) -> &'a E {

View file

@ -756,7 +756,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
}
fn init_empty(&mut self, ln: LiveNode, succ_ln: LiveNode) {
*self.successors.get_mut(ln.get()) = succ_ln;
self.successors[ln.get()] = succ_ln;
// It is not necessary to initialize the
// values to empty because this is the value
@ -770,10 +770,10 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
fn init_from_succ(&mut self, ln: LiveNode, succ_ln: LiveNode) {
// more efficient version of init_empty() / merge_from_succ()
*self.successors.get_mut(ln.get()) = succ_ln;
self.successors[ln.get()] = succ_ln;
self.indices2(ln, succ_ln, |this, idx, succ_idx| {
*this.users.get_mut(idx) = this.users[succ_idx]
this.users[idx] = this.users[succ_idx]
});
debug!("init_from_succ(ln={}, succ={})",
self.ln_str(ln), self.ln_str(succ_ln));
@ -789,11 +789,11 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
let mut changed = false;
self.indices2(ln, succ_ln, |this, idx, succ_idx| {
changed |= copy_if_invalid(this.users[succ_idx].reader,
&mut this.users.get_mut(idx).reader);
&mut this.users[idx].reader);
changed |= copy_if_invalid(this.users[succ_idx].writer,
&mut this.users.get_mut(idx).writer);
&mut this.users[idx].writer);
if this.users[succ_idx].used && !this.users[idx].used {
this.users.get_mut(idx).used = true;
this.users[idx].used = true;
changed = true;
}
});
@ -817,8 +817,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
// this) so we just clear out all the data.
fn define(&mut self, writer: LiveNode, var: Variable) {
let idx = self.idx(writer, var);
self.users.get_mut(idx).reader = invalid_node();
self.users.get_mut(idx).writer = invalid_node();
self.users[idx].reader = invalid_node();
self.users[idx].writer = invalid_node();
debug!("{} defines {} (idx={}): {}", writer.to_string(), var.to_string(),
idx, self.ln_str(writer));
@ -830,7 +830,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
ln.to_string(), acc, var.to_string(), self.ln_str(ln));
let idx = self.idx(ln, var);
let user = self.users.get_mut(idx);
let user = &mut self.users[idx];
if (acc & ACC_WRITE) != 0 {
user.reader = invalid_node();

View file

@ -2596,7 +2596,7 @@ impl<'a> Resolver<'a> {
// We've successfully resolved the import. Write the results in.
let mut import_resolutions = module_.import_resolutions.borrow_mut();
let import_resolution = import_resolutions.get_mut(&target);
let import_resolution = &mut (*import_resolutions)[target];
match value_result {
BoundResult(ref target_module, ref name_bindings) => {
@ -5697,7 +5697,7 @@ impl<'a> Resolver<'a> {
let mut smallest = 0;
for (i, other) in maybes.iter().enumerate() {
*values.get_mut(i) = name.lev_distance(other.get());
values[i] = name.lev_distance(other.get());
if values[i] <= values[smallest] {
smallest = i;

View file

@ -403,7 +403,7 @@ fn expand_nested_bindings<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
}
let mut pats = br.pats.clone();
*pats.get_mut(col) = pat;
pats[col] = pat;
Match {
pats: pats,
data: &*br.data,

View file

@ -469,7 +469,7 @@ impl<'blk, 'tcx> CleanupMethods<'blk, 'tcx> for FunctionContext<'blk, 'tcx> {
assert!(self.is_valid_custom_scope(custom_scope));
let mut scopes = self.scopes.borrow_mut();
let scope = scopes.get_mut(custom_scope.index);
let scope = &mut (*scopes)[custom_scope.index];
scope.cleanups.push(cleanup);
scope.clear_cached_exits();
}

View file

@ -1331,7 +1331,7 @@ fn trans_struct<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
field_ty.name == field.ident.node.name);
match opt_pos {
Some(i) => {
*need_base.get_mut(i) = false;
need_base[i] = false;
(i, &*field.expr)
}
None => {

View file

@ -284,7 +284,7 @@ impl Type {
return Vec::new();
}
let mut elts = Vec::from_elem(n_elts, 0 as TypeRef);
llvm::LLVMGetStructElementTypes(self.to_ref(), elts.get_mut(0));
llvm::LLVMGetStructElementTypes(self.to_ref(), &mut elts[0]);
mem::transmute(elts)
}
}

View file

@ -650,7 +650,7 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
ByValueExplicitSelfCategory => {
let mut n = (*m).clone();
let self_ty = n.fty.sig.inputs[0];
*n.fty.sig.inputs.get_mut(0) = ty::mk_uniq(tcx, self_ty);
n.fty.sig.inputs[0] = ty::mk_uniq(tcx, self_ty);
m = Rc::new(n);
}
_ => { }

View file

@ -5455,7 +5455,7 @@ pub fn check_bounds_are_used(ccx: &CrateCtxt,
match ty::get(t).sty {
ty::ty_param(ParamTy {idx, ..}) => {
debug!("Found use of ty param num {}", idx);
*tps_used.get_mut(idx) = true;
tps_used[idx] = true;
}
_ => ()
}

View file

@ -1757,7 +1757,7 @@ fn adjust_upvar_borrow_kind_for_mut(rcx: &Rcx,
// is inferred to mutable if necessary
let mut upvar_borrow_map =
rcx.fcx.inh.upvar_borrow_map.borrow_mut();
let ub = upvar_borrow_map.get_mut(upvar_id);
let ub = &mut (*upvar_borrow_map)[*upvar_id];
return adjust_upvar_borrow_kind(rcx, *upvar_id, ub, ty::MutBorrow);
}
@ -1807,7 +1807,7 @@ fn adjust_upvar_borrow_kind_for_unique(rcx: &Rcx, cmt: mc::cmt) {
// borrow_kind of the upvar to make sure it
// is inferred to unique if necessary
let mut ub = rcx.fcx.inh.upvar_borrow_map.borrow_mut();
let ub = ub.get_mut(upvar_id);
let ub = &mut (*ub)[*upvar_id];
return adjust_upvar_borrow_kind(rcx, *upvar_id, ub, ty::UniqueImmBorrow);
}

View file

@ -261,7 +261,7 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
if snapshot.length == 0 {
undo_log.truncate(0);
} else {
*undo_log.get_mut(snapshot.length) = CommitedSnapshot;
(*undo_log)[snapshot.length] = CommitedSnapshot;
}
}

View file

@ -159,12 +159,12 @@ impl sv::SnapshotVecDelegate<TypeVariableData,UndoEntry> for Delegate {
action: UndoEntry) {
match action {
SpecifyVar(vid, relations) => {
values.get_mut(vid.index).value = Bounded(relations);
values[vid.index].value = Bounded(relations);
}
Relate(a, b) => {
relations(values.get_mut(a.index)).pop();
relations(values.get_mut(b.index)).pop();
relations(&mut (*values)[a.index]).pop();
relations(&mut (*values)[b.index]).pop();
}
}
}

View file

@ -994,7 +994,7 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
new_value,
term.to_string());
*self.solutions.get_mut(inferred) = new_value;
self.solutions[inferred] = new_value;
changed = true;
}
}

View file

@ -105,7 +105,7 @@ impl<T,U,D:SnapshotVecDelegate<T,U>> SnapshotVec<T,U,D> {
* action.
*/
self.values.get_mut(index)
&mut self.values[index]
}
pub fn set(&mut self, index: uint, new_elem: T) {
@ -114,7 +114,7 @@ impl<T,U,D:SnapshotVecDelegate<T,U>> SnapshotVec<T,U,D> {
* saved (and perhaps restored) if a snapshot is active.
*/
let old_elem = mem::replace(self.values.get_mut(index), new_elem);
let old_elem = mem::replace(&mut self.values[index], new_elem);
if self.in_snapshot() {
self.undo_log.push(SetElem(index, old_elem));
}
@ -162,7 +162,7 @@ impl<T,U,D:SnapshotVecDelegate<T,U>> SnapshotVec<T,U,D> {
}
SetElem(i, v) => {
*self.values.get_mut(i) = v;
self.values[i] = v;
}
Other(u) => {
@ -189,7 +189,7 @@ impl<T,U,D:SnapshotVecDelegate<T,U>> SnapshotVec<T,U,D> {
// The root snapshot.
self.undo_log.truncate(0);
} else {
*self.undo_log.get_mut(snapshot.length) = CommittedSnapshot;
self.undo_log[snapshot.length] = CommittedSnapshot;
}
}
}