Skip to content

Commit 753efb5

Browse files
committed
rollup merge of rust-lang#23601: nikomatsakis/by-value-index
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 388e5ae + 57cf2de commit 753efb5

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
@@ -891,34 +891,66 @@ impl<'a> Add<&'a str> for String {
891891
#[stable(feature = "rust1", since = "1.0.0")]
892892
impl ops::Index<ops::Range<usize>> for String {
893893
type Output = str;
894+
895+
#[cfg(stage0)]
894896
#[inline]
895897
fn index(&self, index: &ops::Range<usize>) -> &str {
896898
&self[..][*index]
897899
}
900+
901+
#[cfg(not(stage0))]
902+
#[inline]
903+
fn index(&self, index: ops::Range<usize>) -> &str {
904+
&self[..][index]
905+
}
898906
}
899907
#[stable(feature = "rust1", since = "1.0.0")]
900908
impl ops::Index<ops::RangeTo<usize>> for String {
901909
type Output = str;
910+
911+
#[cfg(stage0)]
902912
#[inline]
903913
fn index(&self, index: &ops::RangeTo<usize>) -> &str {
904914
&self[..][*index]
905915
}
916+
917+
#[cfg(not(stage0))]
918+
#[inline]
919+
fn index(&self, index: ops::RangeTo<usize>) -> &str {
920+
&self[..][index]
921+
}
906922
}
907923
#[stable(feature = "rust1", since = "1.0.0")]
908924
impl ops::Index<ops::RangeFrom<usize>> for String {
909925
type Output = str;
926+
927+
#[cfg(stage0)]
910928
#[inline]
911929
fn index(&self, index: &ops::RangeFrom<usize>) -> &str {
912930
&self[..][*index]
913931
}
932+
933+
#[cfg(not(stage0))]
934+
#[inline]
935+
fn index(&self, index: ops::RangeFrom<usize>) -> &str {
936+
&self[..][index]
937+
}
914938
}
915939
#[stable(feature = "rust1", since = "1.0.0")]
916940
impl ops::Index<ops::RangeFull> for String {
917941
type Output = str;
942+
943+
#[cfg(stage0)]
918944
#[inline]
919945
fn index(&self, _index: &ops::RangeFull) -> &str {
920946
unsafe { mem::transmute(&*self.vec) }
921947
}
948+
949+
#[cfg(not(stage0))]
950+
#[inline]
951+
fn index(&self, _index: ops::RangeFull) -> &str {
952+
unsafe { mem::transmute(&*self.vec) }
953+
}
922954
}
923955

924956
#[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
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)