Skip to content

Commit 5fe0bb7

Browse files
committed
Future-proof indexing on maps: remove IndexMut
This commit removes the `IndexMut` impls on `HashMap` and `BTreeMap`, in order to future-proof the API against the eventual inclusion of an `IndexSet` trait. Ideally, we would eventually be able to support: ```rust map[owned_key] = val; map[borrowed_key].mutating_method(arguments); &mut map[borrowed_key]; ``` but to keep the design space as unconstrained as possible, we do not currently want to support `IndexMut`, in case some other strategy will eventually be needed. Code currently using mutating index notation can use `get_mut` instead. [breaking-change] Closes #23448
1 parent cbc660b commit 5fe0bb7

File tree

5 files changed

+5
-26
lines changed

5 files changed

+5
-26
lines changed

src/libcollections/btree/map.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use core::default::Default;
2424
use core::fmt::Debug;
2525
use core::hash::{Hash, Hasher};
2626
use core::iter::{Map, FromIterator, IntoIterator};
27-
use core::ops::{Index, IndexMut};
27+
use core::ops::{Index};
2828
use core::{iter, fmt, mem, usize};
2929
use Bound::{self, Included, Excluded, Unbounded};
3030

@@ -925,15 +925,6 @@ impl<K: Ord, Q: ?Sized, V> Index<Q> for BTreeMap<K, V>
925925
}
926926
}
927927

928-
#[stable(feature = "rust1", since = "1.0.0")]
929-
impl<K: Ord, Q: ?Sized, V> IndexMut<Q> for BTreeMap<K, V>
930-
where K: Borrow<Q>, Q: Ord
931-
{
932-
fn index_mut(&mut self, key: &Q) -> &mut V {
933-
self.get_mut(key).expect("no entry found for key")
934-
}
935-
}
936-
937928
/// Genericises over how to get the correct type of iterator from the correct type
938929
/// of Node ownership.
939930
trait Traverse<N> {

src/librustc_resolve/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
900900
return;
901901
}
902902
if self.glob_map.contains_key(&import_id) {
903-
self.glob_map[import_id].insert(name);
903+
self.glob_map.get_mut(&import_id).unwrap().insert(name);
904904
return;
905905
}
906906

src/librustc_resolve/resolve_imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
603603

604604
// We've successfully resolved the import. Write the results in.
605605
let mut import_resolutions = module_.import_resolutions.borrow_mut();
606-
let import_resolution = &mut (*import_resolutions)[target];
606+
let import_resolution = import_resolutions.get_mut(&target).unwrap();
607607

608608
{
609609
let mut check_and_write_import = |namespace, result: &_, used_public: &mut bool| {

src/librustc_typeck/check/upvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl<'a,'tcx> AdjustBorrowKind<'a,'tcx> {
380380
// borrow_kind of the upvar to make sure it
381381
// is inferred to mutable if necessary
382382
let mut upvar_capture_map = self.fcx.inh.upvar_capture_map.borrow_mut();
383-
let ub = &mut upvar_capture_map[upvar_id];
383+
let ub = upvar_capture_map.get_mut(&upvar_id).unwrap();
384384
self.adjust_upvar_borrow_kind(upvar_id, ub, borrow_kind);
385385

386386
// also need to be in an FnMut closure since this is not an ImmBorrow

src/libstd/collections/hash/map.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use hash::{Hash, SipHasher};
2323
use iter::{self, Iterator, ExactSizeIterator, IntoIterator, IteratorExt, FromIterator, Extend, Map};
2424
use marker::Sized;
2525
use mem::{self, replace};
26-
use ops::{Deref, FnMut, Index, IndexMut};
26+
use ops::{Deref, FnMut, Index};
2727
use option::Option::{self, Some, None};
2828
use rand::{self, Rng};
2929
use result::Result::{self, Ok, Err};
@@ -1258,18 +1258,6 @@ impl<K, Q: ?Sized, V, S> Index<Q> for HashMap<K, V, S>
12581258
}
12591259
}
12601260

1261-
#[stable(feature = "rust1", since = "1.0.0")]
1262-
impl<K, V, S, Q: ?Sized> IndexMut<Q> for HashMap<K, V, S>
1263-
where K: Eq + Hash + Borrow<Q>,
1264-
Q: Eq + Hash,
1265-
S: HashState,
1266-
{
1267-
#[inline]
1268-
fn index_mut<'a>(&'a mut self, index: &Q) -> &'a mut V {
1269-
self.get_mut(index).expect("no entry found for key")
1270-
}
1271-
}
1272-
12731261
/// HashMap iterator.
12741262
#[stable(feature = "rust1", since = "1.0.0")]
12751263
pub struct Iter<'a, K: 'a, V: 'a> {

0 commit comments

Comments
 (0)