about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-02-19 18:56:33 -0800
committerAlex Crichton <alex@alexcrichton.com>2014-02-23 20:51:56 -0800
commitb78b749810f4ed53e8287adfb284f9f32f16b73c (patch)
tree0894e97a61d0ab09dd42d73202aeefca175463eb /src/test
parent7cc6b5e0a3a2038d66301906f76cdb778304a3bd (diff)
downloadrust-b78b749810f4ed53e8287adfb284f9f32f16b73c.tar.gz
rust-b78b749810f4ed53e8287adfb284f9f32f16b73c.zip
Remove all ToStr impls, add Show impls
This commit changes the ToStr trait to:

    impl<T: fmt::Show> ToStr for T {
        fn to_str(&self) -> ~str { format!("{}", *self) }
    }

The ToStr trait has been on the chopping block for quite awhile now, and this is
the final nail in its coffin. The trait and the corresponding method are not
being removed as part of this commit, but rather any implementations of the
`ToStr` trait are being forbidden because of the generic impl. The new way to
get the `to_str()` method to work is to implement `fmt::Show`.

Formatting into a `&mut Writer` (as `format!` does) is much more efficient than
`ToStr` when building up large strings. The `ToStr` trait forces many
intermediate allocations to be made while the `fmt::Show` trait allows
incremental buildup in the same heap allocated buffer. Additionally, the
`fmt::Show` trait is much more extensible in terms of interoperation with other
`Writer` instances and in more situations. By design the `ToStr` trait requires
at least one allocation whereas the `fmt::Show` trait does not require any
allocations.

Closes #8242
Closes #9806
Diffstat (limited to 'src/test')
-rw-r--r--src/test/auxiliary/cci_class_cast.rs8
-rw-r--r--src/test/compile-fail/use-after-move-implicity-coerced-object.rs8
-rw-r--r--src/test/run-pass/class-separate-impl.rs9
-rw-r--r--src/test/run-pass/deriving-global.rs6
-rw-r--r--src/test/run-pass/deriving-in-fn.rs4
-rw-r--r--src/test/run-pass/deriving-show-2.rs (renamed from src/test/run-pass/deriving-to-str.rs)32
-rw-r--r--src/test/run-pass/issue-2904.rs10
-rw-r--r--src/test/run-pass/issue-3563-3.rs7
-rw-r--r--src/test/run-pass/new-impl-syntax.rs14
9 files changed, 57 insertions, 41 deletions
diff --git a/src/test/auxiliary/cci_class_cast.rs b/src/test/auxiliary/cci_class_cast.rs
index 8fac4a3f322..79bb5aef764 100644
--- a/src/test/auxiliary/cci_class_cast.rs
+++ b/src/test/auxiliary/cci_class_cast.rs
@@ -9,14 +9,18 @@
 // except according to those terms.
 
 pub mod kitty {
+    use std::fmt;
+
     pub struct cat {
       priv meows : uint,
       how_hungry : int,
       name : ~str,
     }
 
-    impl ToStr for cat {
-       fn to_str(&self) -> ~str { self.name.clone() }
+    impl fmt::Show for cat {
+        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+            write!(f.buf, "{}", self.name)
+        }
     }
 
     impl cat {
diff --git a/src/test/compile-fail/use-after-move-implicity-coerced-object.rs b/src/test/compile-fail/use-after-move-implicity-coerced-object.rs
index 537da535457..085ed5db6df 100644
--- a/src/test/compile-fail/use-after-move-implicity-coerced-object.rs
+++ b/src/test/compile-fail/use-after-move-implicity-coerced-object.rs
@@ -10,13 +10,15 @@
 
 // ignore-tidy-linelength
 
+use std::fmt;
+
 struct Number {
     n: i64
 }
 
-impl ToStr for Number {
-    fn to_str(&self) -> ~str {
-        self.n.to_str()
+impl fmt::Show for Number {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f.buf, "{}", self.n)
     }
 }
 
diff --git a/src/test/run-pass/class-separate-impl.rs b/src/test/run-pass/class-separate-impl.rs
index e813210f4f4..55fa783391a 100644
--- a/src/test/run-pass/class-separate-impl.rs
+++ b/src/test/run-pass/class-separate-impl.rs
@@ -9,6 +9,9 @@
 // except according to those terms.
 
 // ignore-fast
+
+use std::fmt;
+
 struct cat {
     meows : uint,
 
@@ -50,9 +53,9 @@ fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
     }
 }
 
-impl ToStr for cat {
-    fn to_str(&self) -> ~str {
-        self.name.clone()
+impl fmt::Show for cat {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f.buf, "{}", self.name)
     }
 }
 
diff --git a/src/test/run-pass/deriving-global.rs b/src/test/run-pass/deriving-global.rs
index ffca06dc628..cce3575178e 100644
--- a/src/test/run-pass/deriving-global.rs
+++ b/src/test/run-pass/deriving-global.rs
@@ -28,19 +28,19 @@ mod submod {
     // cause errors about unrecognised module `std` (or `extra`)
     #[deriving(Eq, Ord, TotalEq, TotalOrd,
                Clone, DeepClone,
-               ToStr, Rand,
+               Show, Rand,
                Encodable, Decodable)]
     enum A { A1(uint), A2(int) }
 
     #[deriving(Eq, Ord, TotalEq, TotalOrd,
                Clone, DeepClone,
-               ToStr, Rand,
+               Show, Rand,
                Encodable, Decodable)]
     struct B { x: uint, y: int }
 
     #[deriving(Eq, Ord, TotalEq, TotalOrd,
                Clone, DeepClone,
-               ToStr, Rand,
+               Show, Rand,
                Encodable, Decodable)]
     struct C(uint, int);
 
diff --git a/src/test/run-pass/deriving-in-fn.rs b/src/test/run-pass/deriving-in-fn.rs
index 7fb7d601b81..baa036ee039 100644
--- a/src/test/run-pass/deriving-in-fn.rs
+++ b/src/test/run-pass/deriving-in-fn.rs
@@ -9,11 +9,11 @@
 // except according to those terms.
 
 pub fn main() {
-    #[deriving(ToStr)]
+    #[deriving(Show)]
     struct Foo {
         foo: int,
     }
 
     let f = Foo { foo: 10 };
-    let _ = f.to_str();
+    format!("{}", f);
 }
diff --git a/src/test/run-pass/deriving-to-str.rs b/src/test/run-pass/deriving-show-2.rs
index d9f69bd4a49..a2451c39400 100644
--- a/src/test/run-pass/deriving-to-str.rs
+++ b/src/test/run-pass/deriving-show-2.rs
@@ -10,30 +10,34 @@
 
 #[feature(struct_variant)];
 
-#[deriving(ToStr)]
+use std::fmt;
+
+#[deriving(Show)]
 enum A {}
-#[deriving(ToStr)]
+#[deriving(Show)]
 enum B { B1, B2, B3 }
-#[deriving(ToStr)]
+#[deriving(Show)]
 enum C { C1(int), C2(B), C3(~str) }
-#[deriving(ToStr)]
+#[deriving(Show)]
 enum D { D1{ a: int } }
-#[deriving(ToStr)]
+#[deriving(Show)]
 struct E;
-#[deriving(ToStr)]
+#[deriving(Show)]
 struct F(int);
-#[deriving(ToStr)]
+#[deriving(Show)]
 struct G(int, int);
-#[deriving(ToStr)]
+#[deriving(Show)]
 struct H { a: int }
-#[deriving(ToStr)]
+#[deriving(Show)]
 struct I { a: int, b: int }
-#[deriving(ToStr)]
+#[deriving(Show)]
 struct J(Custom);
 
 struct Custom;
-impl ToStr for Custom {
-    fn to_str(&self) -> ~str { ~"yay" }
+impl fmt::Show for Custom {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f.buf, "yay")
+    }
 }
 
 pub fn main() {
@@ -41,11 +45,11 @@ pub fn main() {
     assert_eq!(B2.to_str(), ~"B2");
     assert_eq!(C1(3).to_str(), ~"C1(3)");
     assert_eq!(C2(B2).to_str(), ~"C2(B2)");
-    assert_eq!(D1{ a: 2 }.to_str(), ~"D1{a: 2}");
+    assert_eq!(D1{ a: 2 }.to_str(), ~"D1 { a: 2 }");
     assert_eq!(E.to_str(), ~"E");
     assert_eq!(F(3).to_str(), ~"F(3)");
     assert_eq!(G(3, 4).to_str(), ~"G(3, 4)");
     assert_eq!(G(3, 4).to_str(), ~"G(3, 4)");
-    assert_eq!(I{ a: 2, b: 4 }.to_str(), ~"I{a: 2, b: 4}");
+    assert_eq!(I{ a: 2, b: 4 }.to_str(), ~"I { a: 2, b: 4 }");
     assert_eq!(J(Custom).to_str(), ~"J(yay)");
 }
diff --git a/src/test/run-pass/issue-2904.rs b/src/test/run-pass/issue-2904.rs
index 2816609ef97..45f59fe9cd4 100644
--- a/src/test/run-pass/issue-2904.rs
+++ b/src/test/run-pass/issue-2904.rs
@@ -17,7 +17,7 @@
 extern crate extra;
 
 use std::io;
-use std::to_str;
+use std::fmt;
 
 enum square {
     bot,
@@ -30,9 +30,9 @@ enum square {
     empty
 }
 
-impl to_str::ToStr for square {
-    fn to_str(&self) -> ~str {
-        match *self {
+impl fmt::Show for square {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f.buf, "{}", match *self {
           bot => { ~"R" }
           wall => { ~"#" }
           rock => { ~"*" }
@@ -41,7 +41,7 @@ impl to_str::ToStr for square {
           open_lift => { ~"O" }
           earth => { ~"." }
           empty => { ~" " }
-        }
+        })
     }
 }
 
diff --git a/src/test/run-pass/issue-3563-3.rs b/src/test/run-pass/issue-3563-3.rs
index 8e5dd762c63..ae65c46ce71 100644
--- a/src/test/run-pass/issue-3563-3.rs
+++ b/src/test/run-pass/issue-3563-3.rs
@@ -22,6 +22,7 @@ extern crate extra;
 // already linked in. Using WriterUtil allows us to use the write_line method.
 use std::str;
 use std::vec;
+use std::fmt;
 
 // Represents a position on a canvas.
 struct Point {
@@ -94,13 +95,13 @@ impl AsciiArt {
 
 // Allows AsciiArt to be converted to a string using the libcore ToStr trait.
 // Note that the %s fmt! specifier will not call this automatically.
-impl ToStr for AsciiArt {
-    fn to_str(&self) -> ~str {
+impl fmt::Show for AsciiArt {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         // Convert each line into a string.
         let lines = self.lines.map(|line| str::from_chars(*line));
 
         // Concatenate the lines together using a new-line.
-        lines.connect("\n")
+        write!(f.buf, "{}", lines.connect("\n"))
     }
 }
 
diff --git a/src/test/run-pass/new-impl-syntax.rs b/src/test/run-pass/new-impl-syntax.rs
index 6def21a389a..30200d4cb18 100644
--- a/src/test/run-pass/new-impl-syntax.rs
+++ b/src/test/run-pass/new-impl-syntax.rs
@@ -8,14 +8,16 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use std::fmt;
+
 struct Thingy {
     x: int,
     y: int
 }
 
-impl ToStr for Thingy {
-    fn to_str(&self) -> ~str {
-        format!("\\{ x: {}, y: {} \\}", self.x, self.y)
+impl fmt::Show for Thingy {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f.buf, "\\{ x: {}, y: {} \\}", self.x, self.y)
     }
 }
 
@@ -23,9 +25,9 @@ struct PolymorphicThingy<T> {
     x: T
 }
 
-impl<T:ToStr> ToStr for PolymorphicThingy<T> {
-    fn to_str(&self) -> ~str {
-        self.x.to_str()
+impl<T:fmt::Show> fmt::Show for PolymorphicThingy<T> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f.buf, "{}", self.x)
     }
 }