diff options
| author | bors <bors@rust-lang.org> | 2014-12-08 02:32:31 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-12-08 02:32:31 +0000 |
| commit | 83a44c7fa676b4e5e546ce3d4624e585f9a1e899 (patch) | |
| tree | 36d7db1d2567d86816d4ac6a1ec86276974dbc65 /src/libstd/path | |
| parent | 8bca470c5acf13aa20022a2c462a89f72de721fc (diff) | |
| parent | 1fea900de7f11d665086141806246842c03b9fc5 (diff) | |
| download | rust-83a44c7fa676b4e5e546ce3d4624e585f9a1e899.tar.gz rust-83a44c7fa676b4e5e546ce3d4624e585f9a1e899.zip | |
auto merge of #19378 : japaric/rust/no-as-slice, r=alexcrichton
Now that we have an overloaded comparison (`==`) operator, and that `Vec`/`String` deref to `[T]`/`str` on method calls, many `as_slice()`/`as_mut_slice()`/`to_string()` calls have become redundant. This patch removes them. These were the most common patterns: - `assert_eq(test_output.as_slice(), "ground truth")` -> `assert_eq(test_output, "ground truth")` - `assert_eq(test_output, "ground truth".to_string())` -> `assert_eq(test_output, "ground truth")` - `vec.as_mut_slice().sort()` -> `vec.sort()` - `vec.as_slice().slice(from, to)` -> `vec.slice(from_to)` --- Note that e.g. `a_string.push_str(b_string.as_slice())` has been left untouched in this PR, since we first need to settle down whether we want to favor the `&*b_string` or the `b_string[]` notation. This is rebased on top of #19167 cc @alexcrichton @aturon
Diffstat (limited to 'src/libstd/path')
| -rw-r--r-- | src/libstd/path/mod.rs | 2 | ||||
| -rw-r--r-- | src/libstd/path/posix.rs | 38 | ||||
| -rw-r--r-- | src/libstd/path/windows.rs | 58 |
3 files changed, 49 insertions, 49 deletions
diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index 01b42395471..c7feff8705e 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -942,6 +942,6 @@ mod tests { let input = r"\foo\bar\baz"; let path: WindowsPath = WindowsPath::new(input.to_c_str()); - assert_eq!(path.as_str().unwrap(), input.as_slice()); + assert_eq!(path.as_str().unwrap(), input); } } diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index d6d27daf4ae..ba2c89bf1ce 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -132,7 +132,7 @@ impl GenericPathUnsafe for Path { unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) { let filename = filename.container_as_bytes(); match self.sepidx { - None if b".." == self.repr.as_slice() => { + None if b".." == self.repr => { let mut v = Vec::with_capacity(3 + filename.len()); v.push_all(dot_dot_static); v.push(SEP_BYTE); @@ -159,7 +159,7 @@ impl GenericPathUnsafe for Path { self.repr = Path::normalize(v.as_slice()); } } - self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE); + self.sepidx = self.repr.rposition_elem(&SEP_BYTE); } unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) { @@ -175,7 +175,7 @@ impl GenericPathUnsafe for Path { // FIXME: this is slow self.repr = Path::normalize(v.as_slice()); } - self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE); + self.sepidx = self.repr.rposition_elem(&SEP_BYTE); } } } @@ -192,7 +192,7 @@ impl GenericPath for Path { fn dirname<'a>(&'a self) -> &'a [u8] { match self.sepidx { - None if b".." == self.repr.as_slice() => self.repr.as_slice(), + None if b".." == self.repr => self.repr.as_slice(), None => dot_static, Some(0) => self.repr[..1], Some(idx) if self.repr[idx+1..] == b".." => self.repr.as_slice(), @@ -202,8 +202,8 @@ impl GenericPath for Path { fn filename<'a>(&'a self) -> Option<&'a [u8]> { match self.sepidx { - None if b"." == self.repr.as_slice() || - b".." == self.repr.as_slice() => None, + None if b"." == self.repr || + b".." == self.repr => None, None => Some(self.repr.as_slice()), Some(idx) if self.repr[idx+1..] == b".." => None, Some(0) if self.repr[1..].is_empty() => None, @@ -213,20 +213,20 @@ impl GenericPath for Path { fn pop(&mut self) -> bool { match self.sepidx { - None if b"." == self.repr.as_slice() => false, + None if b"." == self.repr => false, None => { self.repr = vec![b'.']; self.sepidx = None; true } - Some(0) if b"/" == self.repr.as_slice() => false, + Some(0) if b"/" == self.repr => false, Some(idx) => { if idx == 0 { self.repr.truncate(idx+1); } else { self.repr.truncate(idx); } - self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE); + self.sepidx = self.repr.rposition_elem(&SEP_BYTE); true } } @@ -251,7 +251,7 @@ impl GenericPath for Path { } else { let mut ita = self.components(); let mut itb = other.components(); - if b"." == self.repr.as_slice() { + if b"." == self.repr { return match itb.next() { None => true, Some(b) => b != b".." @@ -306,7 +306,7 @@ impl GenericPath for Path { } } } - Some(Path::new(comps.as_slice().connect_vec(&SEP_BYTE))) + Some(Path::new(comps.connect_vec(&SEP_BYTE))) } } @@ -407,7 +407,7 @@ impl Path { // None result means the byte vector didn't need normalizing fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<Vec<&'a [u8]>> { - if is_abs && v.as_slice().is_empty() { + if is_abs && v.is_empty() { return None; } let mut comps: Vec<&'a [u8]> = vec![]; @@ -496,8 +496,8 @@ mod tests { t!(s: Path::new("foo/../../.."), "../.."); t!(s: Path::new("foo/../../bar"), "../bar"); - assert_eq!(Path::new(b"foo/bar").into_vec().as_slice(), b"foo/bar"); - assert_eq!(Path::new(b"/foo/../../bar").into_vec().as_slice(), + assert_eq!(Path::new(b"foo/bar").into_vec(), b"foo/bar"); + assert_eq!(Path::new(b"/foo/../../bar").into_vec(), b"/bar"); let p = Path::new(b"foo/bar\x80"); @@ -537,7 +537,7 @@ mod tests { ($path:expr, $disp:ident, $exp:expr) => ( { let path = Path::new($path); - assert!(path.$disp().to_string().as_slice() == $exp); + assert!(path.$disp().to_string() == $exp); } ) ) @@ -580,9 +580,9 @@ mod tests { { let path = Path::new($path); let f = format!("{}", path.display()); - assert!(f.as_slice() == $exp); + assert!(f == $exp); let f = format!("{}", path.filename_display()); - assert!(f.as_slice() == $expf); + assert!(f == $expf); } ) ) @@ -1180,7 +1180,7 @@ mod tests { let path = Path::new($arg); let comps = path.components().collect::<Vec<&[u8]>>(); let exp: &[&[u8]] = &[$($exp),*]; - assert_eq!(comps.as_slice(), exp); + assert_eq!(comps, exp); let comps = path.components().rev().collect::<Vec<&[u8]>>(); let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>(); assert_eq!(comps, exp) @@ -1212,7 +1212,7 @@ mod tests { let path = Path::new($arg); let comps = path.str_components().collect::<Vec<Option<&str>>>(); let exp: &[Option<&str>] = &$exp; - assert_eq!(comps.as_slice(), exp); + assert_eq!(comps, exp); let comps = path.str_components().rev().collect::<Vec<Option<&str>>>(); let exp = exp.iter().rev().map(|&x|x).collect::<Vec<Option<&str>>>(); assert_eq!(comps, exp); diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index 08e318d32b9..b53e6b2a5e0 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -182,7 +182,7 @@ impl GenericPathUnsafe for Path { unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) { let filename = filename.container_as_str().unwrap(); match self.sepidx_or_prefix_len() { - None if ".." == self.repr.as_slice() => { + None if ".." == self.repr => { let mut s = String::with_capacity(3 + filename.len()); s.push_str(".."); s.push(SEP); @@ -192,22 +192,22 @@ impl GenericPathUnsafe for Path { None => { self.update_normalized(filename); } - Some((_,idxa,end)) if self.repr.as_slice().slice(idxa,end) == ".." => { + Some((_,idxa,end)) if self.repr.slice(idxa,end) == ".." => { let mut s = String::with_capacity(end + 1 + filename.len()); - s.push_str(self.repr.as_slice().slice_to(end)); + s.push_str(self.repr.slice_to(end)); s.push(SEP); s.push_str(filename); self.update_normalized(s); } Some((idxb,idxa,_)) if self.prefix == Some(DiskPrefix) && idxa == self.prefix_len() => { let mut s = String::with_capacity(idxb + filename.len()); - s.push_str(self.repr.as_slice().slice_to(idxb)); + s.push_str(self.repr.slice_to(idxb)); s.push_str(filename); self.update_normalized(s); } Some((idxb,_,_)) => { let mut s = String::with_capacity(idxb + 1 + filename.len()); - s.push_str(self.repr.as_slice().slice_to(idxb)); + s.push_str(self.repr.slice_to(idxb)); s.push(SEP); s.push_str(filename); self.update_normalized(s); @@ -356,21 +356,21 @@ impl GenericPath for Path { /// Always returns a `Some` value. fn dirname_str<'a>(&'a self) -> Option<&'a str> { Some(match self.sepidx_or_prefix_len() { - None if ".." == self.repr.as_slice() => self.repr.as_slice(), + None if ".." == self.repr => self.repr.as_slice(), None => ".", - Some((_,idxa,end)) if self.repr.as_slice().slice(idxa, end) == ".." => { + Some((_,idxa,end)) if self.repr.slice(idxa, end) == ".." => { self.repr.as_slice() } - Some((idxb,_,end)) if self.repr.as_slice().slice(idxb, end) == "\\" => { + Some((idxb,_,end)) if self.repr.slice(idxb, end) == "\\" => { self.repr.as_slice() } - Some((0,idxa,_)) => self.repr.as_slice().slice_to(idxa), + Some((0,idxa,_)) => self.repr.slice_to(idxa), Some((idxb,idxa,_)) => { match self.prefix { Some(DiskPrefix) | Some(VerbatimDiskPrefix) if idxb == self.prefix_len() => { - self.repr.as_slice().slice_to(idxa) + self.repr.slice_to(idxa) } - _ => self.repr.as_slice().slice_to(idxb) + _ => self.repr.slice_to(idxb) } } }) @@ -415,14 +415,14 @@ impl GenericPath for Path { #[inline] fn pop(&mut self) -> bool { match self.sepidx_or_prefix_len() { - None if "." == self.repr.as_slice() => false, + None if "." == self.repr => false, None => { self.repr = String::from_str("."); self.sepidx = None; true } Some((idxb,idxa,end)) if idxb == idxa && idxb == end => false, - Some((idxb,_,end)) if self.repr.as_slice().slice(idxb, end) == "\\" => false, + Some((idxb,_,end)) if self.repr.slice(idxb, end) == "\\" => false, Some((idxb,idxa,_)) => { let trunc = match self.prefix { Some(DiskPrefix) | Some(VerbatimDiskPrefix) | None => { @@ -442,15 +442,15 @@ impl GenericPath for Path { if self.prefix.is_some() { Some(Path::new(match self.prefix { Some(DiskPrefix) if self.is_absolute() => { - self.repr.as_slice().slice_to(self.prefix_len()+1) + self.repr.slice_to(self.prefix_len()+1) } Some(VerbatimDiskPrefix) => { - self.repr.as_slice().slice_to(self.prefix_len()+1) + self.repr.slice_to(self.prefix_len()+1) } - _ => self.repr.as_slice().slice_to(self.prefix_len()) + _ => self.repr.slice_to(self.prefix_len()) })) } else if is_vol_relative(self) { - Some(Path::new(self.repr.as_slice().slice_to(1))) + Some(Path::new(self.repr.slice_to(1))) } else { None } @@ -469,7 +469,7 @@ impl GenericPath for Path { fn is_absolute(&self) -> bool { match self.prefix { Some(DiskPrefix) => { - let rest = self.repr.as_slice().slice_from(self.prefix_len()); + let rest = self.repr.slice_from(self.prefix_len()); rest.len() > 0 && rest.as_bytes()[0] == SEP_BYTE } Some(_) => true, @@ -491,7 +491,7 @@ impl GenericPath for Path { } else { let mut ita = self.str_components().map(|x|x.unwrap()); let mut itb = other.str_components().map(|x|x.unwrap()); - if "." == self.repr.as_slice() { + if "." == self.repr { return itb.next() != Some(".."); } loop { @@ -827,7 +827,7 @@ impl Path { fn update_sepidx(&mut self) { let s = if self.has_nonsemantic_trailing_slash() { - self.repr.as_slice().slice_to(self.repr.len()-1) + self.repr.slice_to(self.repr.len()-1) } else { self.repr.as_slice() }; let idx = s.rfind(if !prefix_is_verbatim(self.prefix) { is_sep } else { is_sep_verbatim }); @@ -923,7 +923,7 @@ pub fn make_non_verbatim(path: &Path) -> Option<Path> { } // now ensure normalization didn't change anything if repr.slice_from(path.prefix_len()) == - new_path.repr.as_slice().slice_from(new_path.prefix_len()) { + new_path.repr.slice_from(new_path.prefix_len()) { Some(new_path) } else { None @@ -1233,8 +1233,8 @@ mod tests { t!(s: Path::new("foo\\..\\..\\.."), "..\\.."); t!(s: Path::new("foo\\..\\..\\bar"), "..\\bar"); - assert_eq!(Path::new(b"foo\\bar").into_vec().as_slice(), b"foo\\bar"); - assert_eq!(Path::new(b"\\foo\\..\\..\\bar").into_vec().as_slice(), b"\\bar"); + assert_eq!(Path::new(b"foo\\bar").into_vec(), b"foo\\bar"); + assert_eq!(Path::new(b"\\foo\\..\\..\\bar").into_vec(), b"\\bar"); t!(s: Path::new("\\\\a"), "\\a"); t!(s: Path::new("\\\\a\\"), "\\a"); @@ -1322,9 +1322,9 @@ mod tests { #[test] fn test_display_str() { let path = Path::new("foo"); - assert_eq!(path.display().to_string(), "foo".to_string()); + assert_eq!(path.display().to_string(), "foo"); let path = Path::new(b"\\"); - assert_eq!(path.filename_display().to_string(), "".to_string()); + assert_eq!(path.filename_display().to_string(), ""); let path = Path::new("foo"); let mo = path.display().as_cow(); @@ -1341,9 +1341,9 @@ mod tests { { let path = Path::new($path); let f = format!("{}", path.display()); - assert_eq!(f.as_slice(), $exp); + assert_eq!(f, $exp); let f = format!("{}", path.filename_display()); - assert_eq!(f.as_slice(), $expf); + assert_eq!(f, $expf); } ) ) @@ -2246,7 +2246,7 @@ mod tests { let comps = path.str_components().map(|x|x.unwrap()) .collect::<Vec<&str>>(); let exp: &[&str] = &$exp; - assert_eq!(comps.as_slice(), exp); + assert_eq!(comps, exp); let comps = path.str_components().rev().map(|x|x.unwrap()) .collect::<Vec<&str>>(); let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&str>>(); @@ -2303,7 +2303,7 @@ mod tests { let path = Path::new($path); let comps = path.components().collect::<Vec<&[u8]>>(); let exp: &[&[u8]] = &$exp; - assert_eq!(comps.as_slice(), exp); + assert_eq!(comps, exp); let comps = path.components().rev().collect::<Vec<&[u8]>>(); let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>(); assert_eq!(comps, exp); |
