Skip to content

Commit b4d4daf

Browse files
committed
Adjust Index/IndexMut impls. For generic collections, we take
references. For collections whose keys are integers, we take both references and by-value.
1 parent bc1dde4 commit b4d4daf

File tree

12 files changed

+600
-8
lines changed

12 files changed

+600
-8
lines changed

src/libcollections/btree/map.rs

+15
Original file line numberDiff line numberDiff line change
@@ -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/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")]

src/libcollections/vec_deque.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1689,18 +1689,32 @@ impl<A: Hash> Hash for VecDeque<A> {
16891689
impl<A> Index<usize> for VecDeque<A> {
16901690
type Output = A;
16911691

1692+
#[cfg(stage0)]
16921693
#[inline]
16931694
fn index(&self, i: &usize) -> &A {
16941695
self.get(*i).expect("Out of bounds access")
16951696
}
1697+
1698+
#[cfg(not(stage0))]
1699+
#[inline]
1700+
fn index(&self, i: usize) -> &A {
1701+
self.get(i).expect("Out of bounds access")
1702+
}
16961703
}
16971704

16981705
#[stable(feature = "rust1", since = "1.0.0")]
16991706
impl<A> IndexMut<usize> for VecDeque<A> {
1707+
#[cfg(stage0)]
17001708
#[inline]
17011709
fn index_mut(&mut self, i: &usize) -> &mut A {
17021710
self.get_mut(*i).expect("Out of bounds access")
17031711
}
1712+
1713+
#[cfg(not(stage0))]
1714+
#[inline]
1715+
fn index_mut(&mut self, i: usize) -> &mut A {
1716+
self.get_mut(i).expect("Out of bounds access")
1717+
}
17041718
}
17051719

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

src/libcollections/vec_map.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@ impl<V> Extend<(usize, V)> for VecMap<V> {
798798
}
799799
}
800800

801+
#[cfg(stage0)]
801802
impl<V> Index<usize> for VecMap<V> {
802803
type Output = V;
803804

@@ -807,10 +808,49 @@ impl<V> Index<usize> for VecMap<V> {
807808
}
808809
}
809810

811+
#[cfg(not(stage0))]
812+
impl<V> Index<usize> for VecMap<V> {
813+
type Output = V;
814+
815+
#[inline]
816+
fn index<'a>(&'a self, i: usize) -> &'a V {
817+
self.get(&i).expect("key not present")
818+
}
819+
}
820+
821+
#[cfg(not(stage0))]
822+
impl<'a,V> Index<&'a usize> for VecMap<V> {
823+
type Output = V;
824+
825+
#[inline]
826+
fn index(&self, i: &usize) -> &V {
827+
self.get(i).expect("key not present")
828+
}
829+
}
830+
831+
#[cfg(stage0)]
832+
#[stable(feature = "rust1", since = "1.0.0")]
833+
impl<V> IndexMut<usize> for VecMap<V> {
834+
#[inline]
835+
fn index_mut(&mut self, i: &usize) -> &mut V {
836+
self.get_mut(&i).expect("key not present")
837+
}
838+
}
839+
840+
#[cfg(not(stage0))]
810841
#[stable(feature = "rust1", since = "1.0.0")]
811842
impl<V> IndexMut<usize> for VecMap<V> {
812843
#[inline]
813-
fn index_mut<'a>(&'a mut self, i: &usize) -> &'a mut V {
844+
fn index_mut(&mut self, i: usize) -> &mut V {
845+
self.get_mut(&i).expect("key not present")
846+
}
847+
}
848+
849+
#[cfg(not(stage0))]
850+
#[stable(feature = "rust1", since = "1.0.0")]
851+
impl<'a, V> IndexMut<&'a usize> for VecMap<V> {
852+
#[inline]
853+
fn index_mut(&mut self, i: &usize) -> &mut V {
814854
self.get_mut(i).expect("key not present")
815855
}
816856
}

src/libcore/ops.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
898898
/// impl Index<Bar> for Foo {
899899
/// type Output = Foo;
900900
///
901-
/// fn index<'a>(&'a self, _index: &Bar) -> &'a Foo {
901+
/// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
902902
/// println!("Indexing!");
903903
/// self
904904
/// }
@@ -945,13 +945,13 @@ pub trait Index<Idx: ?Sized> {
945945
/// impl Index<Bar> for Foo {
946946
/// type Output = Foo;
947947
///
948-
/// fn index<'a>(&'a self, _index: &Bar) -> &'a Foo {
948+
/// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
949949
/// self
950950
/// }
951951
/// }
952952
///
953953
/// impl IndexMut<Bar> for Foo {
954-
/// fn index_mut<'a>(&'a mut self, _index: &Bar) -> &'a mut Foo {
954+
/// fn index_mut<'a>(&'a mut self, _index: Bar) -> &'a mut Foo {
955955
/// println!("Indexing!");
956956
/// self
957957
/// }

0 commit comments

Comments
 (0)