Skip to content

Commit de8a23b

Browse files
committed
Auto merge of #23601 - nikomatsakis:by-value-index, r=japaric
This is a [breaking-change]. When indexing a generic map (hashmap, etc) using the `[]` operator, it is now necessary to borrow explicitly, so change `map[key]` to `map[&key]` (consistent with the `get` routine). However, indexing of string-valued maps with constant strings can now be written `map["abc"]`. r? @japaric cc @aturon @gankro
2 parents 809a554 + 57cf2de commit de8a23b

Some content is hidden

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

72 files changed

+953
-177
lines changed

src/libcollections/bit.rs

+12
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ pub struct BitVec {
169169
impl Index<usize> for BitVec {
170170
type Output = bool;
171171

172+
173+
#[cfg(stage0)]
172174
#[inline]
173175
fn index(&self, i: &usize) -> &bool {
174176
if self.get(*i).expect("index out of bounds") {
@@ -177,6 +179,16 @@ impl Index<usize> for BitVec {
177179
&FALSE
178180
}
179181
}
182+
183+
#[cfg(not(stage0))]
184+
#[inline]
185+
fn index(&self, i: usize) -> &bool {
186+
if self.get(i).expect("index out of bounds") {
187+
&TRUE
188+
} else {
189+
&FALSE
190+
}
191+
}
180192
}
181193

182194
/// Computes how many blocks are needed to store that many bits

src/libcollections/btree/map.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
264264
/// Some(x) => *x = "b",
265265
/// None => (),
266266
/// }
267-
/// assert_eq!(map[1], "b");
267+
/// assert_eq!(map[&1], "b");
268268
/// ```
269269
// See `get` for implementation notes, this is basically a copy-paste with mut's added
270270
#[stable(feature = "rust1", since = "1.0.0")]
@@ -326,7 +326,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
326326
///
327327
/// map.insert(37, "b");
328328
/// assert_eq!(map.insert(37, "c"), Some("b"));
329-
/// assert_eq!(map[37], "c");
329+
/// assert_eq!(map[&37], "c");
330330
/// ```
331331
#[stable(feature = "rust1", since = "1.0.0")]
332332
pub fn insert(&mut self, mut key: K, mut value: V) -> Option<V> {
@@ -914,12 +914,27 @@ impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
914914
}
915915
}
916916

917+
#[cfg(stage0)]
917918
#[stable(feature = "rust1", since = "1.0.0")]
918919
impl<K: Ord, Q: ?Sized, V> Index<Q> for BTreeMap<K, V>
919920
where K: Borrow<Q>, Q: Ord
920921
{
921922
type Output = V;
922923

924+
#[inline]
925+
fn index(&self, key: &Q) -> &V {
926+
self.get(key).expect("no entry found for key")
927+
}
928+
}
929+
930+
#[cfg(not(stage0))]
931+
#[stable(feature = "rust1", since = "1.0.0")]
932+
impl<'a, K: Ord, Q: ?Sized, V> Index<&'a Q> for BTreeMap<K, V>
933+
where K: Borrow<Q>, Q: Ord
934+
{
935+
type Output = V;
936+
937+
#[inline]
923938
fn index(&self, key: &Q) -> &V {
924939
self.get(key).expect("no entry found for key")
925940
}

src/libcollections/btree/node.rs

+61
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,7 @@ macro_rules! node_slice_impl {
15221522
}
15231523

15241524
/// Returns a sub-slice with elements starting with `min_key`.
1525+
#[cfg(stage0)]
15251526
pub fn slice_from(self, min_key: &K) -> $NodeSlice<'a, K, V> {
15261527
// _______________
15271528
// |_1_|_3_|_5_|_7_|
@@ -1549,7 +1550,37 @@ macro_rules! node_slice_impl {
15491550
}
15501551
}
15511552

1553+
/// Returns a sub-slice with elements starting with `min_key`.
1554+
#[cfg(not(stage0))]
1555+
pub fn slice_from(self, min_key: &K) -> $NodeSlice<'a, K, V> {
1556+
// _______________
1557+
// |_1_|_3_|_5_|_7_|
1558+
// | | | | |
1559+
// 0 0 1 1 2 2 3 3 4 index
1560+
// | | | | |
1561+
// \___|___|___|___/ slice_from(&0); pos = 0
1562+
// \___|___|___/ slice_from(&2); pos = 1
1563+
// |___|___|___/ slice_from(&3); pos = 1; result.head_is_edge = false
1564+
// \___|___/ slice_from(&4); pos = 2
1565+
// \___/ slice_from(&6); pos = 3
1566+
// \|/ slice_from(&999); pos = 4
1567+
let (pos, pos_is_kv) = self.search_linear(min_key);
1568+
$NodeSlice {
1569+
has_edges: self.has_edges,
1570+
edges: if !self.has_edges {
1571+
self.edges
1572+
} else {
1573+
self.edges.$index(pos ..)
1574+
},
1575+
keys: &self.keys[pos ..],
1576+
vals: self.vals.$index(pos ..),
1577+
head_is_edge: !pos_is_kv,
1578+
tail_is_edge: self.tail_is_edge,
1579+
}
1580+
}
1581+
15521582
/// Returns a sub-slice with elements up to and including `max_key`.
1583+
#[cfg(stage0)]
15531584
pub fn slice_to(self, max_key: &K) -> $NodeSlice<'a, K, V> {
15541585
// _______________
15551586
// |_1_|_3_|_5_|_7_|
@@ -1577,6 +1608,36 @@ macro_rules! node_slice_impl {
15771608
tail_is_edge: !pos_is_kv,
15781609
}
15791610
}
1611+
1612+
/// Returns a sub-slice with elements up to and including `max_key`.
1613+
#[cfg(not(stage0))]
1614+
pub fn slice_to(self, max_key: &K) -> $NodeSlice<'a, K, V> {
1615+
// _______________
1616+
// |_1_|_3_|_5_|_7_|
1617+
// | | | | |
1618+
// 0 0 1 1 2 2 3 3 4 index
1619+
// | | | | |
1620+
//\|/ | | | | slice_to(&0); pos = 0
1621+
// \___/ | | | slice_to(&2); pos = 1
1622+
// \___|___| | | slice_to(&3); pos = 1; result.tail_is_edge = false
1623+
// \___|___/ | | slice_to(&4); pos = 2
1624+
// \___|___|___/ | slice_to(&6); pos = 3
1625+
// \___|___|___|___/ slice_to(&999); pos = 4
1626+
let (pos, pos_is_kv) = self.search_linear(max_key);
1627+
let pos = pos + if pos_is_kv { 1 } else { 0 };
1628+
$NodeSlice {
1629+
has_edges: self.has_edges,
1630+
edges: if !self.has_edges {
1631+
self.edges
1632+
} else {
1633+
self.edges.$index(.. (pos + 1))
1634+
},
1635+
keys: &self.keys[..pos],
1636+
vals: self.vals.$index(.. pos),
1637+
head_is_edge: self.head_is_edge,
1638+
tail_is_edge: !pos_is_kv,
1639+
}
1640+
}
15801641
}
15811642

15821643
impl<'a, K: 'a, V: 'a> $NodeSlice<'a, K, V> {

src/libcollections/string.rs

+32
Original file line numberDiff line numberDiff line change
@@ -870,34 +870,66 @@ impl<'a> Add<&'a str> for String {
870870
#[stable(feature = "rust1", since = "1.0.0")]
871871
impl ops::Index<ops::Range<usize>> for String {
872872
type Output = str;
873+
874+
#[cfg(stage0)]
873875
#[inline]
874876
fn index(&self, index: &ops::Range<usize>) -> &str {
875877
&self[..][*index]
876878
}
879+
880+
#[cfg(not(stage0))]
881+
#[inline]
882+
fn index(&self, index: ops::Range<usize>) -> &str {
883+
&self[..][index]
884+
}
877885
}
878886
#[stable(feature = "rust1", since = "1.0.0")]
879887
impl ops::Index<ops::RangeTo<usize>> for String {
880888
type Output = str;
889+
890+
#[cfg(stage0)]
881891
#[inline]
882892
fn index(&self, index: &ops::RangeTo<usize>) -> &str {
883893
&self[..][*index]
884894
}
895+
896+
#[cfg(not(stage0))]
897+
#[inline]
898+
fn index(&self, index: ops::RangeTo<usize>) -> &str {
899+
&self[..][index]
900+
}
885901
}
886902
#[stable(feature = "rust1", since = "1.0.0")]
887903
impl ops::Index<ops::RangeFrom<usize>> for String {
888904
type Output = str;
905+
906+
#[cfg(stage0)]
889907
#[inline]
890908
fn index(&self, index: &ops::RangeFrom<usize>) -> &str {
891909
&self[..][*index]
892910
}
911+
912+
#[cfg(not(stage0))]
913+
#[inline]
914+
fn index(&self, index: ops::RangeFrom<usize>) -> &str {
915+
&self[..][index]
916+
}
893917
}
894918
#[stable(feature = "rust1", since = "1.0.0")]
895919
impl ops::Index<ops::RangeFull> for String {
896920
type Output = str;
921+
922+
#[cfg(stage0)]
897923
#[inline]
898924
fn index(&self, _index: &ops::RangeFull) -> &str {
899925
unsafe { mem::transmute(&*self.vec) }
900926
}
927+
928+
#[cfg(not(stage0))]
929+
#[inline]
930+
fn index(&self, _index: ops::RangeFull) -> &str {
931+
unsafe { mem::transmute(&*self.vec) }
932+
}
901933
}
902934

903935
#[stable(feature = "rust1", since = "1.0.0")]

src/libcollections/vec.rs

+82
Original file line numberDiff line numberDiff line change
@@ -1323,83 +1323,165 @@ impl<T: Hash> Hash for Vec<T> {
13231323
impl<T> Index<usize> for Vec<T> {
13241324
type Output = T;
13251325

1326+
1327+
#[cfg(stage0)]
13261328
#[inline]
13271329
fn index(&self, index: &usize) -> &T {
13281330
// NB built-in indexing via `&[T]`
13291331
&(**self)[*index]
13301332
}
1333+
1334+
#[cfg(not(stage0))]
1335+
#[inline]
1336+
fn index(&self, index: usize) -> &T {
1337+
// NB built-in indexing via `&[T]`
1338+
&(**self)[index]
1339+
}
13311340
}
13321341

13331342
#[stable(feature = "rust1", since = "1.0.0")]
13341343
impl<T> IndexMut<usize> for Vec<T> {
1344+
1345+
#[cfg(stage0)]
13351346
#[inline]
13361347
fn index_mut(&mut self, index: &usize) -> &mut T {
13371348
// NB built-in indexing via `&mut [T]`
13381349
&mut (**self)[*index]
13391350
}
1351+
1352+
#[cfg(not(stage0))]
1353+
#[inline]
1354+
fn index_mut(&mut self, index: usize) -> &mut T {
1355+
// NB built-in indexing via `&mut [T]`
1356+
&mut (**self)[index]
1357+
}
13401358
}
13411359

13421360

13431361
#[stable(feature = "rust1", since = "1.0.0")]
13441362
impl<T> ops::Index<ops::Range<usize>> for Vec<T> {
13451363
type Output = [T];
1364+
1365+
#[cfg(stage0)]
13461366
#[inline]
13471367
fn index(&self, index: &ops::Range<usize>) -> &[T] {
13481368
Index::index(&**self, index)
13491369
}
1370+
1371+
#[cfg(not(stage0))]
1372+
#[inline]
1373+
fn index(&self, index: ops::Range<usize>) -> &[T] {
1374+
Index::index(&**self, index)
1375+
}
13501376
}
13511377
#[stable(feature = "rust1", since = "1.0.0")]
13521378
impl<T> ops::Index<ops::RangeTo<usize>> for Vec<T> {
13531379
type Output = [T];
1380+
1381+
#[cfg(stage0)]
13541382
#[inline]
13551383
fn index(&self, index: &ops::RangeTo<usize>) -> &[T] {
13561384
Index::index(&**self, index)
13571385
}
1386+
1387+
#[cfg(not(stage0))]
1388+
#[inline]
1389+
fn index(&self, index: ops::RangeTo<usize>) -> &[T] {
1390+
Index::index(&**self, index)
1391+
}
13581392
}
13591393
#[stable(feature = "rust1", since = "1.0.0")]
13601394
impl<T> ops::Index<ops::RangeFrom<usize>> for Vec<T> {
13611395
type Output = [T];
1396+
1397+
#[cfg(stage0)]
13621398
#[inline]
13631399
fn index(&self, index: &ops::RangeFrom<usize>) -> &[T] {
13641400
Index::index(&**self, index)
13651401
}
1402+
1403+
#[cfg(not(stage0))]
1404+
#[inline]
1405+
fn index(&self, index: ops::RangeFrom<usize>) -> &[T] {
1406+
Index::index(&**self, index)
1407+
}
13661408
}
13671409
#[stable(feature = "rust1", since = "1.0.0")]
13681410
impl<T> ops::Index<ops::RangeFull> for Vec<T> {
13691411
type Output = [T];
1412+
1413+
#[cfg(stage0)]
13701414
#[inline]
13711415
fn index(&self, _index: &ops::RangeFull) -> &[T] {
13721416
self.as_slice()
13731417
}
1418+
1419+
#[cfg(not(stage0))]
1420+
#[inline]
1421+
fn index(&self, _index: ops::RangeFull) -> &[T] {
1422+
self.as_slice()
1423+
}
13741424
}
13751425

13761426
#[stable(feature = "rust1", since = "1.0.0")]
13771427
impl<T> ops::IndexMut<ops::Range<usize>> for Vec<T> {
1428+
1429+
#[cfg(stage0)]
13781430
#[inline]
13791431
fn index_mut(&mut self, index: &ops::Range<usize>) -> &mut [T] {
13801432
IndexMut::index_mut(&mut **self, index)
13811433
}
1434+
1435+
#[cfg(not(stage0))]
1436+
#[inline]
1437+
fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [T] {
1438+
IndexMut::index_mut(&mut **self, index)
1439+
}
13821440
}
13831441
#[stable(feature = "rust1", since = "1.0.0")]
13841442
impl<T> ops::IndexMut<ops::RangeTo<usize>> for Vec<T> {
1443+
1444+
#[cfg(stage0)]
13851445
#[inline]
13861446
fn index_mut(&mut self, index: &ops::RangeTo<usize>) -> &mut [T] {
13871447
IndexMut::index_mut(&mut **self, index)
13881448
}
1449+
1450+
#[cfg(not(stage0))]
1451+
#[inline]
1452+
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut [T] {
1453+
IndexMut::index_mut(&mut **self, index)
1454+
}
13891455
}
13901456
#[stable(feature = "rust1", since = "1.0.0")]
13911457
impl<T> ops::IndexMut<ops::RangeFrom<usize>> for Vec<T> {
1458+
1459+
#[cfg(stage0)]
13921460
#[inline]
13931461
fn index_mut(&mut self, index: &ops::RangeFrom<usize>) -> &mut [T] {
13941462
IndexMut::index_mut(&mut **self, index)
13951463
}
1464+
1465+
#[cfg(not(stage0))]
1466+
#[inline]
1467+
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut [T] {
1468+
IndexMut::index_mut(&mut **self, index)
1469+
}
13961470
}
13971471
#[stable(feature = "rust1", since = "1.0.0")]
13981472
impl<T> ops::IndexMut<ops::RangeFull> for Vec<T> {
1473+
1474+
#[cfg(stage0)]
13991475
#[inline]
14001476
fn index_mut(&mut self, _index: &ops::RangeFull) -> &mut [T] {
14011477
self.as_mut_slice()
14021478
}
1479+
1480+
#[cfg(not(stage0))]
1481+
#[inline]
1482+
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut [T] {
1483+
self.as_mut_slice()
1484+
}
14031485
}
14041486

14051487
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)