about summary refs log tree commit diff
path: root/src/libtest
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-12-12 10:59:41 -0800
committerAlex Crichton <alex@alexcrichton.com>2015-01-01 22:04:46 -0800
commite423fcf0e0166da55f88233e0be5eacba55bc0bc (patch)
tree0c4a4ffadfa870dcf23e1d55b224a1d7c2805f2a /src/libtest
parentcd614164e692cca3a1460737f581fcb6d4630baf (diff)
downloadrust-e423fcf0e0166da55f88233e0be5eacba55bc0bc.tar.gz
rust-e423fcf0e0166da55f88233e0be5eacba55bc0bc.zip
std: Enforce Unicode in fmt::Writer
This commit is an implementation of [RFC 526][rfc] which is a change to alter
the definition of the old `fmt::FormatWriter`. The new trait, renamed to
`Writer`, now only exposes one method `write_str` in order to guarantee that all
implementations of the formatting traits can only produce valid Unicode.

[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0526-fmt-text-writer.md

One of the primary improvements of this patch is the performance of the
`.to_string()` method by avoiding an almost-always redundant UTF-8 check. This
is a breaking change due to the renaming of the trait as well as the loss of the
`write` method, but migration paths should be relatively easy:

* All usage of `write` should move to `write_str`. If truly binary data was
  being written in an implementation of `Show`, then it will need to use a
  different trait or an altogether different code path.

* All usage of `write!` should continue to work as-is with no modifications.

* All usage of `Show` where implementations just delegate to another should
  continue to work as-is.

[breaking-change]

Closes #20352
Diffstat (limited to 'src/libtest')
-rw-r--r--src/libtest/lib.rs7
1 files changed, 3 insertions, 4 deletions
diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs
index 19821ecb7ca..565c98797b8 100644
--- a/src/libtest/lib.rs
+++ b/src/libtest/lib.rs
@@ -171,14 +171,14 @@ impl TestFn {
 
 impl fmt::Show for TestFn {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        f.write(match *self {
+        f.write_str(match *self {
             StaticTestFn(..) => "StaticTestFn(..)",
             StaticBenchFn(..) => "StaticBenchFn(..)",
             StaticMetricFn(..) => "StaticMetricFn(..)",
             DynTestFn(..) => "DynTestFn(..)",
             DynMetricFn(..) => "DynMetricFn(..)",
             DynBenchFn(..) => "DynBenchFn(..)"
-        }.as_bytes())
+        })
     }
 }
 
@@ -1212,8 +1212,7 @@ impl MetricMap {
     pub fn save(&self, p: &Path) -> io::IoResult<()> {
         let mut file = try!(File::create(p));
         let MetricMap(ref map) = *self;
-        let mut enc = json::PrettyEncoder::new(&mut file);
-        map.encode(&mut enc)
+        write!(&mut file, "{}", json::as_json(map))
     }
 
     /// Compare against another MetricMap. Optionally compare all