From 3675e42334e9e58ae16446f52b2c6c951e8e1628 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 18 Oct 2013 15:16:18 -0700 Subject: [PATCH] std: Move sys::log_str to repr::repr_to_str. Further work on #2240. --- src/libstd/fmt/mod.rs | 10 +++------- src/libstd/repr.rs | 10 ++++++++++ src/libstd/sys.rs | 11 ----------- src/libsyntax/ext/deriving/to_str.rs | 7 ++++--- src/test/run-pass/fixed_length_vec_glue.rs | 4 ++-- src/test/run-pass/log-str.rs | 4 ++-- 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index 3223ca9a731cb..e7fa81fc87ac9 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -466,7 +466,7 @@ use rt::io::Decorator; use rt::io::mem::MemWriter; use rt::io; use str; -use sys; +use repr; use util; use vec; @@ -1087,17 +1087,13 @@ impl Poly for T { fn fmt(t: &T, f: &mut Formatter) { match (f.width, f.precision) { (None, None) => { - // XXX: sys::log_str should have a variant which takes a stream - // and we should directly call that (avoids unnecessary - // allocations) - let s = sys::log_str(t); - f.buf.write(s.as_bytes()); + repr::write_repr(f.buf, t); } // If we have a specified width for formatting, then we have to make // this allocation of a new string _ => { - let s = sys::log_str(t); + let s = repr::repr_to_str(t); f.pad(s); } } diff --git a/src/libstd/repr.rs b/src/libstd/repr.rs index 031fd7993eb9a..a788064293ff0 100644 --- a/src/libstd/repr.rs +++ b/src/libstd/repr.rs @@ -616,6 +616,16 @@ pub fn write_repr(writer: &mut io::Writer, object: &T) { } } +pub fn repr_to_str(t: &T) -> ~str { + use str; + use rt::io; + use rt::io::Decorator; + + let mut result = io::mem::MemWriter::new(); + write_repr(&mut result as &mut io::Writer, t); + str::from_utf8_owned(result.inner()) +} + #[cfg(test)] struct P {a: int, b: f64} diff --git a/src/libstd/sys.rs b/src/libstd/sys.rs index 0f38671bfc293..d20a6696e278f 100644 --- a/src/libstd/sys.rs +++ b/src/libstd/sys.rs @@ -15,18 +15,7 @@ use c_str::ToCStr; use libc::size_t; use libc; -use repr; use rt::task; -use str; - -pub fn log_str(t: &T) -> ~str { - use rt::io; - use rt::io::Decorator; - - let mut result = io::mem::MemWriter::new(); - repr::write_repr(&mut result as &mut io::Writer, t); - str::from_utf8_owned(result.inner()) -} /// Trait for initiating task failure. pub trait FailWithCause { diff --git a/src/libsyntax/ext/deriving/to_str.rs b/src/libsyntax/ext/deriving/to_str.rs index fa13f78d0f9b6..77dbafa5ad7b0 100644 --- a/src/libsyntax/ext/deriving/to_str.rs +++ b/src/libsyntax/ext/deriving/to_str.rs @@ -40,9 +40,10 @@ pub fn expand_deriving_to_str(cx: @ExtCtxt, } // It used to be the case that this deriving implementation invoked -// std::sys::log_str, but this isn't sufficient because it doesn't invoke the -// to_str() method on each field. Hence we mirror the logic of the log_str() -// method, but with tweaks to call to_str() on sub-fields. +// std::repr::repr_to_str, but this isn't sufficient because it +// doesn't invoke the to_str() method on each field. Hence we mirror +// the logic of the repr_to_str() method, but with tweaks to call to_str() +// on sub-fields. fn to_str_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let to_str = cx.ident_of("to_str"); diff --git a/src/test/run-pass/fixed_length_vec_glue.rs b/src/test/run-pass/fixed_length_vec_glue.rs index ab34245a8f088..d778844f4d79e 100644 --- a/src/test/run-pass/fixed_length_vec_glue.rs +++ b/src/test/run-pass/fixed_length_vec_glue.rs @@ -10,13 +10,13 @@ // xfail-fast: check-fast screws up repr paths -use std::sys; +use std::repr; struct Struc { a: u8, b: [int, ..3], c: int } pub fn main() { let arr = [1,2,3]; let struc = Struc {a: 13u8, b: arr, c: 42}; - let s = sys::log_str(&struc); + let s = repr::repr_to_str(&struc); assert_eq!(s, ~"Struc{a: 13u8, b: [1, 2, 3], c: 42}"); } diff --git a/src/test/run-pass/log-str.rs b/src/test/run-pass/log-str.rs index a8914de917e7d..8859b5336260c 100644 --- a/src/test/run-pass/log-str.rs +++ b/src/test/run-pass/log-str.rs @@ -8,10 +8,10 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::sys; +use std::repr; pub fn main() { - let act = sys::log_str(&~[1, 2, 3]); + let act = repr::repr_to_str(&~[1, 2, 3]); assert_eq!(~"~[1, 2, 3]", act); let act = format!("{:?}/{:6?}", ~[1, 2, 3], ~"hi");