From f58a8c9d760aead49e138fa8045e229a47217644 Mon Sep 17 00:00:00 2001 From: Jonathan S Date: Sun, 20 Apr 2014 21:28:38 -0500 Subject: [PATCH 1/2] Provide an implementation of DoubleEndedIterator for the results of &[T]::split and &[T]::rsplit This makes the splitting functions in std::slice return DoubleEndedIterators. Unfortunately, splitn and rsplitn cannot provide such an interface and so must return different types. As a result, the following changes were made: * RevSplits was removed in favor of explicitly using Rev * Splits can no longer bound the number of splits done * Splits now implements DoubleEndedIterator * SplitsN was added, taking the role of what both Splits and RevSplits used to be * rsplit returns Rev> instead of RevSplits<'a, T> * splitn returns SplitsN<'a, T> instead of Splits<'a, T> * rsplitn returns SplitsN<'a, T> instead of RevSplits<'a, T> All functions that were previously implemented on each return value still are, so outside of changing of type annotations, existing code should work out of the box. In the rare case that code relied on the return types of split and splitn or of rsplit and rsplitn being the same, the previous behavior can be emulated by calling splitn or rsplitn with a bount of uint::MAX. The value of this change comes in multiple parts: * Consistency. The splitting code in std::str is structured similarly to the new slice splitting code, having separate CharSplits and CharSplitsN types. * Smaller API. Although this commit doesn't implement it, using a DoubleEndedIterator for splitting means that rsplit, path::RevComponents, path::RevStrComponents, Path::rev_components, and Path::rev_str_components are no longer needed - they can be emulated simply with .rev(). * Power. DoubleEndedIterators are able to traverse the list from both sides at once instead of only forwards or backwards. * Efficiency. For the common case of using split instead of splitn, the iterator is slightly smaller and slightly faster. [breaking-change] --- src/libstd/path/posix.rs | 23 +++----- src/libstd/path/windows.rs | 14 ++--- src/libstd/slice.rs | 111 ++++++++++++++++++------------------- 3 files changed, 64 insertions(+), 84 deletions(-) diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index 4affea37e35..eceeb73a015 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -16,11 +16,11 @@ use clone::Clone; use cmp::{Eq, TotalEq}; use from_str::FromStr; use io::Writer; -use iter::{AdditiveIterator, Extendable, Iterator, Map}; +use iter::{DoubleEndedIterator, Rev, AdditiveIterator, Extendable, Iterator, Map}; use option::{Option, None, Some}; use str; use str::Str; -use slice::{CloneableVector, RevSplits, Splits, Vector, VectorVector, +use slice::{CloneableVector, Splits, Vector, VectorVector, ImmutableEqVector, OwnedVector, ImmutableVector}; use vec::Vec; @@ -29,14 +29,13 @@ use super::{BytesContainer, GenericPath, GenericPathUnsafe}; /// Iterator that yields successive components of a Path as &[u8] pub type Components<'a> = Splits<'a, u8>; /// Iterator that yields components of a Path in reverse as &[u8] -pub type RevComponents<'a> = RevSplits<'a, u8>; +pub type RevComponents<'a> = Rev>; /// Iterator that yields successive components of a Path as Option<&str> pub type StrComponents<'a> = Map<'a, &'a [u8], Option<&'a str>, Components<'a>>; /// Iterator that yields components of a Path in reverse as Option<&str> -pub type RevStrComponents<'a> = Map<'a, &'a [u8], Option<&'a str>, - RevComponents<'a>>; +pub type RevStrComponents<'a> = Rev>; /// Represents a POSIX file path #[deriving(Clone)] @@ -397,15 +396,7 @@ impl Path { /// Returns an iterator that yields each component of the path in reverse. /// See components() for details. pub fn rev_components<'a>(&'a self) -> RevComponents<'a> { - let v = if *self.repr.get(0) == SEP_BYTE { - self.repr.slice_from(1) - } else { self.repr.as_slice() }; - let mut ret = v.rsplit(is_sep_byte); - if v.is_empty() { - // consume the empty "" component - ret.next(); - } - ret + self.components().rev() } /// Returns an iterator that yields each component of the path as Option<&str>. @@ -417,7 +408,7 @@ impl Path { /// Returns an iterator that yields each component of the path in reverse as Option<&str>. /// See components() for details. pub fn rev_str_components<'a>(&'a self) -> RevStrComponents<'a> { - self.rev_components().map(str::from_utf8) + self.str_components().rev() } } diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index 74ca8dc5785..679075fe6ca 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -37,15 +37,13 @@ pub type StrComponents<'a> = Map<'a, &'a str, Option<&'a str>, /// /// Each component is yielded as Option<&str> for compatibility with PosixPath, but /// every component in WindowsPath is guaranteed to be Some. -pub type RevStrComponents<'a> = Rev, - CharSplits<'a, char>>>; +pub type RevStrComponents<'a> = Rev>; /// Iterator that yields successive components of a Path as &[u8] pub type Components<'a> = Map<'a, Option<&'a str>, &'a [u8], StrComponents<'a>>; /// Iterator that yields components of a Path in reverse as &[u8] -pub type RevComponents<'a> = Map<'a, Option<&'a str>, &'a [u8], - RevStrComponents<'a>>; +pub type RevComponents<'a> = Rev>; /// Represents a Windows path // Notes for Windows path impl: @@ -650,11 +648,7 @@ impl Path { /// Returns an iterator that yields each component of the path in reverse as a &[u8]. /// See str_components() for details. pub fn rev_components<'a>(&'a self) -> RevComponents<'a> { - fn convert<'a>(x: Option<&'a str>) -> &'a [u8] { - #![inline] - x.unwrap().as_bytes() - } - self.rev_str_components().map(convert) + self.components().rev() } fn equiv_prefix(&self, other: &Path) -> bool { diff --git a/src/libstd/slice.rs b/src/libstd/slice.rs index f5e064942e6..180a142dea1 100644 --- a/src/libstd/slice.rs +++ b/src/libstd/slice.rs @@ -119,7 +119,6 @@ use result::{Ok, Err}; use mem; use mem::size_of; use kinds::marker; -use uint; use unstable::finally::try_finally; use raw::{Repr, Slice}; use RawVec = raw::Vec; @@ -148,7 +147,6 @@ pub fn mut_ref_slice<'a, A>(s: &'a mut A) -> &'a mut [A] { /// match a predicate function. pub struct Splits<'a, T> { v: &'a [T], - n: uint, pred: |t: &T|: 'a -> bool, finished: bool } @@ -158,11 +156,6 @@ impl<'a, T> Iterator<&'a [T]> for Splits<'a, T> { fn next(&mut self) -> Option<&'a [T]> { if self.finished { return None; } - if self.n == 0 { - self.finished = true; - return Some(self.v); - } - match self.v.iter().position(|x| (self.pred)(x)) { None => { self.finished = true; @@ -171,7 +164,6 @@ impl<'a, T> Iterator<&'a [T]> for Splits<'a, T> { Some(idx) => { let ret = Some(self.v.slice(0, idx)); self.v = self.v.slice(idx + 1, self.v.len()); - self.n -= 1; ret } } @@ -180,38 +172,18 @@ impl<'a, T> Iterator<&'a [T]> for Splits<'a, T> { #[inline] fn size_hint(&self) -> (uint, Option) { if self.finished { - return (0, Some(0)) - } - // if the predicate doesn't match anything, we yield one slice - // if it matches every element, we yield N+1 empty slices where - // N is either the number of elements or the number of splits. - match (self.v.len(), self.n) { - (0,_) => (1, Some(1)), - (_,0) => (1, Some(1)), - (l,n) => (1, cmp::min(l,n).checked_add(&1u)) + (0, Some(0)) + } else { + (1, Some(self.v.len() + 1)) } } } -/// An iterator over the slices of a vector separated by elements that -/// match a predicate function, from back to front. -pub struct RevSplits<'a, T> { - v: &'a [T], - n: uint, - pred: |t: &T|: 'a -> bool, - finished: bool -} - -impl<'a, T> Iterator<&'a [T]> for RevSplits<'a, T> { +impl<'a, T> DoubleEndedIterator<&'a [T]> for Splits<'a, T> { #[inline] - fn next(&mut self) -> Option<&'a [T]> { + fn next_back(&mut self) -> Option<&'a [T]> { if self.finished { return None; } - if self.n == 0 { - self.finished = true; - return Some(self.v); - } - match self.v.iter().rposition(|x| (self.pred)(x)) { None => { self.finished = true; @@ -220,21 +192,42 @@ impl<'a, T> Iterator<&'a [T]> for RevSplits<'a, T> { Some(idx) => { let ret = Some(self.v.slice(idx + 1, self.v.len())); self.v = self.v.slice(0, idx); - self.n -= 1; ret } } } +} + +/// An iterator over the slices of a vector separated by elements that +/// match a predicate function, splitting at most a fixed number of times. +pub struct SplitsN<'a, T> { + iter: Splits<'a, T>, + count: uint, + invert: bool +} + +impl<'a, T> Iterator<&'a [T]> for SplitsN<'a, T> { + #[inline] + fn next(&mut self) -> Option<&'a [T]> { + if self.count == 0 { + if self.iter.finished { + None + } else { + self.iter.finished = true; + Some(self.iter.v) + } + } else { + self.count -= 1; + if self.invert { self.iter.next_back() } else { self.iter.next() } + } + } #[inline] fn size_hint(&self) -> (uint, Option) { - if self.finished { - return (0, Some(0)) - } - match (self.v.len(), self.n) { - (0,_) => (1, Some(1)), - (_,0) => (1, Some(1)), - (l,n) => (1, cmp::min(l,n).checked_add(&1u)) + if self.iter.finished { + (0, Some(0)) + } else { + (1, Some(cmp::min(self.count, self.iter.v.len()) + 1)) } } } @@ -747,18 +740,18 @@ pub trait ImmutableVector<'a, T> { /// separated by elements that match `pred`, limited to splitting /// at most `n` times. The matched element is not contained in /// the subslices. - fn splitn(self, n: uint, pred: |&T|: 'a -> bool) -> Splits<'a, T>; + fn splitn(self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<'a, T>; /// Returns an iterator over the subslices of the vector which are /// separated by elements that match `pred`. This starts at the /// end of the vector and works backwards. The matched element is /// not contained in the subslices. - fn rsplit(self, pred: |&T|: 'a -> bool) -> RevSplits<'a, T>; + fn rsplit(self, pred: |&T|: 'a -> bool) -> Rev>; /// Returns an iterator over the subslices of the vector which are /// separated by elements that match `pred` limited to splitting /// at most `n` times. This starts at the end of the vector and /// works backwards. The matched element is not contained in the /// subslices. - fn rsplitn(self, n: uint, pred: |&T|: 'a -> bool) -> RevSplits<'a, T>; + fn rsplitn(self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<'a, T>; /** * Returns an iterator over all contiguous windows of length @@ -936,31 +929,33 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { #[inline] fn split(self, pred: |&T|: 'a -> bool) -> Splits<'a, T> { - self.splitn(uint::MAX, pred) - } - - #[inline] - fn splitn(self, n: uint, pred: |&T|: 'a -> bool) -> Splits<'a, T> { Splits { v: self, - n: n, pred: pred, finished: false } } #[inline] - fn rsplit(self, pred: |&T|: 'a -> bool) -> RevSplits<'a, T> { - self.rsplitn(uint::MAX, pred) + fn splitn(self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<'a, T> { + SplitsN { + iter: self.split(pred), + count: n, + invert: false + } } #[inline] - fn rsplitn(self, n: uint, pred: |&T|: 'a -> bool) -> RevSplits<'a, T> { - RevSplits { - v: self, - n: n, - pred: pred, - finished: false + fn rsplit(self, pred: |&T|: 'a -> bool) -> Rev> { + self.split(pred).rev() + } + + #[inline] + fn rsplitn(self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<'a, T> { + SplitsN { + iter: self.split(pred), + count: n, + invert: true } } From 03609e5a5e8bdca9452327d44e25407ce888d0bb Mon Sep 17 00:00:00 2001 From: Jonathan S Date: Sun, 20 Apr 2014 23:59:12 -0500 Subject: [PATCH 2/2] Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap) This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also deprecates related functions like rsplit, rev_components, and rev_str_components. In every case, these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this more concrete, a translation table for all functional changes necessary follows: * container.rev_iter() -> container.iter().rev() * container.mut_rev_iter() -> container.mut_iter().rev() * container.move_rev_iter() -> container.move_iter().rev() * sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev() * path.rev_components() -> path.components().rev() * path.rev_str_components() -> path.str_components().rev() In terms of the type system, this change also deprecates any specialized reversed iterator types (except in treemap), opting instead to use Rev directly if any type annotations are needed. However, since methods directly returning reversed iterators are now discouraged, the need for such annotations should be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in the original reversed name and surround it with Rev<>: * RevComponents<'a> -> Rev> * RevStrComponents<'a> -> Rev> * RevItems<'a, T> -> Rev> * etc. The reasoning behind this change is that it makes the standard API much simpler without reducing readability, performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries (all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft goes away. [breaking-change] --- src/doc/guide-container.md | 14 ++-- src/libcollections/bitv.rs | 1 + src/libcollections/dlist.rs | 25 +++--- src/libcollections/priority_queue.rs | 4 +- src/libcollections/ringbuf.rs | 22 +++--- src/libcollections/smallintmap.rs | 23 +++--- src/libcollections/trie.rs | 2 +- src/libnum/bigint.rs | 6 +- src/librustc/middle/liveness.rs | 2 +- src/librustc/middle/typeck/check/vtable.rs | 4 +- src/librustdoc/lib.rs | 2 +- src/libserialize/json.rs | 4 +- src/libstd/path/posix.rs | 22 +++--- src/libstd/path/windows.rs | 20 +++-- src/libstd/slice.rs | 43 +++++----- src/libstd/str.rs | 92 +++++++++++----------- src/libsyntax/attr.rs | 5 +- 17 files changed, 152 insertions(+), 139 deletions(-) diff --git a/src/doc/guide-container.md b/src/doc/guide-container.md index bb2f3c79567..4fb75111f3b 100644 --- a/src/doc/guide-container.md +++ b/src/doc/guide-container.md @@ -120,12 +120,13 @@ differently. Containers implement iteration over the contained elements by returning an iterator object. For example, vector slices several iterators available: -* `iter()` and `rev_iter()`, for immutable references to the elements -* `mut_iter()` and `mut_rev_iter()`, for mutable references to the elements -* `move_iter()` and `move_rev_iter()`, to move the elements out by-value +* `iter()` for immutable references to the elements +* `mut_iter()` for mutable references to the elements +* `move_iter()` to move the elements out by-value A typical mutable container will implement at least `iter()`, `mut_iter()` and -`move_iter()` along with the reverse variants if it maintains an order. +`move_iter()`. If it maintains an order, the returned iterators will be +`DoubleEndedIterator`s, which are described below. ### Freezing @@ -265,7 +266,7 @@ Iterators offer generic conversion to containers with the `collect` adaptor: ~~~ let xs = [0, 1, 1, 2, 3, 5, 8]; -let ys = xs.rev_iter().skip(1).map(|&x| x * 2).collect::<~[int]>(); +let ys = xs.iter().rev().skip(1).map(|&x| x * 2).collect::<~[int]>(); assert_eq!(ys, ~[10, 6, 4, 2, 2, 0]); ~~~ @@ -358,9 +359,6 @@ for &x in it.rev() { } ~~~ -The `rev_iter` and `mut_rev_iter` methods on vectors just return an inverted -version of the standard immutable and mutable vector iterators. - The `chain`, `map`, `filter`, `filter_map` and `inspect` adaptors are `DoubleEndedIterator` implementations if the underlying iterators are. diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs index 13180cdfa5b..424eb54d8da 100644 --- a/src/libcollections/bitv.rs +++ b/src/libcollections/bitv.rs @@ -425,6 +425,7 @@ impl Bitv { } #[inline] + #[deprecated = "replaced by .iter().rev()"] pub fn rev_iter<'a>(&'a self) -> Rev> { self.iter().rev() } diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs index eb5f2169707..df1bc28508c 100644 --- a/src/libcollections/dlist.rs +++ b/src/libcollections/dlist.rs @@ -1,4 +1,4 @@ -// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -370,8 +370,8 @@ impl DList { Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail} } - /// Provide a reverse iterator #[inline] + #[deprecated = "replaced by .iter().rev()"] pub fn rev_iter<'a>(&'a self) -> Rev> { self.iter().rev() } @@ -390,8 +390,9 @@ impl DList { list: self } } - /// Provide a reverse iterator with mutable references + #[inline] + #[deprecated = "replaced by .mut_iter().rev()"] pub fn mut_rev_iter<'a>(&'a mut self) -> Rev> { self.mut_iter().rev() } @@ -403,8 +404,8 @@ impl DList { MoveItems{list: self} } - /// Consume the list into an iterator yielding elements by value, in reverse #[inline] + #[deprecated = "replaced by .move_iter().rev()"] pub fn move_rev_iter(self) -> Rev> { self.move_iter().rev() } @@ -849,13 +850,13 @@ mod tests { #[test] fn test_rev_iter() { let m = generate_test(); - for (i, elt) in m.rev_iter().enumerate() { + for (i, elt) in m.iter().rev().enumerate() { assert_eq!((6 - i) as int, *elt); } let mut n = DList::new(); - assert_eq!(n.rev_iter().next(), None); + assert_eq!(n.iter().rev().next(), None); n.push_front(4); - let mut it = n.rev_iter(); + let mut it = n.iter().rev(); assert_eq!(it.size_hint(), (1, Some(1))); assert_eq!(it.next().unwrap(), &4); assert_eq!(it.size_hint(), (0, Some(0))); @@ -958,13 +959,13 @@ mod tests { #[test] fn test_mut_rev_iter() { let mut m = generate_test(); - for (i, elt) in m.mut_rev_iter().enumerate() { + for (i, elt) in m.mut_iter().rev().enumerate() { assert_eq!((6-i) as int, *elt); } let mut n = DList::new(); - assert!(n.mut_rev_iter().next().is_none()); + assert!(n.mut_iter().rev().next().is_none()); n.push_front(4); - let mut it = n.mut_rev_iter(); + let mut it = n.mut_iter().rev(); assert!(it.next().is_some()); assert!(it.next().is_none()); } @@ -1164,7 +1165,7 @@ mod tests { let v = &[0, ..128]; let m: DList = v.iter().map(|&x|x).collect(); b.iter(|| { - assert!(m.rev_iter().len() == 128); + assert!(m.iter().rev().len() == 128); }) } #[bench] @@ -1172,7 +1173,7 @@ mod tests { let v = &[0, ..128]; let mut m: DList = v.iter().map(|&x|x).collect(); b.iter(|| { - assert!(m.mut_rev_iter().len() == 128); + assert!(m.mut_iter().rev().len() == 128); }) } } diff --git a/src/libcollections/priority_queue.rs b/src/libcollections/priority_queue.rs index 2ebbf65b9e5..dd1db85fc20 100644 --- a/src/libcollections/priority_queue.rs +++ b/src/libcollections/priority_queue.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -382,7 +382,7 @@ mod tests { fn test_from_iter() { let xs = vec!(9u, 8, 7, 6, 5, 4, 3, 2, 1); - let mut q: PriorityQueue = xs.as_slice().rev_iter().map(|&x| x).collect(); + let mut q: PriorityQueue = xs.as_slice().iter().rev().map(|&x| x).collect(); for &x in xs.iter() { assert_eq!(q.pop(), x); diff --git a/src/libcollections/ringbuf.rs b/src/libcollections/ringbuf.rs index 9204a9ca400..12e4fa8b51f 100644 --- a/src/libcollections/ringbuf.rs +++ b/src/libcollections/ringbuf.rs @@ -1,4 +1,4 @@ -// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -190,7 +190,7 @@ impl RingBuf { Items{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts.as_slice()} } - /// Back-to-front iterator. + #[deprecated = "replaced by .iter().rev()"] pub fn rev_iter<'a>(&'a self) -> Rev> { self.iter().rev() } @@ -221,7 +221,7 @@ impl RingBuf { } } - /// Back-to-front iterator which returns mutable values. + #[deprecated = "replaced by .mut_iter().rev()"] pub fn mut_rev_iter<'a>(&'a mut self) -> Rev> { self.mut_iter().rev() } @@ -702,23 +702,23 @@ mod tests { #[test] fn test_rev_iter() { let mut d = RingBuf::new(); - assert_eq!(d.rev_iter().next(), None); + assert_eq!(d.iter().rev().next(), None); for i in range(0, 5) { d.push_back(i); } - assert_eq!(d.rev_iter().collect::>().as_slice(), &[&4,&3,&2,&1,&0]); + assert_eq!(d.iter().rev().collect::>().as_slice(), &[&4,&3,&2,&1,&0]); for i in range(6, 9) { d.push_front(i); } - assert_eq!(d.rev_iter().collect::>().as_slice(), &[&4,&3,&2,&1,&0,&6,&7,&8]); + assert_eq!(d.iter().rev().collect::>().as_slice(), &[&4,&3,&2,&1,&0,&6,&7,&8]); } #[test] fn test_mut_rev_iter_wrap() { let mut d = RingBuf::with_capacity(3); - assert!(d.mut_rev_iter().next().is_none()); + assert!(d.mut_iter().rev().next().is_none()); d.push_back(1); d.push_back(2); @@ -726,7 +726,7 @@ mod tests { assert_eq!(d.pop_front(), Some(1)); d.push_back(4); - assert_eq!(d.mut_rev_iter().map(|x| *x).collect::>(), + assert_eq!(d.mut_iter().rev().map(|x| *x).collect::>(), vec!(4, 3, 2)); } @@ -756,19 +756,19 @@ mod tests { #[test] fn test_mut_rev_iter() { let mut d = RingBuf::new(); - assert!(d.mut_rev_iter().next().is_none()); + assert!(d.mut_iter().rev().next().is_none()); for i in range(0u, 3) { d.push_front(i); } - for (i, elt) in d.mut_rev_iter().enumerate() { + for (i, elt) in d.mut_iter().rev().enumerate() { assert_eq!(*elt, i); *elt = i; } { - let mut it = d.mut_rev_iter(); + let mut it = d.mut_iter().rev(); assert_eq!(*it.next().unwrap(), 0); assert_eq!(*it.next().unwrap(), 1); assert_eq!(*it.next().unwrap(), 2); diff --git a/src/libcollections/smallintmap.rs b/src/libcollections/smallintmap.rs index 6d7100aa404..5bfe04c7e51 100644 --- a/src/libcollections/smallintmap.rs +++ b/src/libcollections/smallintmap.rs @@ -1,4 +1,4 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -142,16 +142,13 @@ impl SmallIntMap { } } - /// An iterator visiting all key-value pairs in descending order by the keys. - /// Iterator element type is (uint, &'r V) - pub fn rev_iter<'r>(&'r self) -> RevEntries<'r, V> { + #[deprecated = "replaced by .iter().rev()"] + pub fn rev_iter<'r>(&'r self) -> Rev> { self.iter().rev() } - /// An iterator visiting all key-value pairs in descending order by the keys, - /// with mutable references to the values - /// Iterator element type is (uint, &'r mut V) - pub fn mut_rev_iter<'r>(&'r mut self) -> RevMutEntries <'r, V> { + #[deprecated = "replaced by .mut_iter().rev()"] + pub fn mut_rev_iter<'r>(&'r mut self) -> Rev> { self.mut_iter().rev() } @@ -246,6 +243,7 @@ pub struct Entries<'a, T> { iterator!(impl Entries -> (uint, &'a T), get_ref) double_ended_iterator!(impl Entries -> (uint, &'a T), get_ref) +#[deprecated = "replaced by Rev>"] pub type RevEntries<'a, T> = Rev>; pub struct MutEntries<'a, T> { @@ -256,6 +254,7 @@ pub struct MutEntries<'a, T> { iterator!(impl MutEntries -> (uint, &'a mut T), get_mut_ref) double_ended_iterator!(impl MutEntries -> (uint, &'a mut T), get_mut_ref) +#[deprecated = "replaced by Rev"] pub type RevMutEntries<'a, T> = Rev>; #[cfg(test)] @@ -387,9 +386,9 @@ mod test_map { assert!(m.insert(10, 11)); assert_eq!(m.iter().size_hint(), (0, Some(11))); - assert_eq!(m.rev_iter().size_hint(), (0, Some(11))); + assert_eq!(m.iter().rev().size_hint(), (0, Some(11))); assert_eq!(m.mut_iter().size_hint(), (0, Some(11))); - assert_eq!(m.mut_rev_iter().size_hint(), (0, Some(11))); + assert_eq!(m.mut_iter().rev().size_hint(), (0, Some(11))); } #[test] @@ -425,7 +424,7 @@ mod test_map { assert!(m.insert(6, 10)); assert!(m.insert(10, 11)); - let mut it = m.rev_iter(); + let mut it = m.iter().rev(); assert_eq!(it.next().unwrap(), (10, &11)); assert_eq!(it.next().unwrap(), (6, &10)); assert_eq!(it.next().unwrap(), (3, &5)); @@ -444,7 +443,7 @@ mod test_map { assert!(m.insert(6, 10)); assert!(m.insert(10, 11)); - for (k, v) in m.mut_rev_iter() { + for (k, v) in m.mut_iter().rev() { *v += k as int; } diff --git a/src/libcollections/trie.rs b/src/libcollections/trie.rs index e040bb0a9e5..3a0d81070fe 100644 --- a/src/libcollections/trie.rs +++ b/src/libcollections/trie.rs @@ -395,7 +395,7 @@ impl TrieNode { impl TrieNode { fn each_reverse<'a>(&'a self, f: |&uint, &'a T| -> bool) -> bool { - for elt in self.children.rev_iter() { + for elt in self.children.iter().rev() { match *elt { Internal(ref x) => if !x.each_reverse(|i,t| f(i,t)) { return false }, External(k, ref v) => if !f(&k, v) { return false }, diff --git a/src/libnum/bigint.rs b/src/libnum/bigint.rs index 45cd7ee4701..083fb1e794b 100644 --- a/src/libnum/bigint.rs +++ b/src/libnum/bigint.rs @@ -439,7 +439,7 @@ impl Integer for BigUint { let bn = *b.data.last().unwrap(); let mut d = Vec::with_capacity(an.len()); let mut carry = 0; - for elt in an.rev_iter() { + for elt in an.iter().rev() { let ai = BigDigit::to_uint(carry, *elt); let di = ai / (bn as uint); assert!(di < BigDigit::base); @@ -668,7 +668,7 @@ impl ToStrRadix for BigUint { fn fill_concat(v: &[BigDigit], radix: uint, l: uint) -> ~str { if v.is_empty() { return "0".to_owned() } let mut s = StrBuf::with_capacity(v.len() * l); - for n in v.rev_iter() { + for n in v.iter().rev() { let ss = (*n as uint).to_str_radix(radix); s.push_str("0".repeat(l - ss.len())); s.push_str(ss); @@ -2187,7 +2187,7 @@ mod bigint_tests { fn test_cmp() { let vs = [ &[2 as BigDigit], &[1, 1], &[2, 1], &[1, 1, 1] ]; let mut nums = Vec::new(); - for s in vs.rev_iter() { + for s in vs.iter().rev() { nums.push(BigInt::from_slice(Minus, *s)); } nums.push(Zero::zero()); diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index 4be9992367d..070e45d92c4 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -927,7 +927,7 @@ impl<'a> Liveness<'a> { fn propagate_through_exprs(&mut self, exprs: &[@Expr], succ: LiveNode) -> LiveNode { - exprs.rev_iter().fold(succ, |succ, expr| { + exprs.iter().rev().fold(succ, |succ, expr| { self.propagate_through_expr(*expr, succ) }) } diff --git a/src/librustc/middle/typeck/check/vtable.rs b/src/librustc/middle/typeck/check/vtable.rs index bb57fa2646b..8b3794aff72 100644 --- a/src/librustc/middle/typeck/check/vtable.rs +++ b/src/librustc/middle/typeck/check/vtable.rs @@ -1,4 +1,4 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -94,7 +94,7 @@ fn lookup_vtables(vcx: &VtableContext, let mut result: Vec = substs.tps.iter() .rev() - .zip(type_param_defs.rev_iter()) + .zip(type_param_defs.iter().rev()) .map(|(ty, def)| lookup_vtables_for_param(vcx, span, Some(substs), &*def.bounds, *ty, is_early)) diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index d45a48a75e9..f4c21519169 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -304,7 +304,7 @@ fn rust_input(cratefile: &str, matches: &getopts::Matches) -> Output { None => {} } if default_passes { - for name in DEFAULT_PASSES.rev_iter() { + for name in DEFAULT_PASSES.iter().rev() { passes.unshift(name.to_owned()); } } diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index 93a5e2ec4c8..2d23bebb21b 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -1446,7 +1446,7 @@ impl ::Decoder for Decoder { }; match o.pop(&"fields".to_owned()) { Some(List(l)) => { - for field in l.move_rev_iter() { + for field in l.move_iter().rev() { self.stack.push(field.clone()); } }, @@ -1557,7 +1557,7 @@ impl ::Decoder for Decoder { debug!("read_seq()"); let list = try!(expect!(self.pop(), List)); let len = list.len(); - for v in list.move_rev_iter() { + for v in list.move_iter().rev() { self.stack.push(v); } f(self, len) diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index eceeb73a015..d69e9b448be 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -29,12 +29,14 @@ use super::{BytesContainer, GenericPath, GenericPathUnsafe}; /// Iterator that yields successive components of a Path as &[u8] pub type Components<'a> = Splits<'a, u8>; /// Iterator that yields components of a Path in reverse as &[u8] +#[deprecated = "replaced by Rev>"] pub type RevComponents<'a> = Rev>; /// Iterator that yields successive components of a Path as Option<&str> pub type StrComponents<'a> = Map<'a, &'a [u8], Option<&'a str>, Components<'a>>; /// Iterator that yields components of a Path in reverse as Option<&str> +#[deprecated = "replaced by Rev>"] pub type RevStrComponents<'a> = Rev>; /// Represents a POSIX file path @@ -307,8 +309,8 @@ impl GenericPath for Path { fn ends_with_path(&self, child: &Path) -> bool { if !child.is_relative() { return false; } - let mut selfit = self.rev_components(); - let mut childit = child.rev_components(); + let mut selfit = self.components().rev(); + let mut childit = child.components().rev(); loop { match (selfit.next(), childit.next()) { (Some(a), Some(b)) => if a != b { return false; }, @@ -395,7 +397,8 @@ impl Path { /// Returns an iterator that yields each component of the path in reverse. /// See components() for details. - pub fn rev_components<'a>(&'a self) -> RevComponents<'a> { + #[deprecated = "replaced by .components().rev()"] + pub fn rev_components<'a>(&'a self) -> Rev> { self.components().rev() } @@ -407,7 +410,8 @@ impl Path { /// Returns an iterator that yields each component of the path in reverse as Option<&str>. /// See components() for details. - pub fn rev_str_components<'a>(&'a self) -> RevStrComponents<'a> { + #[deprecated = "replaced by .str_components().rev()"] + pub fn rev_str_components<'a>(&'a self) -> Rev> { self.str_components().rev() } } @@ -1183,7 +1187,7 @@ mod tests { let exps = exp.iter().map(|x| x.as_bytes()).collect::>(); assert!(comps == exps, "components: Expected {:?}, found {:?}", comps, exps); - let comps = path.rev_components().collect::>(); + let comps = path.components().rev().collect::>(); let exps = exps.move_iter().rev().collect::>(); assert!(comps == exps, "rev_components: Expected {:?}, found {:?}", comps, exps); @@ -1195,8 +1199,8 @@ mod tests { let comps = path.components().collect::>(); let exp: &[&[u8]] = [$(b!($($exp),*)),*]; assert_eq!(comps.as_slice(), exp); - let comps = path.rev_components().collect::>(); - let exp = exp.rev_iter().map(|&x|x).collect::>(); + let comps = path.components().rev().collect::>(); + let exp = exp.iter().rev().map(|&x|x).collect::>(); assert_eq!(comps, exp) } ) @@ -1227,8 +1231,8 @@ mod tests { let comps = path.str_components().collect::>>(); let exp: &[Option<&str>] = $exp; assert_eq!(comps.as_slice(), exp); - let comps = path.rev_str_components().collect::>>(); - let exp = exp.rev_iter().map(|&x|x).collect::>>(); + let comps = path.str_components().rev().collect::>>(); + let exp = exp.iter().rev().map(|&x|x).collect::>>(); assert_eq!(comps, exp); } ) diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index 679075fe6ca..758a76167cd 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -37,12 +37,14 @@ pub type StrComponents<'a> = Map<'a, &'a str, Option<&'a str>, /// /// Each component is yielded as Option<&str> for compatibility with PosixPath, but /// every component in WindowsPath is guaranteed to be Some. +#[deprecated = "replaced by Rev>"] pub type RevStrComponents<'a> = Rev>; /// Iterator that yields successive components of a Path as &[u8] pub type Components<'a> = Map<'a, Option<&'a str>, &'a [u8], StrComponents<'a>>; /// Iterator that yields components of a Path in reverse as &[u8] +#[deprecated = "replaced by Rev>"] pub type RevComponents<'a> = Rev>; /// Represents a Windows path @@ -631,7 +633,8 @@ impl Path { /// Returns an iterator that yields each component of the path in reverse as an Option<&str> /// See str_components() for details. - pub fn rev_str_components<'a>(&'a self) -> RevStrComponents<'a> { + #[deprecated = "replaced by .str_components().rev()"] + pub fn rev_str_components<'a>(&'a self) -> Rev> { self.str_components().rev() } @@ -647,7 +650,8 @@ impl Path { /// Returns an iterator that yields each component of the path in reverse as a &[u8]. /// See str_components() for details. - pub fn rev_components<'a>(&'a self) -> RevComponents<'a> { + #[deprecated = "replaced by .components().rev()"] + pub fn rev_components<'a>(&'a self) -> Rev> { self.components().rev() } @@ -2233,9 +2237,9 @@ mod tests { .collect::>(); let exp: &[&str] = $exp; assert_eq!(comps.as_slice(), exp); - let comps = path.rev_str_components().map(|x|x.unwrap()) + let comps = path.str_components().rev().map(|x|x.unwrap()) .collect::>(); - let exp = exp.rev_iter().map(|&x|x).collect::>(); + let exp = exp.iter().rev().map(|&x|x).collect::>(); assert_eq!(comps, exp); } ); @@ -2245,9 +2249,9 @@ mod tests { let comps = path.str_components().map(|x|x.unwrap()).collect::>(); let exp: &[&str] = $exp; assert_eq!(comps.as_slice(), exp); - let comps = path.rev_str_components().map(|x|x.unwrap()) + let comps = path.str_components().rev().map(|x|x.unwrap()) .collect::>(); - let exp = exp.rev_iter().map(|&x|x).collect::>(); + let exp = exp.iter().rev().map(|&x|x).collect::>(); assert_eq!(comps, exp); } ) @@ -2302,8 +2306,8 @@ mod tests { let comps = path.components().collect::>(); let exp: &[&[u8]] = $exp; assert_eq!(comps.as_slice(), exp); - let comps = path.rev_components().collect::>(); - let exp = exp.rev_iter().map(|&x|x).collect::>(); + let comps = path.components().rev().collect::>(); + let exp = exp.iter().rev().map(|&x|x).collect::>(); assert_eq!(comps, exp); } ) diff --git a/src/libstd/slice.rs b/src/libstd/slice.rs index 180a142dea1..64f6b59be24 100644 --- a/src/libstd/slice.rs +++ b/src/libstd/slice.rs @@ -81,8 +81,6 @@ for &x in numbers.iter() { } ``` -* `.rev_iter()` returns an iterator with the same values as `.iter()`, - but going in the reverse order, starting with the back element. * `.mut_iter()` returns an iterator that allows modifying each value. * `.move_iter()` converts an owned vector into an iterator that moves out a value from the vector each iteration. @@ -731,7 +729,8 @@ pub trait ImmutableVector<'a, T> { /// Returns an iterator over the vector fn iter(self) -> Items<'a, T>; /// Returns a reversed iterator over a vector - fn rev_iter(self) -> RevItems<'a, T>; + #[deprecated = "replaced by .iter().rev()"] + fn rev_iter(self) -> Rev>; /// Returns an iterator over the subslices of the vector which are /// separated by elements that match `pred`. The matched element /// is not contained in the subslices. @@ -745,6 +744,7 @@ pub trait ImmutableVector<'a, T> { /// separated by elements that match `pred`. This starts at the /// end of the vector and works backwards. The matched element is /// not contained in the subslices. + #[deprecated = "replaced by .split(pred).rev()"] fn rsplit(self, pred: |&T|: 'a -> bool) -> Rev>; /// Returns an iterator over the subslices of the vector which are /// separated by elements that match `pred` limited to splitting @@ -923,7 +923,8 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { } #[inline] - fn rev_iter(self) -> RevItems<'a, T> { + #[deprecated = "replaced by .iter().rev()"] + fn rev_iter(self) -> Rev> { self.iter().rev() } @@ -946,6 +947,7 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { } #[inline] + #[deprecated = "replaced by .split(pred).rev()"] fn rsplit(self, pred: |&T|: 'a -> bool) -> Rev> { self.split(pred).rev() } @@ -1167,7 +1169,8 @@ pub trait OwnedVector { fn move_iter(self) -> MoveItems; /// Creates a consuming iterator that moves out of the vector in /// reverse order. - fn move_rev_iter(self) -> RevMoveItems; + #[deprecated = "replaced by .move_iter().rev()"] + fn move_rev_iter(self) -> Rev>; /** * Partitions the vector into two vectors `(A,B)`, where all @@ -1187,7 +1190,8 @@ impl OwnedVector for ~[T] { } #[inline] - fn move_rev_iter(self) -> RevMoveItems { + #[deprecated = "replaced by .move_iter().rev()"] + fn move_rev_iter(self) -> Rev> { self.move_iter().rev() } @@ -1442,7 +1446,8 @@ pub trait MutableVector<'a, T> { fn mut_last(self) -> Option<&'a mut T>; /// Returns a reversed iterator that allows modifying each value - fn mut_rev_iter(self) -> RevMutItems<'a, T>; + #[deprecated = "replaced by .mut_iter().rev()"] + fn mut_rev_iter(self) -> Rev>; /// Returns an iterator over the mutable subslices of the vector /// which are separated by elements that match `pred`. The @@ -1714,7 +1719,8 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] { } #[inline] - fn mut_rev_iter(self) -> RevMutItems<'a, T> { + #[deprecated = "replaced by .mut_iter().rev()"] + fn mut_rev_iter(self) -> Rev> { self.mut_iter().rev() } @@ -2129,6 +2135,7 @@ impl<'a, T> RandomAccessIterator<&'a T> for Items<'a, T> { } iterator!{struct Items -> *T, &'a T} +#[deprecated = "replaced by Rev>"] pub type RevItems<'a, T> = Rev>; impl<'a, T> ExactSize<&'a T> for Items<'a, T> {} @@ -2139,6 +2146,7 @@ impl<'a, T> Clone for Items<'a, T> { } iterator!{struct MutItems -> *mut T, &'a mut T} +#[deprecated = "replaced by Rev>"] pub type RevMutItems<'a, T> = Rev>; /// An iterator over the subslices of the vector which are separated @@ -2299,6 +2307,7 @@ impl Drop for MoveItems { } /// An iterator that moves out of a vector in reverse order. +#[deprecated = "replaced by Rev>"] pub type RevMoveItems = Rev>; impl FromIterator for ~[A] { @@ -3228,9 +3237,7 @@ mod tests { use iter::*; let mut xs = [1, 2, 5, 10, 11]; assert_eq!(xs.iter().size_hint(), (5, Some(5))); - assert_eq!(xs.rev_iter().size_hint(), (5, Some(5))); assert_eq!(xs.mut_iter().size_hint(), (5, Some(5))); - assert_eq!(xs.mut_rev_iter().size_hint(), (5, Some(5))); } #[test] @@ -3261,7 +3268,7 @@ mod tests { let xs = [1, 2, 5, 10, 11]; let ys = [11, 10, 5, 2, 1]; let mut i = 0; - for &x in xs.rev_iter() { + for &x in xs.iter().rev() { assert_eq!(x, ys[i]); i += 1; } @@ -3272,7 +3279,7 @@ mod tests { fn test_mut_rev_iterator() { use iter::*; let mut xs = [1u, 2, 3, 4, 5]; - for (i,x) in xs.mut_rev_iter().enumerate() { + for (i,x) in xs.mut_iter().rev().enumerate() { *x += i; } assert!(xs == [5, 5, 5, 5, 5]) @@ -3289,7 +3296,7 @@ mod tests { fn test_move_rev_iterator() { use iter::*; let xs = ~[1u,2,3,4,5]; - assert_eq!(xs.move_rev_iter().fold(0, |a: uint, b: uint| 10*a + b), 54321); + assert_eq!(xs.move_iter().rev().fold(0, |a: uint, b: uint| 10*a + b), 54321); } #[test] @@ -3330,17 +3337,17 @@ mod tests { fn test_rsplitator() { let xs = &[1i,2,3,4,5]; - assert_eq!(xs.rsplit(|x| *x % 2 == 0).collect::<~[&[int]]>(), + assert_eq!(xs.split(|x| *x % 2 == 0).rev().collect::<~[&[int]]>(), ~[&[5], &[3], &[1]]); - assert_eq!(xs.rsplit(|x| *x == 1).collect::<~[&[int]]>(), + assert_eq!(xs.split(|x| *x == 1).rev().collect::<~[&[int]]>(), ~[&[2,3,4,5], &[]]); - assert_eq!(xs.rsplit(|x| *x == 5).collect::<~[&[int]]>(), + assert_eq!(xs.split(|x| *x == 5).rev().collect::<~[&[int]]>(), ~[&[], &[1,2,3,4]]); - assert_eq!(xs.rsplit(|x| *x == 10).collect::<~[&[int]]>(), + assert_eq!(xs.split(|x| *x == 10).rev().collect::<~[&[int]]>(), ~[&[1,2,3,4,5]]); let xs: &[int] = &[]; - assert_eq!(xs.rsplit(|x| *x == 5).collect::<~[&[int]]>(), ~[&[]]); + assert_eq!(xs.split(|x| *x == 5).rev().collect::<~[&[int]]>(), ~[&[]]); } #[test] diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 99f1c66e702..bc7943dd777 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -343,12 +343,10 @@ impl<'a> DoubleEndedIterator<(uint, char)> for CharOffsets<'a> { } } -/// External iterator for a string's characters in reverse order. -/// Use with the `std::iter` module. +#[deprecated = "replaced by Rev>"] pub type RevChars<'a> = Rev>; -/// External iterator for a string's characters and their byte offsets in reverse order. -/// Use with the `std::iter` module. +#[deprecated = "replaced by Rev>"] pub type RevCharOffsets<'a> = Rev>; /// External iterator for a string's bytes. @@ -356,8 +354,7 @@ pub type RevCharOffsets<'a> = Rev>; pub type Bytes<'a> = Map<'a, &'a u8, u8, slice::Items<'a, u8>>; -/// External iterator for a string's bytes in reverse order. -/// Use with the `std::iter` module. +#[deprecated = "replaced by Rev>"] pub type RevBytes<'a> = Rev>; /// An iterator over the substrings of a string, separated by `sep`. @@ -372,8 +369,7 @@ pub struct CharSplits<'a, Sep> { finished: bool, } -/// An iterator over the substrings of a string, separated by `sep`, -/// starting from the back of the string. +#[deprecated = "replaced by Rev>"] pub type RevCharSplits<'a, Sep> = Rev>; /// An iterator over the substrings of a string, separated by `sep`, @@ -462,7 +458,7 @@ for CharSplits<'a, Sep> { } } } else { - for (idx, ch) in self.string.char_indices_rev() { + for (idx, ch) in self.string.char_indices().rev() { if self.sep.matches(ch) { next_split = Some((idx, self.string.char_range_at(idx).next)); break; @@ -1626,21 +1622,23 @@ pub trait StrSlice<'a> { /// ``` fn chars(&self) -> Chars<'a>; - /// An iterator over the characters of `self`, in reverse order. - fn chars_rev(&self) -> RevChars<'a>; + /// Do not use this - it is deprecated. + #[deprecated = "replaced by .chars().rev()"] + fn chars_rev(&self) -> Rev>; /// An iterator over the bytes of `self` fn bytes(&self) -> Bytes<'a>; - /// An iterator over the bytes of `self`, in reverse order - fn bytes_rev(&self) -> RevBytes<'a>; + /// Do not use this - it is deprecated. + #[deprecated = "replaced by .bytes().rev()"] + fn bytes_rev(&self) -> Rev>; /// An iterator over the characters of `self` and their byte offsets. fn char_indices(&self) -> CharOffsets<'a>; - /// An iterator over the characters of `self` and their byte offsets, - /// in reverse order. - fn char_indices_rev(&self) -> RevCharOffsets<'a>; + /// Do not use this - it is deprecated. + #[deprecated = "replaced by .char_indices().rev()"] + fn char_indices_rev(&self) -> Rev>; /// An iterator over substrings of `self`, separated by characters /// matched by `sep`. @@ -1691,25 +1689,21 @@ pub trait StrSlice<'a> { /// /// let v: ~[&str] = "A..B..".split_terminator('.').collect(); /// assert_eq!(v, ~["A", "", "B", ""]); + /// + /// let v: ~[&str] = "Mary had a little lamb".split(' ').rev().collect(); + /// assert_eq!(v, ~["lamb", "little", "a", "had", "Mary"]); + /// + /// let v: ~[&str] = "abc1def2ghi".split(|c: char| c.is_digit()).rev().collect(); + /// assert_eq!(v, ~["ghi", "def", "abc"]); + /// + /// let v: ~[&str] = "lionXXtigerXleopard".split('X').rev().collect(); + /// assert_eq!(v, ~["leopard", "tiger", "", "lion"]); /// ``` fn split_terminator(&self, sep: Sep) -> CharSplits<'a, Sep>; - /// An iterator over substrings of `self`, separated by characters - /// matched by `sep`, in reverse order. - /// - /// # Example - /// - /// ```rust - /// let v: ~[&str] = "Mary had a little lamb".rsplit(' ').collect(); - /// assert_eq!(v, ~["lamb", "little", "a", "had", "Mary"]); - /// - /// let v: ~[&str] = "abc1def2ghi".rsplit(|c: char| c.is_digit()).collect(); - /// assert_eq!(v, ~["ghi", "def", "abc"]); - /// - /// let v: ~[&str] = "lionXXtigerXleopard".rsplit('X').collect(); - /// assert_eq!(v, ~["leopard", "tiger", "", "lion"]); - /// ``` - fn rsplit(&self, sep: Sep) -> RevCharSplits<'a, Sep>; + /// Do not use this - it is deprecated. + #[deprecated = "replaced by .split(sep).rev()"] + fn rsplit(&self, sep: Sep) -> Rev>; /// An iterator over substrings of `self`, separated by characters /// matched by `sep`, starting from the end of the string. @@ -2281,7 +2275,8 @@ impl<'a> StrSlice<'a> for &'a str { } #[inline] - fn chars_rev(&self) -> RevChars<'a> { + #[deprecated = "replaced by .chars().rev()"] + fn chars_rev(&self) -> Rev> { self.chars().rev() } @@ -2291,7 +2286,8 @@ impl<'a> StrSlice<'a> for &'a str { } #[inline] - fn bytes_rev(&self) -> RevBytes<'a> { + #[deprecated = "replaced by .bytes().rev()"] + fn bytes_rev(&self) -> Rev> { self.bytes().rev() } @@ -2301,7 +2297,8 @@ impl<'a> StrSlice<'a> for &'a str { } #[inline] - fn char_indices_rev(&self) -> RevCharOffsets<'a> { + #[deprecated = "replaced by .char_indices().rev()"] + fn char_indices_rev(&self) -> Rev> { self.char_indices().rev() } @@ -2336,7 +2333,8 @@ impl<'a> StrSlice<'a> for &'a str { } #[inline] - fn rsplit(&self, sep: Sep) -> RevCharSplits<'a, Sep> { + #[deprecated = "replaced by .split(sep).rev()"] + fn rsplit(&self, sep: Sep) -> Rev> { self.split(sep).rev() } @@ -2656,7 +2654,7 @@ impl<'a> StrSlice<'a> for &'a str { if search.only_ascii() { self.bytes().rposition(|b| search.matches(b as char)) } else { - for (index, c) in self.char_indices_rev() { + for (index, c) in self.char_indices().rev() { if search.matches(c) { return Some(index); } } None @@ -3573,7 +3571,7 @@ mod tests { let s = "ศไทย中华Việt Nam".to_owned(); let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; let mut pos = s.len(); - for ch in v.rev_iter() { + for ch in v.iter().rev() { assert!(s.char_at_reverse(pos) == *ch); pos -= from_char(*ch).len(); } @@ -3673,7 +3671,7 @@ mod tests { let v = ~['m', 'a', 'N', ' ', 't', 'ệ','i','V','华','中','ย','ท','ไ','ศ']; let mut pos = 0; - let mut it = s.chars_rev(); + let mut it = s.chars().rev(); for c in it { assert_eq!(c, v[pos]); @@ -3716,7 +3714,7 @@ mod tests { ]; let mut pos = v.len(); - for b in s.bytes_rev() { + for b in s.bytes().rev() { pos -= 1; assert_eq!(b, v[pos]); } @@ -3748,7 +3746,7 @@ mod tests { let v = ['m', 'a', 'N', ' ', 't', 'ệ','i','V','华','中','ย','ท','ไ','ศ']; let mut pos = 0; - let mut it = s.char_indices_rev(); + let mut it = s.char_indices().rev(); for c in it { assert_eq!(c, (p[pos], v[pos])); @@ -3765,14 +3763,14 @@ mod tests { let split: ~[&str] = data.split(' ').collect(); assert_eq!( split, ~["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]); - let mut rsplit: ~[&str] = data.rsplit(' ').collect(); + let mut rsplit: ~[&str] = data.split(' ').rev().collect(); rsplit.reverse(); assert_eq!(rsplit, ~["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]); let split: ~[&str] = data.split(|c: char| c == ' ').collect(); assert_eq!( split, ~["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]); - let mut rsplit: ~[&str] = data.rsplit(|c: char| c == ' ').collect(); + let mut rsplit: ~[&str] = data.split(|c: char| c == ' ').rev().collect(); rsplit.reverse(); assert_eq!(rsplit, ~["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]); @@ -3780,14 +3778,14 @@ mod tests { let split: ~[&str] = data.split('ä').collect(); assert_eq!( split, ~["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]); - let mut rsplit: ~[&str] = data.rsplit('ä').collect(); + let mut rsplit: ~[&str] = data.split('ä').rev().collect(); rsplit.reverse(); assert_eq!(rsplit, ~["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]); let split: ~[&str] = data.split(|c: char| c == 'ä').collect(); assert_eq!( split, ~["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]); - let mut rsplit: ~[&str] = data.rsplit(|c: char| c == 'ä').collect(); + let mut rsplit: ~[&str] = data.split(|c: char| c == 'ä').rev().collect(); rsplit.reverse(); assert_eq!(rsplit, ~["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]); } @@ -4103,7 +4101,7 @@ mod bench { let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb"; let len = s.char_len(); - b.iter(|| assert_eq!(s.chars_rev().len(), len)); + b.iter(|| assert_eq!(s.chars().rev().len(), len)); } #[bench] @@ -4119,7 +4117,7 @@ mod bench { let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb"; let len = s.char_len(); - b.iter(|| assert_eq!(s.char_indices_rev().len(), len)); + b.iter(|| assert_eq!(s.char_indices().rev().len(), len)); } #[bench] diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 0c0d7bbb535..38cdf23b7b3 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -1,4 +1,4 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -205,7 +205,8 @@ pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str) pub fn last_meta_item_value_str_by_name(items: &[@MetaItem], name: &str) -> Option { - items.rev_iter() + items.iter() + .rev() .find(|mi| mi.name().equiv(&name)) .and_then(|i| i.value_str()) }