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/io | |
| 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/io')
| -rw-r--r-- | src/libstd/io/buffered.rs | 10 | ||||
| -rw-r--r-- | src/libstd/io/comm_adapters.rs | 10 | ||||
| -rw-r--r-- | src/libstd/io/fs.rs | 6 | ||||
| -rw-r--r-- | src/libstd/io/mem.rs | 26 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 22 | ||||
| -rw-r--r-- | src/libstd/io/net/ip.rs | 6 | ||||
| -rw-r--r-- | src/libstd/io/process.rs | 31 | ||||
| -rw-r--r-- | src/libstd/io/stdio.rs | 4 |
8 files changed, 56 insertions, 59 deletions
diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index 0b2c6843c96..a8de7356fe7 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -450,30 +450,30 @@ mod test { let nread = reader.read(&mut buf); assert_eq!(Ok(3), nread); let b: &[_] = &[5, 6, 7]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); let mut buf = [0, 0]; let nread = reader.read(&mut buf); assert_eq!(Ok(2), nread); let b: &[_] = &[0, 1]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); let mut buf = [0]; let nread = reader.read(&mut buf); assert_eq!(Ok(1), nread); let b: &[_] = &[2]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); let mut buf = [0, 0, 0]; let nread = reader.read(&mut buf); assert_eq!(Ok(1), nread); let b: &[_] = &[3, 0, 0]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); let nread = reader.read(&mut buf); assert_eq!(Ok(1), nread); let b: &[_] = &[4, 0, 0]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); assert!(reader.read(&mut buf).is_err()); } diff --git a/src/libstd/io/comm_adapters.rs b/src/libstd/io/comm_adapters.rs index 3de0a7be95e..4ec1a3764db 100644 --- a/src/libstd/io/comm_adapters.rs +++ b/src/libstd/io/comm_adapters.rs @@ -176,28 +176,28 @@ mod test { assert_eq!(Ok(3), reader.read(&mut buf)); let a: &[u8] = &[1,2,3]; - assert_eq!(a, buf.as_slice()); + assert_eq!(a, buf); assert_eq!(Ok(3), reader.read(&mut buf)); let a: &[u8] = &[4,5,6]; - assert_eq!(a, buf.as_slice()); + assert_eq!(a, buf); assert_eq!(Ok(2), reader.read(&mut buf)); let a: &[u8] = &[7,8,6]; - assert_eq!(a, buf.as_slice()); + assert_eq!(a, buf); match reader.read(buf.as_mut_slice()) { Ok(..) => panic!(), Err(e) => assert_eq!(e.kind, io::EndOfFile), } - assert_eq!(a, buf.as_slice()); + assert_eq!(a, buf); // Ensure it continues to panic in the same way. match reader.read(buf.as_mut_slice()) { Ok(..) => panic!(), Err(e) => assert_eq!(e.kind, io::EndOfFile), } - assert_eq!(a, buf.as_slice()); + assert_eq!(a, buf); } #[test] diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index 86b2dc4d940..5611c6526ad 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -838,7 +838,7 @@ mod test { macro_rules! error( ($e:expr, $s:expr) => ( match $e { Ok(_) => panic!("Unexpected success. Should've been: {}", $s), - Err(ref err) => assert!(err.to_string().as_slice().contains($s.as_slice()), + Err(ref err) => assert!(err.to_string().contains($s.as_slice()), format!("`{}` did not contain `{}`", err, $s)) } ) ) @@ -996,7 +996,7 @@ mod test { } check!(unlink(filename)); let read_str = str::from_utf8(&read_mem).unwrap(); - assert!(read_str.as_slice() == final_msg.as_slice()); + assert!(read_str == final_msg); } #[test] @@ -1104,7 +1104,7 @@ mod test { let f = dir.join(format!("{}.txt", n)); let mut w = check!(File::create(&f)); let msg_str = format!("{}{}", prefix, n.to_string()); - let msg = msg_str.as_slice().as_bytes(); + let msg = msg_str.as_bytes(); check!(w.write(msg)); } let files = check!(readdir(dir)); diff --git a/src/libstd/io/mem.rs b/src/libstd/io/mem.rs index 43e02d62b0b..4dc87278e2b 100644 --- a/src/libstd/io/mem.rs +++ b/src/libstd/io/mem.rs @@ -444,7 +444,7 @@ mod test { assert_eq!(writer.write(&[10]).unwrap_err().kind, io::EndOfFile); } let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); } #[test] @@ -473,7 +473,7 @@ mod test { } let b: &[_] = &[1, 3, 2, 0, 0, 0, 0, 4]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); } #[test] @@ -498,12 +498,12 @@ mod test { assert_eq!(reader.read(&mut buf), Ok(1)); assert_eq!(reader.tell(), Ok(1)); let b: &[_] = &[0]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); let mut buf = [0, ..4]; assert_eq!(reader.read(&mut buf), Ok(4)); assert_eq!(reader.tell(), Ok(5)); let b: &[_] = &[1, 2, 3, 4]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); assert_eq!(reader.read(&mut buf), Ok(3)); let b: &[_] = &[5, 6, 7]; assert_eq!(buf[0..3], b); @@ -551,12 +551,12 @@ mod test { assert_eq!(reader.read(&mut buf), Ok(1)); assert_eq!(reader.tell(), Ok(1)); let b: &[_] = &[0]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); let mut buf = [0, ..4]; assert_eq!(reader.read(&mut buf), Ok(4)); assert_eq!(reader.tell(), Ok(5)); let b: &[_] = &[1, 2, 3, 4]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); assert_eq!(reader.read(&mut buf), Ok(3)); let b: &[_] = &[5, 6, 7]; assert_eq!(buf[0..3], b); @@ -592,7 +592,7 @@ mod test { writer.write_line("testing").unwrap(); writer.write_str("testing").unwrap(); let mut r = BufReader::new(writer.get_ref()); - assert_eq!(r.read_to_string().unwrap(), "testingtesting\ntesting".to_string()); + assert_eq!(r.read_to_string().unwrap(), "testingtesting\ntesting"); } #[test] @@ -602,7 +602,7 @@ mod test { writer.write_char('\n').unwrap(); writer.write_char('ệ').unwrap(); let mut r = BufReader::new(writer.get_ref()); - assert_eq!(r.read_to_string().unwrap(), "a\nệ".to_string()); + assert_eq!(r.read_to_string().unwrap(), "a\nệ"); } #[test] @@ -652,15 +652,15 @@ mod test { let mut buf = [0, ..3]; assert!(r.read_at_least(buf.len(), &mut buf).is_ok()); let b: &[_] = &[1, 2, 3]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); assert!(r.read_at_least(0, buf[mut ..0]).is_ok()); - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); assert!(r.read_at_least(buf.len(), &mut buf).is_ok()); let b: &[_] = &[4, 5, 6]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); assert!(r.read_at_least(buf.len(), &mut buf).is_err()); let b: &[_] = &[7, 8, 6]; - assert_eq!(buf.as_slice(), b); + assert_eq!(buf, b); } fn do_bench_mem_writer(b: &mut Bencher, times: uint, len: uint) { @@ -757,7 +757,7 @@ mod test { for _i in range(0u, 10) { let mut buf = [0 as u8, .. 10]; rdr.read(&mut buf).unwrap(); - assert_eq!(buf.as_slice(), [5, .. 10].as_slice()); + assert_eq!(buf, [5, .. 10]); } } }); diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index e0857fd27ff..d43a7a66c5b 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -240,8 +240,8 @@ use boxed::Box; use result::Result; use result::Result::{Ok, Err}; use sys; -use slice::{AsSlice, SlicePrelude}; -use str::{Str, StrPrelude}; +use slice::SlicePrelude; +use str::StrPrelude; use str; use string::String; use uint; @@ -318,7 +318,7 @@ impl IoError { pub fn from_errno(errno: uint, detail: bool) -> IoError { let mut err = sys::decode_error(errno as i32); if detail && err.kind == OtherIoError { - err.detail = Some(os::error_string(errno).as_slice().chars() + err.detail = Some(os::error_string(errno).chars() .map(|c| c.to_lowercase()).collect()) } err @@ -2007,14 +2007,14 @@ mod tests { fn test_show() { use super::*; - assert_eq!(format!("{}", USER_READ), "0400".to_string()); - assert_eq!(format!("{}", USER_FILE), "0644".to_string()); - assert_eq!(format!("{}", USER_EXEC), "0755".to_string()); - assert_eq!(format!("{}", USER_RWX), "0700".to_string()); - assert_eq!(format!("{}", GROUP_RWX), "0070".to_string()); - assert_eq!(format!("{}", OTHER_RWX), "0007".to_string()); - assert_eq!(format!("{}", ALL_PERMISSIONS), "0777".to_string()); - assert_eq!(format!("{}", USER_READ | USER_WRITE | OTHER_WRITE), "0602".to_string()); + assert_eq!(format!("{}", USER_READ), "0400"); + assert_eq!(format!("{}", USER_FILE), "0644"); + assert_eq!(format!("{}", USER_EXEC), "0755"); + assert_eq!(format!("{}", USER_RWX), "0700"); + assert_eq!(format!("{}", GROUP_RWX), "0070"); + assert_eq!(format!("{}", OTHER_RWX), "0007"); + assert_eq!(format!("{}", ALL_PERMISSIONS), "0777"); + assert_eq!(format!("{}", USER_READ | USER_WRITE | OTHER_WRITE), "0602"); } fn _ensure_buffer_is_object_safe<T: Buffer>(x: &T) -> &Buffer { diff --git a/src/libstd/io/net/ip.rs b/src/libstd/io/net/ip.rs index 9c0fbbe274b..3fa6f4a6091 100644 --- a/src/libstd/io/net/ip.rs +++ b/src/libstd/io/net/ip.rs @@ -641,10 +641,10 @@ mod test { #[test] fn ipv6_addr_to_string() { let a1 = Ipv6Addr(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280); - assert!(a1.to_string() == "::ffff:192.0.2.128".to_string() || - a1.to_string() == "::FFFF:192.0.2.128".to_string()); + assert!(a1.to_string() == "::ffff:192.0.2.128" || + a1.to_string() == "::FFFF:192.0.2.128"); assert_eq!(Ipv6Addr(8, 9, 10, 11, 12, 13, 14, 15).to_string(), - "8:9:a:b:c:d:e:f".to_string()); + "8:9:a:b:c:d:e:f"); } #[test] diff --git a/src/libstd/io/process.rs b/src/libstd/io/process.rs index d4d24c1e12f..61ebfb06c71 100644 --- a/src/libstd/io/process.rs +++ b/src/libstd/io/process.rs @@ -236,8 +236,8 @@ impl Command { // if the env is currently just inheriting from the parent's, // materialize the parent's env into a hashtable. self.env = Some(os::env_as_bytes().into_iter() - .map(|(k, v)| (EnvKey(k.as_slice().to_c_str()), - v.as_slice().to_c_str())) + .map(|(k, v)| (EnvKey(k.to_c_str()), + v.to_c_str())) .collect()); self.env.as_mut().unwrap() } @@ -810,7 +810,7 @@ mod tests { fn stdout_works() { let mut cmd = Command::new("echo"); cmd.arg("foobar").stdout(CreatePipe(false, true)); - assert_eq!(run_output(cmd), "foobar\n".to_string()); + assert_eq!(run_output(cmd), "foobar\n"); } #[cfg(all(unix, not(target_os="android")))] @@ -820,7 +820,7 @@ mod tests { cmd.arg("-c").arg("pwd") .cwd(&Path::new("/")) .stdout(CreatePipe(false, true)); - assert_eq!(run_output(cmd), "/\n".to_string()); + assert_eq!(run_output(cmd), "/\n"); } #[cfg(all(unix, not(target_os="android")))] @@ -835,7 +835,7 @@ mod tests { drop(p.stdin.take()); let out = read_all(p.stdout.as_mut().unwrap() as &mut Reader); assert!(p.wait().unwrap().success()); - assert_eq!(out, "foobar\n".to_string()); + assert_eq!(out, "foobar\n"); } #[cfg(not(target_os="android"))] @@ -900,7 +900,7 @@ mod tests { let output_str = str::from_utf8(output.as_slice()).unwrap(); assert!(status.success()); - assert_eq!(output_str.trim().to_string(), "hello".to_string()); + assert_eq!(output_str.trim().to_string(), "hello"); // FIXME #7224 if !running_on_valgrind() { assert_eq!(error, Vec::new()); @@ -941,7 +941,7 @@ mod tests { let output_str = str::from_utf8(output.as_slice()).unwrap(); assert!(status.success()); - assert_eq!(output_str.trim().to_string(), "hello".to_string()); + assert_eq!(output_str.trim().to_string(), "hello"); // FIXME #7224 if !running_on_valgrind() { assert_eq!(error, Vec::new()); @@ -973,7 +973,7 @@ mod tests { let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap(); let parent_dir = os::getcwd().unwrap(); - let child_dir = Path::new(output.as_slice().trim()); + let child_dir = Path::new(output.trim()); let parent_stat = parent_dir.stat().unwrap(); let child_stat = child_dir.stat().unwrap(); @@ -991,7 +991,7 @@ mod tests { let prog = pwd_cmd().cwd(&parent_dir).spawn().unwrap(); let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap(); - let child_dir = Path::new(output.as_slice().trim()); + let child_dir = Path::new(output.trim()); let parent_stat = parent_dir.stat().unwrap(); let child_stat = child_dir.stat().unwrap(); @@ -1031,8 +1031,7 @@ mod tests { for &(ref k, ref v) in r.iter() { // don't check windows magical empty-named variables assert!(k.is_empty() || - output.as_slice() - .contains(format!("{}={}", *k, *v).as_slice()), + output.contains(format!("{}={}", *k, *v).as_slice()), "output doesn't contain `{}={}`\n{}", k, v, output); } @@ -1050,12 +1049,10 @@ mod tests { for &(ref k, ref v) in r.iter() { // don't check android RANDOM variables if *k != "RANDOM".to_string() { - assert!(output.as_slice() - .contains(format!("{}={}", + assert!(output.contains(format!("{}={}", *k, *v).as_slice()) || - output.as_slice() - .contains(format!("{}=\'{}\'", + output.contains(format!("{}=\'{}\'", *k, *v).as_slice())); } @@ -1084,7 +1081,7 @@ mod tests { let result = prog.wait_with_output().unwrap(); let output = String::from_utf8_lossy(result.output.as_slice()).into_string(); - assert!(output.as_slice().contains("RUN_TEST_NEW_ENV=123"), + assert!(output.contains("RUN_TEST_NEW_ENV=123"), "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output); } @@ -1094,7 +1091,7 @@ mod tests { let result = prog.wait_with_output().unwrap(); let output = String::from_utf8_lossy(result.output.as_slice()).into_string(); - assert!(output.as_slice().contains("RUN_TEST_NEW_ENV=123"), + assert!(output.contains("RUN_TEST_NEW_ENV=123"), "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output); } diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index cf2df0da7fe..48c333f0733 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -528,7 +528,7 @@ mod tests { set_stdout(box w); println!("hello!"); }); - assert_eq!(r.read_to_string().unwrap(), "hello!\n".to_string()); + assert_eq!(r.read_to_string().unwrap(), "hello!\n"); } #[test] @@ -543,6 +543,6 @@ mod tests { panic!("my special message"); }); let s = r.read_to_string().unwrap(); - assert!(s.as_slice().contains("my special message")); + assert!(s.contains("my special message")); } } |
