diff options
| author | bors <bors@rust-lang.org> | 2014-02-24 04:11:53 -0800 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-02-24 04:11:53 -0800 |
| commit | 672097753a217d4990129cbdfab16ef8c9b08b21 (patch) | |
| tree | cf206fc1ba6465032dac4fdce670860538da0140 /src/libextra | |
| parent | a5342d5970d57dd0cc4805ba0f5385d7f3859c94 (diff) | |
| parent | 8761f79485f11ef03eb6cb569ccb9f4c73e68f11 (diff) | |
| download | rust-672097753a217d4990129cbdfab16ef8c9b08b21.tar.gz rust-672097753a217d4990129cbdfab16ef8c9b08b21.zip | |
auto merge of #12412 : alexcrichton/rust/deriving-show, r=huonw
This commit removes deriving(ToStr) in favor of deriving(Show), migrating all impls of ToStr to fmt::Show. Most of the details can be found in the first commit message. Closes #12477
Diffstat (limited to 'src/libextra')
| -rw-r--r-- | src/libextra/json.rs | 16 | ||||
| -rw-r--r-- | src/libextra/url.rs | 149 |
2 files changed, 78 insertions, 87 deletions
diff --git a/src/libextra/json.rs b/src/libextra/json.rs index 4cc210aaa6c..d438b00b992 100644 --- a/src/libextra/json.rs +++ b/src/libextra/json.rs @@ -240,7 +240,7 @@ use std::io; use std::io::MemWriter; use std::num; use std::str; -use std::to_str; +use std::fmt; use serialize::Encodable; use serialize; @@ -1576,18 +1576,16 @@ impl<A:ToJson> ToJson for Option<A> { } } -impl to_str::ToStr for Json { +impl fmt::Show for Json { /// Encodes a json value into a string - fn to_str(&self) -> ~str { - let mut s = MemWriter::new(); - self.to_writer(&mut s as &mut io::Writer).unwrap(); - str::from_utf8_owned(s.unwrap()).unwrap() + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.to_writer(f.buf) } } -impl to_str::ToStr for Error { - fn to_str(&self) -> ~str { - format!("{}:{}: {}", self.line, self.col, self.msg) +impl fmt::Show for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f.buf, "{}:{}: {}", self.line, self.col, self.msg) } } diff --git a/src/libextra/url.rs b/src/libextra/url.rs index 41d6d95c6bb..0292a18817c 100644 --- a/src/libextra/url.rs +++ b/src/libextra/url.rs @@ -12,12 +12,14 @@ #[allow(missing_doc)]; -use std::io::BufReader; use std::cmp::Eq; -use collections::HashMap; +use std::fmt; use std::hash::{Hash, sip}; +use std::io::BufReader; use std::uint; +use collections::HashMap; + /// A Uniform Resource Locator (URL). A URL is a form of URI (Uniform Resource /// Identifier) that includes network location information, such as hostname or /// port number. @@ -407,10 +409,12 @@ fn split_char_first(s: &str, c: char) -> (~str, ~str) { } } -fn userinfo_to_str(userinfo: &UserInfo) -> ~str { - match userinfo.pass { - Some(ref pass) => format!("{}:{}@", userinfo.user, *pass), - None => format!("{}@", userinfo.user), +impl fmt::Show for UserInfo { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.pass { + Some(ref pass) => write!(f.buf, "{}:{}@", self.user, *pass), + None => write!(f.buf, "{}@", self.user), + } } } @@ -437,19 +441,18 @@ fn query_from_str(rawquery: &str) -> Query { * println!("{}", url::query_to_str(&query)); // title=The%20Village&north=52.91&west=4.10 * ``` */ +#[allow(unused_must_use)] pub fn query_to_str(query: &Query) -> ~str { - let mut strvec = ~[]; - for kv in query.iter() { - match kv { - &(ref k, ref v) => { - strvec.push(format!("{}={}", - encode_component(*k), - encode_component(*v)) - ); - } - } + use std::io::MemWriter; + use std::str; + + let mut writer = MemWriter::new(); + for (i, &(ref k, ref v)) in query.iter().enumerate() { + if i != 0 { write!(&mut writer, "&"); } + write!(&mut writer, "{}={}", encode_component(*k), + encode_component(*v)); } - return strvec.connect("&"); + str::from_utf8_lossy(writer.unwrap()).into_owned() } /** @@ -784,74 +787,64 @@ impl FromStr for Path { } } -/** - * Converts a URL from `Url` to string representation. - * - * # Arguments - * - * `url` - a URL. - * - * # Returns - * - * A string that contains the formatted URL. Note that this will usually - * be an inverse of `from_str` but might strip out unneeded separators; - * for example, "http://somehost.com?", when parsed and formatted, will - * result in just "http://somehost.com". - */ -pub fn to_str(url: &Url) -> ~str { - let user = match url.user { - Some(ref user) => userinfo_to_str(user), - None => ~"", - }; - - let authority = if url.host.is_empty() { - // If port is Some, we're in a nonsensical situation. Too bad. - ~"" - } else { - match url.port { - Some(ref port) => format!("//{}{}:{}", user, url.host, *port), - None => format!("//{}{}", user, url.host), +impl fmt::Show for Url { + /** + * Converts a URL from `Url` to string representation. + * + * # Arguments + * + * `url` - a URL. + * + * # Returns + * + * A string that contains the formatted URL. Note that this will usually + * be an inverse of `from_str` but might strip out unneeded separators; + * for example, "http://somehost.com?", when parsed and formatted, will + * result in just "http://somehost.com". + */ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + try!(write!(f.buf, "{}:", self.scheme)); + + if !self.host.is_empty() { + try!(write!(f.buf, "//")); + match self.user { + Some(ref user) => try!(write!(f.buf, "{}", *user)), + None => {} + } + match self.port { + Some(ref port) => try!(write!(f.buf, "{}:{}", self.host, + *port)), + None => try!(write!(f.buf, "{}", self.host)), + } } - }; - - let query = if url.query.is_empty() { - ~"" - } else { - format!("?{}", query_to_str(&url.query)) - }; - - let fragment = match url.fragment { - Some(ref fragment) => format!("\\#{}", encode_component(*fragment)), - None => ~"", - }; - - format!("{}:{}{}{}{}", url.scheme, authority, url.path, query, fragment) -} - -pub fn path_to_str(path: &Path) -> ~str { - let query = if path.query.is_empty() { - ~"" - } else { - format!("?{}", query_to_str(&path.query)) - }; - let fragment = match path.fragment { - Some(ref fragment) => format!("\\#{}", encode_component(*fragment)), - None => ~"", - }; + try!(write!(f.buf, "{}", self.path)); - format!("{}{}{}", path.path, query, fragment) -} + if !self.query.is_empty() { + try!(write!(f.buf, "?{}", query_to_str(&self.query))); + } -impl ToStr for Url { - fn to_str(&self) -> ~str { - to_str(self) + match self.fragment { + Some(ref fragment) => write!(f.buf, "\\#{}", + encode_component(*fragment)), + None => Ok(()), + } } } -impl ToStr for Path { - fn to_str(&self) -> ~str { - path_to_str(self) +impl fmt::Show for Path { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + try!(write!(f.buf, "{}", self.path)); + if !self.query.is_empty() { + try!(write!(f.buf, "?{}", self.query)) + } + + match self.fragment { + Some(ref fragment) => { + write!(f.buf, "\\#{}", encode_component(*fragment)) + } + None => Ok(()) + } } } |
