Skip to content

Commit

Permalink
auto merge of #15493 : brson/rust/tostr, r=pcwalton
Browse files Browse the repository at this point in the history
This updates #15075.

Rename `ToStr::to_str` to `ToString::to_string`. The naive renaming ends up with two `to_string` functions defined on strings in the prelude (the other defined via `collections::str::StrAllocating`). To remedy this I removed `StrAllocating::to_string`, making all conversions from `&str` to `String` go through `Show`. This has a measurable impact on the speed of this conversion, but the sense I get from others is that it's best to go ahead and unify `to_string` and address performance for all `to_string` conversions in `core::fmt`. `String::from_str(...)` still works as a manual fast-path.

Note that the patch was done with a script, and ended up renaming a number of other `*_to_str` functions, particularly inside of rustc. All the ones I saw looked correct, and I didn't notice any additional API breakage.

Closes #15046.
  • Loading branch information
bors committed Jul 8, 2014
2 parents 35e2134 + 12c334a commit 8bb34a3
Show file tree
Hide file tree
Showing 208 changed files with 1,557 additions and 1,390 deletions.
2 changes: 1 addition & 1 deletion src/compiletest/compiletest.rs
Expand Up @@ -187,7 +187,7 @@ pub fn log_config(config: &Config) {
opt_str(&config.filter
.as_ref()
.map(|re| {
re.to_str().into_string()
re.to_string().into_string()
}))));
logv(c, format!("runtool: {}", opt_str(&config.runtool)));
logv(c, format!("host-rustcflags: {}",
Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/errors.rs
Expand Up @@ -31,7 +31,7 @@ pub fn load_errors(re: &Regex, testfile: &Path) -> Vec<ExpectedError> {
fn parse_expected(line_num: uint, line: &str, re: &Regex) -> Option<ExpectedError> {
re.captures(line).and_then(|caps| {
let adjusts = caps.name("adjusts").len();
let kind = caps.name("kind").to_ascii().to_lower().into_str();
let kind = caps.name("kind").to_ascii().to_lower().into_string();
let msg = caps.name("msg").trim().to_string();

debug!("line={} kind={} msg={}", line_num, kind, msg);
Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/util.rs
Expand Up @@ -41,7 +41,7 @@ pub fn make_new_path(path: &str) -> String {
Some(curr) => {
format!("{}{}{}", path, path_div(), curr)
}
None => path.to_str()
None => path.to_string()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/doc/guide-tasks.md
Expand Up @@ -465,7 +465,7 @@ fn stringifier(channel: &DuplexStream<String, uint>) {
let mut value: uint;
loop {
value = channel.recv();
channel.send(value.to_str());
channel.send(value.to_string());
if value == 0 { break; }
}
}
Expand All @@ -478,7 +478,7 @@ send strings (the first type parameter) and receive `uint` messages
(the second type parameter). The body itself simply loops, reading
from the channel and then sending its response back. The actual
response itself is simply the stringified version of the received value,
`uint::to_str(value)`.
`uint::to_string(value)`.

Here is the code for the parent task:

Expand All @@ -492,7 +492,7 @@ use std::comm::duplex;
# let mut value: uint;
# loop {
# value = channel.recv();
# channel.send(value.to_str());
# channel.send(value.to_string());
# if value == 0u { break; }
# }
# }
Expand Down
4 changes: 2 additions & 2 deletions src/doc/po/ja/complement-cheatsheet.md.po
Expand Up @@ -23,7 +23,7 @@ msgstr ""
#| "[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz [win-exe]: "
#| "http://static.rust-lang.org/dist/rust-nightly-install.exe"
msgid ""
"Use [`ToStr`](http://static.rust-lang.org/doc/master/std/to_str/trait.ToStr."
"Use [`ToString`](http://static.rust-lang.org/doc/master/std/to_str/trait.ToString."
"html)."
msgstr ""
"[tarball]: http://static.rust-lang.org/dist/rust-nightly.tar.gz\n"
Expand All @@ -34,7 +34,7 @@ msgstr ""
#, fuzzy
#| msgid ""
#| "~~~~ let x: f64 = 4.0; let y: uint = x as uint; assert!(y == 4u); ~~~~"
msgid "~~~ let x: int = 42; let y: String = x.to_str(); ~~~"
msgid "~~~ let x: int = 42; let y: String = x.to_string(); ~~~"
msgstr ""
"~~~~\n"
"let x: f64 = 4.0;\n"
Expand Down
2 changes: 1 addition & 1 deletion src/doc/po/ja/rust.md.po
Expand Up @@ -1656,7 +1656,7 @@ msgstr ""
#| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
msgid ""
"impl Printable for int {\n"
" fn to_string(&self) -> String { self.to_str() }\n"
" fn to_string(&self) -> String { self.to_string() }\n"
"}\n"
msgstr ""
"~~~~ {.ignore}\n"
Expand Down
10 changes: 5 additions & 5 deletions src/doc/po/ja/tutorial.md.po
Expand Up @@ -4410,9 +4410,9 @@ msgstr ""

#. type: Plain text
#: src/doc/tutorial.md:2528
msgid "#[deriving(Rand, ToStr)] enum ABC { A, B, C } ~~~"
msgid "#[deriving(Rand, ToString)] enum ABC { A, B, C } ~~~"
msgstr ""
"#[deriving(Rand, ToStr)]\n"
"#[deriving(Rand, ToString)]\n"
"enum ABC { A, B, C }\n"
"~~~"

Expand All @@ -4422,15 +4422,15 @@ msgstr ""
#| msgid ""
#| "The full list of derivable traits is `Eq`, `TotalEq`, `Ord`, `TotalOrd`, "
#| "`Encodable` `Decodable`, `Clone`, `DeepClone`, `Hash`, `Rand`, "
#| "`Zero`, and `ToStr`."
#| "`Zero`, and `ToString`."
msgid ""
"The full list of derivable traits is `Eq`, `TotalEq`, `Ord`, `TotalOrd`, "
"`Encodable` `Decodable`, `Clone`, `DeepClone`, `Hash`, `Rand`, "
"`Default`, `Zero`, and `ToStr`."
"`Default`, `Zero`, and `ToString`."
msgstr ""
"実装を自動的に導出可能なトレイトは、 `Eq`, `TotalEq`, `Ord`, `TotalOrd`, "
"`Encodable` `Decodable`, `Clone`, `DeepClone`, `Hash`, `Rand`, `Zero`, "
"および `ToStr` です。."
"および `ToString` です。."

#. type: Plain text
#: src/doc/tutorial.md:2534
Expand Down
6 changes: 3 additions & 3 deletions src/doc/rust.md
Expand Up @@ -3671,15 +3671,15 @@ An example of an object type:

~~~~
trait Printable {
fn to_string(&self) -> String;
fn stringify(&self) -> String;
}
impl Printable for int {
fn to_string(&self) -> String { self.to_str() }
fn stringify(&self) -> String { self.to_string() }
}
fn print(a: Box<Printable>) {
println!("{}", a.to_string());
println!("{}", a.stringify());
}
fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/etc/vim/syntax/rust.vim
Expand Up @@ -100,7 +100,7 @@ syn keyword rustTrait RawPtr
syn keyword rustTrait Buffer Writer Reader Seek
syn keyword rustTrait Str StrVector StrSlice OwnedStr
syn keyword rustTrait IntoMaybeOwned StrAllocating
syn keyword rustTrait ToStr IntoStr
syn keyword rustTrait ToString IntoStr
syn keyword rustTrait Tuple1 Tuple2 Tuple3 Tuple4
syn keyword rustTrait Tuple5 Tuple6 Tuple7 Tuple8
syn keyword rustTrait Tuple9 Tuple10 Tuple11 Tuple12
Expand Down
18 changes: 9 additions & 9 deletions src/libcollections/bitv.rs
Expand Up @@ -60,17 +60,17 @@ enum BitvVariant { Big(BigBitv), Small(SmallBitv) }
/// bv.set(3, true);
/// bv.set(5, true);
/// bv.set(7, true);
/// println!("{}", bv.to_str());
/// println!("{}", bv.to_string());
/// println!("total bits set to true: {}", bv.iter().filter(|x| *x).count());
///
/// // flip all values in bitvector, producing non-primes less than 10
/// bv.negate();
/// println!("{}", bv.to_str());
/// println!("{}", bv.to_string());
/// println!("total bits set to true: {}", bv.iter().filter(|x| *x).count());
///
/// // reset bitvector to empty
/// bv.clear();
/// println!("{}", bv.to_str());
/// println!("{}", bv.to_string());
/// println!("total bits set to true: {}", bv.iter().filter(|x| *x).count());
/// ```
pub struct Bitv {
Expand Down Expand Up @@ -996,10 +996,10 @@ mod tests {
#[test]
fn test_to_str() {
let zerolen = Bitv::new();
assert_eq!(zerolen.to_str().as_slice(), "");
assert_eq!(zerolen.to_string().as_slice(), "");

let eightbits = Bitv::with_capacity(8u, false);
assert_eq!(eightbits.to_str().as_slice(), "00000000")
assert_eq!(eightbits.to_string().as_slice(), "00000000")
}

#[test]
Expand All @@ -1022,7 +1022,7 @@ mod tests {
let mut b = bitv::Bitv::with_capacity(2, false);
b.set(0, true);
b.set(1, false);
assert_eq!(b.to_str().as_slice(), "10");
assert_eq!(b.to_string().as_slice(), "10");
}

#[test]
Expand Down Expand Up @@ -1333,7 +1333,7 @@ mod tests {
fn test_from_bytes() {
let bitv = from_bytes([0b10110110, 0b00000000, 0b11111111]);
let str = format!("{}{}{}", "10110110", "00000000", "11111111");
assert_eq!(bitv.to_str().as_slice(), str.as_slice());
assert_eq!(bitv.to_string().as_slice(), str.as_slice());
}

#[test]
Expand All @@ -1352,7 +1352,7 @@ mod tests {
fn test_from_bools() {
let bools = vec![true, false, true, true];
let bitv: Bitv = bools.iter().map(|n| *n).collect();
assert_eq!(bitv.to_str().as_slice(), "1011");
assert_eq!(bitv.to_string().as_slice(), "1011");
}

#[test]
Expand Down Expand Up @@ -1787,7 +1787,7 @@ mod tests {
s.insert(10);
s.insert(50);
s.insert(2);
assert_eq!("{1, 2, 10, 50}".to_string(), s.to_str());
assert_eq!("{1, 2, 10, 50}".to_string(), s.to_string());
}

fn rng() -> rand::IsaacRng {
Expand Down
11 changes: 5 additions & 6 deletions src/libcollections/btree.rs
Expand Up @@ -787,7 +787,6 @@ mod test_btree {
fn insert_test_one() {
let b = BTree::new(1i, "abc".to_string(), 2);
let is_insert = b.insert(2i, "xyz".to_string());
//println!("{}", is_insert.clone().to_str());
assert!(is_insert.root.is_leaf());
}

Expand All @@ -798,7 +797,7 @@ mod test_btree {
let leaf_elt_3 = LeafElt::new(3i, "ccc".to_string());
let n = Node::new_leaf(vec!(leaf_elt_1, leaf_elt_2, leaf_elt_3));
let b = BTree::new_with_node_len(n, 3, 2);
//println!("{}", b.clone().insert(4, "ddd".to_string()).to_str());
//println!("{}", b.clone().insert(4, "ddd".to_string()).to_string());
assert!(b.insert(4, "ddd".to_string()).root.is_leaf());
}

Expand All @@ -810,7 +809,7 @@ mod test_btree {
let leaf_elt_4 = LeafElt::new(4i, "ddd".to_string());
let n = Node::new_leaf(vec!(leaf_elt_1, leaf_elt_2, leaf_elt_3, leaf_elt_4));
let b = BTree::new_with_node_len(n, 3, 2);
//println!("{}", b.clone().insert(5, "eee".to_string()).to_str());
//println!("{}", b.clone().insert(5, "eee".to_string()).to_string());
assert!(!b.insert(5, "eee".to_string()).root.is_leaf());
}

Expand All @@ -827,7 +826,7 @@ mod test_btree {
b = b.clone().insert(7, "ggg".to_string());
b = b.clone().insert(8, "hhh".to_string());
b = b.clone().insert(0, "omg".to_string());
//println!("{}", b.clone().to_str());
//println!("{}", b.clone().to_string());
assert!(!b.root.is_leaf());
}

Expand Down Expand Up @@ -905,11 +904,11 @@ mod test_btree {
assert!(&b2.cmp(&b) == &Greater)
}

//Tests the BTree's to_str() method.
//Tests the BTree's to_string() method.
#[test]
fn btree_tostr_test() {
let b = BTree::new(1i, "abc".to_string(), 2);
assert_eq!(b.to_str(), "Key: 1, value: abc;".to_string())
assert_eq!(b.to_string(), "Key: 1, value: abc;".to_string())
}

}
4 changes: 2 additions & 2 deletions src/libcollections/dlist.rs
Expand Up @@ -1041,12 +1041,12 @@ mod tests {
#[test]
fn test_show() {
let list: DList<int> = range(0i, 10).collect();
assert!(list.to_str().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
assert!(list.to_string().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");

let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
.map(|&s| s)
.collect();
assert!(list.to_str().as_slice() == "[just, one, test, more]");
assert!(list.to_string().as_slice() == "[just, one, test, more]");
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/smallintmap.rs
Expand Up @@ -491,7 +491,7 @@ mod test_map {
map.insert(1, 2i);
map.insert(3, 4i);

let map_str = map.to_str();
let map_str = map.to_string();
let map_str = map_str.as_slice();
assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
assert_eq!(format!("{}", empty), "{}".to_string());
Expand Down

0 comments on commit 8bb34a3

Please sign in to comment.