}
}
+#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<K: Ord, Q: ?Sized, V> Index<Q> for BTreeMap<K, V>
where K: Borrow<Q>, Q: Ord
{
type Output = V;
+ #[inline]
+ fn index(&self, key: &Q) -> &V {
+ self.get(key).expect("no entry found for key")
+ }
+}
+
+#[cfg(not(stage0))]
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, K: Ord, Q: ?Sized, V> Index<&'a Q> for BTreeMap<K, V>
+ where K: Borrow<Q>, Q: Ord
+{
+ type Output = V;
+
+ #[inline]
fn index(&self, key: &Q) -> &V {
self.get(key).expect("no entry found for key")
}
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::Range<usize>> for String {
type Output = str;
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::Range<usize>) -> &str {
&self[..][*index]
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::Range<usize>) -> &str {
+ &self[..][index]
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeTo<usize>> for String {
type Output = str;
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeTo<usize>) -> &str {
&self[..][*index]
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeTo<usize>) -> &str {
+ &self[..][index]
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeFrom<usize>> for String {
type Output = str;
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeFrom<usize>) -> &str {
&self[..][*index]
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeFrom<usize>) -> &str {
+ &self[..][index]
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeFull> for String {
type Output = str;
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, _index: &ops::RangeFull) -> &str {
unsafe { mem::transmute(&*self.vec) }
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, _index: ops::RangeFull) -> &str {
+ unsafe { mem::transmute(&*self.vec) }
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Index<usize> for Vec<T> {
type Output = T;
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &usize) -> &T {
// NB built-in indexing via `&[T]`
&(**self)[*index]
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: usize) -> &T {
+ // NB built-in indexing via `&[T]`
+ &(**self)[index]
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> IndexMut<usize> for Vec<T> {
+
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, index: &usize) -> &mut T {
// NB built-in indexing via `&mut [T]`
&mut (**self)[*index]
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, index: usize) -> &mut T {
+ // NB built-in indexing via `&mut [T]`
+ &mut (**self)[index]
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::Range<usize>> for Vec<T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::Range<usize>) -> &[T] {
Index::index(&**self, index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::Range<usize>) -> &[T] {
+ Index::index(&**self, index)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeTo<usize>> for Vec<T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeTo<usize>) -> &[T] {
Index::index(&**self, index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeTo<usize>) -> &[T] {
+ Index::index(&**self, index)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeFrom<usize>> for Vec<T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeFrom<usize>) -> &[T] {
Index::index(&**self, index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeFrom<usize>) -> &[T] {
+ Index::index(&**self, index)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeFull> for Vec<T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, _index: &ops::RangeFull) -> &[T] {
self.as_slice()
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, _index: ops::RangeFull) -> &[T] {
+ self.as_slice()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::Range<usize>> for Vec<T> {
+
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, index: &ops::Range<usize>) -> &mut [T] {
IndexMut::index_mut(&mut **self, index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [T] {
+ IndexMut::index_mut(&mut **self, index)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeTo<usize>> for Vec<T> {
+
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, index: &ops::RangeTo<usize>) -> &mut [T] {
IndexMut::index_mut(&mut **self, index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut [T] {
+ IndexMut::index_mut(&mut **self, index)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeFrom<usize>> for Vec<T> {
+
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, index: &ops::RangeFrom<usize>) -> &mut [T] {
IndexMut::index_mut(&mut **self, index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut [T] {
+ IndexMut::index_mut(&mut **self, index)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeFull> for Vec<T> {
+
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, _index: &ops::RangeFull) -> &mut [T] {
self.as_mut_slice()
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, _index: ops::RangeFull) -> &mut [T] {
+ self.as_mut_slice()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A> Index<usize> for VecDeque<A> {
type Output = A;
+ #[cfg(stage0)]
#[inline]
fn index(&self, i: &usize) -> &A {
self.get(*i).expect("Out of bounds access")
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, i: usize) -> &A {
+ self.get(i).expect("Out of bounds access")
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A> IndexMut<usize> for VecDeque<A> {
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, i: &usize) -> &mut A {
self.get_mut(*i).expect("Out of bounds access")
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, i: usize) -> &mut A {
+ self.get_mut(i).expect("Out of bounds access")
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
}
}
+#[cfg(stage0)]
impl<V> Index<usize> for VecMap<V> {
type Output = V;
}
}
+#[cfg(not(stage0))]
+impl<V> Index<usize> for VecMap<V> {
+ type Output = V;
+
+ #[inline]
+ fn index<'a>(&'a self, i: usize) -> &'a V {
+ self.get(&i).expect("key not present")
+ }
+}
+
+#[cfg(not(stage0))]
+impl<'a,V> Index<&'a usize> for VecMap<V> {
+ type Output = V;
+
+ #[inline]
+ fn index(&self, i: &usize) -> &V {
+ self.get(i).expect("key not present")
+ }
+}
+
+#[cfg(stage0)]
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<V> IndexMut<usize> for VecMap<V> {
+ #[inline]
+ fn index_mut(&mut self, i: &usize) -> &mut V {
+ self.get_mut(&i).expect("key not present")
+ }
+}
+
+#[cfg(not(stage0))]
#[stable(feature = "rust1", since = "1.0.0")]
impl<V> IndexMut<usize> for VecMap<V> {
#[inline]
- fn index_mut<'a>(&'a mut self, i: &usize) -> &'a mut V {
+ fn index_mut(&mut self, i: usize) -> &mut V {
+ self.get_mut(&i).expect("key not present")
+ }
+}
+
+#[cfg(not(stage0))]
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, V> IndexMut<&'a usize> for VecMap<V> {
+ #[inline]
+ fn index_mut(&mut self, i: &usize) -> &mut V {
self.get_mut(i).expect("key not present")
}
}
/// impl Index<Bar> for Foo {
/// type Output = Foo;
///
-/// fn index<'a>(&'a self, _index: &Bar) -> &'a Foo {
+/// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
/// println!("Indexing!");
/// self
/// }
/// impl Index<Bar> for Foo {
/// type Output = Foo;
///
-/// fn index<'a>(&'a self, _index: &Bar) -> &'a Foo {
+/// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
/// self
/// }
/// }
///
/// impl IndexMut<Bar> for Foo {
-/// fn index_mut<'a>(&'a mut self, _index: &Bar) -> &'a mut Foo {
+/// fn index_mut<'a>(&'a mut self, _index: Bar) -> &'a mut Foo {
/// println!("Indexing!");
/// self
/// }
#[inline]
fn as_mut_slice(&mut self) -> &mut [T] { self }
+ #[cfg(stage0)]
#[inline]
fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
unsafe {
}
}
+ #[cfg(not(stage0))]
+ #[inline]
+ fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
+ unsafe {
+ let self2: &mut [T] = mem::transmute_copy(&self);
+
+ (ops::IndexMut::index_mut(self, ops::RangeTo { end: mid } ),
+ ops::IndexMut::index_mut(self2, ops::RangeFrom { start: mid } ))
+ }
+ }
+
#[inline]
fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> {
unsafe {
impl<T> ops::Index<usize> for [T] {
type Output = T;
+ #[cfg(stage0)]
fn index(&self, &index: &usize) -> &T {
assert!(index < self.len());
unsafe { mem::transmute(self.repr().data.offset(index as isize)) }
}
+
+ #[cfg(not(stage0))]
+ fn index(&self, index: usize) -> &T {
+ assert!(index < self.len());
+
+ unsafe { mem::transmute(self.repr().data.offset(index as isize)) }
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<usize> for [T] {
+ #[cfg(stage0)]
+ #[inline]
fn index_mut(&mut self, &index: &usize) -> &mut T {
assert!(index < self.len());
unsafe { mem::transmute(self.repr().data.offset(index as isize)) }
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, index: usize) -> &mut T {
+ assert!(index < self.len());
+
+ unsafe { mem::transmute(self.repr().data.offset(index as isize)) }
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::Range<usize>> for [T] {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::Range<usize>) -> &[T] {
assert!(index.start <= index.end);
)
}
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::Range<usize>) -> &[T] {
+ assert!(index.start <= index.end);
+ assert!(index.end <= self.len());
+ unsafe {
+ from_raw_parts (
+ self.as_ptr().offset(index.start as isize),
+ index.end - index.start
+ )
+ }
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeTo<usize>> for [T] {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeTo<usize>) -> &[T] {
self.index(&ops::Range{ start: 0, end: index.end })
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeTo<usize>) -> &[T] {
+ self.index(ops::Range{ start: 0, end: index.end })
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeFrom<usize>> for [T] {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeFrom<usize>) -> &[T] {
self.index(&ops::Range{ start: index.start, end: self.len() })
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeFrom<usize>) -> &[T] {
+ self.index(ops::Range{ start: index.start, end: self.len() })
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<RangeFull> for [T] {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, _index: &RangeFull) -> &[T] {
self
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, _index: RangeFull) -> &[T] {
+ self
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::Range<usize>> for [T] {
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, index: &ops::Range<usize>) -> &mut [T] {
assert!(index.start <= index.end);
)
}
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [T] {
+ assert!(index.start <= index.end);
+ assert!(index.end <= self.len());
+ unsafe {
+ from_raw_parts_mut(
+ self.as_mut_ptr().offset(index.start as isize),
+ index.end - index.start
+ )
+ }
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeTo<usize>> for [T] {
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, index: &ops::RangeTo<usize>) -> &mut [T] {
self.index_mut(&ops::Range{ start: 0, end: index.end })
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut [T] {
+ self.index_mut(ops::Range{ start: 0, end: index.end })
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeFrom<usize>> for [T] {
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, index: &ops::RangeFrom<usize>) -> &mut [T] {
let len = self.len();
self.index_mut(&ops::Range{ start: index.start, end: len })
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut [T] {
+ let len = self.len();
+ self.index_mut(ops::Range{ start: index.start, end: len })
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<RangeFull> for [T] {
+
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] {
self
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, _index: RangeFull) -> &mut [T] {
+ self
+ }
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::Range<usize>> for Iter<'a, T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::Range<usize>) -> &[T] {
self.as_slice().index(index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::Range<usize>) -> &[T] {
+ self.as_slice().index(index)
+ }
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeTo<usize>> for Iter<'a, T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeTo<usize>) -> &[T] {
self.as_slice().index(index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeTo<usize>) -> &[T] {
+ self.as_slice().index(index)
+ }
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeFrom<usize>> for Iter<'a, T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeFrom<usize>) -> &[T] {
self.as_slice().index(index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeFrom<usize>) -> &[T] {
+ self.as_slice().index(index)
+ }
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<RangeFull> for Iter<'a, T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, _index: &RangeFull) -> &[T] {
self.as_slice()
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, _index: RangeFull) -> &[T] {
+ self.as_slice()
+ }
}
impl<'a, T> Iter<'a, T> {
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::Range<usize>> for IterMut<'a, T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::Range<usize>) -> &[T] {
self.index(&RangeFull).index(index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::Range<usize>) -> &[T] {
+ self.index(RangeFull).index(index)
+ }
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeTo<usize>> for IterMut<'a, T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeTo<usize>) -> &[T] {
self.index(&RangeFull).index(index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeTo<usize>) -> &[T] {
+ self.index(RangeFull).index(index)
+ }
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeFrom<usize>> for IterMut<'a, T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeFrom<usize>) -> &[T] {
self.index(&RangeFull).index(index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeFrom<usize>) -> &[T] {
+ self.index(RangeFull).index(index)
+ }
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<RangeFull> for IterMut<'a, T> {
type Output = [T];
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, _index: &RangeFull) -> &[T] {
make_slice!(T => &[T]: self.ptr, self.end)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, _index: RangeFull) -> &[T] {
+ make_slice!(T => &[T]: self.ptr, self.end)
+ }
}
#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::Range<usize>> for IterMut<'a, T> {
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, index: &ops::Range<usize>) -> &mut [T] {
self.index_mut(&RangeFull).index_mut(index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [T] {
+ self.index_mut(RangeFull).index_mut(index)
+ }
}
#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::RangeTo<usize>> for IterMut<'a, T> {
+
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, index: &ops::RangeTo<usize>) -> &mut [T] {
self.index_mut(&RangeFull).index_mut(index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut [T] {
+ self.index_mut(RangeFull).index_mut(index)
+ }
}
#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::RangeFrom<usize>> for IterMut<'a, T> {
+
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, index: &ops::RangeFrom<usize>) -> &mut [T] {
self.index_mut(&RangeFull).index_mut(index)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut [T] {
+ self.index_mut(RangeFull).index_mut(index)
+ }
}
#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<RangeFull> for IterMut<'a, T> {
+
+ #[cfg(stage0)]
#[inline]
fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] {
make_mut_slice!(T => &mut [T]: self.ptr, self.end)
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index_mut(&mut self, _index: RangeFull) -> &mut [T] {
+ make_mut_slice!(T => &mut [T]: self.ptr, self.end)
+ }
}
/// // byte 100 is outside the string
/// // &s[3 .. 100];
/// ```
+ #[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::Range<usize>> for str {
type Output = str;
}
}
+ /// Returns a slice of the given string from the byte range
+ /// [`begin`..`end`).
+ ///
+ /// This operation is `O(1)`.
+ ///
+ /// Panics when `begin` and `end` do not point to valid characters
+ /// or point beyond the last character of the string.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let s = "Löwe 老虎 Léopard";
+ /// assert_eq!(&s[0 .. 1], "L");
+ ///
+ /// assert_eq!(&s[1 .. 9], "öwe 老");
+ ///
+ /// // these will panic:
+ /// // byte 2 lies within `ö`:
+ /// // &s[2 ..3];
+ ///
+ /// // byte 8 lies within `老`
+ /// // &s[1 .. 8];
+ ///
+ /// // byte 100 is outside the string
+ /// // &s[3 .. 100];
+ /// ```
+ #[cfg(not(stage0))]
+ #[stable(feature = "rust1", since = "1.0.0")]
+ impl ops::Index<ops::Range<usize>> for str {
+ type Output = str;
+ #[inline]
+ fn index(&self, index: ops::Range<usize>) -> &str {
+ // is_char_boundary checks that the index is in [0, .len()]
+ if index.start <= index.end &&
+ self.is_char_boundary(index.start) &&
+ self.is_char_boundary(index.end) {
+ unsafe { self.slice_unchecked(index.start, index.end) }
+ } else {
+ super::slice_error_fail(self, index.start, index.end)
+ }
+ }
+ }
+
/// Returns a slice of the string from the beginning to byte
/// `end`.
///
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeTo<usize>> for str {
type Output = str;
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeTo<usize>) -> &str {
// is_char_boundary checks that the index is in [0, .len()]
super::slice_error_fail(self, 0, index.end)
}
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeTo<usize>) -> &str {
+ // is_char_boundary checks that the index is in [0, .len()]
+ if self.is_char_boundary(index.end) {
+ unsafe { self.slice_unchecked(0, index.end) }
+ } else {
+ super::slice_error_fail(self, 0, index.end)
+ }
+ }
}
/// Returns a slice of the string from `begin` to its end.
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeFrom<usize>> for str {
type Output = str;
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, index: &ops::RangeFrom<usize>) -> &str {
// is_char_boundary checks that the index is in [0, .len()]
super::slice_error_fail(self, index.start, self.len())
}
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, index: ops::RangeFrom<usize>) -> &str {
+ // is_char_boundary checks that the index is in [0, .len()]
+ if self.is_char_boundary(index.start) {
+ unsafe { self.slice_unchecked(index.start, self.len()) }
+ } else {
+ super::slice_error_fail(self, index.start, self.len())
+ }
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeFull> for str {
type Output = str;
+
+ #[cfg(stage0)]
#[inline]
fn index(&self, _index: &ops::RangeFull) -> &str {
self
}
+
+ #[cfg(not(stage0))]
+ #[inline]
+ fn index(&self, _index: ops::RangeFull) -> &str {
+ self
+ }
}
}
}
}
+#[cfg(stage0)]
impl<'a> Index<&'a str> for Json {
type Output = Json;
}
}
+#[cfg(not(stage0))]
+impl<'a> Index<&'a str> for Json {
+ type Output = Json;
+
+ fn index(&self, idx: &'a str) -> &Json {
+ self.find(idx).unwrap()
+ }
+}
+
+#[cfg(stage0)]
impl Index<uint> for Json {
type Output = Json;
}
}
+#[cfg(not(stage0))]
+impl Index<uint> for Json {
+ type Output = Json;
+
+ fn index<'a>(&'a self, idx: uint) -> &'a Json {
+ match self {
+ &Json::Array(ref v) => &v[idx],
+ _ => panic!("can only index Json with uint if it is an array")
+ }
+ }
+}
+
/// The output of the streaming parser.
#[derive(PartialEq, Clone, Debug)]
pub enum JsonEvent {
/// Some(x) => *x = "b",
/// None => (),
/// }
- /// assert_eq!(map[1], "b");
+ /// assert_eq!(map[&1], "b");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
///
/// map.insert(37, "b");
/// assert_eq!(map.insert(37, "c"), Some("b"));
- /// assert_eq!(map[37], "c");
+ /// assert_eq!(map[&37], "c");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
}
}
+#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<K, Q: ?Sized, V, S> Index<Q> for HashMap<K, V, S>
where K: Eq + Hash + Borrow<Q>,
}
}
+#[cfg(not(stage0))]
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S>
+ where K: Eq + Hash + Borrow<Q>,
+ Q: Eq + Hash,
+ S: HashState,
+{
+ type Output = V;
+
+ #[inline]
+ fn index(&self, index: &Q) -> &V {
+ self.get(index).expect("no entry found for key")
+ }
+}
+
/// HashMap iterator.
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, K: 'a, V: 'a> {
map.insert(2, 1);
map.insert(3, 4);
- assert_eq!(map[2], 1);
+ assert_eq!(map[&2], 1);
}
#[test]
map.insert(2, 1);
map.insert(3, 4);
- map[4];
+ map[&4];
}
#[test]
}
}
+#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeFull> for OsString {
type Output = OsStr;
}
}
+#[cfg(not(stage0))]
+#[stable(feature = "rust1", since = "1.0.0")]
+impl ops::Index<ops::RangeFull> for OsString {
+ type Output = OsStr;
+
+ #[inline]
+ fn index(&self, _index: ops::RangeFull) -> &OsStr {
+ unsafe { mem::transmute(self.inner.as_slice()) }
+ }
+}
+
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Deref for OsString {
type Target = OsStr;
///
/// Panics when `begin` and `end` do not point to code point boundaries,
/// or point beyond the end of the string.
+#[cfg(stage0)]
impl ops::Index<ops::Range<usize>> for Wtf8 {
type Output = Wtf8;
}
}
+/// Return a slice of the given string for the byte range [`begin`..`end`).
+///
+/// # Panics
+///
+/// Panics when `begin` and `end` do not point to code point boundaries,
+/// or point beyond the end of the string.
+#[cfg(not(stage0))]
+impl ops::Index<ops::Range<usize>> for Wtf8 {
+ type Output = Wtf8;
+
+ #[inline]
+ fn index(&self, range: ops::Range<usize>) -> &Wtf8 {
+ // is_code_point_boundary checks that the index is in [0, .len()]
+ if range.start <= range.end &&
+ is_code_point_boundary(self, range.start) &&
+ is_code_point_boundary(self, range.end) {
+ unsafe { slice_unchecked(self, range.start, range.end) }
+ } else {
+ slice_error_fail(self, range.start, range.end)
+ }
+ }
+}
+
/// Return a slice of the given string from byte `begin` to its end.
///
/// # Panics
///
/// Panics when `begin` is not at a code point boundary,
/// or is beyond the end of the string.
+#[cfg(stage0)]
impl ops::Index<ops::RangeFrom<usize>> for Wtf8 {
type Output = Wtf8;
}
}
+/// Return a slice of the given string from byte `begin` to its end.
+///
+/// # Panics
+///
+/// Panics when `begin` is not at a code point boundary,
+/// or is beyond the end of the string.
+#[cfg(not(stage0))]
+impl ops::Index<ops::RangeFrom<usize>> for Wtf8 {
+ type Output = Wtf8;
+
+ #[inline]
+ fn index(&self, range: ops::RangeFrom<usize>) -> &Wtf8 {
+ // is_code_point_boundary checks that the index is in [0, .len()]
+ if is_code_point_boundary(self, range.start) {
+ unsafe { slice_unchecked(self, range.start, self.len()) }
+ } else {
+ slice_error_fail(self, range.start, self.len())
+ }
+ }
+}
+
/// Return a slice of the given string from its beginning to byte `end`.
///
/// # Panics
///
/// Panics when `end` is not at a code point boundary,
/// or is beyond the end of the string.
+#[cfg(stage0)]
impl ops::Index<ops::RangeTo<usize>> for Wtf8 {
type Output = Wtf8;
}
}
+/// Return a slice of the given string from its beginning to byte `end`.
+///
+/// # Panics
+///
+/// Panics when `end` is not at a code point boundary,
+/// or is beyond the end of the string.
+#[cfg(not(stage0))]
+impl ops::Index<ops::RangeTo<usize>> for Wtf8 {
+ type Output = Wtf8;
+
+ #[inline]
+ fn index(&self, range: ops::RangeTo<usize>) -> &Wtf8 {
+ // is_code_point_boundary checks that the index is in [0, .len()]
+ if is_code_point_boundary(self, range.end) {
+ unsafe { slice_unchecked(self, 0, range.end) }
+ } else {
+ slice_error_fail(self, 0, range.end)
+ }
+ }
+}
+
+#[cfg(stage0)]
impl ops::Index<ops::RangeFull> for Wtf8 {
type Output = Wtf8;
}
}
+#[cfg(not(stage0))]
+impl ops::Index<ops::RangeFull> for Wtf8 {
+ type Output = Wtf8;
+
+ #[inline]
+ fn index(&self, _range: ops::RangeFull) -> &Wtf8 {
+ self
+ }
+}
+
#[inline]
fn decode_surrogate(second_byte: u8, third_byte: u8) -> u16 {
// The first byte is assumed to be 0xED