From 03609e5a5e8bdca9452327d44e25407ce888d0bb Mon Sep 17 00:00:00 2001 From: Jonathan S Date: Sun, 20 Apr 2014 23:59:12 -0500 Subject: 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/libstd/path/posix.rs | 22 +++++++++++++--------- src/libstd/path/windows.rs | 20 ++++++++++++-------- 2 files changed, 25 insertions(+), 17 deletions(-) (limited to 'src/libstd/path') 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); } ) -- cgit 1.4.1-3-g733a5