auto merge of #9937 : brson/rust/log_str, r=alexcrichton
This commit is contained in:
commit
7e4404b6fb
@ -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<T> 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);
|
||||
}
|
||||
}
|
||||
|
@ -616,6 +616,16 @@ pub fn write_repr<T>(writer: &mut io::Writer, object: &T) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn repr_to_str<T>(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}
|
||||
|
||||
|
@ -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: &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 {
|
||||
|
@ -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");
|
||||
|
@ -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}");
|
||||
}
|
||||
|
@ -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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user