Skip to content

std: Move sys::log_str to repr::repr_to_str. Further work on #2240. #9937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/libstd/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/libstd/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
11 changes: 0 additions & 11 deletions src/libstd/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions src/libsyntax/ext/deriving/to_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/fixed_length_vec_glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
4 changes: 2 additions & 2 deletions src/test/run-pass/log-str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down