From 42da7aa765e4fdb0ca444b3d4722214728f739e6 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 24 Apr 2015 18:37:39 -0400 Subject: [PATCH] General cleanup and modernization Read through some of the code, cleaned up a few things along the way --- src/format.rs | 8 ++++---- src/lib.rs | 10 +++++----- src/percent_encoding.rs | 13 +++++-------- src/punycode.rs | 4 ++-- src/tests.rs | 4 ++-- 5 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/format.rs b/src/format.rs index 0a18804e..c448bd53 100644 --- a/src/format.rs +++ b/src/format.rs @@ -26,7 +26,7 @@ impl<'a, T: fmt::Display> fmt::Display for PathFormatter<'a, T> { if self.path.is_empty() { formatter.write_str("/") } else { - for path_part in self.path.iter() { + for path_part in self.path { try!("/".fmt(formatter)); try!(path_part.fmt(formatter)); } @@ -95,7 +95,7 @@ mod tests { (vec!["test", "path"], "/test/path"), (vec!["test", "path", ""], "/test/path/") ]; - for &(ref path, result) in data.iter() { + for &(ref path, result) in &data { assert_eq!(PathFormatter { path: path }.to_string(), result.to_string()); @@ -113,7 +113,7 @@ mod tests { ("username", Some(""), "username:@"), ("username", Some("password"), "username:password@") ]; - for &(username, password, result) in data.iter() { + for &(username, password, result) in &data { assert_eq!(UserInfoFormatter { username: username, password: password @@ -133,7 +133,7 @@ mod tests { ("http://slashquery.com/path/?q=something", "http://slashquery.com/path/?q=something"), ("http://noslashquery.com/path?q=something", "http://noslashquery.com/path?q=something") ]; - for &(input, result) in data.iter() { + for &(input, result) in &data { let url = Url::parse(input).unwrap(); assert_eq!(url.to_string(), result.to_string()); } diff --git a/src/lib.rs b/src/lib.rs index 3164b12b..27551bc5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -616,7 +616,7 @@ impl Url { #[inline] pub fn password<'a>(&'a self) -> Option<&'a str> { self.relative_scheme_data().and_then(|scheme_data| - scheme_data.password.as_ref().map(|password| &**password)) + scheme_data.password.as_ref().map(|password| password as &str)) } /// If the URL is in a *relative scheme*, return a mutable reference to its password, if any. @@ -878,7 +878,7 @@ impl RelativeSchemeData { pub fn serialize_userinfo(&self) -> String { UserInfoFormatter { username: &self.username, - password: self.password.as_ref().map(|s| &**s) + password: self.password.as_ref().map(|s| s as &str) }.to_string() } } @@ -892,7 +892,7 @@ impl fmt::Display for RelativeSchemeData { // Write the user info. try!(UserInfoFormatter { username: &self.username, - password: self.password.as_ref().map(|s| &**s) + password: self.password.as_ref().map(|s| s as &str) }.fmt(formatter)); // Write the host. @@ -970,7 +970,7 @@ fn file_url_path_to_pathbuf(path: &[String]) -> Result { return Ok(PathBuf::from("/")) } let mut bytes = Vec::new(); - for path_part in path.iter() { + for path_part in path { bytes.push(b'/'); percent_decode_to(path_part.as_bytes(), &mut bytes); } @@ -994,7 +994,7 @@ fn file_url_path_to_pathbuf(path: &[String]) -> Result { return Err(()) } let mut string = prefix.to_string(); - for path_part in path[1..].iter() { + for path_part in path[1..] { string.push('\\'); // Currently non-unicode windows paths cannot be represented diff --git a/src/percent_encoding.rs b/src/percent_encoding.rs index 02765f2d..90b8f90f 100644 --- a/src/percent_encoding.rs +++ b/src/percent_encoding.rs @@ -61,7 +61,7 @@ pub static FORM_URLENCODED_ENCODE_SET: EncodeSet = EncodeSet { /// The pushed strings are within the ASCII range. #[inline] pub fn percent_encode_to(input: &[u8], encode_set: EncodeSet, output: &mut String) { - for &byte in input.iter() { + for &byte in input { output.push_str(encode_set.map[byte as usize]) } } @@ -104,13 +104,10 @@ pub fn percent_decode_to(input: &[u8], output: &mut Vec) { while i < input.len() { let c = input[i]; if c == b'%' && i + 2 < input.len() { - match (from_hex(input[i + 1]), from_hex(input[i + 2])) { - (Some(h), Some(l)) => { - output.push(h * 0x10 + l); - i += 3; - continue - }, - _ => (), + if let (Some(h), Some(l)) = (from_hex(input[i + 1]), from_hex(input[i + 2])) { + output.push(h * 0x10 + l); + i += 3; + continue } } diff --git a/src/punycode.rs b/src/punycode.rs index 38620d1e..5acb9fa8 100644 --- a/src/punycode.rs +++ b/src/punycode.rs @@ -165,7 +165,7 @@ pub fn encode(input: &[char]) -> Option { // Increase delta to advance the decoder’s state to delta += (min_code_point - code_point) * (processed + 1); code_point = min_code_point; - for &c in input.iter() { + for &c in input { let c = c as u32; if c < code_point { delta += 1; @@ -251,7 +251,7 @@ mod tests { fn test_punycode() { match Json::from_str(include_str!("punycode_tests.json")) { - Ok(Json::Array(tests)) => for test in tests.iter() { + Ok(Json::Array(tests)) => for test in &tests { match test { &Json::Object(ref o) => one_test( get_string(o, "description"), diff --git a/src/tests.rs b/src/tests.rs index d80a5b35..d663ff01 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -13,7 +13,7 @@ use super::{UrlParser, Url, SchemeData, RelativeSchemeData, Host}; #[test] fn url_parsing() { - for test in parse_test_data(include_str!("urltestdata.txt")).into_iter() { + for test in parse_test_data(include_str!("urltestdata.txt")) { let Test { input, base, @@ -138,7 +138,7 @@ fn parse_test_data(input: &str) -> Vec { fragment: None, expected_failure: expected_failure, }; - for piece in pieces.into_iter() { + for piece in pieces { if piece == "" || piece.starts_with("#") { continue }