From f7ff37e4c52a1d6562635fcd5bab6309cf75ea08 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Fri, 2 Jan 2015 13:56:28 +1300 Subject: Replace full slice notation with index calls --- src/libcollections/bit.rs | 2 +- src/libcollections/ring_buf.rs | 2 +- src/libcollections/slice.rs | 26 +++++----- src/libcollections/str.rs | 106 ++++++++++++++++++++--------------------- src/libcollections/string.rs | 38 +++++++-------- src/libcollections/vec.rs | 35 ++++++-------- src/libcollections/vec_map.rs | 3 +- 7 files changed, 103 insertions(+), 109 deletions(-) (limited to 'src/libcollections') diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index c092e000215..3d1779445e1 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -330,7 +330,7 @@ impl Bitv { if extra_bytes > 0 { let mut last_word = 0u32; - for (i, &byte) in bytes[complete_words*4..].iter().enumerate() { + for (i, &byte) in bytes.index(&((complete_words*4)..)).iter().enumerate() { last_word |= (reverse_bits(byte) as u32) << (i * 8); } bitv.storage.push(last_word); diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index 11775f62b1c..eff25b6d385 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -556,7 +556,7 @@ impl RingBuf { let buf = self.buffer_as_slice(); if contiguous { let (empty, buf) = buf.split_at(0); - (buf[self.tail..self.head], empty) + (buf.index(&(self.tail..self.head)), empty) } else { let (mid, right) = buf.split_at(self.tail); let (left, _) = mid.split_at(self.head); diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 9e5aa7d645b..ca1fed0f78f 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -55,7 +55,7 @@ //! #![feature(slicing_syntax)] //! fn main() { //! let numbers = [0i, 1i, 2i]; -//! let last_numbers = numbers[1..3]; +//! let last_numbers = numbers.index(&(1..3)); //! // last_numbers is now &[1i, 2i] //! } //! ``` @@ -98,7 +98,7 @@ use core::iter::{range, range_step, MultiplicativeIterator}; use core::kinds::Sized; use core::mem::size_of; use core::mem; -use core::ops::{FnMut, SliceMut}; +use core::ops::{FnMut, FullRange, Index, IndexMut}; use core::option::Option::{self, Some, None}; use core::ptr::PtrExt; use core::ptr; @@ -1065,12 +1065,12 @@ impl ElementSwaps { #[unstable = "trait is unstable"] impl BorrowFrom> for [T] { - fn borrow_from(owned: &Vec) -> &[T] { owned[] } + fn borrow_from(owned: &Vec) -> &[T] { owned.index(&FullRange) } } #[unstable = "trait is unstable"] impl BorrowFromMut> for [T] { - fn borrow_from_mut(owned: &mut Vec) -> &mut [T] { owned.as_mut_slice_() } + fn borrow_from_mut(owned: &mut Vec) -> &mut [T] { owned.index_mut(&FullRange) } } #[unstable = "trait is unstable"] @@ -1606,7 +1606,7 @@ mod tests { // Test on stack. let vec_stack: &[_] = &[1i, 2, 3]; - let v_b = vec_stack[1u..3u].to_vec(); + let v_b = vec_stack.index(&(1u..3u)).to_vec(); assert_eq!(v_b.len(), 2u); let v_b = v_b.as_slice(); assert_eq!(v_b[0], 2); @@ -1614,7 +1614,7 @@ mod tests { // Test `Box<[T]>` let vec_unique = vec![1i, 2, 3, 4, 5, 6]; - let v_d = vec_unique[1u..6u].to_vec(); + let v_d = vec_unique.index(&(1u..6u)).to_vec(); assert_eq!(v_d.len(), 5u); let v_d = v_d.as_slice(); assert_eq!(v_d[0], 2); @@ -1627,21 +1627,21 @@ mod tests { #[test] fn test_slice_from() { let vec: &[int] = &[1, 2, 3, 4]; - assert_eq!(vec[0..], vec); + assert_eq!(vec.index(&(0..)), vec); let b: &[int] = &[3, 4]; - assert_eq!(vec[2..], b); + assert_eq!(vec.index(&(2..)), b); let b: &[int] = &[]; - assert_eq!(vec[4..], b); + assert_eq!(vec.index(&(4..)), b); } #[test] fn test_slice_to() { let vec: &[int] = &[1, 2, 3, 4]; - assert_eq!(vec[..4], vec); + assert_eq!(vec.index(&(0..4)), vec); let b: &[int] = &[1, 2]; - assert_eq!(vec[..2], b); + assert_eq!(vec.index(&(0..2)), b); let b: &[int] = &[]; - assert_eq!(vec[..0], b); + assert_eq!(vec.index(&(0..0)), b); } @@ -2567,7 +2567,7 @@ mod tests { } assert_eq!(cnt, 3); - for f in v[1..3].iter() { + for f in v.index(&(1..3)).iter() { assert!(*f == Foo); cnt += 1; } diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index c0482702ccd..cb4264ec34f 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -60,7 +60,7 @@ use core::char::CharExt; use core::clone::Clone; use core::iter::AdditiveIterator; use core::iter::{range, Iterator, IteratorExt}; -use core::ops; +use core::ops::{self, llRange, Index}; use core::option::Option::{self, Some, None}; use core::slice::AsSlice; use core::str as core_str; @@ -386,7 +386,7 @@ macro_rules! utf8_acc_cont_byte { #[unstable = "trait is unstable"] impl BorrowFrom for str { - fn borrow_from(owned: &String) -> &str { owned[] } + fn borrow_from(owned: &String) -> &str { owned.index(&FullRange) } } #[unstable = "trait is unstable"] @@ -408,7 +408,7 @@ Section: Trait implementations /// Any string that can be represented as a slice. #[stable] -pub trait StrExt: ops::Slice { +pub trait StrExt: Index { /// Escapes each char in `s` with `char::escape_default`. #[unstable = "return type may change to be an iterator"] fn escape_default(&self) -> String { @@ -464,7 +464,7 @@ pub trait StrExt: ops::Slice { #[unstable = "this functionality may be moved to libunicode"] fn nfd_chars<'a>(&'a self) -> Decompositions<'a> { Decompositions { - iter: self[].chars(), + iter: self.index(&FullRange).chars(), buffer: Vec::new(), sorted: false, kind: Canonical @@ -477,7 +477,7 @@ pub trait StrExt: ops::Slice { #[unstable = "this functionality may be moved to libunicode"] fn nfkd_chars<'a>(&'a self) -> Decompositions<'a> { Decompositions { - iter: self[].chars(), + iter: self.index(&FullRange).chars(), buffer: Vec::new(), sorted: false, kind: Compatible @@ -525,7 +525,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn contains(&self, pat: &str) -> bool { - core_str::StrExt::contains(self[], pat) + core_str::StrExt::contains(self.index(&FullRange), pat) } /// Returns true if a string contains a char pattern. @@ -541,7 +541,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "might get removed in favour of a more generic contains()"] fn contains_char(&self, pat: P) -> bool { - core_str::StrExt::contains_char(self[], pat) + core_str::StrExt::contains_char(self.index(&FullRange), pat) } /// An iterator over the characters of `self`. Note, this iterates @@ -555,7 +555,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn chars(&self) -> Chars { - core_str::StrExt::chars(self[]) + core_str::StrExt::chars(self.index(&FullRange)) } /// An iterator over the bytes of `self` @@ -568,13 +568,13 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn bytes(&self) -> Bytes { - core_str::StrExt::bytes(self[]) + core_str::StrExt::bytes(self.index(&FullRange)) } /// An iterator over the characters of `self` and their byte offsets. #[stable] fn char_indices(&self) -> CharIndices { - core_str::StrExt::char_indices(self[]) + core_str::StrExt::char_indices(self.index(&FullRange)) } /// An iterator over substrings of `self`, separated by characters @@ -597,7 +597,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn split(&self, pat: P) -> Split

{ - core_str::StrExt::split(self[], pat) + core_str::StrExt::split(self.index(&FullRange), pat) } /// An iterator over substrings of `self`, separated by characters @@ -624,7 +624,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn splitn(&self, count: uint, pat: P) -> SplitN

{ - core_str::StrExt::splitn(self[], count, pat) + core_str::StrExt::splitn(self.index(&FullRange), count, pat) } /// An iterator over substrings of `self`, separated by characters @@ -653,7 +653,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "might get removed"] fn split_terminator(&self, pat: P) -> SplitTerminator

{ - core_str::StrExt::split_terminator(self[], pat) + core_str::StrExt::split_terminator(self.index(&FullRange), pat) } /// An iterator over substrings of `self`, separated by characters @@ -674,7 +674,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn rsplitn(&self, count: uint, pat: P) -> RSplitN

{ - core_str::StrExt::rsplitn(self[], count, pat) + core_str::StrExt::rsplitn(self.index(&FullRange), count, pat) } /// An iterator over the start and end indices of the disjoint @@ -699,7 +699,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "might have its iterator type changed"] fn match_indices<'a>(&'a self, pat: &'a str) -> MatchIndices<'a> { - core_str::StrExt::match_indices(self[], pat) + core_str::StrExt::match_indices(self.index(&FullRange), pat) } /// An iterator over the substrings of `self` separated by the pattern `sep`. @@ -715,7 +715,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "might get removed in the future in favor of a more generic split()"] fn split_str<'a>(&'a self, pat: &'a str) -> SplitStr<'a> { - core_str::StrExt::split_str(self[], pat) + core_str::StrExt::split_str(self.index(&FullRange), pat) } /// An iterator over the lines of a string (subsequences separated @@ -731,7 +731,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn lines(&self) -> Lines { - core_str::StrExt::lines(self[]) + core_str::StrExt::lines(self.index(&FullRange)) } /// An iterator over the lines of a string, separated by either @@ -747,7 +747,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn lines_any(&self) -> LinesAny { - core_str::StrExt::lines_any(self[]) + core_str::StrExt::lines_any(self.index(&FullRange)) } /// Returns a slice of the given string from the byte range @@ -782,7 +782,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "use slice notation [a..b] instead"] fn slice(&self, begin: uint, end: uint) -> &str { - core_str::StrExt::slice(self[], begin, end) + core_str::StrExt::slice(self.index(&FullRange), begin, end) } /// Returns a slice of the string from `begin` to its end. @@ -795,7 +795,7 @@ pub trait StrExt: ops::Slice { /// See also `slice`, `slice_to` and `slice_chars`. #[unstable = "use slice notation [a..] instead"] fn slice_from(&self, begin: uint) -> &str { - core_str::StrExt::slice_from(self[], begin) + core_str::StrExt::slice_from(self.index(&FullRange), begin) } /// Returns a slice of the string from the beginning to byte @@ -809,7 +809,7 @@ pub trait StrExt: ops::Slice { /// See also `slice`, `slice_from` and `slice_chars`. #[unstable = "use slice notation [0..a] instead"] fn slice_to(&self, end: uint) -> &str { - core_str::StrExt::slice_to(self[], end) + core_str::StrExt::slice_to(self.index(&FullRange), end) } /// Returns a slice of the string from the character range @@ -837,7 +837,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "may have yet to prove its worth"] fn slice_chars(&self, begin: uint, end: uint) -> &str { - core_str::StrExt::slice_chars(self[], begin, end) + core_str::StrExt::slice_chars(self.index(&FullRange), begin, end) } /// Takes a bytewise (not UTF-8) slice from a string. @@ -848,7 +848,7 @@ pub trait StrExt: ops::Slice { /// the entire slice as well. #[stable] unsafe fn slice_unchecked(&self, begin: uint, end: uint) -> &str { - core_str::StrExt::slice_unchecked(self[], begin, end) + core_str::StrExt::slice_unchecked(self.index(&FullRange), begin, end) } /// Returns true if the pattern `pat` is a prefix of the string. @@ -860,7 +860,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn starts_with(&self, pat: &str) -> bool { - core_str::StrExt::starts_with(self[], pat) + core_str::StrExt::starts_with(self.index(&FullRange), pat) } /// Returns true if the pattern `pat` is a suffix of the string. @@ -872,7 +872,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn ends_with(&self, pat: &str) -> bool { - core_str::StrExt::ends_with(self[], pat) + core_str::StrExt::ends_with(self.index(&FullRange), pat) } /// Returns a string with all pre- and suffixes that match @@ -892,7 +892,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn trim_matches(&self, pat: P) -> &str { - core_str::StrExt::trim_matches(self[], pat) + core_str::StrExt::trim_matches(self.index(&FullRange), pat) } /// Returns a string with all prefixes that match @@ -912,7 +912,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn trim_left_matches(&self, pat: P) -> &str { - core_str::StrExt::trim_left_matches(self[], pat) + core_str::StrExt::trim_left_matches(self.index(&FullRange), pat) } /// Returns a string with all suffixes that match @@ -932,7 +932,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn trim_right_matches(&self, pat: P) -> &str { - core_str::StrExt::trim_right_matches(self[], pat) + core_str::StrExt::trim_right_matches(self.index(&FullRange), pat) } /// Check that `index`-th byte lies at the start and/or end of a @@ -960,7 +960,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "naming is uncertain with container conventions"] fn is_char_boundary(&self, index: uint) -> bool { - core_str::StrExt::is_char_boundary(self[], index) + core_str::StrExt::is_char_boundary(self.index(&FullRange), index) } /// Pluck a character out of a string and return the index of the next @@ -1018,7 +1018,7 @@ pub trait StrExt: ops::Slice { /// If `i` is not the index of the beginning of a valid UTF-8 character. #[unstable = "naming is uncertain with container conventions"] fn char_range_at(&self, start: uint) -> CharRange { - core_str::StrExt::char_range_at(self[], start) + core_str::StrExt::char_range_at(self.index(&FullRange), start) } /// Given a byte position and a str, return the previous char and its position. @@ -1033,7 +1033,7 @@ pub trait StrExt: ops::Slice { /// If `i` is not an index following a valid UTF-8 character. #[unstable = "naming is uncertain with container conventions"] fn char_range_at_reverse(&self, start: uint) -> CharRange { - core_str::StrExt::char_range_at_reverse(self[], start) + core_str::StrExt::char_range_at_reverse(self.index(&FullRange), start) } /// Plucks the character starting at the `i`th byte of a string. @@ -1053,7 +1053,7 @@ pub trait StrExt: ops::Slice { /// If `i` is not the index of the beginning of a valid UTF-8 character. #[unstable = "naming is uncertain with container conventions"] fn char_at(&self, i: uint) -> char { - core_str::StrExt::char_at(self[], i) + core_str::StrExt::char_at(self.index(&FullRange), i) } /// Plucks the character ending at the `i`th byte of a string. @@ -1064,7 +1064,7 @@ pub trait StrExt: ops::Slice { /// If `i` is not an index following a valid UTF-8 character. #[unstable = "naming is uncertain with container conventions"] fn char_at_reverse(&self, i: uint) -> char { - core_str::StrExt::char_at_reverse(self[], i) + core_str::StrExt::char_at_reverse(self.index(&FullRange), i) } /// Work with the byte buffer of a string as a byte slice. @@ -1076,7 +1076,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn as_bytes(&self) -> &[u8] { - core_str::StrExt::as_bytes(self[]) + core_str::StrExt::as_bytes(self.index(&FullRange)) } /// Returns the byte index of the first character of `self` that @@ -1104,7 +1104,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn find(&self, pat: P) -> Option { - core_str::StrExt::find(self[], pat) + core_str::StrExt::find(self.index(&FullRange), pat) } /// Returns the byte index of the last character of `self` that @@ -1132,7 +1132,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn rfind(&self, pat: P) -> Option { - core_str::StrExt::rfind(self[], pat) + core_str::StrExt::rfind(self.index(&FullRange), pat) } /// Returns the byte index of the first matching substring @@ -1156,7 +1156,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "might get removed in favor of a more generic find in the future"] fn find_str(&self, needle: &str) -> Option { - core_str::StrExt::find_str(self[], needle) + core_str::StrExt::find_str(self.index(&FullRange), needle) } /// Retrieves the first character from a string slice and returns @@ -1179,7 +1179,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "awaiting conventions about shifting and slices"] fn slice_shift_char(&self) -> Option<(char, &str)> { - core_str::StrExt::slice_shift_char(self[]) + core_str::StrExt::slice_shift_char(self.index(&FullRange)) } /// Returns the byte offset of an inner slice relative to an enclosing outer slice. @@ -1198,7 +1198,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "awaiting convention about comparability of arbitrary slices"] fn subslice_offset(&self, inner: &str) -> uint { - core_str::StrExt::subslice_offset(self[], inner) + core_str::StrExt::subslice_offset(self.index(&FullRange), inner) } /// Return an unsafe pointer to the strings buffer. @@ -1209,13 +1209,13 @@ pub trait StrExt: ops::Slice { #[stable] #[inline] fn as_ptr(&self) -> *const u8 { - core_str::StrExt::as_ptr(self[]) + core_str::StrExt::as_ptr(self.index(&FullRange)) } /// Return an iterator of `u16` over the string encoded as UTF-16. #[unstable = "this functionality may only be provided by libunicode"] fn utf16_units(&self) -> Utf16Units { - Utf16Units { encoder: Utf16Encoder::new(self[].chars()) } + Utf16Units { encoder: Utf16Encoder::new(self.index(&FullRange).chars()) } } /// Return the number of bytes in this string @@ -1229,7 +1229,7 @@ pub trait StrExt: ops::Slice { #[stable] #[inline] fn len(&self) -> uint { - core_str::StrExt::len(self[]) + core_str::StrExt::len(self.index(&FullRange)) } /// Returns true if this slice contains no bytes @@ -1242,7 +1242,7 @@ pub trait StrExt: ops::Slice { #[inline] #[stable] fn is_empty(&self) -> bool { - core_str::StrExt::is_empty(self[]) + core_str::StrExt::is_empty(self.index(&FullRange)) } /// Parse this string into the specified type. @@ -1256,7 +1256,7 @@ pub trait StrExt: ops::Slice { #[inline] #[unstable = "this method was just created"] fn parse(&self) -> Option { - core_str::StrExt::parse(self[]) + core_str::StrExt::parse(self.index(&FullRange)) } /// Returns an iterator over the @@ -1280,7 +1280,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "this functionality may only be provided by libunicode"] fn graphemes(&self, is_extended: bool) -> Graphemes { - UnicodeStr::graphemes(self[], is_extended) + UnicodeStr::graphemes(self.index(&FullRange), is_extended) } /// Returns an iterator over the grapheme clusters of self and their byte offsets. @@ -1295,7 +1295,7 @@ pub trait StrExt: ops::Slice { /// ``` #[unstable = "this functionality may only be provided by libunicode"] fn grapheme_indices(&self, is_extended: bool) -> GraphemeIndices { - UnicodeStr::grapheme_indices(self[], is_extended) + UnicodeStr::grapheme_indices(self.index(&FullRange), is_extended) } /// An iterator over the words of a string (subsequences separated @@ -1311,7 +1311,7 @@ pub trait StrExt: ops::Slice { /// ``` #[stable] fn words(&self) -> Words { - UnicodeStr::words(self[]) + UnicodeStr::words(self.index(&FullRange)) } /// Returns a string's displayed width in columns, treating control @@ -1325,25 +1325,25 @@ pub trait StrExt: ops::Slice { /// `is_cjk` = `false`) if the locale is unknown. #[unstable = "this functionality may only be provided by libunicode"] fn width(&self, is_cjk: bool) -> uint { - UnicodeStr::width(self[], is_cjk) + UnicodeStr::width(self.index(&FullRange), is_cjk) } /// Returns a string with leading and trailing whitespace removed. #[stable] fn trim(&self) -> &str { - UnicodeStr::trim(self[]) + UnicodeStr::trim(self.index(&FullRange)) } /// Returns a string with leading whitespace removed. #[stable] fn trim_left(&self) -> &str { - UnicodeStr::trim_left(self[]) + UnicodeStr::trim_left(self.index(&FullRange)) } /// Returns a string with trailing whitespace removed. #[stable] fn trim_right(&self) -> &str { - UnicodeStr::trim_right(self[]) + UnicodeStr::trim_right(self.index(&FullRange)) } } @@ -2133,7 +2133,7 @@ mod tests { let mut bytes = [0u8; 4]; for c in range(0u32, 0x110000).filter_map(|c| ::core::char::from_u32(c)) { let len = c.encode_utf8(&mut bytes).unwrap_or(0); - let s = ::core::str::from_utf8(bytes[..len]).unwrap(); + let s = ::core::str::from_utf8(bytes.index(&(0..len))).unwrap(); if Some(c) != s.chars().next() { panic!("character {:x}={} does not decode correctly", c as u32, c); } @@ -2145,7 +2145,7 @@ mod tests { let mut bytes = [0u8; 4]; for c in range(0u32, 0x110000).filter_map(|c| ::core::char::from_u32(c)) { let len = c.encode_utf8(&mut bytes).unwrap_or(0); - let s = ::core::str::from_utf8(bytes[..len]).unwrap(); + let s = ::core::str::from_utf8(bytes.index(&(0..len))).unwrap(); if Some(c) != s.chars().rev().next() { panic!("character {:x}={} does not decode correctly", c as u32, c); } diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index b05b5e276e8..69ff513a85b 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -168,7 +168,7 @@ impl String { if i > 0 { unsafe { - res.as_mut_vec().push_all(v[..i]) + res.as_mut_vec().push_all(v.index(&(0..i))) }; } @@ -185,7 +185,7 @@ impl String { macro_rules! error { () => ({ unsafe { if subseqidx != i_ { - res.as_mut_vec().push_all(v[subseqidx..i_]); + res.as_mut_vec().push_all(v.index(&(subseqidx..i_))); } subseqidx = i; res.as_mut_vec().push_all(REPLACEMENT); @@ -254,7 +254,7 @@ impl String { } if subseqidx < total { unsafe { - res.as_mut_vec().push_all(v[subseqidx..total]) + res.as_mut_vec().push_all(v.index(&(subseqidx..total))) }; } Cow::Owned(res) @@ -818,30 +818,30 @@ impl<'a> Add<&'a str> for String { } } -impl ops::Index, str> for String { +impl ops::Index, str> for String { #[inline] - fn index(&self, &index: &ops::Range) -> &str { - self[][*index] + fn index(&self, index: &ops::Range) -> &str { + &self.index(&FullRange)[*index] } } -impl ops::Index, str> for String { +impl ops::Index, str> for String { #[inline] - fn index(&self, &index: &ops::RangeTo) -> &str { - self[][*index] + fn index(&self, index: &ops::RangeTo) -> &str { + &self.index(&FullRange)[*index] } } -impl ops::Index, str> for String { +impl ops::Index, str> for String { #[inline] - fn index(&self, &index: &ops::RangeFrom) -> &str { - self[][*index] + fn index(&self, index: &ops::RangeFrom) -> &str { + &self.index(&FullRange)[*index] } } -impl ops::Index, str> for String { +impl ops::Index for String { #[inline] - fn index(&self, &index: &ops::FullRange) -> &str { + fn index(&self, _index: &ops::FullRange) -> &str { unsafe { mem::transmute(self.vec.as_slice()) } } } @@ -851,7 +851,7 @@ impl ops::Deref for String { type Target = str; fn deref<'a>(&'a self) -> &'a str { - unsafe { mem::transmute(self.vec[]) } + unsafe { mem::transmute(self.vec.index(&FullRange)) } } } @@ -1230,10 +1230,10 @@ mod tests { #[test] fn test_slicing() { let s = "foobar".to_string(); - assert_eq!("foobar", s[]); - assert_eq!("foo", s[..3]); - assert_eq!("bar", s[3..]); - assert_eq!("oob", s[1..4]); + assert_eq!("foobar", s.index(&FullRange)); + assert_eq!("foo", s.index(&(0..3))); + assert_eq!("bar", s.index(&(3..))); + assert_eq!("oob", s.index(&(1..4))); } #[test] diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index c057939df2b..3f667698bae 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1178,7 +1178,7 @@ impl Clone for Vec { // self.len <= other.len due to the truncate above, so the // slice here is always in-bounds. - let slice = other[self.len()..]; + let slice = other.index(&(self.len()..)); self.push_all(slice); } } @@ -1209,65 +1209,58 @@ impl IndexMut for Vec { } } -impl ops::Slice for Vec { - #[inline] - fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T { - &mut self.as_mut_slice()[*index] - } -} - impl ops::Index, [T]> for Vec { #[inline] - fn index(&self, &index: &ops::Range) -> &[T] { + fn index(&self, index: &ops::Range) -> &[T] { self.as_slice().index(index) } } impl ops::Index, [T]> for Vec { #[inline] - fn index(&self, &index: &ops::RangeTo) -> &[T] { + fn index(&self, index: &ops::RangeTo) -> &[T] { self.as_slice().index(index) } } impl ops::Index, [T]> for Vec { #[inline] - fn index(&self, &index: &ops::RangeFrom) -> &[T] { + fn index(&self, index: &ops::RangeFrom) -> &[T] { self.as_slice().index(index) } } -impl ops::Index, [T]> for Vec { +impl ops::Index for Vec { #[inline] - fn index(&self, &index: &ops::FullRange) -> &[T] { + fn index(&self, _index: &ops::FullRange) -> &[T] { self.as_slice() } } impl ops::IndexMut, [T]> for Vec { #[inline] - fn index_mut(&mut self, &index: &ops::Range) -> &mut [T] { + fn index_mut(&mut self, index: &ops::Range) -> &mut [T] { self.as_mut_slice().index_mut(index) } } impl ops::IndexMut, [T]> for Vec { #[inline] - fn index_mut(&mut self, &index: &ops::RangeTo) -> &mut [T] { + fn index_mut(&mut self, index: &ops::RangeTo) -> &mut [T] { self.as_mut_slice().index_mut(index) } } impl ops::IndexMut, [T]> for Vec { #[inline] - fn index_mut(&mut self, &index: &ops::RangeFrom) -> &mut [T] { + fn index_mut(&mut self, index: &ops::RangeFrom) -> &mut [T] { self.as_mut_slice().index_mut(index) } } -impl ops::IndexMut, [T]> for Vec { +impl ops::IndexMut for Vec { #[inline] - fn index_mut(&mut self, &index: &ops::FullRange) -> &mut [T] { + fn index_mut(&mut self, _index: &ops::FullRange) -> &mut [T] { self.as_mut_slice() } } @@ -2125,7 +2118,7 @@ mod tests { #[should_fail] fn test_slice_out_of_bounds_2() { let x: Vec = vec![1, 2, 3, 4, 5]; - x[..6]; + x.index(&(0..6)); } #[test] @@ -2139,14 +2132,14 @@ mod tests { #[should_fail] fn test_slice_out_of_bounds_4() { let x: Vec = vec![1, 2, 3, 4, 5]; - x[1..6]; + x.index(&(1..6)); } #[test] #[should_fail] fn test_slice_out_of_bounds_5() { let x: Vec = vec![1, 2, 3, 4, 5]; - x[3..2]; + x.index(&(3..2)); } #[test] diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index cc757b65623..68469059fea 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -455,7 +455,8 @@ impl VecMap { if *key >= self.v.len() { return None; } - self.v[*key].take() + let result = &mut self.v[*key]; + result.take() } } -- cgit 1.4.1-3-g733a5